sbo__relationship_widgetTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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_ids, $expect.
  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. $chado_records = factory('chado.'. $base_table, 2)->create(['type_id' => $bundle_details->type_id]);
  46. }
  47. else {
  48. $chado_records = factory('chado.'. $base_table, 2)->create();
  49. }
  50. // Then publish them so we have entities.
  51. $this->publish($base_table);
  52. // Find our fake entities from the above bundle.
  53. $entity_ids = [];
  54. $entity_ids[] = db_query('SELECT entity_id FROM chado_'.$bundle_name.' WHERE record_id=:chado_id',
  55. array(':chado_id' => $chado_records[0]->{$base_table.'_id'}))->fetchField();
  56. $entity_ids[] = db_query('SELECT entity_id FROM chado_'.$bundle_name.' WHERE record_id=:chado_id',
  57. array(':chado_id' => $chado_records[1]->{$base_table.'_id'}))->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_ids, $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_ids, $expect) {
  86. $entity_id = $entity_ids[0];
  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_ids, $expect) {
  123. $entity_id = $entity_ids[0];
  124. // Load the entity.
  125. $entity = entity_load('TripalEntity', [$entity_ids]);
  126. $entity = $entity[$entity_id];
  127. // Initialize the widget class via the TripalFieldTestHelper class.
  128. $machine_names = array(
  129. 'field_name' => $field_name,
  130. 'widget_name' => $widget_name,
  131. );
  132. $field_info = field_info_field($field_name);
  133. $instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  134. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity, $field_info, $instance_info);
  135. $widget_class = $helper->getInitializedClass();
  136. $base_table = $entity->chado_table;
  137. // Stub out a fake objects.
  138. $delta = 1;
  139. $langcode = LANGUAGE_NONE;
  140. $widget = $helper->mockElement($delta, $langcode);
  141. $form = $helper->mockForm($delta, $langcode);
  142. $form_state = $helper->mockFormState($delta, $langcode);
  143. $element = $helper->mockElement($delta, $langcode);
  144. $items = [
  145. 'value' => '',
  146. 'chado-'.$base_table.'_relationship__organism_relationship_id' => '',
  147. 'chado-'.$base_table.'_relationship__subject_id' => '',
  148. 'chado-'.$base_table.'_relationship__object_id' => '',
  149. 'chado-'.$base_table.'_relationship__type_id' => '',
  150. 'object_name' => '',
  151. 'subject_name' => '',
  152. 'type_name' => '',
  153. ];
  154. // Execute the form method.
  155. $widget_class->form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  156. // Check the resulting for array
  157. $this->assertArrayHasKey('subject_name', $widget,
  158. "The form for $bundle_name($base_table) does not have a subject element.");
  159. $this->assertArrayHasKey('type_name', $widget,
  160. "The form for $bundle_name($base_table) does not have a type element.");
  161. $this->assertArrayHasKey('object_name', $widget,
  162. "The form for $bundle_name($base_table) does not have a object element.");
  163. // Check the subject/object keys were correctly determined.
  164. $this->assertEquals($expect['subject_key'], $widget['#subject_id_key'],
  165. "The form didn't determine the subject key correctly.");
  166. $this->assertEquals($expect['object_key'], $widget['#object_id_key'],
  167. "The form didn't determine the object key correctly.");
  168. }
  169. /**
  170. * Case: WidgetValidate on existing relationship.
  171. *
  172. * @dataProvider provideEntities()
  173. *
  174. * @group widget
  175. * @group sbo__relationship
  176. */
  177. public function testWidgetValidate_existing($bundle_name, $field_name, $widget_name, $entity_ids, $expect) {
  178. // Load the entities.
  179. $entities = entity_load('TripalEntity', $entity_ids);
  180. $entity1 = $entities[$entity_ids[0]];
  181. $entity2 = $entities[$entity_ids[1]];
  182. $base_table = $entity1->chado_table;
  183. // Initialize the widget class via the TripalFieldTestHelper class.
  184. $machine_names = array(
  185. 'field_name' => $field_name,
  186. 'widget_name' => $widget_name,
  187. );
  188. $field_info = field_info_field($field_name);
  189. $instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  190. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity1, $field_info, $instance_info);
  191. $widget_class = $helper->getInitializedClass();
  192. // Set some initial values.
  193. $cvterm = factory('chado.cvterm')->create();
  194. $initial_values = [
  195. 'subject_name' => $entity2->chado_record->name,
  196. 'type_name' => $cvterm->name,
  197. 'vocabulary' => $cvterm->cv_id,
  198. 'object_name' => $entity1->chado_record->name,
  199. // Both the form and load set the chado values
  200. // so we will set them here as well.
  201. 'chado-'.$base_table.'_relationship__'.$expect['subject_key'] => $entity2->chado_record->{$base_table.'_id'},
  202. 'chado-'.$base_table.'_relationship__type_id' => $cvterm->cvterm_id,
  203. 'chado-'.$base_table.'_relationship__'.$expect['object_key'] => $entity1->chado_record->{$base_table.'_id'},
  204. ];
  205. if ($base_table == 'organism') {
  206. $initial_values['subject_name'] = $entity2->chado_record->species;
  207. $initial_values['object_name'] = $entity1->chado_record->species;
  208. }
  209. // Mock objects.
  210. $delta = 1;
  211. $langcode = LANGUAGE_NONE;
  212. $widget = $helper->mockElement($delta, $langcode);
  213. $form = $helper->mockForm($delta, $langcode);
  214. $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
  215. $element = $helper->mockElement($delta, $langcode);
  216. $widget_class->validate($element, $form, $form_state, $langcode, $delta);
  217. // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
  218. // Ensure the chado-table__column entries are there.
  219. $this->assertArrayHasKey(
  220. 'chado-'.$base_table.'_relationship__'.$expect['subject_key'],
  221. $form_state['values'][$field_name][$langcode][$delta],
  222. '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.'
  223. );
  224. $this->assertArrayHasKey(
  225. 'chado-'.$base_table.'_relationship__'.$expect['object_key'],
  226. $form_state['values'][$field_name][$langcode][$delta],
  227. '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.'
  228. );
  229. $this->assertArrayHasKey(
  230. 'chado-'.$base_table.'_relationship__type_id',
  231. $form_state['values'][$field_name][$langcode][$delta],
  232. '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.'
  233. );
  234. // Check for errors.
  235. $errors = form_get_errors();
  236. // @debug print "Errors: " . print_r($errors, TRUE)."\n";
  237. $this->assertEmpty($errors,
  238. "There should be no form errors when subject and object are pre-existing and both are supplied. Initial values: ".print_r($initial_values,TRUE)." But these were registered: ".print_r($errors, TRUE));
  239. // Clean up after ourselves by removing any errors we logged.
  240. form_clear_error();
  241. }
  242. /**
  243. * Case: WidgetValidate on new relationship filled out properly.
  244. *
  245. * @dataProvider provideEntities()
  246. *
  247. * @group widget
  248. * @group sbo__relationship
  249. */
  250. public function testWidgetValidate_create($bundle_name, $field_name, $widget_name, $entity_ids, $expect) {
  251. // Load the entities.
  252. $entities = entity_load('TripalEntity', $entity_ids);
  253. $entity1 = $entities[$entity_ids[0]];
  254. $entity2 = $entities[$entity_ids[1]];
  255. $base_table = $entity1->chado_table;
  256. // Initialize the widget class via the TripalFieldTestHelper class.
  257. $machine_names = array(
  258. 'field_name' => $field_name,
  259. 'widget_name' => $widget_name,
  260. );
  261. $field_info = field_info_field($field_name);
  262. $instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  263. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity1, $field_info, $instance_info);
  264. $widget_class = $helper->getInitializedClass();
  265. // Set some initial values.
  266. $cvterm = factory('chado.cvterm')->create();
  267. $initial_values = [
  268. 'subject_name' => $entity2->chado_record->name,
  269. 'type_name' => $cvterm->name,
  270. 'vocabulary' => $cvterm->cv_id,
  271. 'object_name' => $entity1->chado_record->name,
  272. // These are not set on the creation form.
  273. 'chado-'.$base_table.'_relationship__'.$expect['subject_key'] => NULL,
  274. 'chado-'.$base_table.'_relationship__type_id' => NULL,
  275. 'chado-'.$base_table.'_relationship__'.$expect['object_key'] => NULL,
  276. ];
  277. if ($base_table == 'organism') {
  278. $initial_values['subject_name'] = $entity2->chado_record->species;
  279. $initial_values['object_name'] = $entity1->chado_record->species;
  280. }
  281. // Mock objects.
  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. $this->assertEmpty($errors,
  310. "There should be no form errors when subject and object are pre-existing and both are supplied. Initial values: ".print_r($initial_values,TRUE)." But these were registered: ".print_r($errors, TRUE));
  311. // Clean up after ourselves by removing any errors we logged.
  312. form_clear_error();
  313. }
  314. /**
  315. * Case: WidgetValidate on new relationship missing subject.
  316. *
  317. * @dataProvider provideEntities()
  318. *
  319. * @group widget
  320. * @group sbo__relationship
  321. */
  322. public function testWidgetValidate_nosubject($bundle_name, $field_name, $widget_name, $entity_ids, $expect) {
  323. // Load the entities.
  324. $entities = entity_load('TripalEntity', $entity_ids);
  325. $entity1 = $entities[$entity_ids[0]];
  326. $entity2 = $entities[$entity_ids[1]];
  327. $base_table = $entity1->chado_table;
  328. // Initialize the widget class via the TripalFieldTestHelper class.
  329. $machine_names = array(
  330. 'field_name' => $field_name,
  331. 'widget_name' => $widget_name,
  332. );
  333. $field_info = field_info_field($field_name);
  334. $instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  335. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity1, $field_info, $instance_info);
  336. $widget_class = $helper->getInitializedClass();
  337. // Set some initial values.
  338. $cvterm = factory('chado.cvterm')->create();
  339. $initial_values = [
  340. 'subject_name' => '',
  341. 'type_name' => $cvterm->name,
  342. 'vocabulary' => $cvterm->cv_id,
  343. 'object_name' => $entity1->chado_record->name,
  344. // Both the form and load set the chado values
  345. // so we will set them here as well.
  346. 'chado-'.$base_table.'_relationship__'.$expect['subject_key'] => NULL,
  347. 'chado-'.$base_table.'_relationship__type_id' => $cvterm->cvterm_id,
  348. 'chado-'.$base_table.'_relationship__'.$expect['object_key'] => $entity1->chado_record->{$base_table.'_id'},
  349. ];
  350. if ($base_table == 'organism') {
  351. $initial_values['object_name'] = $entity1->chado_record->species;
  352. }
  353. // Mock objects.
  354. $delta = 1;
  355. $langcode = LANGUAGE_NONE;
  356. $widget = $helper->mockElement($delta, $langcode);
  357. $form = $helper->mockForm($delta, $langcode);
  358. $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
  359. $element = $helper->mockElement($delta, $langcode);
  360. $widget_class->validate($element, $form, $form_state, $langcode, $delta);
  361. // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
  362. // Ensure the chado-table__column entries are there.
  363. $this->assertArrayHasKey(
  364. 'chado-'.$base_table.'_relationship__'.$expect['subject_key'],
  365. $form_state['values'][$field_name][$langcode][$delta],
  366. '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.'
  367. );
  368. $this->assertArrayHasKey(
  369. 'chado-'.$base_table.'_relationship__'.$expect['object_key'],
  370. $form_state['values'][$field_name][$langcode][$delta],
  371. '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.'
  372. );
  373. $this->assertArrayHasKey(
  374. 'chado-'.$base_table.'_relationship__type_id',
  375. $form_state['values'][$field_name][$langcode][$delta],
  376. '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.'
  377. );
  378. // Check for errors.
  379. $errors = form_get_errors();
  380. // @debug print "Errors: " . print_r($errors, TRUE)."\n";
  381. $this->assertNotEmpty($errors,
  382. "There should be form errors when subject is not supplied. Initial values: ".print_r($initial_values,TRUE)." But these were registered: ".print_r($errors, TRUE));
  383. // Clean up after ourselves by removing any errors we logged.
  384. form_clear_error();
  385. }
  386. /**
  387. * Case: WidgetValidate on new relationship missing object.
  388. *
  389. * @dataProvider provideEntities()
  390. *
  391. * @group widget
  392. * @group sbo__relationship
  393. */
  394. public function testWidgetValidate_noobject($bundle_name, $field_name, $widget_name, $entity_ids, $expect) {
  395. // Load the entities.
  396. $entities = entity_load('TripalEntity', $entity_ids);
  397. $entity1 = $entities[$entity_ids[0]];
  398. $entity2 = $entities[$entity_ids[1]];
  399. $base_table = $entity1->chado_table;
  400. // Initialize the widget class via the TripalFieldTestHelper class.
  401. $machine_names = array(
  402. 'field_name' => $field_name,
  403. 'widget_name' => $widget_name,
  404. );
  405. $field_info = field_info_field($field_name);
  406. $instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  407. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity1, $field_info, $instance_info);
  408. $widget_class = $helper->getInitializedClass();
  409. // Set some initial values.
  410. $cvterm = factory('chado.cvterm')->create();
  411. $initial_values = [
  412. 'subject_name' => $entity2->chado_record->name,
  413. 'type_name' => $cvterm->name,
  414. 'vocabulary' => $cvterm->cv_id,
  415. 'object_name' => '',
  416. // Both the form and load set the chado values
  417. // so we will set them here as well.
  418. 'chado-'.$base_table.'_relationship__'.$expect['subject_key'] => $entity2->chado_record->{$base_table.'_id'},
  419. 'chado-'.$base_table.'_relationship__type_id' => $cvterm->cvterm_id,
  420. 'chado-'.$base_table.'_relationship__'.$expect['object_key'] => NULL,
  421. ];
  422. if ($base_table == 'organism') {
  423. $initial_values['subject_name'] = $entity2->chado_record->species;
  424. }
  425. // Mock objects.
  426. $delta = 1;
  427. $langcode = LANGUAGE_NONE;
  428. $widget = $helper->mockElement($delta, $langcode);
  429. $form = $helper->mockForm($delta, $langcode);
  430. $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
  431. $element = $helper->mockElement($delta, $langcode);
  432. $widget_class->validate($element, $form, $form_state, $langcode, $delta);
  433. // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
  434. // Ensure the chado-table__column entries are there.
  435. $this->assertArrayHasKey(
  436. 'chado-'.$base_table.'_relationship__'.$expect['subject_key'],
  437. $form_state['values'][$field_name][$langcode][$delta],
  438. '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.'
  439. );
  440. $this->assertArrayHasKey(
  441. 'chado-'.$base_table.'_relationship__'.$expect['object_key'],
  442. $form_state['values'][$field_name][$langcode][$delta],
  443. '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.'
  444. );
  445. $this->assertArrayHasKey(
  446. 'chado-'.$base_table.'_relationship__type_id',
  447. $form_state['values'][$field_name][$langcode][$delta],
  448. '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.'
  449. );
  450. // Check for errors.
  451. $errors = form_get_errors();
  452. // @debug print "Errors: " . print_r($errors, TRUE)."\n";
  453. $this->assertNotEmpty($errors,
  454. "There should be form errors when the object is not supplied. Initial values: ".print_r($initial_values,TRUE)." But these were registered: ".print_r($errors, TRUE));
  455. // Clean up after ourselves by removing any errors we logged.
  456. form_clear_error();
  457. }
  458. /**
  459. * Case: WidgetValidate on new relationship missing type.
  460. *
  461. * @dataProvider provideEntities()
  462. *
  463. * @group widget
  464. * @group sbo__relationship
  465. */
  466. public function testWidgetValidate_notype($bundle_name, $field_name, $widget_name, $entity_ids, $expect) {
  467. // Load the entities.
  468. $entities = entity_load('TripalEntity', $entity_ids);
  469. $entity1 = $entities[$entity_ids[0]];
  470. $entity2 = $entities[$entity_ids[1]];
  471. $base_table = $entity1->chado_table;
  472. // Initialize the widget class via the TripalFieldTestHelper class.
  473. $machine_names = array(
  474. 'field_name' => $field_name,
  475. 'widget_name' => $widget_name,
  476. );
  477. $field_info = field_info_field($field_name);
  478. $instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  479. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity1, $field_info, $instance_info);
  480. $widget_class = $helper->getInitializedClass();
  481. // Set some initial values.
  482. $initial_values = [
  483. 'subject_name' => $entity2->chado_record->name,
  484. 'type_name' => '',
  485. 'vocabulary' => NULL,
  486. 'object_name' => $entity1->chado_record->name,
  487. // Both the form and load set the chado values
  488. // so we will set them here as well.
  489. 'chado-'.$base_table.'_relationship__'.$expect['subject_key'] => $entity2->chado_record->{$base_table.'_id'},
  490. 'chado-'.$base_table.'_relationship__type_id' => NULL,
  491. 'chado-'.$base_table.'_relationship__'.$expect['object_key'] => $entity1->chado_record->{$base_table.'_id'},
  492. ];
  493. if ($base_table == 'organism') {
  494. $initial_values['subject_name'] = $entity2->chado_record->species;
  495. $initial_values['object_name'] = $entity1->chado_record->species;
  496. }
  497. // Mock objects.
  498. $delta = 1;
  499. $langcode = LANGUAGE_NONE;
  500. $widget = $helper->mockElement($delta, $langcode);
  501. $form = $helper->mockForm($delta, $langcode);
  502. $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
  503. $element = $helper->mockElement($delta, $langcode);
  504. $widget_class->validate($element, $form, $form_state, $langcode, $delta);
  505. // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
  506. // Ensure the chado-table__column entries are there.
  507. $this->assertArrayHasKey(
  508. 'chado-'.$base_table.'_relationship__'.$expect['subject_key'],
  509. $form_state['values'][$field_name][$langcode][$delta],
  510. '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.'
  511. );
  512. $this->assertArrayHasKey(
  513. 'chado-'.$base_table.'_relationship__'.$expect['object_key'],
  514. $form_state['values'][$field_name][$langcode][$delta],
  515. '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.'
  516. );
  517. $this->assertArrayHasKey(
  518. 'chado-'.$base_table.'_relationship__type_id',
  519. $form_state['values'][$field_name][$langcode][$delta],
  520. '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.'
  521. );
  522. // Check for errors.
  523. $errors = form_get_errors();
  524. // @debug print "Errors: " . print_r($errors, TRUE)."\n";
  525. $this->assertNotEmpty($errors,
  526. "There should be form errors when type is not supplied. Initial values: ".print_r($initial_values,TRUE)." But these were registered: ".print_r($errors, TRUE));
  527. // Clean up after ourselves by removing any errors we logged.
  528. form_clear_error();
  529. }
  530. }