TripalFieldTestHelper.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * This class can be included at the top of your TripalTestCase to facillitate testing
  4. * fields, widgets and formatters.
  5. */
  6. class TripalFieldTestHelper {
  7. // This is for the initialized field, widget or formatter as indicated by
  8. // $machine_names in the constructor.
  9. public $initialized_class;
  10. // The name of the class being initialized.
  11. public $class_name;
  12. // Information for the field.
  13. public $field_info;
  14. // Information for the field instance.
  15. public $instance_info;
  16. // The loaded bundle the field is attached to.
  17. public $bundle;
  18. // The entity the field is attached to.
  19. public $entity;
  20. // The type of class we are initializing.
  21. // One of 'field', 'widget', or 'formatter'.
  22. public $type;
  23. // The name of the field for the class being tested.
  24. public $field_name;
  25. /**
  26. * Create an instance of TripalFieldTestHelper.
  27. *
  28. * Specifcally, initialize the widget class and save it for further testing.
  29. *
  30. * @param $bundle_name
  31. * The name of the bundle the field should be part of. This bundle must already exist.
  32. * @param $machine_names
  33. * An array of machine names including:
  34. * - field_name: the name of the field (REQUIRED)
  35. * - widget_name: the name of the widget (Only required for widget testing)
  36. * - formatter_name: the name of the formatter (Only required for formatter testing)
  37. * @param $field_info
  38. * The Drupal information for the field you want to test.
  39. * @param $instance_info
  40. * The Drupal information for the field instance you want to test.
  41. */
  42. public function __construct($bundle_name, $machine_names, $entity, $field_info, $instance_info) {
  43. // @debug print "BUNDLE: " .$bundle_name."\n";
  44. // What type of class are we initializing?
  45. $this->type = 'field';
  46. $this->class_name = $machine_names['field_name'];
  47. if (isset($machine_names['widget_name'])) {
  48. $this->type = 'widget';
  49. $this->class_name = $machine_names['widget_name'];
  50. }
  51. elseif (isset($machine_names['formatter_name'])) {
  52. $this->type = 'formatter';
  53. $this->class_name = $machine_names['formatter_name'];
  54. }
  55. $this->field_name = $machine_names['field_name'];
  56. $class_name = '\\' . $this->class_name;
  57. $class_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'tripal_chado')
  58. . '/includes/TripalFields/'.$machine_names['field_name'].'/'.$this->class_name.'.inc';
  59. if ((include_once($class_path)) == TRUE) {
  60. // Save the field information.
  61. if (!$field_info) {
  62. $field_info = $this->getFieldInfo($machine_names['field_name']);
  63. }
  64. $this->field_info = $field_info;
  65. // Save the field instance information.
  66. if (!$instance_info) {
  67. $instance_info = $this->getFieldInfo($bundle_name, $machine_names['field_name']);
  68. }
  69. $this->instance_info = $instance_info;
  70. // Load the bundle.
  71. $this->bundle = tripal_load_bundle_entity(array('name'=> $bundle_name));
  72. // The entity from the specified bundle that the field should be attached to.
  73. $this->entity = $entity;
  74. // @debug print_r($instance_info);
  75. // Initialize the class.
  76. $this->initialized_class = new $class_name($this->field_info, $this->instance_info);
  77. }
  78. }
  79. /**
  80. * Retrieve the initialized class for testing!
  81. */
  82. public function getInitializedClass() {
  83. return $this->initialized_class;
  84. }
  85. /**
  86. * Retrieve the field information for a fiven field.
  87. * @see https://api.drupal.org/api/drupal/modules%21field%21field.info.inc/function/field_info_field/7.x
  88. *
  89. * @param $field_name
  90. * The name of the field to retrieve. $field_name can only refer to a
  91. * non-deleted, active field.
  92. * @return
  93. * The field array as returned by field_info_field() and used when initializing
  94. * this class.
  95. */
  96. public function getFieldInfo($field_name) {
  97. if (empty($this->field_info)) {
  98. $this->field_info = field_info_field($field_name);
  99. }
  100. return $this->field_info;
  101. }
  102. /**
  103. * Retrieve the field instance information for a fiven field.
  104. * @see https://api.drupal.org/api/drupal/modules%21field%21field.info.inc/function/field_info_instance/7.x
  105. *
  106. * @param $bundle_name
  107. * The name of the bundle you want the field attached to. For example, bio_data_1.
  108. * @param $field_name
  109. * The name of the field to retrieve the instance of. $field_name can only refer to a
  110. * non-deleted, active field.
  111. * @return
  112. * The field instance array as returned by field_info_instance() and used when
  113. * initializing this class.
  114. */
  115. public function getInstanceInfo($bundle_name, $field_name) {
  116. if (empty($this->instance_info)) {
  117. $this->instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  118. }
  119. return $this->instance_info;
  120. }
  121. /**
  122. *
  123. */
  124. public function mockElement($delta = 0, $langcode = LANGUAGE_NONE, $required = FALSE) {
  125. return [
  126. '#entity_type' => 'TripalEntity',
  127. '#entity' => $this->entity,
  128. '#bundle' => $this->bundle,
  129. '#field_name' => $this->field_name,
  130. '#language' => $langcode,
  131. '#field_parents' => [],
  132. '#columns' => [],
  133. '#title' => '',
  134. '#description' => '',
  135. '#required' => $required,
  136. '#delta' => $delta,
  137. '#weight' => $delta, //same as delta.
  138. 'value' => [
  139. '#type' => 'value',
  140. '#value' => '',
  141. ],
  142. '#field' => $this->field_info,
  143. '#instance' => $this->instance_info,
  144. '#theme' => 'tripal_field_default',
  145. 'element_validate' => ['tripal_field_widget_form_validate']
  146. ];
  147. }
  148. /**
  149. *
  150. */
  151. public function mockForm() {
  152. return [
  153. '#parents' => [],
  154. '#entity' => $this->entity,
  155. ];
  156. }
  157. /**
  158. *
  159. */
  160. public function mockFormState($delta = 0, $langcode = LANGUAGE_NONE, $values = NULL) {
  161. $form_state = [
  162. 'build_info' => [
  163. 'args' => [
  164. 0 => NULL,
  165. 1 => $entity,
  166. ],
  167. 'form_id' => 'tripal_entity_form',
  168. ],
  169. 'rebuild' => FALSE,
  170. 'rebuild_info' => [],
  171. 'redirect' => NULL,
  172. 'temporary' => [],
  173. 'submitted' => FALSE,
  174. ];
  175. if ($values !== NULL) {
  176. $form_state['values'][$this->field_name][$langcode][$delta] = $values;
  177. }
  178. return $form_state;
  179. }
  180. }