sbo__relationship_widgetTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. <?php
  2. namespace Tests\tripal_chado\fields;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. /**
  6. *
  7. */
  8. class sbo__relationship_widgetTest extends TripalTestCase {
  9. // Uncomment to auto start and rollback db transactions per test method.
  10. use DBTransaction;
  11. /**
  12. * Data Provider: provides entities matching important test cases.
  13. *
  14. * Specifically, we will cover three relationship tables, which represent
  15. * the diversity in the chado schema v1.3:
  16. * organism_relationship: subject_id, type_id, object_id,
  17. * stock_relationship: subject_id, type_id, object_id, value, rank,
  18. * project_relationship: subject_project_id, type_id, object_project_id,
  19. * rank.
  20. *
  21. * @returns
  22. * Returns an array where each item to be tested has the paramaters
  23. * needed for initializeWidgetClass(). Specfically, $bundle_name,
  24. * $field_name, $widget_name, $entity_ids, $expect.
  25. */
  26. public function provideEntities() {
  27. $data = [];
  28. foreach (['organism', 'stock', 'project'] as $base_table) {
  29. $field_name = 'sbo__relationship';
  30. $widget_name = 'sbo__relationship_widget';
  31. // Find a bundle which stores it's data in the given base table.
  32. // This will work on Travis since Tripal creates matching bundles by default.
  33. $bundle_details = db_query(
  34. "
  35. SELECT bundle_id, type_column, type_id
  36. FROM chado_bundle b
  37. WHERE data_table=:table AND type_linker_table=''
  38. ORDER BY bundle_id ASC LIMIT 1", [':table' => $base_table]
  39. )->fetchObject();
  40. if (isset($bundle_details->bundle_id)) {
  41. $bundle_id = $bundle_details->bundle_id;
  42. }
  43. else {
  44. continue;
  45. }
  46. $bundle_name = 'bio_data_' . $bundle_id;
  47. // Create some entities so that we know there are some available to find.
  48. if ($bundle_details->type_column == 'type_id') {
  49. $chado_records = factory('chado.' . $base_table, 2)->create(
  50. ['type_id' => $bundle_details->type_id]
  51. );
  52. }
  53. else {
  54. $chado_records = factory('chado.' . $base_table, 2)->create();
  55. }
  56. // Then publish them so we have entities.
  57. $this->publish($base_table);
  58. // Find our fake entities from the above bundle.
  59. $entity_ids = [];
  60. $entity_ids[] = db_query(
  61. 'SELECT entity_id FROM chado_' . $bundle_name
  62. . ' WHERE record_id=:chado_id',
  63. [':chado_id' => $chado_records[0]->{$base_table . '_id'}]
  64. )->fetchField();
  65. $entity_ids[] = db_query(
  66. 'SELECT entity_id FROM chado_' . $bundle_name
  67. . ' WHERE record_id=:chado_id',
  68. [':chado_id' => $chado_records[1]->{$base_table . '_id'}]
  69. )->fetchField();
  70. // Set variables to guide testing.
  71. $expect = [
  72. 'has_rank' => TRUE,
  73. 'has_value' => FALSE,
  74. 'subject_key' => 'subject_id',
  75. 'object_key' => 'object_id',
  76. 'base_table' => $base_table,
  77. 'relationship_table' => $base_table . '_relationship',
  78. ];
  79. if ($base_table == 'organism') {
  80. $expect['has_rank'] = FALSE;
  81. }
  82. if ($base_table == 'stock') {
  83. $expect['has_value'] = TRUE;
  84. }
  85. if ($base_table == 'project') {
  86. $expect['subject_key'] = 'subject_project_id';
  87. $expect['object_key'] = 'object_project_id';
  88. }
  89. $data[] = [$bundle_name, $field_name, $widget_name, $entity_ids, $expect];
  90. }
  91. return $data;
  92. }
  93. /**
  94. * Test that we can initialize the widget properly.
  95. *
  96. * @group widget
  97. * @group sbo__relationship
  98. */
  99. public function testWidgetClassInitialization() {
  100. foreach ($this->provideEntities() as $dataEntry) {
  101. list(
  102. $bundle_name, $field_name, $widget_name, $entity_ids, $expect
  103. ) = $dataEntry;
  104. $entity_id = $entity_ids[0];
  105. // Load the entity.
  106. $entity = entity_load('TripalEntity', [$entity_id]);
  107. $entity = $entity[$entity_id];
  108. // Initialize the widget class via the TripalFieldTestHelper class.
  109. $machine_names = [
  110. 'field_name' => $field_name,
  111. 'widget_name' => $widget_name,
  112. ];
  113. $field_info = field_info_field($field_name);
  114. $instance_info = field_info_instance(
  115. 'TripalEntity', $field_name, $bundle_name
  116. );
  117. $helper = new \TripalFieldTestHelper(
  118. $bundle_name, $machine_names, $entity, $field_info, $instance_info
  119. );
  120. $widget_class = $helper->getInitializedClass();
  121. // Check we have the variables we initialized.
  122. $this->assertNotEmpty(
  123. $helper->bundle, "Could not load the bundle."
  124. );
  125. $this->assertNotEmpty(
  126. $helper->getFieldInfo($field_name),
  127. "Could not lookup the field information."
  128. );
  129. $this->assertNotEmpty(
  130. $helper->getInstanceInfo($bundle_name, $field_name),
  131. "Could not lookup the instance information."
  132. );
  133. $this->assertNotEmpty(
  134. $widget_class, "Couldn't create a widget class instance."
  135. );
  136. $this->assertNotEmpty(
  137. $entity, "Couldn't load an entity."
  138. );
  139. // Check a little deeper...
  140. $this->assertEquals(
  141. $helper->instance_info['settings']['chado_table'],
  142. $expect['relationship_table'],
  143. "Instance settings were not initialized fully."
  144. );
  145. }
  146. }
  147. /**
  148. * Test the widget Form.
  149. *
  150. * @group widget
  151. * @group sbo__relationship
  152. */
  153. public function testWidgetForm() {
  154. foreach ($this->provideEntities() as $dataEntry) {
  155. list(
  156. $bundle_name, $field_name, $widget_name, $entity_ids, $expect
  157. ) = $dataEntry;
  158. $entity_id = $entity_ids[0];
  159. // Load the entity.
  160. $entity = entity_load('TripalEntity', [$entity_ids]);
  161. $entity = $entity[$entity_id];
  162. // Initialize the widget class via the TripalFieldTestHelper class.
  163. $machine_names = [
  164. 'field_name' => $field_name,
  165. 'widget_name' => $widget_name,
  166. ];
  167. $field_info = field_info_field($field_name);
  168. $instance_info = field_info_instance(
  169. 'TripalEntity', $field_name, $bundle_name
  170. );
  171. $helper = new \TripalFieldTestHelper(
  172. $bundle_name, $machine_names, $entity, $field_info, $instance_info
  173. );
  174. $widget_class = $helper->getInitializedClass();
  175. $base_table = $entity->chado_table;
  176. // Stub out a fake objects.
  177. $delta = 1;
  178. $langcode = LANGUAGE_NONE;
  179. $widget = $helper->mockElement($delta, $langcode);
  180. $form = $helper->mockForm($delta, $langcode);
  181. $form_state = $helper->mockFormState($delta, $langcode);
  182. $element = $helper->mockElement($delta, $langcode);
  183. $items = [
  184. 'value' => '',
  185. 'chado-' . $base_table . '_relationship__organism_relationship_id' => '',
  186. 'chado-' . $base_table . '_relationship__subject_id' => '',
  187. 'chado-' . $base_table . '_relationship__object_id' => '',
  188. 'chado-' . $base_table . '_relationship__type_id' => '',
  189. 'object_name' => '',
  190. 'subject_name' => '',
  191. 'type_name' => '',
  192. ];
  193. // Execute the form method.
  194. $widget_class->form(
  195. $widget, $form, $form_state, $langcode, $items, $delta, $element
  196. );
  197. // Check the resulting for array.
  198. $this->assertArrayHasKey(
  199. 'subject_name', $widget,
  200. "The form for $bundle_name($base_table) does not have a subject element."
  201. );
  202. $this->assertArrayHasKey(
  203. 'type_name', $widget,
  204. "The form for $bundle_name($base_table) does not have a type element."
  205. );
  206. $this->assertArrayHasKey(
  207. 'object_name', $widget,
  208. "The form for $bundle_name($base_table) does not have a object element."
  209. );
  210. // Check the subject/object keys were correctly determined.
  211. $this->assertEquals(
  212. $expect['subject_key'], $widget['#subject_id_key'],
  213. "The form didn't determine the subject key correctly."
  214. );
  215. $this->assertEquals(
  216. $expect['object_key'], $widget['#object_id_key'],
  217. "The form didn't determine the object key correctly."
  218. );
  219. }
  220. }
  221. /**
  222. * Case: WidgetValidate on existing relationship.
  223. *
  224. * @group widget
  225. * @group sbo__relationship
  226. */
  227. public function testWidgetValidate_existing() {
  228. foreach ($this->provideEntities() as $dataEntry) {
  229. list(
  230. $bundle_name, $field_name, $widget_name, $entity_ids, $expect
  231. ) = $dataEntry;
  232. // Load the entities.
  233. $entities = entity_load('TripalEntity', $entity_ids);
  234. $entity1 = $entities[$entity_ids[0]];
  235. $entity2 = $entities[$entity_ids[1]];
  236. $base_table = $entity1->chado_table;
  237. // Initialize the widget class via the TripalFieldTestHelper class.
  238. $machine_names = [
  239. 'field_name' => $field_name,
  240. 'widget_name' => $widget_name,
  241. ];
  242. $field_info = field_info_field($field_name);
  243. $instance_info = field_info_instance(
  244. 'TripalEntity', $field_name, $bundle_name
  245. );
  246. $helper = new \TripalFieldTestHelper(
  247. $bundle_name, $machine_names, $entity1, $field_info, $instance_info
  248. );
  249. $widget_class = $helper->getInitializedClass();
  250. // Set some initial values.
  251. $cvterm = factory('chado.cvterm')->create();
  252. $initial_values = [
  253. 'subject_name' => $entity2->chado_record->name,
  254. 'type_name' => $cvterm->name,
  255. 'vocabulary' => $cvterm->cv_id,
  256. 'object_name' => $entity1->chado_record->name,
  257. // Both the form and load set the chado values
  258. // so we will set them here as well.
  259. 'chado-' . $base_table . '_relationship__' . $expect['subject_key'] => $entity2->chado_record->{$base_table . '_id'},
  260. 'chado-' . $base_table . '_relationship__type_id' => $cvterm->cvterm_id,
  261. 'chado-' . $base_table . '_relationship__' . $expect['object_key'] => $entity1->chado_record->{$base_table . '_id'},
  262. ];
  263. if ($base_table == 'organism') {
  264. $initial_values['subject_name'] = $entity2->chado_record->species;
  265. $initial_values['object_name'] = $entity1->chado_record->species;
  266. }
  267. // Mock objects.
  268. $delta = 1;
  269. $langcode = LANGUAGE_NONE;
  270. $widget = $helper->mockElement($delta, $langcode);
  271. $form = $helper->mockForm($delta, $langcode);
  272. $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
  273. $element = $helper->mockElement($delta, $langcode);
  274. $widget_class->validate($element, $form, $form_state, $langcode, $delta);
  275. // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
  276. // Ensure the chado-table__column entries are there.
  277. $this->assertArrayHasKey(
  278. 'chado-' . $base_table . '_relationship__' . $expect['subject_key'],
  279. $form_state['values'][$field_name][$langcode][$delta],
  280. 'Failed to find the subject_id in the processed values (Base: '
  281. . $base_table
  282. . '). This implies the validate function was not able to validate the subject.'
  283. );
  284. $this->assertArrayHasKey(
  285. 'chado-' . $base_table . '_relationship__' . $expect['object_key'],
  286. $form_state['values'][$field_name][$langcode][$delta],
  287. 'Failed to find the object_id in the processed values (Base: '
  288. . $base_table
  289. . '). This implies the validate function was not able to validate the object.'
  290. );
  291. $this->assertArrayHasKey(
  292. 'chado-' . $base_table . '_relationship__type_id',
  293. $form_state['values'][$field_name][$langcode][$delta],
  294. 'Failed to find the type_id in the processed values (Base: '
  295. . $base_table
  296. . '). This implies the validate function was not able to validate the type.'
  297. );
  298. // Check for errors.
  299. $errors = form_get_errors();
  300. // @debug print "Errors: " . print_r($errors, TRUE)."\n";
  301. $this->assertEmpty(
  302. $errors,
  303. "There should be no form errors when subject and object are pre-existing and both are supplied. Initial values: "
  304. . print_r($initial_values, TRUE) . " But these were registered: "
  305. . print_r($errors, TRUE)
  306. );
  307. // Clean up after ourselves by removing any errors we logged.
  308. form_clear_error();
  309. }
  310. }
  311. /**
  312. * Case: WidgetValidate on new relationship filled out properly.
  313. *
  314. * @group widget
  315. * @group sbo__relationship
  316. */
  317. public function testWidgetValidate_create() {
  318. foreach ($this->provideEntities() as $dataEntry) {
  319. list(
  320. $bundle_name, $field_name, $widget_name, $entity_ids, $expect
  321. ) = $dataEntry;
  322. // Load the entities.
  323. $entities = entity_load('TripalEntity', $entity_ids);
  324. $entity1 = $entities[$entity_ids[0]];
  325. $entity2 = $entities[$entity_ids[1]];
  326. $base_table = $entity1->chado_table;
  327. // Initialize the widget class via the TripalFieldTestHelper class.
  328. $machine_names = [
  329. 'field_name' => $field_name,
  330. 'widget_name' => $widget_name,
  331. ];
  332. $field_info = field_info_field($field_name);
  333. $instance_info = field_info_instance(
  334. 'TripalEntity', $field_name, $bundle_name
  335. );
  336. $helper = new \TripalFieldTestHelper(
  337. $bundle_name, $machine_names, $entity1, $field_info, $instance_info
  338. );
  339. $widget_class = $helper->getInitializedClass();
  340. // Set some initial values.
  341. $cvterm = factory('chado.cvterm')->create();
  342. $initial_values = [
  343. 'subject_name' => $entity2->chado_record->name,
  344. 'type_name' => $cvterm->name,
  345. 'vocabulary' => $cvterm->cv_id,
  346. 'object_name' => $entity1->chado_record->name,
  347. // These are not set on the creation form.
  348. 'chado-' . $base_table . '_relationship__' . $expect['subject_key'] => NULL,
  349. 'chado-' . $base_table . '_relationship__type_id' => NULL,
  350. 'chado-' . $base_table . '_relationship__' . $expect['object_key'] => NULL,
  351. ];
  352. if ($base_table == 'organism') {
  353. $initial_values['subject_name'] = $entity2->chado_record->species;
  354. $initial_values['object_name'] = $entity1->chado_record->species;
  355. }
  356. // Mock objects.
  357. $delta = 1;
  358. $langcode = LANGUAGE_NONE;
  359. $widget = $helper->mockElement($delta, $langcode);
  360. $form = $helper->mockForm($delta, $langcode);
  361. $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
  362. $element = $helper->mockElement($delta, $langcode);
  363. $widget_class->validate($element, $form, $form_state, $langcode, $delta);
  364. // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
  365. // Ensure the chado-table__column entries are there.
  366. $this->assertArrayHasKey(
  367. 'chado-' . $base_table . '_relationship__' . $expect['subject_key'],
  368. $form_state['values'][$field_name][$langcode][$delta],
  369. 'Failed to find the subject_id in the processed values (Base: '
  370. . $base_table
  371. . '). This implies the validate function was not able to validate the subject.'
  372. );
  373. $this->assertArrayHasKey(
  374. 'chado-' . $base_table . '_relationship__' . $expect['object_key'],
  375. $form_state['values'][$field_name][$langcode][$delta],
  376. 'Failed to find the object_id in the processed values (Base: '
  377. . $base_table
  378. . '). This implies the validate function was not able to validate the object.'
  379. );
  380. $this->assertArrayHasKey(
  381. 'chado-' . $base_table . '_relationship__type_id',
  382. $form_state['values'][$field_name][$langcode][$delta],
  383. 'Failed to find the type_id in the processed values (Base: '
  384. . $base_table
  385. . '). This implies the validate function was not able to validate the type.'
  386. );
  387. // Check for errors.
  388. $errors = form_get_errors();
  389. // @debug print "Errors: " . print_r($errors, TRUE)."\n";
  390. $this->assertEmpty(
  391. $errors,
  392. "There should be no form errors when subject and object are pre-existing and both are supplied. Initial values: "
  393. . print_r($initial_values, TRUE) . " But these were registered: "
  394. . print_r($errors, TRUE)
  395. );
  396. // Clean up after ourselves by removing any errors we logged.
  397. form_clear_error();
  398. }
  399. }
  400. /**
  401. * Case: WidgetValidate on new relationship missing subject.
  402. *
  403. * @group widget
  404. * @group sbo__relationship
  405. */
  406. public function testWidgetValidate_nosubject() {
  407. foreach ($this->provideEntities() as $dataEntry) {
  408. list(
  409. $bundle_name, $field_name, $widget_name, $entity_ids, $expect
  410. ) = $dataEntry;
  411. // Load the entities.
  412. $entities = entity_load('TripalEntity', $entity_ids);
  413. $entity1 = $entities[$entity_ids[0]];
  414. $entity2 = $entities[$entity_ids[1]];
  415. $base_table = $entity1->chado_table;
  416. // Initialize the widget class via the TripalFieldTestHelper class.
  417. $machine_names = [
  418. 'field_name' => $field_name,
  419. 'widget_name' => $widget_name,
  420. ];
  421. $field_info = field_info_field($field_name);
  422. $instance_info = field_info_instance(
  423. 'TripalEntity', $field_name, $bundle_name
  424. );
  425. $helper = new \TripalFieldTestHelper(
  426. $bundle_name, $machine_names, $entity1, $field_info, $instance_info
  427. );
  428. $widget_class = $helper->getInitializedClass();
  429. // Set some initial values.
  430. $cvterm = factory('chado.cvterm')->create();
  431. $initial_values = [
  432. 'subject_name' => '',
  433. 'type_name' => $cvterm->name,
  434. 'vocabulary' => $cvterm->cv_id,
  435. 'object_name' => $entity1->chado_record->name,
  436. // Both the form and load set the chado values
  437. // so we will set them here as well.
  438. 'chado-' . $base_table . '_relationship__' . $expect['subject_key'] => NULL,
  439. 'chado-' . $base_table . '_relationship__type_id' => $cvterm->cvterm_id,
  440. 'chado-' . $base_table . '_relationship__' . $expect['object_key'] => $entity1->chado_record->{$base_table . '_id'},
  441. ];
  442. if ($base_table == 'organism') {
  443. $initial_values['object_name'] = $entity1->chado_record->species;
  444. }
  445. // Mock objects.
  446. $delta = 1;
  447. $langcode = LANGUAGE_NONE;
  448. $widget = $helper->mockElement($delta, $langcode);
  449. $form = $helper->mockForm($delta, $langcode);
  450. $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
  451. $element = $helper->mockElement($delta, $langcode);
  452. $widget_class->validate($element, $form, $form_state, $langcode, $delta);
  453. // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
  454. // Ensure the chado-table__column entries are there.
  455. $this->assertArrayHasKey(
  456. 'chado-' . $base_table . '_relationship__' . $expect['subject_key'],
  457. $form_state['values'][$field_name][$langcode][$delta],
  458. 'Failed to find the subject_id in the processed values (Base: '
  459. . $base_table
  460. . '). This implies the validate function was not able to validate the subject.'
  461. );
  462. $this->assertArrayHasKey(
  463. 'chado-' . $base_table . '_relationship__' . $expect['object_key'],
  464. $form_state['values'][$field_name][$langcode][$delta],
  465. 'Failed to find the object_id in the processed values (Base: '
  466. . $base_table
  467. . '). This implies the validate function was not able to validate the object.'
  468. );
  469. $this->assertArrayHasKey(
  470. 'chado-' . $base_table . '_relationship__type_id',
  471. $form_state['values'][$field_name][$langcode][$delta],
  472. 'Failed to find the type_id in the processed values (Base: '
  473. . $base_table
  474. . '). This implies the validate function was not able to validate the type.'
  475. );
  476. // Check for errors.
  477. $errors = form_get_errors();
  478. // @debug print "Errors: " . print_r($errors, TRUE)."\n";
  479. $this->assertNotEmpty(
  480. $errors,
  481. "There should be form errors when subject is not supplied. Initial values: "
  482. . print_r($initial_values, TRUE) . " But these were registered: "
  483. . print_r($errors, TRUE)
  484. );
  485. // Clean up after ourselves by removing any errors we logged.
  486. form_clear_error();
  487. }
  488. }
  489. /**
  490. * Case: WidgetValidate on new relationship missing object.
  491. *
  492. * @group widget
  493. * @group sbo__relationship
  494. */
  495. public function testWidgetValidate_noobject() {
  496. foreach ($this->provideEntities() as $dataEntry) {
  497. list(
  498. $bundle_name, $field_name, $widget_name, $entity_ids, $expect
  499. ) = $dataEntry;
  500. // Load the entities.
  501. $entities = entity_load('TripalEntity', $entity_ids);
  502. $entity1 = $entities[$entity_ids[0]];
  503. $entity2 = $entities[$entity_ids[1]];
  504. $base_table = $entity1->chado_table;
  505. // Initialize the widget class via the TripalFieldTestHelper class.
  506. $machine_names = [
  507. 'field_name' => $field_name,
  508. 'widget_name' => $widget_name,
  509. ];
  510. $field_info = field_info_field($field_name);
  511. $instance_info = field_info_instance(
  512. 'TripalEntity', $field_name, $bundle_name
  513. );
  514. $helper = new \TripalFieldTestHelper(
  515. $bundle_name, $machine_names, $entity1, $field_info, $instance_info
  516. );
  517. $widget_class = $helper->getInitializedClass();
  518. // Set some initial values.
  519. $cvterm = factory('chado.cvterm')->create();
  520. $initial_values = [
  521. 'subject_name' => $entity2->chado_record->name,
  522. 'type_name' => $cvterm->name,
  523. 'vocabulary' => $cvterm->cv_id,
  524. 'object_name' => '',
  525. // Both the form and load set the chado values
  526. // so we will set them here as well.
  527. 'chado-' . $base_table . '_relationship__' . $expect['subject_key'] => $entity2->chado_record->{$base_table . '_id'},
  528. 'chado-' . $base_table . '_relationship__type_id' => $cvterm->cvterm_id,
  529. 'chado-' . $base_table . '_relationship__' . $expect['object_key'] => NULL,
  530. ];
  531. if ($base_table == 'organism') {
  532. $initial_values['subject_name'] = $entity2->chado_record->species;
  533. }
  534. // Mock objects.
  535. $delta = 1;
  536. $langcode = LANGUAGE_NONE;
  537. $widget = $helper->mockElement($delta, $langcode);
  538. $form = $helper->mockForm($delta, $langcode);
  539. $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
  540. $element = $helper->mockElement($delta, $langcode);
  541. $widget_class->validate($element, $form, $form_state, $langcode, $delta);
  542. // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
  543. // Ensure the chado-table__column entries are there.
  544. $this->assertArrayHasKey(
  545. 'chado-' . $base_table . '_relationship__' . $expect['subject_key'],
  546. $form_state['values'][$field_name][$langcode][$delta],
  547. 'Failed to find the subject_id in the processed values (Base: '
  548. . $base_table
  549. . '). This implies the validate function was not able to validate the subject.'
  550. );
  551. $this->assertArrayHasKey(
  552. 'chado-' . $base_table . '_relationship__' . $expect['object_key'],
  553. $form_state['values'][$field_name][$langcode][$delta],
  554. 'Failed to find the object_id in the processed values (Base: '
  555. . $base_table
  556. . '). This implies the validate function was not able to validate the object.'
  557. );
  558. $this->assertArrayHasKey(
  559. 'chado-' . $base_table . '_relationship__type_id',
  560. $form_state['values'][$field_name][$langcode][$delta],
  561. 'Failed to find the type_id in the processed values (Base: '
  562. . $base_table
  563. . '). This implies the validate function was not able to validate the type.'
  564. );
  565. // Check for errors.
  566. $errors = form_get_errors();
  567. // @debug print "Errors: " . print_r($errors, TRUE)."\n";
  568. $this->assertNotEmpty(
  569. $errors,
  570. "There should be form errors when the object is not supplied. Initial values: "
  571. . print_r($initial_values, TRUE) . " But these were registered: "
  572. . print_r($errors, TRUE)
  573. );
  574. // Clean up after ourselves by removing any errors we logged.
  575. form_clear_error();
  576. }
  577. }
  578. /**
  579. * Case: WidgetValidate on new relationship missing type.
  580. *
  581. * @group widget
  582. * @group sbo__relationship
  583. */
  584. public function testWidgetValidate_notype() {
  585. foreach ($this->provideEntities() as $dataEntry) {
  586. list(
  587. $bundle_name, $field_name, $widget_name, $entity_ids, $expect
  588. ) = $dataEntry;
  589. // Load the entities.
  590. $entities = entity_load('TripalEntity', $entity_ids);
  591. $entity1 = $entities[$entity_ids[0]];
  592. $entity2 = $entities[$entity_ids[1]];
  593. $base_table = $entity1->chado_table;
  594. // Initialize the widget class via the TripalFieldTestHelper class.
  595. $machine_names = [
  596. 'field_name' => $field_name,
  597. 'widget_name' => $widget_name,
  598. ];
  599. $field_info = field_info_field($field_name);
  600. $instance_info = field_info_instance(
  601. 'TripalEntity', $field_name, $bundle_name
  602. );
  603. $helper = new \TripalFieldTestHelper(
  604. $bundle_name, $machine_names, $entity1, $field_info, $instance_info
  605. );
  606. $widget_class = $helper->getInitializedClass();
  607. // Set some initial values.
  608. $initial_values = [
  609. 'subject_name' => $entity2->chado_record->name,
  610. 'type_name' => '',
  611. 'vocabulary' => NULL,
  612. 'object_name' => $entity1->chado_record->name,
  613. // Both the form and load set the chado values
  614. // so we will set them here as well.
  615. 'chado-' . $base_table . '_relationship__' . $expect['subject_key'] => $entity2->chado_record->{$base_table . '_id'},
  616. 'chado-' . $base_table . '_relationship__type_id' => NULL,
  617. 'chado-' . $base_table . '_relationship__' . $expect['object_key'] => $entity1->chado_record->{$base_table . '_id'},
  618. ];
  619. if ($base_table == 'organism') {
  620. $initial_values['subject_name'] = $entity2->chado_record->species;
  621. $initial_values['object_name'] = $entity1->chado_record->species;
  622. }
  623. // Mock objects.
  624. $delta = 1;
  625. $langcode = LANGUAGE_NONE;
  626. $widget = $helper->mockElement($delta, $langcode);
  627. $form = $helper->mockForm($delta, $langcode);
  628. $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
  629. $element = $helper->mockElement($delta, $langcode);
  630. $widget_class->validate($element, $form, $form_state, $langcode, $delta);
  631. // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
  632. // Ensure the chado-table__column entries are there.
  633. $this->assertArrayHasKey(
  634. 'chado-' . $base_table . '_relationship__' . $expect['subject_key'],
  635. $form_state['values'][$field_name][$langcode][$delta],
  636. 'Failed to find the subject_id in the processed values (Base: '
  637. . $base_table
  638. . '). This implies the validate function was not able to validate the subject.'
  639. );
  640. $this->assertArrayHasKey(
  641. 'chado-' . $base_table . '_relationship__' . $expect['object_key'],
  642. $form_state['values'][$field_name][$langcode][$delta],
  643. 'Failed to find the object_id in the processed values (Base: '
  644. . $base_table
  645. . '). This implies the validate function was not able to validate the object.'
  646. );
  647. $this->assertArrayHasKey(
  648. 'chado-' . $base_table . '_relationship__type_id',
  649. $form_state['values'][$field_name][$langcode][$delta],
  650. 'Failed to find the type_id in the processed values (Base: '
  651. . $base_table
  652. . '). This implies the validate function was not able to validate the type.'
  653. );
  654. // Check for errors.
  655. $errors = form_get_errors();
  656. // @debug print "Errors: " . print_r($errors, TRUE)."\n";
  657. $this->assertNotEmpty(
  658. $errors,
  659. "There should be form errors when type is not supplied. Initial values: "
  660. . print_r($initial_values, TRUE) . " But these were registered: "
  661. . print_r($errors, TRUE)
  662. );
  663. // Clean up after ourselves by removing any errors we logged.
  664. form_clear_error();
  665. }
  666. }
  667. }