BlastDBNodeTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Tests;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. use Faker\Factory;
  6. /**
  7. * Tests BlastDB Node CRUD
  8. */
  9. class BlastDBNodeTest extends TripalTestCase {
  10. // Uncomment to auto start and rollback db transactions per test method.
  11. use DBTransaction;
  12. /**
  13. * BlastDB Node Type exists.
  14. *
  15. * Check that the BlastDB node type exists. It should be created
  16. * when the module is installed by the Drupal Node API.
  17. */
  18. public function testBlastDBNodeExists() {
  19. // Get a list of all types available.
  20. $types = node_type_get_types();
  21. // The BlastDB node type must be in the list.
  22. $this->assertArrayHasKey('blastdb', $types, '"Blast Database" node type is not registered with Drupal.');
  23. // Additionally, the blastdb node type should be created by blast_ui.\
  24. // This checks the case where the node type might be created by
  25. // a different module.
  26. $this->assertEquals($types['blastdb']->module, 'blast_ui', '"Blast Database" node type was not added by blast_ui.');
  27. }
  28. /**
  29. * Test Creating a BlastDB Node.
  30. *
  31. * Note: We can't test this by submitting the form via PUT because it requires
  32. * permission to access /node/add/blastdb; however, we don't yet have a
  33. * way to do this with TripalTestSuite. Furthermore, testing HTTP Requests
  34. * would not give us access to the data added via the test due to database
  35. * transactions.
  36. */
  37. public function testBlastDBNodeCreate() {
  38. module_load_include('inc', 'node', 'node.pages');
  39. // Log in the god user.
  40. global $user;
  41. $user = user_load(1);
  42. $node = array('type' => 'blastdb');
  43. // Fill in the form.
  44. $faker = Factory::create();
  45. $form_state = array(
  46. 'values' => array(
  47. 'db_name' => $faker->words(3, TRUE),
  48. 'db_path' => '/fake/path/here',
  49. 'db_dbtype' => 'nucleotide',
  50. 'dbxref_linkout_type' => 'none',
  51. 'cvitjs_enabled' => 0,
  52. 'op' => t('Save'),
  53. ),
  54. );
  55. // Execute the node creation form.
  56. drupal_form_submit('blastdb_node_form', $form_state, (object) $node);
  57. // Retrieve any errors.
  58. $errors = form_get_errors();
  59. // Assert that there must not be any.
  60. $this->assertEmpty($errors, 'Form submission returned the following errors:'.print_r($errors,TRUE));
  61. // Check that there is a test blast database.
  62. $result = db_query('SELECT * FROM {blastdb} WHERE name=:name',
  63. array(':name' => $form_state['values']['db_name']));
  64. $this->assertEquals(1, $result->rowCount(), 'Unable to select the blast database using the name.');
  65. // log out the god user.
  66. $user = drupal_anonymous_user();
  67. }
  68. /**
  69. * Update an existing Blast Database Node.
  70. */
  71. public function testBlastDBNodeUpdate() {
  72. module_load_include('inc', 'node', 'node.pages');
  73. // Log in the god user.
  74. global $user;
  75. $user = user_load(1);
  76. // Create the node in the first place.
  77. $seeder = DatabaseSeeders\BlastDBNodeSeeder::seed();
  78. $node = $seeder->getNode();
  79. // Now use the form to edit it :-)
  80. // Specifically, we will change the name and type.
  81. $faker = Factory::create();
  82. $form_state = array(
  83. 'values' => array(
  84. 'db_name' => $faker->words(4, TRUE),
  85. 'db_path' => $node->db_path,
  86. 'db_dbtype' => 'protein',
  87. 'dbxref_linkout_type' => $node->dbxref_linkout_type,
  88. 'cvitjs_enabled' => $node->cvitjs_enabled,
  89. 'op' => t('Save'),
  90. ),
  91. );
  92. // Execute the node creation form.
  93. drupal_form_submit('blastdb_node_form', $form_state, $node);
  94. // Retrieve any errors.
  95. $errors = form_get_errors();
  96. // Assert that there must not be any.
  97. $this->assertEmpty($errors, 'Form submission returned the following errors:'.print_r($errors,TRUE));
  98. // Check that there is a test blast database.
  99. $result = db_query('SELECT * FROM {blastdb} WHERE name=:name AND dbtype=:type',
  100. array(':name' => $form_state['values']['db_name'], ':type' => $form_state['values']['db_dbtype']));
  101. $this->assertEquals(1, $result->rowCount(), 'Unable to select the blast database using the new name and type.');
  102. // log out the god user.
  103. $user = drupal_anonymous_user();
  104. }
  105. /**
  106. * Test deleting a node.
  107. * NOTE: We cannot test this via drupal_form_submit() since it requires a confirmation.
  108. */
  109. }