ApiTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace Tests\tripal_jbrowse_mgmt;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. use Faker\Factory;
  6. class ApiTest extends TripalTestCase {
  7. // Uncomment to auto start and rollback db transactions per test method.
  8. use DBTransaction;
  9. /**
  10. * Tests the Settings API.
  11. *
  12. * Specifically tripal_jbrowse_mgmt_save_settings()
  13. * and tripal_jbrowse_mgmt_get_settings().
  14. */
  15. public function testSettings() {
  16. $test_settings = [
  17. 'bin_path' => 'test/fake/path',
  18. 'link' => 'test/fake/path',
  19. 'data_dir' => 'test/fake/path',
  20. 'menu_template' => [],
  21. ];
  22. tripal_jbrowse_mgmt_save_settings($test_settings);
  23. $retrieved = tripal_jbrowse_mgmt_get_settings();
  24. // Check that all the expected settings are available.
  25. $expected = ['bin_path', 'link', 'data_dir', 'menu_template'];
  26. foreach ($expected as $expected_key) {
  27. $this->assertArrayHasKey($expected_key, $retrieved,
  28. "Retrieved settings do not contain $expected_key");
  29. // Check that the settings match if provided in test.
  30. if (isset($test_settings[$expected_key])) {
  31. $this->assertEquals(
  32. $test_settings[$expected_key],
  33. $retrieved[$expected_key],
  34. "The retrieved value for $expected_key does not match what we set."
  35. );
  36. }
  37. }
  38. }
  39. /**
  40. * Test Instance Create-Retrieve-Update-Delete.
  41. */
  42. public function testInstanceCRUD() {
  43. putenv("TRIPAL_SUPPRESS_ERRORS=TRUE");
  44. $organism = factory('chado.organism')->create();
  45. // Full Fake Instance Details.
  46. $faker = Factory::create();
  47. $testdata = [
  48. 'organism_id' => $organism->organism_id,
  49. 'title' => $faker->words(3, TRUE),
  50. 'description' => $faker->sentence(25, TRUE),
  51. 'created_at' => $faker->unixTime(),
  52. 'file' => '/path/to/fake/file',
  53. ];
  54. // Check we cannot create a JBrowse instance without an organism.
  55. $noOrganism = $testdata;
  56. unset($noOrganism['organism_id']);
  57. $id = tripal_jbrowse_mgmt_create_instance($noOrganism);
  58. $this->assertFalse($id,
  59. "Created an instance without an organism_id!?!");
  60. // Now try to create an instance with all the data.
  61. $id = tripal_jbrowse_mgmt_create_instance($testdata);
  62. $this->assertNotFalse($id,
  63. "Unable to create instance.");
  64. // Try to retrieve the instance we just created.
  65. $retrieved_instance = tripal_jbrowse_mgmt_get_instance($id);
  66. $this->assertNotFalse($id, "We did not retrieve an instance?");
  67. $this->assertEquals($id, $retrieved_instance->id,
  68. "We retrieved a different instance then we asked for?");
  69. $this->assertEquals(
  70. $testdata['organism_id'], $retrieved_instance->organism_id,
  71. "Retreived the same instance but the organism is not correct?");
  72. // Change the title and test the instance was updated.
  73. $new_title = $testdata;
  74. $testdata['title'] = 'NEW FAKE TITLE ' . uniqid();
  75. $success = tripal_jbrowse_mgmt_update_instance($id, $new_title);
  76. $this->assertNotFalse($success, 'Unable to update instance title.');
  77. $retrieved_instance = tripal_jbrowse_mgmt_get_instance($id);
  78. $this->assertEquals(
  79. $new_title['title'], $retrieved_instance->title,
  80. "The title was not updated.");
  81. // Finally delete him!
  82. $success = tripal_jbrowse_mgmt_delete_instance($retrieved_instance);
  83. $this->assertNotFalse($success, 'Unable to delete this instance.');
  84. $one_more_time = tripal_jbrowse_mgmt_get_instance($id);
  85. $this->assertFalse($one_more_time);
  86. // and delete with numeric id.
  87. $success = tripal_jbrowse_mgmt_delete_instance($id);
  88. $this->assertNotFalse($success, 'Unable to delete this instance.');
  89. // and catch an exception during delete.
  90. try {
  91. $success = tripal_jbrowse_mgmt_delete_instance('FAKESTRING');
  92. // If we get here without an exception then this test should fail.
  93. $this->assertTrue(FALSE, "Shouldn't be able to extract instance to delete.");
  94. } catch (\Exception $e) {
  95. // Not worried since this is expected!
  96. }
  97. putenv("TRIPAL_SUPPRESS_ERRORS");
  98. }
  99. /**
  100. * Test Track Create-Retrieve-Update-Delete.
  101. */
  102. public function testTrackCRUD() {
  103. // Fake Instance.
  104. $faker = Factory::create();
  105. $organism = factory('chado.organism')->create();
  106. $instance_details = [
  107. 'organism_id' => $organism->organism_id,
  108. 'title' => $faker->words(3, TRUE),
  109. 'description' => $faker->sentence(25, TRUE),
  110. 'created_at' => $faker->unixTime(),
  111. 'file' => '/path/to/fake/file',
  112. ];
  113. $instance_id = tripal_jbrowse_mgmt_create_instance($instance_details);
  114. $instance = tripal_jbrowse_mgmt_get_instance($instance_id);
  115. $path = tripal_jbrowse_mgmt_get_track_list_file_path($instance);
  116. $this->assertNotFalse($path,
  117. "Unable to retrieve the path to the trackList.json");
  118. // Fake track details.
  119. $testdata = [
  120. 'label' => $faker->words(2, TRUE),
  121. 'track_type' => 'CanvasFeatures',
  122. 'file_type' => 'gff',
  123. 'created_at' => $faker->unixTime(),
  124. 'file' => '/path/to/fake/file',
  125. ];
  126. // First retrieve when there are no tracks.
  127. $tracks = tripal_jbrowse_mgmt_get_tracks($instance);
  128. $this->assertCount(0, $tracks,
  129. "There should not be tracks as we just created this instance.");
  130. // Create tracks.
  131. $track_id = tripal_jbrowse_mgmt_create_track($instance, $testdata);
  132. $this->assertNotFalse($track_id,
  133. "We should have a track created successfully.");
  134. // Retrieve our newly created track.
  135. $track = tripal_jbrowse_mgmt_get_track($track_id);
  136. $this->assertNotFalse($track, "Unable to create track.");
  137. $this->assertEquals($testdata['label'], $track->label,
  138. "The label of our new track didn't match what we submitted.");
  139. $this->assertEquals($instance->id, $track->instance_id,
  140. "The instance_id of our new track didn't match what we submitted.");
  141. $this->assertEquals($instance->organism_id, $track->organism_id,
  142. "The organism_id of our new track didn't match what we submitted.");
  143. // Retrieve track with a condition.
  144. $tracks = tripal_jbrowse_mgmt_get_tracks($instance, ['label' => $testdata['label']]);
  145. $this->assertCount(1, $tracks, "We were not able to select a track we knew should exist.");
  146. // Now update it.
  147. $new_label = 'NEW LABEL ' . uniqid();
  148. $success = tripal_jbrowse_mgmt_update_track($track, ['label' => $new_label]);
  149. $this->assertNotFalse($success, "Unable to update track label.");
  150. $new_track = tripal_jbrowse_mgmt_get_track($track_id);
  151. $this->assertEquals($new_label, $new_track->label,
  152. "The label of the track was not updated.");
  153. // Finally, delete it.
  154. $success = tripal_jbrowse_mgmt_delete_track($track_id);
  155. $this->assertNotFalse($success, "Unable to delete track.");
  156. $success = tripal_jbrowse_mgmt_delete_track($track_id);
  157. $this->assertEquals(0, $success, "Deleted a track that doesn't exist?");
  158. }
  159. /**
  160. * Test organism-related api functions.
  161. */
  162. public function testOrganismAPI() {
  163. // Fake Organism.
  164. $faker = Factory::create();
  165. $organism = factory('chado.organism')->create();
  166. $organism_list = tripal_jbrowse_mgmt_get_organisms_list();
  167. $this->assertNotCount(0, $organism_list, "There should be at least one organism.");
  168. $this->assertArrayContainsObjectValue($organism_list, 'organism_id', $organism->organism_id);
  169. $organism_name = tripal_jbrowse_mgmt_construct_organism_name($organism);
  170. $this->assertRegexp('/'.$organism->genus.'/', $organism_name,
  171. "The organism name did not contain the genus.");
  172. $this->assertRegexp('/'.$organism->species.'/', $organism_name,
  173. "The organism name did not contain the species.");
  174. $slug = tripal_jbrowse_mgmt_make_slug($organism_name);
  175. $this->assertNotRegexp('/ /', $slug,
  176. "The organism slug should not contain spaces.");
  177. }
  178. /**
  179. * Test Instance Properties Create-Retrieve-Update.
  180. */
  181. public function testInstanceProperyCRU() {
  182. // Fake Instance.
  183. $faker = Factory::create();
  184. $organism = factory('chado.organism')->create();
  185. $instance_details = [
  186. 'organism_id' => $organism->organism_id,
  187. 'title' => $faker->words(3, TRUE),
  188. 'description' => $faker->sentence(25, TRUE),
  189. 'created_at' => $faker->unixTime(),
  190. 'file' => '/path/to/fake/file',
  191. ];
  192. $instance_id = tripal_jbrowse_mgmt_create_instance($instance_details);
  193. $testdata = [
  194. $faker->word() . '1' => $faker->words(3, TRUE),
  195. $faker->word() . '2' => $faker->words(2, TRUE),
  196. $faker->word() . '3' => $faker->words(10, TRUE),
  197. ];
  198. // Create.
  199. tripal_jbrowse_mgmt_save_instance_properties($instance_id, $testdata);
  200. // Retrieve.
  201. $properties = tripal_jbrowse_mgmt_get_instance_properties($instance_id);
  202. $this->assertIsArray($properties, "Unable to retrieve newly created properties.");
  203. $this->assertCount(3, $properties,
  204. "There was not the expected count of properties.");
  205. // Update single property.
  206. $property_name = key($testdata);
  207. $new_value = 'NEW VALUE '.uniqid();
  208. tripal_jbrowse_mgmt_save_instance_property($instance_id, $property_name, $new_value);
  209. $retrieved_value = tripal_jbrowse_mgmt_get_instance_property($instance_id, $property_name);
  210. $this->assertEquals($new_value, $retrieved_value,
  211. "We were unable to update a single property.");
  212. }
  213. /**
  214. * Test Miscellaneous API functions.
  215. */
  216. public function testMiscAPI() {
  217. $track_types = tripal_jbrowse_mgmt_get_track_types();
  218. $this->assertIsArray($track_types,
  219. "The track types should be an array.");
  220. }
  221. /**
  222. * Provide an assertion to check properties of an array of objects.
  223. *
  224. * For example, if you have an array of organism objects, the following code
  225. * would check that one object in the array has an organism_id of 50.
  226. * @code $this->assertArrayContainsObjectValue($array, 'organism_id', 50);
  227. */
  228. private function assertArrayContainsObjectValue($theArray, $attribute, $value)
  229. {
  230. foreach($theArray as $arrayItem) {
  231. if($arrayItem->$attribute == $value) {
  232. return true;
  233. }
  234. }
  235. return false;
  236. }
  237. }