sbo__relationship_widgetTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. if (!$bundle_id) {
  59. continue;
  60. }
  61. $bundle_name = 'bio_data_'.$bundle_id;
  62. // Find an entity from the above bundle.
  63. // @todo find a way to create a fake entity for use here.
  64. $entity_id = db_query('SELECT id FROM tripal_entity WHERE bundle=:bundle LIMIT 1',
  65. array(':bundle' => $bundle_name))->fetchField();
  66. // set variables to guide testing.
  67. $expect = [
  68. 'has_rank' => TRUE,
  69. 'has_value' => FALSE,
  70. 'subject_key' => 'subject_id',
  71. 'object_key' => 'object_id',
  72. ];
  73. if ($base_table == 'organism') { $expect['has_rank'] = FALSE; }
  74. if ($base_table == 'stock') { $expect['has_value'] = TRUE; }
  75. if ($base_table == 'project') {
  76. $expect['subject_key'] = 'subject_project_id';
  77. $expect['object_key'] = 'object_project_id';
  78. }
  79. $data[] = [$bundle_name, $field_name, $widget_name, $entity_id, $expect];
  80. }
  81. return $data;
  82. }
  83. /**
  84. * Test that we can initialize the widget properly.
  85. *
  86. * @dataProvider provideEntities()
  87. *
  88. * @group lacey
  89. */
  90. public function testWidgetClassInitialization($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  91. // Initialize our variables.
  92. $vars = $this->initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id);
  93. // Check we have the variables we initialized.
  94. $this->assertNotEmpty($vars['bundle'], "Could not load the bundle.");
  95. $this->assertNotEmpty($vars['field_info'], "Could not lookup the field information.");
  96. $this->assertNotEmpty($vars['instance_info'], "Could not lookup the instance informatiob.");
  97. $this->assertNotEmpty($vars['widget_class'], "Couldn't create a widget class instance.");
  98. $this->assertNotEmpty($vars['entity'], "Couldn't load an entity.");
  99. }
  100. /**
  101. * Test the widget Form.
  102. *
  103. * @dataProvider provideEntities()
  104. *
  105. * @group lacey
  106. */
  107. public function testWidgetForm($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  108. $vars = $this->initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id);
  109. $base_table = $vars['entity']->chado_table;
  110. // Stub out a fake $widget object.
  111. $widget = [
  112. '#entity_type' => 'TripalEntity',
  113. '#entity' => $vars['entity'],
  114. '#bundle' => $vars['bundle'],
  115. '#field_name' => $field_name,
  116. '#language' => LANGUAGE_NONE,
  117. '#field_parents' => [],
  118. '#columns' => [],
  119. '#title' => '',
  120. '#description' => '',
  121. '#required' => FALSE,
  122. '#delta' => 0,
  123. '#weight' => 0, //same as delta.
  124. 'value' => [
  125. '#type' => 'value',
  126. '#value' => '',
  127. ],
  128. '#field' => $vars['field_info'],
  129. '#instance' => $vars['instance_info'],
  130. '#theme' => 'tripal_field_default',
  131. 'element_validate' => ['tripal_field_widget_form_validate']
  132. ];
  133. // Stub out the form and form_state.
  134. $form = [
  135. '#parents' => [],
  136. '#entity' => $vars['entity'],
  137. ];
  138. $form_state = [
  139. 'build_info' => [
  140. 'args' => [
  141. 0 => NULL,
  142. 1 => $vars['entity']
  143. ],
  144. 'form_id' => 'tripal_entity_form',
  145. ],
  146. 'rebuild' => FALSE,
  147. 'rebuild_info' => [],
  148. 'redirect' => NULL,
  149. 'temporary' => [],
  150. 'submitted' => FALSE,
  151. ];
  152. // stub out the data for the field.
  153. $langcode = LANGUAGE_NONE;
  154. $items = [
  155. 'value' => '',
  156. 'chado-organism_relationship__organism_relationship_id' => '',
  157. 'chado-organism_relationship__subject_id' => '',
  158. 'chado-organism_relationship__object_id' => '',
  159. 'chado-organism_relationship__type_id' => '',
  160. 'chado-organism_relationship__rank' => '',
  161. 'object_name' => '',
  162. 'subject_name' => '',
  163. 'type_name' => '',
  164. ];
  165. $delta = 0;
  166. // Stub out the widget element.
  167. $element = [
  168. '#entity_type' => 'TripalEntity',
  169. '#entity' => $vars['entity'],
  170. '#bundle' => $bundle_name,
  171. '#field_name' => $field_name,
  172. '#language' => LANGUAGE_NONE,
  173. '#field_parents' => [],
  174. '#columns' => [],
  175. '#title' => '',
  176. '#description' => '',
  177. '#required' => FALSE,
  178. '#delta' => 0,
  179. '#weight' => 0,
  180. ];
  181. // Execute the form method.
  182. $vars['widget_class']->form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  183. // Check the resulting for array
  184. $this->assertArrayHasKey('subject_name', $widget, "The form for $bundle_name($base_table) does not have a subject element.");
  185. $this->assertArrayHasKey('type_name', $widget, "The form for $bundle_name($base_table) does not have a type element.");
  186. $this->assertArrayHasKey('object_name', $widget, "The form for $bundle_name($base_table) does not have a object element.");
  187. // Check the subject/object keys were correctly determined.
  188. $this->assertEquals($expect['subject_key'], $widget['#subject_id_key'], "The form didn't determine the subject key correctly.");
  189. $this->assertEquals($expect['object_key'], $widget['#object_id_key'], "The form didn't determine the object key correctly.");
  190. // If this table has a rank we want to ensure there is a form element to set it.
  191. if ($expect['has_rank']) {
  192. $this->assertArrayHasKey('rank', $widget, "The form for $bundle_name($base_table) does not have a rank element and it should.");
  193. }
  194. // If this table has a value we want to ensure there is a form element to set it.
  195. // @todo this test may be problematic b/c all fields have values independent of the db.
  196. if ($expect['has_value']) {
  197. $this->assertArrayHasKey('value', $widget, "The form for $bundle_name($base_table) does not have a value element and it should.");
  198. }
  199. }
  200. }