sbo__relationship_widgetTest.php 14 KB

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