BlastDBNodeTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. $this->actingAs(1);
  41. $node = array('type' => 'blastdb');
  42. // Fill in the form.
  43. $faker = Factory::create();
  44. $form_state = array(
  45. 'values' => array(
  46. 'db_name' => $faker->words(3, TRUE),
  47. 'db_path' => '/fake/path/here',
  48. 'db_dbtype' => 'nucleotide',
  49. 'dbxref_linkout_type' => 'none',
  50. 'cvitjs_enabled' => 0,
  51. 'op' => t('Save'),
  52. ),
  53. );
  54. // Execute the node creation form.
  55. drupal_form_submit('blastdb_node_form', $form_state, (object) $node);
  56. // Retrieve any errors.
  57. $errors = form_get_errors();
  58. // Assert that there must not be any.
  59. $this->assertEmpty($errors, 'Form submission returned the following errors:'.print_r($errors,TRUE));
  60. // Check that there is a test blast database.
  61. $result = db_query('SELECT * FROM {blastdb} WHERE name=:name',
  62. array(':name' => $form_state['values']['db_name']));
  63. $this->assertEquals(1, $result->rowCount(), 'Unable to select the blast database using the name.');
  64. }
  65. /**
  66. * Update an existing Blast Database Node.
  67. */
  68. public function testBlastDBNodeUpdate() {
  69. module_load_include('inc', 'node', 'node.pages');
  70. // Log in the god user.
  71. $this->actingAs(1);
  72. // Create the node in the first place.
  73. $seeder = DatabaseSeeders\BlastDBNodeSeeder::seed();
  74. $node = $seeder->getNode();
  75. // Now use the form to edit it :-)
  76. // Specifically, we will change the name and type.
  77. $faker = Factory::create();
  78. $form_state = array(
  79. 'values' => array(
  80. 'db_name' => $faker->words(4, TRUE),
  81. 'db_path' => $node->db_path,
  82. 'db_dbtype' => 'protein',
  83. 'dbxref_linkout_type' => $node->dbxref_linkout_type,
  84. 'cvitjs_enabled' => $node->cvitjs_enabled,
  85. 'op' => t('Save'),
  86. ),
  87. );
  88. // Execute the node creation form.
  89. drupal_form_submit('blastdb_node_form', $form_state, $node);
  90. // Retrieve any errors.
  91. $errors = form_get_errors();
  92. // Assert that there must not be any.
  93. $this->assertEmpty($errors, 'Form submission returned the following errors:'.print_r($errors,TRUE));
  94. // Check that there is a test blast database.
  95. $result = db_query('SELECT * FROM {blastdb} WHERE name=:name AND dbtype=:type',
  96. array(':name' => $form_state['values']['db_name'], ':type' => $form_state['values']['db_dbtype']));
  97. $this->assertEquals(1, $result->rowCount(), 'Unable to select the blast database using the new name and type.');
  98. }
  99. /**
  100. * Test deleting a node.
  101. * NOTE: We cannot test this via drupal_form_submit() since it requires a confirmation.
  102. */
  103. }