sbo__relationship_widgetTest.php 15 KB

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