sbo__relationship_widgetTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace Tests\tripal_chado\fields;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. class sbo__relationship_widgetTest extends TripalTestCase {
  6. // Uncomment to auto start and rollback db transactions per test method.
  7. use DBTransaction;
  8. /**
  9. * Create a fake sbo__relationship_widget field?
  10. */
  11. private function initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id) {
  12. $vars = [];
  13. $vars['widget_class'] = $vars['bundle'] = $vars['entity'] = NULL;
  14. // First include the appropriate class.
  15. $widget_class_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'tripal_chado')
  16. . '/includes/TripalFields/sbo__relationship/sbo__relationship_widget.inc';
  17. if ((include_once($widget_class_path)) == TRUE) {
  18. // Load the bundle and field/instance info.
  19. $vars['bundle'] = tripal_load_bundle_entity(array('name'=> $bundle_name));
  20. $vars['field_info'] = field_info_field($field_name);
  21. $vars['instance_info'] = field_info_instance('TripalEntity', $field_name, $bundle_name);
  22. // Create an instance of the widget class.
  23. $vars['widget_class'] = new \sbo__relationship_widget($vars['field_info'], $vars['instance_info']);
  24. // load an entity to pretend the widget is modifying.
  25. $vars['entity'] = entity_load('TripalEntity', [$entity_id]);
  26. $vars['entity'] = $vars['entity'][$entity_id];
  27. }
  28. return $vars;
  29. }
  30. /**
  31. * Data Provider: provides entities matching important test cases.
  32. *
  33. * Specifically, we will cover three relationship tables, which represent
  34. * the diversity in the chado schema v1.3:
  35. * organism_relationship: subject_id, type_id, object_id,
  36. * stock_relationship: subject_id, type_id, object_id, value, rank,
  37. * project_relationship: subject_project_id, type_id, object_project_id, rank
  38. *
  39. * @returns
  40. * Returns an array where each item to be tested has the paramaters
  41. * needed for initializeWidgetClass(). Specfically, $bundle_name,
  42. * $field_name, $widget_name, $entity_id.
  43. */
  44. public function provideEntities() {
  45. $data = [];
  46. foreach (['organism', 'stock', 'project'] as $base_table) {
  47. $field_name = 'sbo__relationship';
  48. $widget_name = 'sbo__relationship_widget';
  49. // find a bundle which stores it's data in the given base table.
  50. // This will work on Travis since Tripal creates matching bundles by default.
  51. // @todo ideally we would create a fake bundle here.
  52. $bundle_id = db_query("
  53. SELECT bundle_id
  54. FROM chado_bundle b
  55. LEFT JOIN tripal_entity e ON e.bundle='bio_data_'||b.bundle_id
  56. WHERE data_table=:table AND id IS NOT NULL LIMIT 1",
  57. array(':table' => $base_table))->fetchField();
  58. $bundle_name = 'bio_data_'.$bundle_id;
  59. // Find an entity from the above bundle.
  60. // @todo find a way to create a fake entity for use here.
  61. $entity_id = db_query('SELECT id FROM tripal_entity WHERE bundle=:bundle LIMIT 1',
  62. array(':bundle' => $bundle_name))->fetchField();
  63. // set variables to guide testing.
  64. $expect = [
  65. 'has_rank' => TRUE,
  66. 'has_value' => FALSE,
  67. 'subject_key' => 'subject_id',
  68. 'object_key' => 'object_id',
  69. ];
  70. if ($base_table == 'organism') { $expect['has_rank'] = FALSE; }
  71. if ($base_table == 'stock') { $expect['has_value'] = TRUE; }
  72. if ($base_table == 'project') {
  73. $expect['subject_key'] = 'subject_project_id';
  74. $expect['object_key'] = 'object_project_id';
  75. }
  76. $data[] = [$bundle_name, $field_name, $widget_name, $entity_id, $expect];
  77. }
  78. return $data;
  79. }
  80. /**
  81. * Test that we can initialize the widget properly.
  82. *
  83. * @dataProvider provideEntities()
  84. *
  85. * @group lacey
  86. */
  87. public function testWidgetClassInitialization($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  88. // Initialize our variables.
  89. $vars = $this->initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id);
  90. // Check we have the variables we initialized.
  91. $this->assertNotEmpty($vars['bundle'], "Could not load the bundle.");
  92. $this->assertNotEmpty($vars['field_info'], "Could not lookup the field information.");
  93. $this->assertNotEmpty($vars['instance_info'], "Could not lookup the instance informatiob.");
  94. $this->assertNotEmpty($vars['widget_class'], "Couldn't create a widget class instance.");
  95. $this->assertNotEmpty($vars['entity'], "Couldn't load an entity.");
  96. }
  97. /**
  98. * Test the widget Form.
  99. *
  100. * @dataProvider provideEntities()
  101. *
  102. * @group lacey
  103. */
  104. public function testWidgetForm($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  105. $vars = $this->initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id);
  106. $base_table = $vars['entity']->chado_table;
  107. // Stub out a fake $widget object.
  108. $widget = [
  109. '#entity_type' => 'TripalEntity',
  110. '#entity' => $vars['entity'],
  111. '#bundle' => $vars['bundle'],
  112. '#field_name' => $field_name,
  113. '#language' => LANGUAGE_NONE,
  114. '#field_parents' => [],
  115. '#columns' => [],
  116. '#title' => '',
  117. '#description' => '',
  118. '#required' => FALSE,
  119. '#delta' => 0,
  120. '#weight' => 0, //same as delta.
  121. 'value' => [
  122. '#type' => 'value',
  123. '#value' => '',
  124. ],
  125. '#field' => $vars['field_info'],
  126. '#instance' => $vars['instance_info'],
  127. '#theme' => 'tripal_field_default',
  128. 'element_validate' => ['tripal_field_widget_form_validate']
  129. ];
  130. // Stub out the form and form_state.
  131. $form = [
  132. '#parents' => [],
  133. '#entity' => $vars['entity'],
  134. ];
  135. $form_state = [
  136. 'build_info' => [
  137. 'args' => [
  138. 0 => NULL,
  139. 1 => $vars['entity']
  140. ],
  141. 'form_id' => 'tripal_entity_form',
  142. ],
  143. 'rebuild' => FALSE,
  144. 'rebuild_info' => [],
  145. 'redirect' => NULL,
  146. 'temporary' => [],
  147. 'submitted' => FALSE,
  148. ];
  149. // stub out the data for the field.
  150. $langcode = LANGUAGE_NONE;
  151. $items = [
  152. 'value' => '',
  153. 'chado-organism_relationship__organism_relationship_id' => '',
  154. 'chado-organism_relationship__subject_id' => '',
  155. 'chado-organism_relationship__object_id' => '',
  156. 'chado-organism_relationship__type_id' => '',
  157. 'chado-organism_relationship__rank' => '',
  158. 'object_name' => '',
  159. 'subject_name' => '',
  160. 'type_name' => '',
  161. ];
  162. $delta = 0;
  163. // Stub out the widget element.
  164. $element = [
  165. '#entity_type' => 'TripalEntity',
  166. '#entity' => $vars['entity'],
  167. '#bundle' => $bundle_name,
  168. '#field_name' => $field_name,
  169. '#language' => LANGUAGE_NONE,
  170. '#field_parents' => [],
  171. '#columns' => [],
  172. '#title' => '',
  173. '#description' => '',
  174. '#required' => FALSE,
  175. '#delta' => 0,
  176. '#weight' => 0,
  177. ];
  178. // Execute the form method.
  179. $vars['widget_class']->form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  180. // Check the resulting for array
  181. $this->assertArrayHasKey('subject_name', $widget, "The form for $bundle_name($base_table) does not have a subject element.");
  182. $this->assertArrayHasKey('type_name', $widget, "The form for $bundle_name($base_table) does not have a type element.");
  183. $this->assertArrayHasKey('object_name', $widget, "The form for $bundle_name($base_table) does not have a object element.");
  184. // Check the subject/object keys were correctly determined.
  185. $this->assertEquals($expect['subject_key'], $widget['#subject_id_key'], "The form didn't determine the subject key correctly.");
  186. $this->assertEquals($expect['object_key'], $widget['#object_id_key'], "The form didn't determine the object key correctly.");
  187. // If this table has a rank we want to ensure there is a form element to set it.
  188. if ($expect['has_rank']) {
  189. $this->assertArrayHasKey('rank', $widget, "The form for $bundle_name($base_table) does not have a rank element and it should.");
  190. }
  191. // If this table has a value we want to ensure there is a form element to set it.
  192. // @todo this test may be problematic b/c all fields have values independent of the db.
  193. if ($expect['has_value']) {
  194. $this->assertArrayHasKey('value', $widget, "The form for $bundle_name($base_table) does not have a value element and it should.");
  195. }
  196. }
  197. }