sbo__relationship_widgetTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 lacey-wip
  70. * @group widget
  71. * @group sbo__relationship
  72. */
  73. public function testWidgetClassInitialization($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  74. // Load the entity.
  75. $entity = entity_load('TripalEntity', [$entity_id]);
  76. $entity = $entity[$entity_id];
  77. // Initialize the widget class via the TripalFieldTestHelper class.
  78. $machine_names = array(
  79. 'field_name' => $field_name,
  80. 'widget_name' => $widget_name,
  81. );
  82. $field_info = field_info_field($field_name);
  83. $instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  84. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity, $field_info, $instance_info);
  85. $widget_class = $helper->getInitializedClass();
  86. // Check we have the variables we initialized.
  87. $this->assertNotEmpty($helper->bundle,
  88. "Could not load the bundle.");
  89. $this->assertNotEmpty($helper->getFieldInfo(),
  90. "Could not lookup the field information.");
  91. $this->assertNotEmpty($helper->getInstanceInfo(),
  92. "Could not lookup the instance information.");
  93. $this->assertNotEmpty($widget_class,
  94. "Couldn't create a widget class instance.");
  95. $this->assertNotEmpty($entity,
  96. "Couldn't load an entity.");
  97. // Check a little deeper...
  98. $this->assertEquals($helper->instance_info['settings']['chado_table'], $expect['relationship_table'],
  99. "Instance settings were not initialized fully.");
  100. }
  101. /**
  102. * Test the widget Form.
  103. *
  104. * @dataProvider provideEntities()
  105. *
  106. * @group widget
  107. * @group sbo__relationship
  108. */
  109. public function testWidgetForm($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  110. // Load the entity.
  111. $entity = entity_load('TripalEntity', [$entity_id]);
  112. $entity = $entity[$entity_id];
  113. // Initialize the widget class via the TripalFieldTestHelper class.
  114. $machine_names = array(
  115. 'field_name' => $field_name,
  116. 'widget_name' => $widget_name,
  117. );
  118. $field_info = field_info_field($field_name);
  119. $instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  120. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity, $field_info, $instance_info);
  121. $widget_class = $helper->getInitializedClass();
  122. $base_table = $entity->chado_table;
  123. // Stub out a fake $widget object.
  124. $widget = [
  125. '#entity_type' => 'TripalEntity',
  126. '#entity' => $entity,
  127. '#bundle' => $helper->bundle,
  128. '#field_name' => $field_name,
  129. '#language' => LANGUAGE_NONE,
  130. '#field_parents' => [],
  131. '#columns' => [],
  132. '#title' => '',
  133. '#description' => '',
  134. '#required' => FALSE,
  135. '#delta' => 0,
  136. '#weight' => 0, //same as delta.
  137. 'value' => [
  138. '#type' => 'value',
  139. '#value' => '',
  140. ],
  141. '#field' => $helper->field_info,
  142. '#instance' => $helper->instance_info,
  143. '#theme' => 'tripal_field_default',
  144. 'element_validate' => ['tripal_field_widget_form_validate']
  145. ];
  146. // Stub out the form and form_state.
  147. $form = [
  148. '#parents' => [],
  149. '#entity' => $entity,
  150. ];
  151. $form_state = [
  152. 'build_info' => [
  153. 'args' => [
  154. 0 => NULL,
  155. 1 => $entity,
  156. ],
  157. 'form_id' => 'tripal_entity_form',
  158. ],
  159. 'rebuild' => FALSE,
  160. 'rebuild_info' => [],
  161. 'redirect' => NULL,
  162. 'temporary' => [],
  163. 'submitted' => FALSE,
  164. ];
  165. // stub out the data for the field.
  166. $langcode = LANGUAGE_NONE;
  167. $items = [
  168. 'value' => '',
  169. 'chado-'.$base_table.'_relationship__organism_relationship_id' => '',
  170. 'chado-'.$base_table.'_relationship__subject_id' => '',
  171. 'chado-'.$base_table.'_relationship__object_id' => '',
  172. 'chado-'.$base_table.'_relationship__type_id' => '',
  173. 'object_name' => '',
  174. 'subject_name' => '',
  175. 'type_name' => '',
  176. ];
  177. $delta = 0;
  178. // Stub out the widget element.
  179. $element = [
  180. '#entity_type' => 'TripalEntity',
  181. '#entity' => $entity,
  182. '#bundle' => $bundle_name,
  183. '#field_name' => $field_name,
  184. '#language' => LANGUAGE_NONE,
  185. '#field_parents' => [],
  186. '#columns' => [],
  187. '#title' => '',
  188. '#description' => '',
  189. '#required' => FALSE,
  190. '#delta' => 0,
  191. '#weight' => 0,
  192. ];
  193. // Execute the form method.
  194. $widget_class->form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  195. // Check the resulting for array
  196. $this->assertArrayHasKey('subject_name', $widget,
  197. "The form for $bundle_name($base_table) does not have a subject element.");
  198. $this->assertArrayHasKey('type_name', $widget,
  199. "The form for $bundle_name($base_table) does not have a type element.");
  200. $this->assertArrayHasKey('object_name', $widget,
  201. "The form for $bundle_name($base_table) does not have a object element.");
  202. // @debug print_r($widget);
  203. // Check the subject/object keys were correctly determined.
  204. $this->assertEquals($expect['subject_key'], $widget['#subject_id_key'],
  205. "The form didn't determine the subject key correctly.");
  206. $this->assertEquals($expect['object_key'], $widget['#object_id_key'],
  207. "The form didn't determine the object key correctly.");
  208. }
  209. /**
  210. * Test the Relationship Type Options.
  211. * Specfically, sbo__relationship_widget->get_rtype_select_options().
  212. *
  213. * @dataProvider provideEntities()
  214. *
  215. * @group widget
  216. * @group sbo__relationship
  217. */
  218. public function testGetRTypeSelectOptions($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  219. // The different options are set in the instance.
  220. // Therefore we want to make a fake instance to control this setting.
  221. $fake_instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
  222. //$fake_instance['settings']['relationships']['option1_vocabs'] = 5;
  223. $this->assertTrue(true);
  224. }
  225. }