sbo__relationship_widgetTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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['bundle'] = $vars['field_info'] = $vars['instance_info'] = NULL;
  14. $vars['widget_class'] = $vars['entity'] = NULL;
  15. // First include the appropriate class.
  16. $widget_class_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'tripal_chado')
  17. . '/includes/TripalFields/sbo__relationship/sbo__relationship_widget.inc';
  18. if ((include_once($widget_class_path)) == TRUE) {
  19. // Load the bundle and field/instance info.
  20. $vars['bundle'] = tripal_load_bundle_entity(array('name'=> $bundle_name));
  21. $vars['field_info'] = field_info_field($field_name);
  22. $vars['instance_info'] = field_info_instance('TripalEntity', $field_name, $bundle_name);
  23. // Create an instance of the widget class.
  24. $vars['widget_class'] = new \sbo__relationship_widget($this->field_info, $this->instance_info);
  25. // load an entity to pretend the widget is modifying.
  26. $vars['entity'] = entity_load('TripalEntity', [$entity_id]);
  27. }
  28. return $vars;
  29. }
  30. /**
  31. * Test that we can initialize the widget properly.
  32. */
  33. public function testWidgetClassInitialization() {
  34. // Initialize our variables.
  35. $vars = $this->initializeWidgetClass('bio_data_1', 'sbo__relationship', 'sbo__relationship_widget', 8);
  36. // Check we have the variables we initialized.
  37. $this->assertNotEmpty($vars['bundle'], "Could not load the bundle.");
  38. $this->assertNotEmpty($vars['field_info'], "Could not lookup the field information.");
  39. $this->assertNotEmpty($vars['instance_info'], "Could not lookup the instance informatiob.");
  40. $this->assertNotEmpty($vars['widget_class'], "Couldn't create a widget class instance.");
  41. $this->assertNotEmpty($vars['entity'], "Couldn't load an entity.");
  42. }
  43. /**
  44. * Test the widget Form.
  45. *
  46. * @group lacey
  47. */
  48. public function testWidgetForm() {
  49. $field_name = 'sbo__relationship';
  50. $bundle_name = 'bio_data_1';
  51. $widget_name = 'sbo__relationship_widget';
  52. $entity_id = 8;
  53. $vars = $this->initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id);
  54. // Stub out a fake $widget object.
  55. $widget = [
  56. '#entity_type' => 'TripalEntity',
  57. '#entity' => $vars['entity'],
  58. '#bundle' => $vars['bundle'],
  59. '#field_name' => $field_name,
  60. '#language' => LANGUAGE_NONE,
  61. '#field_parents' => [],
  62. '#columns' => [],
  63. '#title' => '',
  64. '#description' => '',
  65. '#required' => FALSE,
  66. '#delta' => 0,
  67. '#weight' => 0, //same as delta.
  68. 'value' => [
  69. '#type' => 'value',
  70. '#value' => '',
  71. ],
  72. '#field' => $vars['field_info'],
  73. '#instance' => $vars['instance_info'],
  74. '#theme' => 'tripal_field_default',
  75. 'element_validate' => ['tripal_field_widget_form_validate']
  76. ];
  77. // Stub out the form and form_state.
  78. $form = [
  79. '#parents' => [],
  80. '#entity' => $vars['entity'],
  81. ];
  82. $form_state = [
  83. 'build_info' => [
  84. 'args' => [
  85. 0 => NULL,
  86. 1 => $vars['entity']
  87. ],
  88. 'form_id' => 'tripal_entity_form',
  89. ],
  90. 'rebuild' => FALSE,
  91. 'rebuild_info' => [],
  92. 'redirect' => NULL,
  93. 'temporary' => [],
  94. 'submitted' => FALSE,
  95. ];
  96. // stub out the data for the field.
  97. $langcode = LANGUAGE_NONE;
  98. $items = [
  99. 'value' => '',
  100. 'chado-organism_relationship__organism_relationship_id' => '',
  101. 'chado-organism_relationship__subject_id' => '',
  102. 'chado-organism_relationship__object_id' => '',
  103. 'chado-organism_relationship__type_id' => '',
  104. 'chado-organism_relationship__rank' => '',
  105. 'object_name' => '',
  106. 'subject_name' => '',
  107. 'type_name' => '',
  108. ];
  109. $delta = 0;
  110. // Stub out the widget element.
  111. $element = [
  112. '#entity_type' => 'TripalEntity',
  113. '#entity' => $vars['entity'],
  114. '#bundle' => $bundle_name,
  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,
  124. ];
  125. // Execute the form method.
  126. $vars['widget_class']->form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  127. // Check the resulting for array
  128. $this->assertArrayHasKey('subject_name', $widget, 'The form does not have a subject element.');
  129. $this->assertArrayHasKey('type_name', $widget, 'The form does not have a type element.');
  130. $this->assertArrayHasKey('object_name', $widget, 'The form does not have a object element.');
  131. }
  132. }