TripalFieldTestHelper.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. *
  121. */
  122. public function mockElement($delta = 0, $langcode = LANGUAGE_NONE, $required = FALSE) {
  123. return [
  124. '#entity_type' => 'TripalEntity',
  125. '#entity' => $this->entity,
  126. '#bundle' => $this->bundle,
  127. '#field_name' => $this->field_name,
  128. '#language' => $langcode,
  129. '#field_parents' => [],
  130. '#columns' => [],
  131. '#title' => '',
  132. '#description' => '',
  133. '#required' => $required,
  134. '#delta' => $delta,
  135. '#weight' => $delta, //same as delta.
  136. 'value' => [
  137. '#type' => 'value',
  138. '#value' => '',
  139. ],
  140. '#field' => $this->field_info,
  141. '#instance' => $this->instance_info,
  142. '#theme' => 'tripal_field_default',
  143. 'element_validate' => ['tripal_field_widget_form_validate']
  144. ];
  145. }
  146. /**
  147. *
  148. */
  149. public function mockForm() {
  150. return [
  151. '#parents' => [],
  152. '#entity' => $this->entity,
  153. ];
  154. }
  155. /**
  156. *
  157. */
  158. public function mockFormState($delta = 0, $langcode = LANGUAGE_NONE, $values = NULL) {
  159. $form_state = [
  160. 'build_info' => [
  161. 'args' => [
  162. 0 => NULL,
  163. 1 => $entity,
  164. ],
  165. 'form_id' => 'tripal_entity_form',
  166. ],
  167. 'rebuild' => FALSE,
  168. 'rebuild_info' => [],
  169. 'redirect' => NULL,
  170. 'temporary' => [],
  171. 'submitted' => FALSE,
  172. ];
  173. if ($values !== NULL) {
  174. $form_state['values'][$this->field_name][$langcode][$delta] = $values;
  175. }
  176. return $form_state;
  177. }
  178. }