sbo__relationship_widgetTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. }
  27. return $vars;
  28. }
  29. /**
  30. * Data Provider: provides entities matching important test cases.
  31. *
  32. * Specifically, we will cover three relationship tables, which represent
  33. * the diversity in the chado schema v1.3:
  34. * organism_relationship: subject_id, type_id, object_id,
  35. * stock_relationship: subject_id, type_id, object_id, value, rank,
  36. * project_relationship: project_subject_id, type_id, project_object_id, rank
  37. *
  38. * @returns
  39. * Returns an array where each item to be tested has the paramaters
  40. * needed for initializeWidgetClass(). Specfically, $bundle_name,
  41. * $field_name, $widget_name, $entity_id.
  42. */
  43. public function provideEntities() {
  44. $data = [];
  45. foreach (['organism', 'stock', 'project'] as $base_table) {
  46. $field_name = 'sbo__relationship';
  47. $widget_name = 'sbo__relationship_widget';
  48. // find a bundle which stores it's data in the given base table.
  49. // This will work on Travis since Tripal creates matching bundles by default.
  50. // @todo ideally we would create a fake bundle here.
  51. $bundle_id = db_query('SELECT bundle_id FROM chado_bundle WHERE data_table=:table LIMIT 1',
  52. array(':table' => $base_table))->fetchField();
  53. $bundle_name = 'bio_data_'.$bundle_id;
  54. // Find an entity from the above bundle.
  55. // @todo find a way to create a fake entity for use here.
  56. $entity_id = db_query('SELECT id FROM tripal_entity WHERE bundle=:bundle LIMIT 1',
  57. array(':bundle' => $bundle_name))->fetchField();
  58. $data[] = [$bundle_name, $field_name, $widget_name, $entity_id];
  59. }
  60. return $data;
  61. }
  62. /**
  63. * Test that we can initialize the widget properly.
  64. *
  65. * @dataProvider provideEntities()
  66. *
  67. * @group lacey
  68. */
  69. public function testWidgetClassInitialization($bundle_name, $field_name, $widget_name, $entity_id) {
  70. // Initialize our variables.
  71. $vars = $this->initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id);
  72. // Check we have the variables we initialized.
  73. $this->assertNotEmpty($vars['bundle'], "Could not load the bundle.");
  74. $this->assertNotEmpty($vars['field_info'], "Could not lookup the field information.");
  75. $this->assertNotEmpty($vars['instance_info'], "Could not lookup the instance informatiob.");
  76. $this->assertNotEmpty($vars['widget_class'], "Couldn't create a widget class instance.");
  77. $this->assertNotEmpty($vars['entity'], "Couldn't load an entity.");
  78. }
  79. /**
  80. * Test the widget Form.
  81. *
  82. * @dataProvider provideEntities()
  83. *
  84. * @group lacey
  85. */
  86. public function testWidgetForm($bundle_name, $field_name, $widget_name, $entity_id) {
  87. $vars = $this->initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id);
  88. // Stub out a fake $widget object.
  89. $widget = [
  90. '#entity_type' => 'TripalEntity',
  91. '#entity' => $vars['entity'],
  92. '#bundle' => $vars['bundle'],
  93. '#field_name' => $field_name,
  94. '#language' => LANGUAGE_NONE,
  95. '#field_parents' => [],
  96. '#columns' => [],
  97. '#title' => '',
  98. '#description' => '',
  99. '#required' => FALSE,
  100. '#delta' => 0,
  101. '#weight' => 0, //same as delta.
  102. 'value' => [
  103. '#type' => 'value',
  104. '#value' => '',
  105. ],
  106. '#field' => $vars['field_info'],
  107. '#instance' => $vars['instance_info'],
  108. '#theme' => 'tripal_field_default',
  109. 'element_validate' => ['tripal_field_widget_form_validate']
  110. ];
  111. // Stub out the form and form_state.
  112. $form = [
  113. '#parents' => [],
  114. '#entity' => $vars['entity'],
  115. ];
  116. $form_state = [
  117. 'build_info' => [
  118. 'args' => [
  119. 0 => NULL,
  120. 1 => $vars['entity']
  121. ],
  122. 'form_id' => 'tripal_entity_form',
  123. ],
  124. 'rebuild' => FALSE,
  125. 'rebuild_info' => [],
  126. 'redirect' => NULL,
  127. 'temporary' => [],
  128. 'submitted' => FALSE,
  129. ];
  130. // stub out the data for the field.
  131. $langcode = LANGUAGE_NONE;
  132. $items = [
  133. 'value' => '',
  134. 'chado-organism_relationship__organism_relationship_id' => '',
  135. 'chado-organism_relationship__subject_id' => '',
  136. 'chado-organism_relationship__object_id' => '',
  137. 'chado-organism_relationship__type_id' => '',
  138. 'chado-organism_relationship__rank' => '',
  139. 'object_name' => '',
  140. 'subject_name' => '',
  141. 'type_name' => '',
  142. ];
  143. $delta = 0;
  144. // Stub out the widget element.
  145. $element = [
  146. '#entity_type' => 'TripalEntity',
  147. '#entity' => $vars['entity'],
  148. '#bundle' => $bundle_name,
  149. '#field_name' => $field_name,
  150. '#language' => LANGUAGE_NONE,
  151. '#field_parents' => [],
  152. '#columns' => [],
  153. '#title' => '',
  154. '#description' => '',
  155. '#required' => FALSE,
  156. '#delta' => 0,
  157. '#weight' => 0,
  158. ];
  159. // Execute the form method.
  160. $vars['widget_class']->form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  161. // Check the resulting for array
  162. $this->assertArrayHasKey('subject_name', $widget, 'The form does not have a subject element.');
  163. $this->assertArrayHasKey('type_name', $widget, 'The form does not have a type element.');
  164. $this->assertArrayHasKey('object_name', $widget, 'The form does not have a object element.');
  165. }
  166. }