TripalFieldTestHelper.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 (OPTIONAL)
  36. * The Drupal information for the field you want to test. @see getFieldInfo
  37. * @param $instance_info (OPTIONAL)
  38. * The Drupal information for the field instance you want to test. @see getInstanceInfo
  39. */
  40. public function __construct($bundle_name, $machine_names, $entity, $field_info = NULL, $instance_info = NULL) {
  41. // @debug print "BUNDLE: " .$bundle_name."\n";
  42. // Save the field information.
  43. if (!$field_info) {
  44. $field_info = $this->getFieldInfo($machine_names['field_name']);
  45. }
  46. $this->field_info = $field_info;
  47. // Save the field instance information.
  48. if (!$instance_info) {
  49. $instance_info = $this->getFieldInfo($bundle_name, $machine_names['field_name']);
  50. }
  51. $this->instance_info = $instance_info;
  52. // Load the bundle.
  53. $this->bundle = tripal_load_bundle_entity(array('name'=> $bundle_name));
  54. // What type of class are we initializing?
  55. $this->type = 'field';
  56. $this->class_name = $machine_names['field_name'];
  57. if (isset($machine_names['widget_name'])) {
  58. $this->type = 'widget';
  59. $this->class_name = $machine_names['widget_name'];
  60. }
  61. elseif (isset($machine_names['formatter_name'])) {
  62. $this->type = 'formatter';
  63. $this->class_name = $machine_names['formatter_name'];
  64. }
  65. // The entity from the specified bundle that the field should be attached to.
  66. $this->entity = $entity;
  67. // @debug print_r($instance_info);
  68. // Initialize the class.
  69. $class_name = '\\' . $this->class_name;
  70. $class_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'tripal_chado')
  71. . '/includes/TripalFields/'.$machine_names['field_name'].'/'.$this->class_name.'.inc';
  72. if ((include_once($class_path)) == TRUE) {
  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. }