sbo__relationship_widgetTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. namespace Tests\tripal_chado\fields;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. module_load_include('php', 'tripal_chado', '../tests/TripalFieldTestHelper');
  6. class sbo__relationship_widgetTest extends TripalTestCase {
  7. // Uncomment to auto start and rollback db transactions per test method.
  8. use DBTransaction;
  9. /**
  10. * Data Provider: provides entities matching important test cases.
  11. *
  12. * Specifically, we will cover three relationship tables, which represent
  13. * the diversity in the chado schema v1.3:
  14. * organism_relationship: subject_id, type_id, object_id,
  15. * stock_relationship: subject_id, type_id, object_id, value, rank,
  16. * project_relationship: subject_project_id, type_id, object_project_id, rank
  17. *
  18. * @returns
  19. * Returns an array where each item to be tested has the paramaters
  20. * needed for initializeWidgetClass(). Specfically, $bundle_name,
  21. * $field_name, $widget_name, $entity_id.
  22. */
  23. public function provideEntities() {
  24. $data = [];
  25. foreach (['organism', 'stock', 'project'] as $base_table) {
  26. $field_name = 'sbo__relationship';
  27. $widget_name = 'sbo__relationship_widget';
  28. // Find a bundle which stores it's data in the given base table.
  29. // This will work on Travis since Tripal creates matching bundles by default.
  30. $bundle_details = db_query("
  31. SELECT bundle_id, type_column, type_id
  32. FROM chado_bundle b
  33. WHERE data_table=:table AND type_linker_table=''
  34. ORDER BY bundle_id ASC LIMIT 1",
  35. array(':table' => $base_table))->fetchObject();
  36. if (isset($bundle_details->bundle_id)) {
  37. $bundle_id = $bundle_details->bundle_id;
  38. }
  39. else {
  40. continue;
  41. }
  42. $bundle_name = 'bio_data_'.$bundle_id;
  43. // Create some entities so that we know there are some available to find.
  44. if ($bundle_details->type_column == 'type_id') {
  45. $record_1 = factory('chado.'. $base_table)->create(['type_id' => $bundle_details->type_id]);
  46. $record_2 = factory('chado.'. $base_table)->create(['type_id' => $bundle_details->type_id]);
  47. }
  48. else {
  49. $record_1 = factory('chado.'. $base_table)->create();
  50. $record_2 = factory('chado.'. $base_table)->create();
  51. }
  52. // Then publish them so we have entities.
  53. $this->publish($base_table);
  54. // Find an entity from the above bundle.
  55. // @todo find a way to create a fake entity for use here.
  56. $entity_id = db_query('SELECT id FROM tripal_entity WHERE bundle=:bundle LIMIT 1',
  57. array(':bundle' => $bundle_name))->fetchField();
  58. // set variables to guide testing.
  59. $expect = [
  60. 'has_rank' => TRUE,
  61. 'has_value' => FALSE,
  62. 'subject_key' => 'subject_id',
  63. 'object_key' => 'object_id',
  64. 'base_table' => $base_table,
  65. 'relationship_table' => $base_table.'_relationship'
  66. ];
  67. if ($base_table == 'organism') { $expect['has_rank'] = FALSE; }
  68. if ($base_table == 'stock') { $expect['has_value'] = TRUE; }
  69. if ($base_table == 'project') {
  70. $expect['subject_key'] = 'subject_project_id';
  71. $expect['object_key'] = 'object_project_id';
  72. }
  73. $data[] = [$bundle_name, $field_name, $widget_name, $entity_id, $expect];
  74. }
  75. return $data;
  76. }
  77. /**
  78. * Test that we can initialize the widget properly.
  79. *
  80. * @dataProvider provideEntities()
  81. *
  82. * @group widget
  83. * @group sbo__relationship
  84. */
  85. public function testWidgetClassInitialization($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  86. // Load the entity.
  87. $entity = entity_load('TripalEntity', [$entity_id]);
  88. $entity = $entity[$entity_id];
  89. // Initialize the widget class via the TripalFieldTestHelper class.
  90. $machine_names = array(
  91. 'field_name' => $field_name,
  92. 'widget_name' => $widget_name,
  93. );
  94. $field_info = field_info_field($field_name);
  95. $instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  96. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity, $field_info, $instance_info);
  97. $widget_class = $helper->getInitializedClass();
  98. // Check we have the variables we initialized.
  99. $this->assertNotEmpty($helper->bundle,
  100. "Could not load the bundle.");
  101. $this->assertNotEmpty($helper->getFieldInfo($field_name),
  102. "Could not lookup the field information.");
  103. $this->assertNotEmpty($helper->getInstanceInfo($bundle_name, $field_name),
  104. "Could not lookup the instance information.");
  105. $this->assertNotEmpty($widget_class,
  106. "Couldn't create a widget class instance.");
  107. $this->assertNotEmpty($entity,
  108. "Couldn't load an entity.");
  109. // Check a little deeper...
  110. $this->assertEquals($helper->instance_info['settings']['chado_table'], $expect['relationship_table'],
  111. "Instance settings were not initialized fully.");
  112. }
  113. /**
  114. * Test the widget Form.
  115. *
  116. * @dataProvider provideEntities()
  117. *
  118. * @group widget
  119. * @group sbo__relationship
  120. */
  121. public function testWidgetForm($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  122. // Load the entity.
  123. $entity = entity_load('TripalEntity', [$entity_id]);
  124. $entity = $entity[$entity_id];
  125. // Initialize the widget class via the TripalFieldTestHelper class.
  126. $machine_names = array(
  127. 'field_name' => $field_name,
  128. 'widget_name' => $widget_name,
  129. );
  130. $field_info = field_info_field($field_name);
  131. $instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  132. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity, $field_info, $instance_info);
  133. $widget_class = $helper->getInitializedClass();
  134. $base_table = $entity->chado_table;
  135. // Stub out a fake objects.
  136. $delta = 1;
  137. $langcode = LANGUAGE_NONE;
  138. $widget = $helper->mockElement($delta, $langcode);
  139. $form = $helper->mockForm($delta, $langcode);
  140. $form_state = $helper->mockFormState($delta, $langcode);
  141. $element = $helper->mockElement($delta, $langcode);
  142. $items = [
  143. 'value' => '',
  144. 'chado-'.$base_table.'_relationship__organism_relationship_id' => '',
  145. 'chado-'.$base_table.'_relationship__subject_id' => '',
  146. 'chado-'.$base_table.'_relationship__object_id' => '',
  147. 'chado-'.$base_table.'_relationship__type_id' => '',
  148. 'object_name' => '',
  149. 'subject_name' => '',
  150. 'type_name' => '',
  151. ];
  152. // Execute the form method.
  153. $widget_class->form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  154. // Check the resulting for array
  155. $this->assertArrayHasKey('subject_name', $widget,
  156. "The form for $bundle_name($base_table) does not have a subject element.");
  157. $this->assertArrayHasKey('type_name', $widget,
  158. "The form for $bundle_name($base_table) does not have a type element.");
  159. $this->assertArrayHasKey('object_name', $widget,
  160. "The form for $bundle_name($base_table) does not have a object element.");
  161. // Check the subject/object keys were correctly determined.
  162. $this->assertEquals($expect['subject_key'], $widget['#subject_id_key'],
  163. "The form didn't determine the subject key correctly.");
  164. $this->assertEquals($expect['object_key'], $widget['#object_id_key'],
  165. "The form didn't determine the object key correctly.");
  166. }
  167. /**
  168. * DataProvider: Provides datasets to validate.
  169. */
  170. public function provideThings2Validate() {
  171. $data = [];
  172. foreach (['organism', 'stock', 'project'] as $base_table) {
  173. $base_table = $base_table;
  174. $field_name = 'sbo__relationship';
  175. $widget_name = 'sbo__relationship_widget';
  176. // find a bundle which stores it's data in the given base table.
  177. // This will work on Travis since Tripal creates matching bundles by default.
  178. // @todo ideally we would create a fake bundle here.
  179. $bundle_id = db_query("
  180. SELECT bundle_id
  181. FROM chado_bundle b
  182. LEFT JOIN tripal_entity e ON e.bundle='bio_data_'||b.bundle_id
  183. WHERE data_table=:table AND id IS NOT NULL LIMIT 1",
  184. array(':table' => $base_table))->fetchField();
  185. if (!$bundle_id) {
  186. continue;
  187. }
  188. $bundle_name = 'bio_data_'.$bundle_id;
  189. // set variables to guide testing.
  190. $expect = [
  191. 'has_rank' => TRUE,
  192. 'has_value' => FALSE,
  193. 'subject_key' => 'subject_id',
  194. 'object_key' => 'object_id',
  195. 'base_table' => $base_table,
  196. 'relationship_table' => $base_table.'_relationship',
  197. ];
  198. if ($base_table == 'organism') { $expect['has_rank'] = FALSE; }
  199. if ($base_table == 'stock') { $expect['has_value'] = TRUE; }
  200. if ($base_table == 'project') {
  201. $expect['subject_key'] = 'subject_project_id';
  202. $expect['object_key'] = 'object_project_id';
  203. }
  204. // Create 5 fake records and publish them.
  205. $records = factory('chado.'.$base_table, 5)->create();
  206. $this->publish($base_table);
  207. $cvterm = factory('chado.cvterm')->create();
  208. // Find an entity from the above bundle.
  209. $ids = db_query('SELECT id FROM {tripal_entity} WHERE bundle=:bundle LIMIT 2',
  210. array(':bundle' => $bundle_name))->fetchCol();
  211. $entities = entity_load('TripalEntity', $ids);
  212. $entity = array_pop($entities);
  213. $entity2 = array_pop($entities);
  214. // Now Build our test cases for this base table.
  215. foreach (['user_create', 'existing', 'no_subject', 'no_object', 'no_type'] as $case) {
  216. $expect['test_case'] = $case;
  217. // First assume "existing" (later we will modify based on case).
  218. $values = [
  219. 'subject_name' => $entity2->chado_record->name,
  220. 'type_name' => $cvterm->name,
  221. 'vocabulary' => $cvterm->cv_id,
  222. 'object_name' => $entity->chado_record->name,
  223. // Both the form and load set the chado values
  224. // so we will set them here as well.
  225. 'chado-'.$base_table.'_relationship__'.$expect['subject_key'] => $entity2->chado_record->{$base_table.'_id'},
  226. 'chado-'.$base_table.'_relationship__type_id' => $cvterm->cvterm_id,
  227. 'chado-'.$base_table.'_relationship__'.$expect['object_key'] => $entity->chado_record->{$base_table.'_id'},
  228. ];
  229. if ($base_table == 'organism') {
  230. $values['subject_name'] = $entity2->chado_record->species;
  231. $values['object_name'] = $entity->chado_record->species;
  232. }
  233. $expect['num_errors'] = 0;
  234. // Now modify based on the case.
  235. switch ($case) {
  236. case 'user_create':
  237. $values[ 'chado-'.$base_table.'_relationship__'.$expect['subject_key'] ] = NULL;
  238. $values[ 'chado-'.$base_table.'_relationship__type_id' ] = NULL;
  239. $values[ 'chado-'.$base_table.'_relationship__'.$expect['object_key'] ] = NULL;
  240. break;
  241. case 'no_subject':
  242. $values['subject_name'] = '';
  243. $values[ 'chado-'.$base_table.'_relationship__'.$expect['subject_key'] ] = NULL;
  244. $expect['num_errors'] = 1;
  245. break;
  246. case 'no_object':
  247. $values['object_name'] = '';
  248. $values[ 'chado-'.$base_table.'_relationship__'.$expect['object_key'] ] = NULL;
  249. $expect['num_errors'] = 1;
  250. break;
  251. case 'no_type':
  252. $values['type_name'] = '';
  253. $values['vocabulary'] = NULL;
  254. $values[ 'chado-'.$base_table.'_relationship__type_id' ] = NULL;
  255. $expect['num_errors'] = 1;
  256. break;
  257. }
  258. $data[] = [
  259. [
  260. 'field_name' => $field_name,
  261. 'widget_name' => $widget_name,
  262. 'bundle_id' => $bundle_id,
  263. 'bundle_name' => $bundle_name,
  264. ],
  265. $entity,
  266. $values,
  267. $expect,
  268. ];
  269. }
  270. }
  271. return $data;
  272. }
  273. /**
  274. * Test sbo__relationship_widget->validate().
  275. *
  276. * @dataProvider provideThings2Validate()
  277. *
  278. * @group lacey-wip
  279. * @group widget
  280. * @group sbo__relationship
  281. */
  282. public function testWidgetValidate($info, $entity, $initial_values, $expect) {
  283. $base_table = $entity->chado_table;
  284. // Initialize the widget class via the TripalFieldTestHelper class.
  285. $machine_names = array(
  286. 'field_name' => $info['field_name'],
  287. 'widget_name' => $info['widget_name'],
  288. );
  289. $field_info = field_info_field($info['field_name']);
  290. $instance_info = field_info_instance('TripalEntity', $info['field_name'], $info['bundle_name']);
  291. $helper = new \TripalFieldTestHelper($info['bundle_name'], $machine_names, $entity, $field_info, $instance_info);
  292. $widget_class = $helper->getInitializedClass();
  293. // Mock objects.
  294. $field_name = $info['field_name'];
  295. $delta = 1;
  296. $langcode = LANGUAGE_NONE;
  297. $widget = $helper->mockElement($delta, $langcode);
  298. $form = $helper->mockForm($delta, $langcode);
  299. $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
  300. $element = $helper->mockElement($delta, $langcode);
  301. $widget_class->validate($element, $form, $form_state, $langcode, $delta);
  302. // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
  303. // Ensure the chado-table__column entries are there.
  304. $this->assertArrayHasKey(
  305. 'chado-'.$base_table.'_relationship__'.$expect['subject_key'],
  306. $form_state['values'][$field_name][$langcode][$delta],
  307. 'Failed to find the subject_id in the processed values (Base: '.$base_table.'). This implies the validate function was not able to validate the subject.'
  308. );
  309. $this->assertArrayHasKey(
  310. 'chado-'.$base_table.'_relationship__'.$expect['object_key'],
  311. $form_state['values'][$field_name][$langcode][$delta],
  312. 'Failed to find the object_id in the processed values (Base: '.$base_table.'). This implies the validate function was not able to validate the object.'
  313. );
  314. $this->assertArrayHasKey(
  315. 'chado-'.$base_table.'_relationship__type_id',
  316. $form_state['values'][$field_name][$langcode][$delta],
  317. 'Failed to find the type_id in the processed values (Base: '.$base_table.'). This implies the validate function was not able to validate the type.'
  318. );
  319. // Check for errors.
  320. $errors = form_get_errors();
  321. // @debug print "Errors: " . print_r($errors, TRUE)."\n";
  322. if ($expect['num_errors'] === 0) {
  323. $this->assertEmpty($errors,
  324. "There should be no form errors for the following initial values: ".print_r($initial_values,TRUE)." But these were registered: ".print_r($errors, TRUE));
  325. }
  326. else {
  327. $this->assertEquals($expect['num_errors'], sizeof($errors),
  328. "The number of errors didn't match what we expected for the following initial values: ".print_r($initial_values,TRUE)." Here are the errors: ".print_r($errors, TRUE));
  329. }
  330. // Clean up after ourselves by removing any errors we logged.
  331. form_clear_error();
  332. }
  333. }