TripalFieldTestHelper.php 7.2 KB

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