sbo__relationship_widgetTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. * Data Provider: provides entities matching important test cases.
  10. *
  11. * Specifically, we will cover three relationship tables, which represent
  12. * the diversity in the chado schema v1.3:
  13. * organism_relationship: subject_id, type_id, object_id,
  14. * stock_relationship: subject_id, type_id, object_id, value, rank,
  15. * project_relationship: subject_project_id, type_id, object_project_id, rank
  16. *
  17. * @returns
  18. * Returns an array where each item to be tested has the paramaters
  19. * needed for initializeWidgetClass(). Specfically, $bundle_name,
  20. * $field_name, $widget_name, $entity_id.
  21. */
  22. public function provideEntities() {
  23. $data = [];
  24. foreach (['organism', 'stock', 'project'] as $base_table) {
  25. $field_name = 'sbo__relationship';
  26. $widget_name = 'sbo__relationship_widget';
  27. // find a bundle which stores it's data in the given base table.
  28. // This will work on Travis since Tripal creates matching bundles by default.
  29. // @todo ideally we would create a fake bundle here.
  30. $bundle_id = db_query("
  31. SELECT bundle_id
  32. FROM chado_bundle b
  33. LEFT JOIN tripal_entity e ON e.bundle='bio_data_'||b.bundle_id
  34. WHERE data_table=:table AND id IS NOT NULL LIMIT 1",
  35. array(':table' => $base_table))->fetchField();
  36. if (!$bundle_id) {
  37. continue;
  38. }
  39. $bundle_name = 'bio_data_'.$bundle_id;
  40. // Find an entity from the above bundle.
  41. // @todo find a way to create a fake entity for use here.
  42. $entity_id = db_query('SELECT id FROM tripal_entity WHERE bundle=:bundle LIMIT 1',
  43. array(':bundle' => $bundle_name))->fetchField();
  44. // set variables to guide testing.
  45. $expect = [
  46. 'has_rank' => TRUE,
  47. 'has_value' => FALSE,
  48. 'subject_key' => 'subject_id',
  49. 'object_key' => 'object_id',
  50. 'base_table' => $base_table,
  51. 'relationship_table' => $base_table.'_relationship'
  52. ];
  53. if ($base_table == 'organism') { $expect['has_rank'] = FALSE; }
  54. if ($base_table == 'stock') { $expect['has_value'] = TRUE; }
  55. if ($base_table == 'project') {
  56. $expect['subject_key'] = 'subject_project_id';
  57. $expect['object_key'] = 'object_project_id';
  58. }
  59. $data[] = [$bundle_name, $field_name, $widget_name, $entity_id, $expect];
  60. }
  61. return $data;
  62. }
  63. /**
  64. * Test that we can initialize the widget properly.
  65. *
  66. * @dataProvider provideEntities()
  67. *
  68. * @group lacey-wip
  69. * @group widget
  70. * @group sbo__relationship
  71. */
  72. public function testWidgetClassInitialization($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  73. // Load the entity.
  74. $entity = entity_load('TripalEntity', [$entity_id]);
  75. $entity = $entity[$entity_id];
  76. // Initialize the widget class via the TripalFieldTestHelper class.
  77. $machine_names = array(
  78. 'field_name' => $field_name,
  79. 'widget_name' => $widget_name,
  80. );
  81. module_load_include('php', 'tripal_chado', '../tests/TripalFieldTestHelper');
  82. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity);
  83. $widget_class = $helper->getInitializedClass();
  84. // Check we have the variables we initialized.
  85. $this->assertNotEmpty($helper->bundle,
  86. "Could not load the bundle.");
  87. $this->assertNotEmpty($helper->getFieldInfo(),
  88. "Could not lookup the field information.");
  89. $this->assertNotEmpty($helper->getInstanceInfo(),
  90. "Could not lookup the instance information.");
  91. $this->assertNotEmpty($widget_class,
  92. "Couldn't create a widget class instance.");
  93. $this->assertNotEmpty($entity,
  94. "Couldn't load an entity.");
  95. // Check a little deeper...
  96. $this->assertEquals($helper->instance_info['settings']['chado_table'], $expect['relationship_table'],
  97. "Instance settings were not initialized fully.");
  98. }
  99. /**
  100. * Test the widget Form.
  101. *
  102. * @dataProvider provideEntities()
  103. *
  104. * @group widget
  105. * @group sbo__relationship
  106. */
  107. public function testWidgetForm($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  108. // Load the entity.
  109. $entity = entity_load('TripalEntity', [$entity_id]);
  110. $entity = $entity[$entity_id];
  111. // Initialize the widget class via the TripalFieldTestHelper class.
  112. $machine_names = array(
  113. 'field_name' => $field_name,
  114. 'widget_name' => $widget_name,
  115. );
  116. module_load_include('php', 'tripal_chado', '../tests/TripalFieldTestHelper');
  117. $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity);
  118. $widget_class = $helper->getInitializedClass();
  119. $base_table = $entity->chado_table;
  120. // Stub out a fake $widget object.
  121. $widget = [
  122. '#entity_type' => 'TripalEntity',
  123. '#entity' => $entity,
  124. '#bundle' => $helper->bundle,
  125. '#field_name' => $field_name,
  126. '#language' => LANGUAGE_NONE,
  127. '#field_parents' => [],
  128. '#columns' => [],
  129. '#title' => '',
  130. '#description' => '',
  131. '#required' => FALSE,
  132. '#delta' => 0,
  133. '#weight' => 0, //same as delta.
  134. 'value' => [
  135. '#type' => 'value',
  136. '#value' => '',
  137. ],
  138. '#field' => $helper->field_info,
  139. '#instance' => $helper->instance_info,
  140. '#theme' => 'tripal_field_default',
  141. 'element_validate' => ['tripal_field_widget_form_validate']
  142. ];
  143. // Stub out the form and form_state.
  144. $form = [
  145. '#parents' => [],
  146. '#entity' => $entity,
  147. ];
  148. $form_state = [
  149. 'build_info' => [
  150. 'args' => [
  151. 0 => NULL,
  152. 1 => $entity,
  153. ],
  154. 'form_id' => 'tripal_entity_form',
  155. ],
  156. 'rebuild' => FALSE,
  157. 'rebuild_info' => [],
  158. 'redirect' => NULL,
  159. 'temporary' => [],
  160. 'submitted' => FALSE,
  161. ];
  162. // stub out the data for the field.
  163. $langcode = LANGUAGE_NONE;
  164. $items = [
  165. 'value' => '',
  166. 'chado-'.$base_table.'_relationship__organism_relationship_id' => '',
  167. 'chado-'.$base_table.'_relationship__subject_id' => '',
  168. 'chado-'.$base_table.'_relationship__object_id' => '',
  169. 'chado-'.$base_table.'_relationship__type_id' => '',
  170. 'object_name' => '',
  171. 'subject_name' => '',
  172. 'type_name' => '',
  173. ];
  174. $delta = 0;
  175. // Stub out the widget element.
  176. $element = [
  177. '#entity_type' => 'TripalEntity',
  178. '#entity' => $entity,
  179. '#bundle' => $bundle_name,
  180. '#field_name' => $field_name,
  181. '#language' => LANGUAGE_NONE,
  182. '#field_parents' => [],
  183. '#columns' => [],
  184. '#title' => '',
  185. '#description' => '',
  186. '#required' => FALSE,
  187. '#delta' => 0,
  188. '#weight' => 0,
  189. ];
  190. // Execute the form method.
  191. $widget_class->form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  192. // Check the resulting for array
  193. $this->assertArrayHasKey('subject_name', $widget,
  194. "The form for $bundle_name($base_table) does not have a subject element.");
  195. $this->assertArrayHasKey('type_name', $widget,
  196. "The form for $bundle_name($base_table) does not have a type element.");
  197. $this->assertArrayHasKey('object_name', $widget,
  198. "The form for $bundle_name($base_table) does not have a object element.");
  199. //@ debug print_r($widget);
  200. // Check the subject/object keys were correctly determined.
  201. $this->assertEquals($expect['subject_key'], $widget['#subject_id_key'],
  202. "The form didn't determine the subject key correctly.");
  203. $this->assertEquals($expect['object_key'], $widget['#object_id_key'],
  204. "The form didn't determine the object key correctly.");
  205. }
  206. /**
  207. * Test the Relationship Type Options.
  208. * Specfically, sbo__relationship_widget->get_rtype_select_options().
  209. *
  210. * @dataProvider provideEntities()
  211. *
  212. * @group widget
  213. * @group sbo__relationship
  214. */
  215. public function testGetRTypeSelectOptions($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
  216. // The different options are set in the instance.
  217. // Therefore we want to make a fake instance to control this setting.
  218. $fake_instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
  219. //$fake_instance['settings']['relationships']['option1_vocabs'] = 5;
  220. $this->assertTrue(true);
  221. }
  222. }