TripalFieldTestHelper.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 attached to. 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. // What type of class are we initializing?
  44. $this->type = 'field';
  45. $this->class_name = $machine_names['field_name'];
  46. if (isset($machine_names['widget_name'])) {
  47. $this->type = 'widget';
  48. $this->class_name = $machine_names['widget_name'];
  49. }
  50. elseif (isset($machine_names['formatter_name'])) {
  51. $this->type = 'formatter';
  52. $this->class_name = $machine_names['formatter_name'];
  53. }
  54. $this->field_name = $machine_names['field_name'];
  55. $class_name = '\\' . $this->class_name;
  56. $class_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'tripal_chado')
  57. . '/includes/TripalFields/'.$machine_names['field_name'].'/'.$this->class_name.'.inc';
  58. if ((include_once($class_path)) == TRUE) {
  59. // Save the field information.
  60. if (!$field_info) {
  61. $field_info = $this->getFieldInfo($machine_names['field_name']);
  62. }
  63. $this->field_info = $field_info;
  64. // Save the field instance information.
  65. if (!$instance_info) {
  66. $instance_info = $this->getFieldInfo($bundle_name, $machine_names['field_name']);
  67. }
  68. $this->instance_info = $instance_info;
  69. // Load the bundle.
  70. $this->bundle = tripal_load_bundle_entity(array('name'=> $bundle_name));
  71. // The entity from the specified bundle that the field should be attached to.
  72. $this->entity = $entity;
  73. // Initialize the class.
  74. $this->initialized_class = new $class_name($this->field_info, $this->instance_info);
  75. }
  76. }
  77. /**
  78. * Retrieve the initialized class for testing!
  79. */
  80. public function getInitializedClass() {
  81. return $this->initialized_class;
  82. }
  83. /**
  84. * Retrieve the field information for a given field.
  85. * @see https://api.drupal.org/api/drupal/modules%21field%21field.info.inc/function/field_info_field/7.x
  86. *
  87. * @param $field_name
  88. * The name of the field to retrieve. $field_name can only refer to a
  89. * non-deleted, active field.
  90. * @return
  91. * The field array as returned by field_info_field() and used when initializing
  92. * this class.
  93. */
  94. public function getFieldInfo($field_name) {
  95. if (empty($this->field_info)) {
  96. $this->field_info = field_info_field($field_name);
  97. }
  98. return $this->field_info;
  99. }
  100. /**
  101. * Retrieve the field instance information for a given field.
  102. * @see https://api.drupal.org/api/drupal/modules%21field%21field.info.inc/function/field_info_instance/7.x
  103. *
  104. * @param $bundle_name
  105. * The name of the bundle you want the field attached to. For example, bio_data_1.
  106. * @param $field_name
  107. * The name of the field to retrieve the instance of. $field_name can only refer to a
  108. * non-deleted, active field.
  109. * @return
  110. * The field instance array as returned by field_info_instance() and used when
  111. * initializing this class.
  112. */
  113. public function getInstanceInfo($bundle_name, $field_name) {
  114. if (empty($this->instance_info)) {
  115. $this->instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
  116. }
  117. return $this->instance_info;
  118. }
  119. /**
  120. * Create a fake version of the $element parameter used in many field methods
  121. * (e.g. TripalFieldWidget::form).
  122. *
  123. * @param $delta
  124. * The delta for the $element you want to fake.
  125. * @param $langcode
  126. * The language code for the field/widget. This should usually be LANGUAGE_NONE.
  127. * @param $required
  128. * True if the widget is required and false otherwise.
  129. *
  130. * @return
  131. * A fake $element variable for use in testing.
  132. */
  133. public function mockElement($delta = 0, $langcode = LANGUAGE_NONE, $required = FALSE) {
  134. return [
  135. '#entity_type' => 'TripalEntity',
  136. '#entity' => $this->entity,
  137. '#bundle' => $this->bundle,
  138. '#field_name' => $this->field_name,
  139. '#language' => $langcode,
  140. '#field_parents' => [],
  141. '#columns' => [],
  142. '#title' => '',
  143. '#description' => '',
  144. '#required' => $required,
  145. '#delta' => $delta,
  146. '#weight' => $delta, //same as delta.
  147. 'value' => [
  148. '#type' => 'value',
  149. '#value' => '',
  150. ],
  151. '#field' => $this->field_info,
  152. '#instance' => $this->instance_info,
  153. '#theme' => 'tripal_field_default',
  154. 'element_validate' => ['tripal_field_widget_form_validate']
  155. ];
  156. }
  157. /**
  158. * Create a fake version of the create/edit content form with the
  159. * current entity attached.
  160. *
  161. * @return
  162. * A fake $form array for use in testing.
  163. */
  164. public function mockForm() {
  165. return [
  166. '#parents' => [],
  167. '#entity' => $this->entity,
  168. ];
  169. }
  170. /**
  171. * Create a fake version of the create/edit content form_state
  172. * with the current entity attached.
  173. *
  174. * @param $delta
  175. * The delta for the $element you want to fake.
  176. * @param $langcode
  177. * The language code for the field/widget. This should usually be LANGUAGE_NONE.
  178. * @param $values
  179. * An array of values where the key is the form element name and the value is
  180. * the fake user submmitted value.
  181. * @return
  182. * A fake $form_state array for use in testing.
  183. */
  184. public function mockFormState($delta = 0, $langcode = LANGUAGE_NONE, $values = NULL) {
  185. $form_state = [
  186. 'build_info' => [
  187. 'args' => [
  188. 0 => NULL,
  189. 1 => $entity,
  190. ],
  191. 'form_id' => 'tripal_entity_form',
  192. ],
  193. 'rebuild' => FALSE,
  194. 'rebuild_info' => [],
  195. 'redirect' => NULL,
  196. 'temporary' => [],
  197. 'submitted' => FALSE,
  198. ];
  199. if ($values !== NULL) {
  200. $form_state['values'][$this->field_name][$langcode][$delta] = $values;
  201. }
  202. return $form_state;
  203. }
  204. }