TripalFieldTestHelper.php 6.1 KB

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