field_test.module 5.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php
// $Id$

/**
 * @file
 * Helper module for the Field API tests.
 *
 * The module defines
 * - an entity type (field_test.entity.inc)
 * - a field type and its formatters and widgets (field_test.field.inc)
 * - a field storage backend (field_test.storage.inc)
 *
 * The main field_test.module file implements generic hooks and provides some
 * test helper functions
 */

17 18 19
require_once DRUPAL_ROOT . '/modules/field/tests/field_test.entity.inc';
require_once DRUPAL_ROOT . '/modules/field/tests/field_test.field.inc';
require_once DRUPAL_ROOT . '/modules/field/tests/field_test.storage.inc';
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

/**
 * Implements hook_permission().
 */
function field_test_permission() {
  $perms = array(
    'access field_test content' => array(
      'title' => t('Access field_test content'),
      'description' => t('View published field_test content.'),
    ),
    'administer field_test content' => array(
      'title' => t('Administer field_test content'),
      'description' => t('Manage field_test content'),
    ),
  );
  return $perms;
}

/**
 * Implements hook_menu().
 */
function field_test_menu() {
  $items = array();
  $bundles = field_info_bundles('test_entity');

  foreach ($bundles as $bundle_name => $bundle_info) {
    $bundle_url_str = str_replace('_', '-', $bundle_name);
    $items['test-entity/add/' . $bundle_url_str] = array(
      'title' => t('Add %bundle test_entity', array('%bundle' => $bundle_info['label'])),
      'page callback' => 'field_test_entity_add',
      'page arguments' => array(2),
      'access arguments' => array('administer field_test content'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  $items['test-entity/%field_test_entity_test/edit'] = array(
    'title' => 'Edit test entity',
    'page callback' => 'field_test_entity_edit',
    'page arguments' => array(1),
    'access arguments' => array('administer field_test content'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

/**
 * Generic op to test _field_invoke behavior.
 *
 * This simulates a field operation callback to be invoked by _field_invoke().
 */
function field_test_field_test_op($obj_type, $object, $field, $instance, $langcode, &$items) {
  return array($langcode => md5(serialize(array($obj_type, $object, $field['field_name'], $langcode, $items))));
}

/**
 * Generic op to test _field_invoke_multiple behavior.
 *
 * This simulates a multiple field operation callback to be invoked by
 * _field_invoke_multiple().
 */
function field_test_field_test_op_multiple($obj_type, $objects, $field, $instances, $langcode, &$items) {
  $result = array();
  foreach ($objects as $id => $object) {
    $result[$id] = array($langcode => md5(serialize(array($obj_type, $object, $field['field_name'], $langcode, $items[$id]))));
  }
  return $result;
}

/**
90
 * Implements hook_field_languages().
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
 */
function field_test_field_languages($obj_type, $field, &$languages) {
  if ($field['settings']['test_hook_in']) {
    // Add an unavailable language.
    $languages[] = 'xx';
    // Remove an available language.
    unset($languages[0]);
  }
}

/**
 * Helper function to enable entity translations.
 */
function field_test_entity_info_translatable($obj_type = NULL, $translatable = NULL) {
  $stored_value = &drupal_static(__FUNCTION__, array());
  if (isset($obj_type)) {
    $stored_value[$obj_type] = $translatable;
108
    entity_info_cache_clear();
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  }
  return $stored_value;
}

/**
 * Store and retrieve keyed data for later verification by unit tests.
 *
 * This function is a simple in-memory key-value store with the
 * distinction that it stores all values for a given key instead of
 * just the most recently set value. field_test module hooks call
 * this function to record their arguments, keyed by hook name. The
 * unit tests later call this function to verify that the correct
 * hooks were called and were passed the correct arguments.
 *
 * This function ignores all calls until the first time it is called
 * with $key of NULL. Each time it is called with $key of NULL, it
 * erases all previously stored data from its internal cache, but also
 * returns the previously stored data to the caller. A typical usage
 * scenario is:
 *
 * @code
 *   // calls to field_test_memorize() here are ignored
 *
 *   // turn on memorization
 *   field_test_memorize();
 *
 *   // call some Field API functions that invoke field_test hooks
 *   $field = field_create_field(...);
 *
 *   // retrieve and reset the memorized hook call data
 *   $mem = field_test_memorize();
 *
 *   // make sure hook_field_create_field() is invoked correctly
 *   assertEqual(count($mem['field_test_field_create_field']), 1);
 *   assertEqual($mem['field_test_field_create_field'][0], array($field));
 * @endcode
 *
 * @param $key
 *   The key under which to store to $value, or NULL as described above.
 * @param $value
 *   A value to store for $key.
 * @return
 *   An array mapping each $key to an array of each $value passed in
 *   for that key.
 */
function field_test_memorize($key = NULL, $value = NULL) {
  $memorize = &drupal_static(__FUNCTION__, NULL);

  if (is_null($key)) {
    $return = $memorize;
    $memorize = array();
    return $return;
  }
  if (is_array($memorize)) {
    $memorize[$key][] = $value;
  }
}

/**
 * Memorize calls to hook_field_create_field().
 */
function field_test_field_create_field($field) {
  $args = func_get_args();
  field_test_memorize(__FUNCTION__, $args);
}

/**
 * Memorize calls to hook_field_insert().
 */
function field_test_field_insert($obj_type, $object, $field, $instance, $items) {
  $args = func_get_args();
  field_test_memorize(__FUNCTION__, $args);
}

/**
 * Memorize calls to hook_field_update().
 */
function field_test_field_update($obj_type, $object, $field, $instance, $items) {
  $args = func_get_args();
  field_test_memorize(__FUNCTION__, $args);
}

/**
 * Memorize calls to hook_field_delete().
 */
function field_test_field_delete($obj_type, $object, $field, $instance, $items) {
  $args = func_get_args();
  field_test_memorize(__FUNCTION__, $args);
}