- <?php
- * @file
- * Tests for field.module.
- */
-
- * Parent class for Field API tests.
- */
- class FieldTestCase extends BackdropWebTestCase {
-
- * Generate random values for a field_test field.
- *
- * @param $cardinality
- * Number of values to generate.
- * @return array
- * An array of random values, in the format expected for field values.
- */
- function _generateTestFieldValues($cardinality) {
- $values = array();
- for ($i = 0; $i < $cardinality; $i++) {
-
- $values[$i]['value'] = mt_rand(1, 127);
- }
- return $values;
- }
-
-
- * Assert that a field has the expected values in an entity.
- *
- * This function only checks a single column in the field values.
- *
- * @param $entity
- * The entity to test.
- * @param $field_name
- * The name of the field to test
- * @param $langcode
- * The language code for the values.
- * @param $expected_values
- * The array of expected values.
- * @param $column
- * (Optional) the name of the column to check.
- */
- function assertFieldValues($entity, $field_name, $langcode, $expected_values, $column = 'value') {
- $e = clone $entity;
- field_attach_load('test_entity', array($e->ftid => $e));
- $values = isset($e->{$field_name}[$langcode]) ? $e->{$field_name}[$langcode] : array();
- $this->assertEqual(count($values), count($expected_values), 'Expected number of values were saved.');
- foreach ($expected_values as $key => $value) {
- $this->assertEqual($values[$key][$column], $value, format_string('Value @value was saved correctly.', array('@value' => $value)));
- }
- }
- }
-
- class FieldAttachTestCase extends FieldTestCase {
- protected $field_name;
- protected $field;
- protected $instance;
-
- function setUp() {
-
-
-
- $modules = func_get_args();
- if (isset($modules[0]) && is_array($modules[0])) {
- $modules = $modules[0];
- }
- if (!in_array('field_test', $modules)) {
- $modules[] = 'field_test';
- }
- parent::setUp($modules);
-
- $this->createFieldWithInstance();
- }
-
-
- * Create a field and an instance of it.
- *
- * @param string $suffix
- * (optional) A string that should only contain characters that are valid in
- * PHP variable names as well.
- */
- function createFieldWithInstance($suffix = '') {
- $field_name = 'field_name' . $suffix;
- $field = 'field' . $suffix;
- $instance = 'instance' . $suffix;
-
- $this->$field_name = backdrop_strtolower($this->randomName() . '_field_name' . $suffix);
- $this->$field = array('field_name' => $this->$field_name, 'type' => 'test_field', 'cardinality' => 4);
- $this->$field = field_create_field($this->$field);
- $this->$instance = array(
- 'field_name' => $this->$field_name,
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'label' => $this->randomName() . '_label',
- 'description' => $this->randomName() . '_description',
- 'weight' => mt_rand(0, 127),
- 'settings' => array(
- 'test_instance_setting' => $this->randomName(),
- ),
- 'widget' => array(
- 'type' => 'test_field_widget',
- 'label' => 'Test Field',
- 'settings' => array(
- 'test_widget_setting' => $this->randomName(),
- )
- )
- );
- field_create_instance($this->$instance);
- }
- }
-
- * Unit test class for storage-related field_attach_* functions.
- *
- * All field_attach_* test work with all field_storage plugins and
- * all hook_field_attach_pre_{load,insert,update}() hooks.
- */
- class FieldAttachStorageTestCase extends FieldAttachTestCase {
-
- * Check field values insert, update and load.
- *
- * Works independently of the underlying field storage backend. Inserts or
- * updates random field data and then loads and verifies the data.
- */
- function testFieldAttachSaveLoad() {
-
-
- $this->instance['settings']['test_hook_field_load'] = TRUE;
- field_update_instance($this->instance);
- $langcode = LANGUAGE_NONE;
-
- $entity_type = 'test_entity';
- $values = array();
-
-
-
-
- $current_revision = NULL;
- $revision = array();
- for ($revision_id = 0; $revision_id < 3; $revision_id++) {
- $revision[$revision_id] = field_test_create_entity(0, $revision_id, $this->instance['bundle']);
-
- $values[$revision_id] = $this->_generateTestFieldValues($this->field['cardinality'] + 1);
- $current_revision = $revision_id;
-
- if (!$revision_id) {
- $revision[$revision_id]->{$this->field_name}[$langcode] = $values[$revision_id];
- field_attach_insert($entity_type, $revision[$revision_id]);
- }
- else {
-
- $revision[$revision_id]->{$this->field_name}[$langcode] = $values[$revision_id];
- field_attach_update($entity_type, $revision[$revision_id]);
- }
- }
-
-
- $entity = field_test_create_entity(0, 0, $this->instance['bundle']);
- field_attach_load($entity_type, array(0 => $entity));
-
- $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field['cardinality'], 'Current revision: expected number of values');
- for ($delta = 0; $delta < $this->field['cardinality']; $delta++) {
-
- $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'] , $values[$current_revision][$delta]['value'], format_string('Current revision: expected value %delta was found.', array('%delta' => $delta)));
-
- $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['additional_key'], 'additional_value', format_string('Current revision: extra information for value %delta was found', array('%delta' => $delta)));
- }
-
-
- foreach (array_keys($revision) as $revision_id) {
- $entity = field_test_create_entity(0, $revision_id, $this->instance['bundle']);
- field_attach_load_revision($entity_type, array(0 => $entity));
-
- $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field['cardinality'], format_string('Revision %revision_id: expected number of values.', array('%revision_id' => $revision_id)));
- for ($delta = 0; $delta < $this->field['cardinality']; $delta++) {
-
- $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'], $values[$revision_id][$delta]['value'], format_string('Revision %revision_id: expected value %delta was found.', array('%revision_id' => $revision_id, '%delta' => $delta)));
-
- $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['additional_key'], 'additional_value', format_string('Revision %revision_id: extra information for value %delta was found', array('%revision_id' => $revision_id, '%delta' => $delta)));
- }
- }
- }
-
-
- * Test the 'multiple' load feature.
- */
- function testFieldAttachLoadMultiple() {
- $entity_type = 'test_entity';
- $langcode = LANGUAGE_NONE;
-
-
- $bundles = array(
- 1 => 'test_bundle_1',
- 2 => 'test_bundle_2',
- );
- field_test_create_bundle($bundles[1]);
- field_test_create_bundle($bundles[2]);
-
-
-
-
- $field_bundles_map = array(
- 1 => array(1, 2),
- 2 => array(1),
- 3 => array(2),
- );
- $field_names = array();
- $entities = array();
- for ($i = 1; $i <= 3; $i++) {
- $field_names[$i] = 'field_' . $i;
- $field = array('field_name' => $field_names[$i], 'type' => 'test_field');
- $field = field_create_field($field);
- $field_names[$i] = $field['field_name'];
- foreach ($field_bundles_map[$i] as $bundle) {
- $instance = array(
- 'field_name' => $field_names[$i],
- 'entity_type' => 'test_entity',
- 'bundle' => $bundles[$bundle],
- 'settings' => array(
-
-
- 'test_hook_field_load' => TRUE,
- ),
- );
- field_create_instance($instance);
- }
- }
-
-
- $values = array();
- foreach ($bundles as $index => $bundle) {
- $entities[$index] = field_test_create_entity($index, $index, $bundle);
- $entity = clone($entities[$index]);
- $instances = field_info_instances('test_entity', $bundle);
- foreach ($instances as $field_name => $instance) {
- $values[$index][$field_name] = mt_rand(1, 127);
- $entity->$field_name = array($langcode => array(array('value' => $values[$index][$field_name])));
- }
- field_attach_insert($entity_type, $entity);
- }
-
-
- field_attach_load($entity_type, $entities);
- foreach ($entities as $index => $entity) {
- $instances = field_info_instances($entity_type, $bundles[$index]);
- foreach ($instances as $field_name => $instance) {
-
- $this->assertEqual($entity->{$field_name}[$langcode][0]['value'], $values[$index][$field_name], format_string('Entity %index: expected value was found.', array('%index' => $index)));
-
- $this->assertEqual($entity->{$field_name}[$langcode][0]['additional_key'], 'additional_value', format_string('Entity %index: extra information was found', array('%index' => $index)));
- }
- }
-
-
- $entity = field_test_create_entity(1, 1, $bundles[1]);
- field_attach_load($entity_type, array(1 => $entity), FIELD_LOAD_CURRENT, array('field_name' => $field_names[1]));
- $this->assertEqual($entity->{$field_names[1]}[$langcode][0]['value'], $values[1][$field_names[1]], format_string('Entity %index: expected value was found.', array('%index' => 1)));
- $this->assertEqual($entity->{$field_names[1]}[$langcode][0]['additional_key'], 'additional_value', format_string('Entity %index: extra information was found', array('%index' => 1)));
- $this->assert(!isset($entity->{$field_names[2]}), format_string('Entity %index: field %field_name is not loaded.', array('%index' => 2, '%field_name' => $field_names[2])));
- $this->assert(!isset($entity->{$field_names[3]}), format_string('Entity %index: field %field_name is not loaded.', array('%index' => 3, '%field_name' => $field_names[3])));
- }
-
-
- * Test saving and loading fields using different storage backends.
- */
- function testFieldAttachSaveLoadDifferentStorage() {
- $entity_type = 'test_entity';
- $langcode = LANGUAGE_NONE;
-
-
- $fields = array(
- array(
- 'field_name' => 'field_1',
- 'type' => 'test_field',
- 'cardinality' => 4,
- 'storage' => array('type' => 'field_sql_storage')
- ),
- array(
- 'field_name' => 'field_2',
- 'type' => 'test_field',
- 'cardinality' => 4,
- 'storage' => array('type' => 'field_test_storage')
- ),
- );
- foreach ($fields as $field) {
- field_create_field($field);
- $instance = array(
- 'field_name' => $field['field_name'],
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- );
- field_create_instance($instance);
- }
-
- $entity_init = field_test_create_entity();
-
-
- $entity = clone($entity_init);
- $values = array();
- foreach ($fields as $field) {
- $values[$field['field_name']] = $this->_generateTestFieldValues($this->field['cardinality']);
- $entity->{$field['field_name']}[$langcode] = $values[$field['field_name']];
- }
- field_attach_insert($entity_type, $entity);
-
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- foreach ($fields as $field) {
- $this->assertEqual($values[$field['field_name']], $entity->{$field['field_name']}[$langcode], format_string('%storage storage: expected values were found.', array('%storage' => $field['storage']['type'])));
- }
- }
-
-
- * Test storage details alteration.
- *
- * @see field_test_storage_details_alter()
- */
- function testFieldStorageDetailsAlter() {
- $field_name = 'field_test_change_my_details';
- $field = array(
- 'field_name' => $field_name,
- 'type' => 'test_field',
- 'cardinality' => 4,
- 'storage' => array('type' => 'field_test_storage'),
- );
- field_create_field($field);
- $instance = array(
- 'field_name' => $field_name,
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- );
- field_create_instance($instance);
-
- $field = field_info_field($instance['field_name']);
-
-
- $this->assertTrue(array_key_exists('backdrop_variables', $field['storage']['details']), 'The storage type is Backdrop variables.');
-
- $details = $field['storage']['details']['backdrop_variables'];
-
-
-
- $this->assertTrue(array_key_exists('moon', $details[FIELD_LOAD_CURRENT]), 'Moon is available in the instance array.');
- $this->assertTrue(array_key_exists('mars', $details[FIELD_LOAD_REVISION]), 'Mars is available in the instance array.');
-
-
-
- foreach ((array) $field['columns'] as $column_name => $attributes) {
- $this->assertEqual($details[FIELD_LOAD_CURRENT]['moon'][$column_name], $column_name, format_string('Column name %value matches the definition in %bin.', array('%value' => $column_name, '%bin' => 'moon[FIELD_LOAD_CURRENT]')));
- $this->assertEqual($details[FIELD_LOAD_REVISION]['mars'][$column_name], $column_name, format_string('Column name %value matches the definition in %bin.', array('%value' => $column_name, '%bin' => 'mars[FIELD_LOAD_REVISION]')));
- }
- }
-
-
- * Tests insert and update with missing or NULL fields.
- */
- function testFieldAttachSaveMissingData() {
- $entity_type = 'test_entity';
- $entity_init = field_test_create_entity();
- $langcode = LANGUAGE_NONE;
-
-
- $entity = clone($entity_init);
- field_attach_insert($entity_type, $entity);
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $this->assertTrue(empty($entity->{$this->field_name}), 'Insert: missing field results in no value saved');
-
-
- field_cache_clear();
- $entity = clone($entity_init);
- $entity->{$this->field_name} = NULL;
- field_attach_insert($entity_type, $entity);
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $this->assertTrue(empty($entity->{$this->field_name}), 'Insert: NULL field results in no value saved');
-
-
- field_cache_clear();
- $entity = clone($entity_init);
- $values = $this->_generateTestFieldValues(1);
- $entity->{$this->field_name}[$langcode] = $values;
- field_attach_insert($entity_type, $entity);
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $this->assertEqual($entity->{$this->field_name}[$langcode], $values, 'Field data saved');
-
-
- field_cache_clear();
- $entity = clone($entity_init);
- field_attach_update($entity_type, $entity);
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $this->assertEqual($entity->{$this->field_name}[$langcode], $values, 'Update: missing field leaves existing values in place');
-
-
- field_cache_clear();
- $entity = clone($entity_init);
- $entity->{$this->field_name} = NULL;
- field_attach_update($entity_type, $entity);
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $this->assertTrue(empty($entity->{$this->field_name}), 'Update: NULL field removes existing values');
-
-
- field_cache_clear();
- $entity = clone($entity_init);
- $values = $this->_generateTestFieldValues(1);
- $entity->{$this->field_name}[$langcode] = $values;
- field_attach_update($entity_type, $entity);
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $this->assertEqual($entity->{$this->field_name}[$langcode], $values, 'Field data saved');
-
-
- field_cache_clear();
- $entity = clone($entity_init);
- $entity->{$this->field_name} = array();
- field_attach_update($entity_type, $entity);
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $this->assertTrue(empty($entity->{$this->field_name}), 'Update: empty array removes existing values');
- }
-
-
- * Test insert with missing or NULL fields, with default value.
- */
- function testFieldAttachSaveMissingDataDefaultValue() {
-
- $this->instance['default_value_function'] = 'field_test_default_value';
- field_update_instance($this->instance);
-
- $entity_type = 'test_entity';
- $entity_init = field_test_create_entity();
- $langcode = LANGUAGE_NONE;
-
-
- $entity = clone($entity_init);
- $entity->{$this->field_name}[$langcode] = NULL;
- field_attach_insert($entity_type, $entity);
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $this->assertTrue(empty($entity->{$this->field_name}[$langcode]), 'Insert: NULL field results in no value saved');
-
-
- field_cache_clear();
- $entity = clone($entity_init);
- field_attach_insert($entity_type, $entity);
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $values = field_test_default_value($entity_type, $entity, $this->field, $this->instance);
- $this->assertEqual($entity->{$this->field_name}[$langcode], $values, 'Insert: missing field results in default value saved');
- }
-
-
- * Test field_has_data().
- */
- function testFieldHasData() {
- $entity_type = 'test_entity';
- $langcode = LANGUAGE_NONE;
-
- $field_name = 'field_1';
- $field = array('field_name' => $field_name, 'type' => 'test_field');
- $field = field_create_field($field);
-
- $this->assertFalse(field_has_data($field), "No data should be detected.");
-
- $revision_table = _field_sql_storage_revision_tablename($field);
-
- $columns = array('entity_type', 'entity_id', 'revision_id', 'delta', 'language', $field_name . '_value');
-
- $eid = 0;
-
-
- $query = db_insert($revision_table)->fields($columns);
- $query->values(array($entity_type, $eid, 0, 0, $langcode, 1));
- $query->execute();
-
- $this->assertTrue(field_has_data($field), "Revision data only should be detected.");
-
- $field_name = 'field_2';
- $field = array('field_name' => $field_name, 'type' => 'test_field');
- $field = field_create_field($field);
-
- $this->assertFalse(field_has_data($field), "No data should be detected.");
-
- $table = _field_sql_storage_tablename($field);
- $columns = array('entity_type', 'entity_id', 'revision_id', 'delta', 'language', $field_name . '_value');
-
- $eid = 1;
-
-
- $query = db_insert($table)->fields($columns);
- $query->values(array($entity_type, $eid, 0, 0, $langcode, 1));
- $query->execute();
-
- $this->assertTrue(field_has_data($field), "Values only in field table should be detected.");
- }
-
-
- * Test field_attach_delete().
- */
- function testFieldAttachDelete() {
- $entity_type = 'test_entity';
- $langcode = LANGUAGE_NONE;
- $rev[0] = field_test_create_entity(0, 0, $this->instance['bundle']);
-
-
- $values = $this->_generateTestFieldValues($this->field['cardinality']);
- $rev[0]->{$this->field_name}[$langcode] = $values;
- field_attach_insert($entity_type, $rev[0]);
-
-
- $rev[1] = field_test_create_entity(0, 1, $this->instance['bundle']);
- $rev[1]->{$this->field_name}[$langcode] = $values;
- field_attach_update($entity_type, $rev[1]);
-
-
- $rev[2] = field_test_create_entity(0, 2, $this->instance['bundle']);
- $rev[2]->{$this->field_name}[$langcode] = $values;
- field_attach_update($entity_type, $rev[2]);
-
-
- foreach (array_keys($rev) as $vid) {
- $read = field_test_create_entity(0, $vid, $this->instance['bundle']);
- field_attach_load_revision($entity_type, array(0 => $read));
- $this->assertEqual(count($read->{$this->field_name}[$langcode]), $this->field['cardinality'], "The test entity revision $vid has {$this->field['cardinality']} values.");
- }
-
-
- field_attach_delete_revision($entity_type, $rev[1]);
- foreach (array(0, 2) as $vid) {
- $read = field_test_create_entity(0, $vid, $this->instance['bundle']);
- field_attach_load_revision($entity_type, array(0 => $read));
- $this->assertEqual(count($read->{$this->field_name}[$langcode]), $this->field['cardinality'], "The test entity revision $vid has {$this->field['cardinality']} values.");
- }
-
-
- $read = field_test_create_entity(0, 2, $this->instance['bundle']);
- field_attach_load($entity_type, array(0 => $read));
- $this->assertEqual(count($read->{$this->field_name}[$langcode]), $this->field['cardinality'], "The test entity current revision has {$this->field['cardinality']} values.");
-
-
- field_attach_delete($entity_type, $rev[2]);
- foreach (array(0, 1, 2) as $vid) {
- $read = field_test_create_entity(0, $vid, $this->instance['bundle']);
- field_attach_load_revision($entity_type, array(0 => $read));
- $this->assertIdentical($read->{$this->field_name}, array(), "The test entity revision $vid is deleted.");
- }
- $read = field_test_create_entity(0, 2, $this->instance['bundle']);
- field_attach_load($entity_type, array(0 => $read));
- $this->assertIdentical($read->{$this->field_name}, array(), 'The test entity current revision is deleted.');
- }
-
-
- * Test field_attach_create_bundle() and field_attach_rename_bundle().
- */
- function testFieldAttachCreateRenameBundle() {
-
-
- $new_bundle = 'test_bundle_' . backdrop_strtolower($this->randomName());
- field_test_create_bundle($new_bundle);
-
-
- $this->instance['bundle'] = $new_bundle;
- field_create_instance($this->instance);
-
-
- $entity = field_test_create_entity(0, 0, $this->instance['bundle']);
- $langcode = LANGUAGE_NONE;
- $values = $this->_generateTestFieldValues($this->field['cardinality']);
- $entity->{$this->field_name}[$langcode] = $values;
- $entity_type = 'test_entity';
- field_attach_insert($entity_type, $entity);
-
-
- $entity = field_test_create_entity(0, 0, $this->instance['bundle']);
- field_attach_load($entity_type, array(0 => $entity));
- $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field['cardinality'], "Data is retrieved for the new bundle");
-
-
-
- $new_bundle = 'test_bundle_' . backdrop_strtolower($this->randomName());
- field_test_rename_bundle($this->instance['bundle'], $new_bundle);
-
-
- $this->instance = field_info_instance($entity_type, $this->field_name, $new_bundle);
- $this->assertIdentical($this->instance['bundle'], $new_bundle, "Bundle name has been updated in the instance.");
-
-
- $entity = field_test_create_entity(0, 0, $new_bundle);
- field_attach_load($entity_type, array(0 => $entity));
- $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field['cardinality'], "Bundle name has been updated in the field storage");
- }
-
-
- * Test field_attach_delete_bundle().
- */
- function testFieldAttachDeleteBundle() {
-
-
- $new_bundle = 'test_bundle_' . backdrop_strtolower($this->randomName());
- field_test_create_bundle($new_bundle);
-
-
- $this->instance['bundle'] = $new_bundle;
- field_create_instance($this->instance);
-
-
- $field_name = backdrop_strtolower($this->randomName() . '_field_name');
- $field = array('field_name' => $field_name, 'type' => 'test_field', 'cardinality' => 1);
- field_create_field($field);
- $instance = array(
- 'field_name' => $field_name,
- 'entity_type' => 'test_entity',
- 'bundle' => $this->instance['bundle'],
- 'label' => $this->randomName() . '_label',
- 'description' => $this->randomName() . '_description',
- 'weight' => mt_rand(0, 127),
-
- 'widget' => array(
- 'type' => 'test_field_widget',
- 'settings' => array(
- 'size' => mt_rand(0, 255))));
- field_create_instance($instance);
-
-
- $entity = field_test_create_entity(0, 0, $this->instance['bundle']);
- $langcode = LANGUAGE_NONE;
- $values = $this->_generateTestFieldValues($this->field['cardinality']);
- $entity->{$this->field_name}[$langcode] = $values;
- $entity->{$field_name}[$langcode] = $this->_generateTestFieldValues(1);
- field_attach_insert('test_entity', $entity);
-
-
- $entity = field_test_create_entity(0, 0, $this->instance['bundle']);
- field_attach_load('test_entity', array(0 => $entity));
- $this->assertEqual(count($entity->{$this->field_name}[$langcode]), 4, 'First field got loaded');
- $this->assertEqual(count($entity->{$field_name}[$langcode]), 1, 'Second field got loaded');
-
-
-
- field_test_delete_bundle($this->instance['bundle']);
-
-
- $entity = field_test_create_entity(0, 0, $this->instance['bundle']);
- field_attach_load('test_entity', array(0 => $entity));
- $this->assertFalse(isset($entity->{$this->field_name}[$langcode]), 'No data for first field');
- $this->assertFalse(isset($entity->{$field_name}[$langcode]), 'No data for second field');
-
-
- $this->assertFalse(field_read_instance('test_entity', $this->field_name, $this->instance['bundle']), "First field is deleted");
- $this->assertFalse(field_read_instance('test_entity', $field_name, $instance['bundle']), "Second field is deleted");
- }
- }
-
- * Unit test class for non-storage related field_attach_* functions.
- */
- class FieldAttachOtherTestCase extends FieldAttachTestCase {
- protected $field_name_2;
- protected $field_2;
- protected $instance_2;
-
-
- * Test field_attach_view() and field_attach_prepare_view().
- */
- function testFieldAttachView() {
- $this->createFieldWithInstance('_2');
-
- $entity_type = 'test_entity';
- $entity_init = field_test_create_entity();
- $langcode = LANGUAGE_NONE;
- $options = array('field_name' => $this->field_name_2);
-
-
- $values = $this->_generateTestFieldValues($this->field['cardinality']);
- $entity_init->{$this->field_name}[$langcode] = $values;
- $values_2 = $this->_generateTestFieldValues($this->field_2['cardinality']);
- $entity_init->{$this->field_name_2}[$langcode] = $values_2;
-
-
- $entity = clone($entity_init);
- $formatter_setting = $this->randomName();
- $this->instance['display'] = array(
- 'full' => array(
- 'label' => 'above',
- 'type' => 'field_test_default',
- 'settings' => array(
- 'test_formatter_setting' => $formatter_setting,
- )
- ),
- );
- field_update_instance($this->instance);
- $formatter_setting_2 = $this->randomName();
- $this->instance_2['display'] = array(
- 'full' => array(
- 'label' => 'above',
- 'type' => 'field_test_default',
- 'settings' => array(
- 'test_formatter_setting' => $formatter_setting_2,
- )
- ),
- );
- field_update_instance($this->instance_2);
-
- field_attach_prepare_view($entity_type, array($entity->ftid => $entity), 'full');
- $entity->content = field_attach_view($entity_type, $entity, 'full');
- $output = backdrop_render($entity->content);
- $this->content = $output;
- $this->assertRaw($this->instance['label'], "First field's label is displayed.");
- foreach ($values as $delta => $value) {
- $this->content = $output;
- $this->assertRaw("$formatter_setting|{$value['value']}", "Value $delta is displayed, formatter settings are applied.");
- }
- $this->assertRaw($this->instance_2['label'], "Second field's label is displayed.");
- foreach ($values_2 as $delta => $value) {
- $this->content = $output;
- $this->assertRaw("$formatter_setting_2|{$value['value']}", "Value $delta is displayed, formatter settings are applied.");
- }
-
- field_attach_prepare_view($entity_type, array($entity->ftid => $entity), 'full', $langcode, $options);
- $entity->content = field_attach_view($entity_type, $entity, 'full', $langcode, $options);
- $output = backdrop_render($entity->content);
- $this->content = $output;
- $this->assertNoRaw($this->instance['label'], "First field's label is not displayed.");
- foreach ($values as $delta => $value) {
- $this->content = $output;
- $this->assertNoRaw("$formatter_setting|{$value['value']}", "Value $delta is displayed, formatter settings are applied.");
- }
- $this->assertRaw($this->instance_2['label'], "Second field's label is displayed.");
- foreach ($values_2 as $delta => $value) {
- $this->content = $output;
- $this->assertRaw("$formatter_setting_2|{$value['value']}", "Value $delta is displayed, formatter settings are applied.");
- }
-
-
- $entity = clone($entity_init);
- $this->instance['display']['full']['label'] = 'hidden';
- field_update_instance($this->instance);
- field_attach_prepare_view($entity_type, array($entity->ftid => $entity), 'full');
- $entity->content = field_attach_view($entity_type, $entity, 'full');
- $output = backdrop_render($entity->content);
- $this->content = $output;
- $this->assertNoRaw($this->instance['label'], "Hidden label: label is not displayed.");
-
-
- $entity = clone($entity_init);
- $this->instance['display'] = array(
- 'full' => array(
- 'label' => 'above',
- 'type' => 'hidden',
- ),
- );
- field_update_instance($this->instance);
- field_attach_prepare_view($entity_type, array($entity->ftid => $entity), 'full');
- $entity->content = field_attach_view($entity_type, $entity, 'full');
- $output = backdrop_render($entity->content);
- $this->content = $output;
- $this->assertNoRaw($this->instance['label'], "Hidden field: label is not displayed.");
- foreach ($values as $delta => $value) {
- $this->assertNoRaw("$formatter_setting|{$value['value']}", "Hidden field: value $delta is not displayed.");
- }
-
-
- $entity = clone($entity_init);
- $formatter_setting = $this->randomName();
- $this->instance['display'] = array(
- 'full' => array(
- 'label' => 'above',
- 'type' => 'field_test_multiple',
- 'settings' => array(
- 'test_formatter_setting_multiple' => $formatter_setting,
- )
- ),
- );
- field_update_instance($this->instance);
- field_attach_prepare_view($entity_type, array($entity->ftid => $entity), 'full');
- $entity->content = field_attach_view($entity_type, $entity, 'full');
- $output = backdrop_render($entity->content);
- $display = $formatter_setting;
- foreach ($values as $delta => $value) {
- $display .= "|$delta:{$value['value']}";
- }
- $this->content = $output;
- $this->assertRaw($display, "Multiple formatter: all values are displayed, formatter settings are applied.");
-
-
- $entity = clone($entity_init);
- $formatter_setting = $this->randomName();
- $this->instance['display'] = array(
- 'full' => array(
- 'label' => 'above',
- 'type' => 'field_test_with_prepare_view',
- 'settings' => array(
- 'test_formatter_setting_additional' => $formatter_setting,
- )
- ),
- );
- field_update_instance($this->instance);
- field_attach_prepare_view($entity_type, array($entity->ftid => $entity), 'full');
- $entity->content = field_attach_view($entity_type, $entity, 'full');
- $output = backdrop_render($entity->content);
- $this->content = $output;
- foreach ($values as $delta => $value) {
- $this->content = $output;
- $expected = $formatter_setting . '|' . $value['value'] . '|' . ($value['value'] + 1);
- $this->assertRaw($expected, "Value $delta is displayed, formatter settings are applied.");
- }
-
-
-
-
-
- $variables = array();
- field_attach_preprocess($entity_type, $entity, $entity->content, $variables);
- $result = TRUE;
- foreach ($values as $delta => $item) {
- if ($variables[$this->field_name][$delta]['value'] !== $item['value']) {
- $result = FALSE;
- break;
- }
- }
- $this->assertTrue($result, format_string('Variable $@field_name correctly populated.', array('@field_name' => $this->field_name)));
- }
-
-
- * Tests the 'multiple entity' behavior of field_attach_prepare_view().
- */
- function testFieldAttachPrepareViewMultiple() {
- $entity_type = 'test_entity';
- $langcode = LANGUAGE_NONE;
-
-
- $this->instance['display']['full']['type'] = 'hidden';
- field_update_instance($this->instance);
-
-
-
- field_test_create_bundle('test_bundle_2');
- $formatter_setting = $this->randomName();
- $this->instance_2 = $this->instance;
- $this->instance_2['bundle'] = 'test_bundle_2';
- $this->instance_2['display']['full'] = array(
- 'type' => 'field_test_with_prepare_view',
- 'settings' => array(
- 'test_formatter_setting_additional' => $formatter_setting,
- )
- );
- field_create_instance($this->instance_2);
-
-
- $entity1_init = field_test_create_entity(1, 1, 'test_bundle');
- $values1 = $this->_generateTestFieldValues($this->field['cardinality']);
- $entity1_init->{$this->field_name}[$langcode] = $values1;
-
- $entity2_init = field_test_create_entity(2, 2, 'test_bundle_2');
- $values2 = $this->_generateTestFieldValues($this->field['cardinality']);
- $entity2_init->{$this->field_name}[$langcode] = $values2;
-
-
- $entity1 = clone($entity1_init);
- $entity2 = clone($entity2_init);
- field_attach_prepare_view($entity_type, array($entity1->ftid => $entity1, $entity2->ftid => $entity2), 'full');
- $this->assertFalse(isset($entity1->{$this->field_name}[$langcode][0]['additional_formatter_value']), 'Entity 1 did not run through the prepare_view hook.');
- $this->assertTrue(isset($entity2->{$this->field_name}[$langcode][0]['additional_formatter_value']), 'Entity 2 ran through the prepare_view hook.');
-
-
- $entity1 = clone($entity1_init);
- $entity2 = clone($entity2_init);
- field_attach_prepare_view($entity_type, array($entity2->ftid => $entity2, $entity1->ftid => $entity1), 'full');
- $this->assertFalse(isset($entity1->{$this->field_name}[$langcode][0]['additional_formatter_value']), 'Entity 1 did not run through the prepare_view hook.');
- $this->assertTrue(isset($entity2->{$this->field_name}[$langcode][0]['additional_formatter_value']), 'Entity 2 ran through the prepare_view hook.');
- }
-
-
- * Test field cache.
- */
- function testFieldAttachCache() {
-
- $entity_init = field_test_create_entity(1, 1, $this->instance['bundle']);
- $langcode = LANGUAGE_NONE;
- $values = $this->_generateTestFieldValues($this->field['cardinality']);
-
-
- $entity_type = 'test_entity';
- $cid = "field:$entity_type:{$entity_init->ftid}";
-
-
- $this->assertFalse(cache('field')->get($cid), 'Non-cached: no initial cache entry');
-
-
- $entity = clone($entity_init);
- $entity->{$this->field_name}[$langcode] = $values;
- field_attach_insert($entity_type, $entity);
- $this->assertFalse(cache('field')->get($cid), 'Non-cached: no cache entry on insert');
-
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $this->assertFalse(cache('field')->get($cid), 'Non-cached: no cache entry on load');
-
-
-
- $entity_type = 'test_cacheable_entity';
- $cid = "field:$entity_type:{$entity_init->ftid}";
- $instance = $this->instance;
- $instance['entity_type'] = $entity_type;
- field_create_instance($instance);
-
-
- $this->assertFalse(cache('field')->get($cid), 'Cached: no initial cache entry');
-
-
- $entity = clone($entity_init);
- $entity->{$this->field_name}[$langcode] = $values;
- field_attach_insert($entity_type, $entity);
- $this->assertFalse(cache('field')->get($cid), 'Cached: no cache entry on insert');
-
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity), FIELD_LOAD_CURRENT, array('field_name' => $this->field_name));
- $this->assertFalse(cache('field')->get($cid), 'Cached: no cache entry on loading a single field');
-
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $cache = cache('field')->get($cid);
- $this->assertEqual($cache->data[$this->field_name][$langcode], $values, 'Cached: correct cache entry on load');
-
-
- $values = $this->_generateTestFieldValues($this->field['cardinality']);
- $entity = clone($entity_init);
- $entity->{$this->field_name}[$langcode] = $values;
- field_attach_update($entity_type, $entity);
- $this->assertFalse(cache('field')->get($cid), 'Cached: no cache entry on update');
-
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $cache = cache('field')->get($cid);
- $this->assertEqual($cache->data[$this->field_name][$langcode], $values, 'Cached: correct cache entry on load');
-
-
- $entity_init = field_test_create_entity(1, 2, $this->instance['bundle']);
- $values = $this->_generateTestFieldValues($this->field['cardinality']);
- $entity = clone($entity_init);
- $entity->{$this->field_name}[$langcode] = $values;
- field_attach_update($entity_type, $entity);
- $this->assertFalse(cache('field')->get($cid), 'Cached: no cache entry on new revision creation');
-
-
- $entity = clone($entity_init);
- field_attach_load($entity_type, array($entity->ftid => $entity));
- $cache = cache('field')->get($cid);
- $this->assertEqual($cache->data[$this->field_name][$langcode], $values, 'Cached: correct cache entry on load');
-
-
- field_attach_delete($entity_type, $entity);
- $this->assertFalse(cache('field')->get($cid), 'Cached: no cache entry after delete');
- }
-
-
- * Test field_attach_validate().
- *
- * Verify that field_attach_validate() invokes the correct
- * hook_field_validate.
- */
- function testFieldAttachValidate() {
- $this->createFieldWithInstance('_2');
-
- $entity_type = 'test_entity';
- $entity = field_test_create_entity(0, 0, $this->instance['bundle']);
- $langcode = LANGUAGE_NONE;
-
-
- $values = array();
- for ($delta = 0; $delta < $this->field['cardinality']; $delta++) {
- $values[$delta]['value'] = -1;
- }
-
- $values[1]['value'] = 1;
- $entity->{$this->field_name}[$langcode] = $values;
-
-
- $values_2 = array();
- for ($delta = 0; $delta < $this->field_2['cardinality']; $delta++) {
- $values_2[$delta]['value'] = -1;
- }
- $entity->{$this->field_name_2}[$langcode] = $values_2;
-
-
- $errors = array();
- try {
- field_attach_validate($entity_type, $entity);
- }
- catch (FieldValidationException $e) {
- $errors = $e->errors;
- }
-
- foreach ($values as $delta => $value) {
- if ($value['value'] != 1) {
- $this->assertIdentical($errors[$this->field_name][$langcode][$delta][0]['error'], 'field_test_invalid', "Error set on first field's value $delta");
- $this->assertEqual(count($errors[$this->field_name][$langcode][$delta]), 1, "Only one error set on first field's value $delta");
- unset($errors[$this->field_name][$langcode][$delta]);
- }
- else {
- $this->assertFalse(isset($errors[$this->field_name][$langcode][$delta]), "No error set on first field's value $delta");
- }
- }
- foreach ($values_2 as $delta => $value) {
- $this->assertIdentical($errors[$this->field_name_2][$langcode][$delta][0]['error'], 'field_test_invalid', "Error set on second field's value $delta");
- $this->assertEqual(count($errors[$this->field_name_2][$langcode][$delta]), 1, "Only one error set on second field's value $delta");
- unset($errors[$this->field_name_2][$langcode][$delta]);
- }
- $this->assertEqual(count($errors[$this->field_name][$langcode]), 0, 'No extraneous errors set for first field');
- $this->assertEqual(count($errors[$this->field_name_2][$langcode]), 0, 'No extraneous errors set for second field');
-
-
- $options = array('field_name' => $this->field_name_2);
- try {
- field_attach_validate($entity_type, $entity, $options);
- }
- catch (FieldValidationException $e) {
- $errors = $e->errors;
- }
-
- foreach ($values_2 as $delta => $value) {
- $this->assertIdentical($errors[$this->field_name_2][$langcode][$delta][0]['error'], 'field_test_invalid', "Error set on second field's value $delta");
- $this->assertEqual(count($errors[$this->field_name_2][$langcode][$delta]), 1, "Only one error set on second field's value $delta");
- unset($errors[$this->field_name_2][$langcode][$delta]);
- }
- $this->assertFalse(isset($errors[$this->field_name]), 'No validation errors are set for the first field, despite it having errors');
- $this->assertEqual(count($errors[$this->field_name_2][$langcode]), 0, 'No extraneous errors set for second field');
-
-
- $entity->{$this->field_name_2}[$langcode] = $this->_generateTestFieldValues($this->field_2['cardinality'] + 1);
-
- try {
- field_attach_validate($entity_type, $entity);
- }
- catch (FieldValidationException $e) {
- $errors = $e->errors;
- }
- $this->assertEqual($errors[$this->field_name_2][$langcode][0][0]['error'], 'field_cardinality', 'Cardinality validation failed.');
-
- try {
- field_attach_validate($entity_type, $entity, $options);
- }
- catch (FieldValidationException $e) {
- $errors = $e->errors;
- }
- $this->assertEqual($errors[$this->field_name_2][$langcode][0][0]['error'], 'field_cardinality', 'Cardinality validation failed.');
- }
-
-
- * Test field_attach_form().
- *
- * This could be much more thorough, but it does verify that the correct
- * widgets show up.
- */
- function testFieldAttachForm() {
- $this->createFieldWithInstance('_2');
-
- $entity_type = 'test_entity';
- $entity = field_test_create_entity(0, 0, $this->instance['bundle']);
- $langcode = LANGUAGE_NONE;
-
-
- $form = array();
- $form_state = form_state_defaults();
- field_attach_form($entity_type, $entity, $form, $form_state);
-
- $this->assertEqual($form[$this->field_name][$langcode]['#title'], $this->instance['label'], "First field's form title is {$this->instance['label']}");
- $this->assertEqual($form[$this->field_name_2][$langcode]['#title'], $this->instance_2['label'], "Second field's form title is {$this->instance_2['label']}");
- for ($delta = 0; $delta < $this->field['cardinality']; $delta++) {
-
- $this->assertEqual($form[$this->field_name][$langcode][$delta]['value']['#type'], 'textfield', "First field's form delta $delta widget is textfield");
- }
- for ($delta = 0; $delta < $this->field_2['cardinality']; $delta++) {
-
- $this->assertEqual($form[$this->field_name_2][$langcode][$delta]['value']['#type'], 'textfield', "Second field's form delta $delta widget is textfield");
- }
-
-
- $options = array('field_name' => $this->field_name_2);
- $form = array();
- $form_state = form_state_defaults();
- field_attach_form($entity_type, $entity, $form, $form_state, NULL, $options);
-
- $this->assertFalse(isset($form[$this->field_name]), 'The first field does not exist in the form');
- $this->assertEqual($form[$this->field_name_2][$langcode]['#title'], $this->instance_2['label'], "Second field's form title is {$this->instance_2['label']}");
- for ($delta = 0; $delta < $this->field_2['cardinality']; $delta++) {
-
- $this->assertEqual($form[$this->field_name_2][$langcode][$delta]['value']['#type'], 'textfield', "Second field's form delta $delta widget is textfield");
- }
- }
-
-
- * Test field_attach_submit().
- */
- function testFieldAttachSubmit() {
- $this->createFieldWithInstance('_2');
-
- $entity_type = 'test_entity';
- $entity_init = field_test_create_entity(0, 0, $this->instance['bundle']);
- $langcode = LANGUAGE_NONE;
-
-
- $form = array();
- $form_state = form_state_defaults();
- field_attach_form($entity_type, $entity_init, $form, $form_state);
-
-
-
- $values = array();
- $weights = array();
- for ($delta = 0; $delta < $this->field['cardinality']; $delta++) {
- $values[$delta]['value'] = mt_rand(1, 127);
-
- do {
- $weight = mt_rand(0, $this->field['cardinality']);
- } while (in_array($weight, $weights));
- $weights[$delta] = $weight;
- $values[$delta]['_weight'] = $weight;
- }
-
- $values[1]['value'] = 0;
-
- $values_2 = array();
- $weights_2 = array();
- for ($delta = 0; $delta < $this->field_2['cardinality']; $delta++) {
- $values_2[$delta]['value'] = mt_rand(1, 127);
-
- do {
- $weight = mt_rand(0, $this->field_2['cardinality']);
- } while (in_array($weight, $weights_2));
- $weights_2[$delta] = $weight;
- $values_2[$delta]['_weight'] = $weight;
- }
-
- $values_2[1]['value'] = 0;
-
- backdrop_prepare_form('field_test_entity_form', $form, $form_state);
- backdrop_process_form('field_test_entity_form', $form, $form_state);
- $form_state['values'][$this->field_name][$langcode] = $values;
- $form_state['values'][$this->field_name_2][$langcode] = $values_2;
-
-
- $entity = clone($entity_init);
- field_attach_submit($entity_type, $entity, $form, $form_state);
-
- asort($weights);
- asort($weights_2);
- $expected_values = array();
- $expected_values_2 = array();
- foreach ($weights as $key => $value) {
- if ($key != 1) {
- $expected_values[] = array('value' => $values[$key]['value']);
- }
- }
- $this->assertIdentical($entity->{$this->field_name}[$langcode], $expected_values, 'Submit filters empty values');
- foreach ($weights_2 as $key => $value) {
- if ($key != 1) {
- $expected_values_2[] = array('value' => $values_2[$key]['value']);
- }
- }
- $this->assertIdentical($entity->{$this->field_name_2}[$langcode], $expected_values_2, 'Submit filters empty values');
-
-
- $options = array('field_name' => $this->field_name_2);
- $entity = clone($entity_init);
- field_attach_submit($entity_type, $entity, $form, $form_state, $options);
- $expected_values_2 = array();
- foreach ($weights_2 as $key => $value) {
- if ($key != 1) {
- $expected_values_2[] = array('value' => $values_2[$key]['value']);
- }
- }
- $this->assertFalse(isset($entity->{$this->field_name}), 'The first field does not exist in the entity object');
- $this->assertIdentical($entity->{$this->field_name_2}[$langcode], $expected_values_2, 'Submit filters empty values');
- }
- }
-
- class FieldInfoTestCase extends FieldTestCase {
- function setUp() {
- parent::setUp('field_test');
- }
-
-
- * Test that field types and field definitions are correcly cached.
- */
- function testFieldInfo() {
-
-
- $field_test_info = field_test_field_info();
-
- foreach (array_keys($field_test_info) as $name) {
- $field_test_info[$name]['instance_settings']['user_register_form'] = FALSE;
- }
- $info = field_info_field_types();
- foreach ($field_test_info as $t_key => $field_type) {
- foreach ($field_type as $key => $val) {
- $this->assertEqual($info[$t_key][$key], $val, format_string('Field type %t_key key %key is %value', array('%t_key' => $t_key, '%key' => $key, '%value' => print_r($val, TRUE))));
- }
- $this->assertEqual($info[$t_key]['module'], 'field_test', "Field type field_test module appears");
- }
-
- $formatter_info = field_test_field_formatter_info();
- $info = field_info_formatter_types();
- foreach ($formatter_info as $f_key => $formatter) {
- foreach ($formatter as $key => $val) {
- $this->assertEqual($info[$f_key][$key], $val, format_string('Formatter type %f_key key %key is %value', array('%f_key' => $f_key, '%key' => $key, '%value' => print_r($val, TRUE))));
- }
- $this->assertEqual($info[$f_key]['module'], 'field_test', "Formatter type field_test module appears");
- }
-
- $widget_info = field_test_field_widget_info();
- $info = field_info_widget_types();
- foreach ($widget_info as $w_key => $widget) {
- foreach ($widget as $key => $val) {
- $this->assertEqual($info[$w_key][$key], $val, format_string('Widget type %w_key key %key is %value', array('%w_key' => $w_key, '%key' => $key, '%value' => print_r($val, TRUE))));
- }
- $this->assertEqual($info[$w_key]['module'], 'field_test', "Widget type field_test module appears");
- }
-
- $storage_info = field_test_field_storage_info();
- $info = field_info_storage_types();
- foreach ($storage_info as $s_key => $storage) {
- foreach ($storage as $key => $val) {
- $this->assertEqual($info[$s_key][$key], $val, format_string('Storage type %s_key key %key is %value', array('%s_key' => $s_key, '%key' => $key, '%value' => print_r($val, TRUE))));
- }
- $this->assertEqual($info[$s_key]['module'], 'field_test', "Storage type field_test module appears");
- }
-
-
- $instances = field_info_instances('test_entity');
- $expected = array('test_bundle' => array());
- $this->assertIdentical($instances, $expected, format_string("field_info_instances('test_entity') returns %expected.", array('%expected' => var_export($expected, TRUE))));
- $instances = field_info_instances('test_entity', 'test_bundle');
- $this->assertIdentical($instances, array(), "field_info_instances('test_entity', 'test_bundle') returns an empty array.");
-
-
- $core_fields = field_info_fields();
- $field = array(
- 'field_name' => backdrop_strtolower($this->randomName()),
- 'type' => 'test_field',
- );
- field_create_field($field);
- $fields = field_info_fields();
- $this->assertEqual(count($fields), count($core_fields) + 1, 'One new field exists');
- $this->assertEqual($fields[$field['field_name']]['field_name'], $field['field_name'], 'info fields contains field name');
- $this->assertEqual($fields[$field['field_name']]['type'], $field['type'], 'info fields contains field type');
- $this->assertEqual($fields[$field['field_name']]['module'], 'field_test', 'info fields contains field module');
- $settings = array('test_field_setting' => 'dummy test string');
- foreach ($settings as $key => $val) {
- $this->assertEqual($fields[$field['field_name']]['settings'][$key], $val, format_string('Field setting %key has correct default value %value', array('%key' => $key, '%value' => $val)));
- }
- $this->assertEqual($fields[$field['field_name']]['cardinality'], 1, 'info fields contains cardinality 1');
- $this->assertEqual($fields[$field['field_name']]['active'], 1, 'info fields contains active 1');
-
-
- $instance = array(
- 'field_name' => $field['field_name'],
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'label' => $this->randomName(),
- 'description' => $this->randomName(),
- 'weight' => mt_rand(0, 127),
-
- 'widget' => array(
- 'type' => 'test_field_widget',
- 'settings' => array(
- 'test_setting' => 999)));
- field_create_instance($instance);
-
- $info = entity_get_info('test_entity');
- $instances = field_info_instances('test_entity', $instance['bundle']);
- $this->assertEqual(count($instances), 1, format_string('One instance shows up in info when attached to a bundle on a @label.', array(
- '@label' => $info['label']
- )));
- $this->assertTrue($instance < $instances[$instance['field_name']], 'Instance appears in info correctly');
-
-
-
-
- $instances = field_info_instances('test_entity', 'invalid_bundle');
- $this->assertIdentical($instances, array(), "field_info_instances('test_entity', 'invalid_bundle') returns an empty array.");
-
-
- $instances = field_info_instances('invalid_entity', $instance['bundle']);
- $this->assertIdentical($instances, array(), "field_info_instances('invalid_entity', 'test_bundle') returns an empty array.");
-
-
- $instances = field_info_instances('invalid_entity');
- $this->assertIdentical($instances, array(), "field_info_instances('invalid_entity') returns an empty array.");
-
-
- $instances = field_info_instances('user');
- $expected = array('user' => array());
- $this->assertIdentical($instances, $expected, format_string("field_info_instances('user') returns %expected.", array('%expected' => var_export($expected, TRUE))));
- $instances = field_info_instances('user', 'user');
- $this->assertIdentical($instances, array(), "field_info_instances('user', 'user') returns an empty array.");
- }
-
-
- * Test that cached field definitions are ready for current runtime context.
- */
- function testFieldPrepare() {
- $field_definition = array(
- 'field_name' => 'field',
- 'type' => 'test_field',
- );
- field_create_field($field_definition);
-
-
-
-
- $config = config('field.field.' . $field_definition['field_name']);
- $config->set('settings', array());
- $config->save();
-
- field_cache_clear();
-
-
- $field = field_info_field($field_definition['field_name']);
-
-
- $field_type = field_info_field_types($field_definition['type']);
- $this->assertIdentical($field['settings'], $field_type['settings'], 'All expected default field settings are present.');
- }
-
-
- * Test that cached instance definitions are ready for current runtime context.
- */
- function testInstancePrepare() {
- $field_definition = array(
- 'field_name' => 'field',
- 'type' => 'test_field',
- );
- field_create_field($field_definition);
- $instance_definition = array(
- 'field_name' => $field_definition['field_name'],
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- );
- field_create_instance($instance_definition);
-
-
-
-
- $config = config('field.instance.test_entity.' . $instance_definition['bundle'] . '.' . $instance_definition['field_name']);
- $instance = $config->get();
- $instance['settings'] = array();
- $instance['widget']['settings'] = 'unavailable_widget';
- $instance['widget']['settings'] = array();
- $instance['display']['default']['type'] = 'unavailable_formatter';
- $instance['display']['default']['settings'] = array();
- $config->setData($instance);
- $config->save();
-
- field_cache_clear();
-
-
- $instance = field_info_instance($instance_definition['entity_type'], $instance_definition['field_name'], $instance_definition['bundle']);
-
-
- $field_type = field_info_field_types($field_definition['type']);
- $this->assertIdentical($instance['settings'], $field_type['instance_settings'] , 'All expected instance settings are present.');
-
-
- $this->assertIdentical($instance['widget']['type'], $field_type['default_widget'], 'Unavailable widget replaced with default widget.');
- $widget_type = field_info_widget_types($instance['widget']['type']);
- $this->assertIdentical($instance['widget']['settings'], $widget_type['settings'] , 'All expected widget settings are present.');
-
-
- $display = $instance['display']['default'];
- $this->assertIdentical($display['type'], $field_type['default_formatter'], "Formatter is set for the 'default' display mode");
- $formatter_type = field_info_formatter_types($display['type']);
- $this->assertIdentical($display['settings'], $formatter_type['settings'] , "Formatter settings are set for the 'default' display mode");
- }
-
-
- * Test that instances on disabled entity types are filtered out.
- */
- function testInstanceDisabledEntityType() {
-
-
- $field_definition = array(
- 'field_name' => 'field',
- 'type' => 'test_field',
- );
- field_create_field($field_definition);
- $instance_definition = array(
- 'field_name' => 'field',
- 'entity_type' => 'comment',
- 'bundle' => 'comment_node_post',
- );
- field_create_instance($instance_definition);
-
-
- module_disable(array('comment'));
- $this->assertNull(field_info_instance('comment', 'field', 'comment_node_post'), 'No instances are returned on disabled entity types.');
- }
-
-
- * Test that the field_info settings convenience functions work.
- */
- function testSettingsInfo() {
- $info = field_test_field_info();
-
- foreach (array_keys($info) as $name) {
- $info[$name]['instance_settings']['user_register_form'] = FALSE;
- }
- foreach ($info as $type => $data) {
- $this->assertIdentical(field_info_field_settings($type), $data['settings'], format_string("field_info_field_settings returns %type's field settings", array('%type' => $type)));
- $this->assertIdentical(field_info_instance_settings($type), $data['instance_settings'], format_string("field_info_field_settings returns %type's field instance settings", array('%type' => $type)));
- }
-
- $info = field_test_field_widget_info();
- foreach ($info as $type => $data) {
- $this->assertIdentical(field_info_widget_settings($type), $data['settings'], format_string("field_info_widget_settings returns %type's widget settings", array('%type' => $type)));
- }
-
- $info = field_test_field_formatter_info();
- foreach ($info as $type => $data) {
- $this->assertIdentical(field_info_formatter_settings($type), $data['settings'], format_string("field_info_formatter_settings returns %type's formatter settings", array('%type' => $type)));
- }
- }
- }
-
- class FieldFormTestCase extends FieldTestCase {
- protected $field;
- protected $field_name;
- protected $instance;
- protected $field_single;
- protected $field_multiple;
- protected $field_unlimited;
-
- function setUp() {
- parent::setUp('field_test');
-
- $web_user = $this->backdropCreateUser(array('access field_test content', 'administer field_test content'));
- $this->backdropLogin($web_user);
-
- $this->field_single = array('field_name' => 'field_single', 'type' => 'test_field');
- $this->field_multiple = array('field_name' => 'field_multiple', 'type' => 'test_field', 'cardinality' => 4);
- $this->field_unlimited = array('field_name' => 'field_unlimited', 'type' => 'test_field', 'cardinality' => FIELD_CARDINALITY_UNLIMITED);
-
- $this->instance = array(
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'label' => $this->randomName() . '_label',
- 'description' => $this->randomName() . '_description',
- 'weight' => mt_rand(0, 127),
- 'settings' => array(
- 'test_instance_setting' => $this->randomName(),
- ),
- 'widget' => array(
- 'type' => 'test_field_widget',
- 'label' => 'Test Field',
- 'settings' => array(
- 'test_widget_setting' => $this->randomName(),
- )
- )
- );
- }
-
- function testFieldFormSingle() {
- $this->field = $this->field_single;
- $this->field_name = $this->field['field_name'];
- $this->instance['field_name'] = $this->field_name;
- field_create_field($this->field);
- field_create_instance($this->instance);
- $langcode = LANGUAGE_NONE;
-
-
- $this->backdropGet('test-entity/add/test-bundle');
- $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', 'Widget is displayed');
- $this->assertNoField("{$this->field_name}[$langcode][1][value]", 'No extraneous widget is displayed');
-
-
-
- $edit = array("{$this->field_name}[$langcode][0][value]" => -1);
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertRaw(t('%name does not accept the value -1.', array('%name' => $this->instance['label'])), 'Field validation fails with invalid input.');
-
-
-
- $value = mt_rand(1, 127);
- $edit = array("{$this->field_name}[$langcode][0][value]" => $value);
- $this->backdropPost(NULL, $edit, t('Save'));
- preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
- $id = $match[1];
- $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
- $entity = field_test_entity_test_load($id);
- $this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was saved');
-
-
- $this->backdropGet('test-entity/manage/' . $id . '/edit');
- $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", $value, 'Widget is displayed with the correct default value');
- $this->assertNoField("{$this->field_name}[$langcode][1][value]", 'No extraneous widget is displayed');
-
-
- $value = mt_rand(1, 127);
- $edit = array("{$this->field_name}[$langcode][0][value]" => $value);
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated');
- $entity = field_test_entity_test_load($id);
- $this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was updated');
-
-
- $value = '';
- $edit = array("{$this->field_name}[$langcode][0][value]" => $value);
- $this->backdropPost('test-entity/manage/' . $id . '/edit', $edit, t('Save'));
- $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated');
- $entity = field_test_entity_test_load($id);
- $this->assertIdentical($entity->{$this->field_name}, array(), 'Field was emptied');
-
- }
-
- function testFieldFormSingleRequired() {
- $this->field = $this->field_single;
- $this->field_name = $this->field['field_name'];
- $this->instance['field_name'] = $this->field_name;
- $this->instance['required'] = TRUE;
- field_create_field($this->field);
- field_create_instance($this->instance);
- $langcode = LANGUAGE_NONE;
-
-
- $edit = array();
- $this->backdropPost('test-entity/add/test-bundle', $edit, t('Save'));
- $this->assertRaw(t('!name field is required.', array('!name' => $this->instance['label'])), 'Required field with no value fails validation');
-
-
- $value = mt_rand(1, 127);
- $edit = array("{$this->field_name}[$langcode][0][value]" => $value);
- $this->backdropPost(NULL, $edit, t('Save'));
- preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
- $id = $match[1];
- $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
- $entity = field_test_entity_test_load($id);
- $this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was saved');
-
-
- $value = '';
- $edit = array("{$this->field_name}[$langcode][0][value]" => $value);
- $this->backdropPost('test-entity/manage/' . $id . '/edit', $edit, t('Save'));
- $this->assertRaw(t('!name field is required.', array('!name' => $this->instance['label'])), 'Required field with no value fails validation');
- }
-
-
- function testFieldFormUnlimited() {
- $this->field = $this->field_unlimited;
- $this->field_name = $this->field['field_name'];
- $this->instance['field_name'] = $this->field_name;
- field_create_field($this->field);
- field_create_instance($this->instance);
- $langcode = LANGUAGE_NONE;
-
-
- $this->backdropGet('test-entity/add/test-bundle');
- $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', 'Widget 1 is displayed');
- $this->assertNoField("{$this->field_name}[$langcode][1][value]", 'No extraneous widget is displayed');
-
-
- $this->backdropPost(NULL, array(), t('Add another item'));
- $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', 'Widget 1 is displayed');
- $this->assertFieldByName("{$this->field_name}[$langcode][1][value]", '', 'New widget is displayed');
- $this->assertNoField("{$this->field_name}[$langcode][2][value]", 'No extraneous widget is displayed');
-
-
-
- $this->backdropPost(NULL, array(), t('Add another item'));
-
-
- $count = 3;
- $delta_range = $count - 1;
- $values = $weights = $pattern = $expected_values = $edit = array();
- for ($delta = 0; $delta <= $delta_range; $delta++) {
-
- do {
- $value = mt_rand(1, 127);
- } while (in_array($value, $values));
- do {
- $weight = mt_rand(-$delta_range, $delta_range);
- } while (in_array($weight, $weights));
- $edit["$this->field_name[$langcode][$delta][value]"] = $value;
- $edit["$this->field_name[$langcode][$delta][_weight]"] = $weight;
-
- $values[$delta] = $value;
- $weights[$delta] = $weight;
- $field_values[$weight]['value'] = (string) $value;
- $pattern[$weight] = "<input [^>]*value=\"$value\" [^>]*";
- }
-
-
- $this->backdropPost(NULL, $edit, t('Add another item'));
- for ($delta = 0; $delta <= $delta_range; $delta++) {
- $this->assertFieldByName("$this->field_name[$langcode][$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value");
- $this->assertFieldByName("$this->field_name[$langcode][$delta][_weight]", $weights[$delta], "Widget $delta has the right weight");
- }
- ksort($pattern);
- $pattern = implode('.*', array_values($pattern));
- $this->assertPattern("|$pattern|s", 'Widgets are displayed in the correct order');
- $this->assertFieldByName("$this->field_name[$langcode][$delta][value]", '', "New widget is displayed");
- $this->assertFieldByName("$this->field_name[$langcode][$delta][_weight]", $delta, "New widget has the right weight");
- $this->assertNoField("$this->field_name[$langcode][" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');
-
-
- $this->backdropPost(NULL, $edit, t('Save'));
- preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
- $id = $match[1];
- $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
- $entity = field_test_entity_test_load($id);
- ksort($field_values);
- $field_values = array_values($field_values);
- $this->assertIdentical($entity->{$this->field_name}[$langcode], $field_values, 'Field values were saved in the correct order');
-
-
-
-
-
-
-
-
- }
-
-
- * Tests widget handling of multiple required radios.
- */
- function testFieldFormMultivalueWithRequiredRadio() {
-
- $this->field = $this->field_unlimited;
- $this->field_name = $this->field['field_name'];
- $this->instance['field_name'] = $this->field_name;
- field_create_field($this->field);
- field_create_instance($this->instance);
- $langcode = LANGUAGE_NONE;
-
-
- field_create_field(array(
- 'field_name' => 'required_radio_test',
- 'type' => 'list_text',
- 'settings' => array(
- 'allowed_values' => array('yes' => 'yes', 'no' => 'no'),
- ),
- ));
- field_create_instance(array(
- 'field_name' => 'required_radio_test',
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'required' => TRUE,
- 'widget' => array(
- 'type' => 'options_buttons',
- ),
- ));
-
-
- $this->backdropGet('test-entity/add/test-bundle');
-
-
- $this->backdropPost(NULL, array(), t('Add another item'));
-
-
- $this->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed.');
-
-
- $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', 'Widget 1 is displayed');
- $this->assertFieldByName("{$this->field_name}[$langcode][1][value]", '', 'New widget is displayed');
- $this->assertNoField("{$this->field_name}[$langcode][2][value]", 'No extraneous widget is displayed');
- }
-
- function testFieldFormJSAddMore() {
- $this->field = $this->field_unlimited;
- $this->field_name = $this->field['field_name'];
- $this->instance['field_name'] = $this->field_name;
- field_create_field($this->field);
- field_create_instance($this->instance);
- $langcode = LANGUAGE_NONE;
-
-
- $this->backdropGet('test-entity/add/test-bundle');
-
-
-
-
- $this->backdropPost(NULL, array(), t('Add another item'));
- $this->backdropPost(NULL, array(), t('Add another item'));
-
-
- $count = 3;
- $delta_range = $count - 1;
- $values = $weights = $pattern = $expected_values = $edit = array();
- for ($delta = 0; $delta <= $delta_range; $delta++) {
-
- do {
- $value = mt_rand(1, 127);
- } while (in_array($value, $values));
- do {
- $weight = mt_rand(-$delta_range, $delta_range);
- } while (in_array($weight, $weights));
- $edit["$this->field_name[$langcode][$delta][value]"] = $value;
- $edit["$this->field_name[$langcode][$delta][_weight]"] = $weight;
-
- $values[$delta] = $value;
- $weights[$delta] = $weight;
- $field_values[$weight]['value'] = (string) $value;
- $pattern[$weight] = "<input [^>]*value=\"$value\" [^>]*";
- }
-
-
- $commands = $this->backdropPostAJAX(NULL, $edit, $this->field_name . '_add_more');
- $this->content = $commands[1]['data'];
-
- for ($delta = 0; $delta <= $delta_range; $delta++) {
- $this->assertFieldByName("$this->field_name[$langcode][$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value");
- $this->assertFieldByName("$this->field_name[$langcode][$delta][_weight]", $weights[$delta], "Widget $delta has the right weight");
- }
- ksort($pattern);
- $pattern = implode('.*', array_values($pattern));
- $this->assertPattern("|$pattern|s", 'Widgets are displayed in the correct order');
- $this->assertFieldByName("$this->field_name[$langcode][$delta][value]", '', "New widget is displayed");
- $this->assertFieldByName("$this->field_name[$langcode][$delta][_weight]", $delta, "New widget has the right weight");
- $this->assertNoField("$this->field_name[$langcode][" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');
- }
-
-
- * Tests widgets handling multiple values.
- */
- function testFieldFormMultipleWidget() {
-
-
- $this->field = $this->field_multiple;
- $this->field_name = $this->field['field_name'];
- $this->instance['field_name'] = $this->field_name;
- $this->instance['widget']['type'] = 'test_field_widget_multiple';
- field_create_field($this->field);
- field_create_instance($this->instance);
- $langcode = LANGUAGE_NONE;
-
-
- $this->backdropGet('test-entity/add/test-bundle');
- $this->assertFieldByName("{$this->field_name}[$langcode]", '', 'Widget is displayed.');
-
-
- $edit = array("{$this->field_name}[$langcode]" => '1, 2, 3');
- $this->backdropPost(NULL, $edit, t('Save'));
- preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
- $id = $match[1];
-
-
- $entity_init = field_test_create_entity($id);
- $this->assertFieldValues($entity_init, $this->field_name, $langcode, array(1, 2, 3));
-
-
- $this->backdropGet('test-entity/manage/' . $id . '/edit');
- $this->assertFieldByName("{$this->field_name}[$langcode]", '1, 2, 3', 'Widget is displayed.');
-
-
- $edit = array("{$this->field_name}[$langcode]" => '1, 2, 3, 4, 5');
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertRaw('this field cannot hold more than 4 values', 'Form validation failed.');
-
- $this->assertFieldValues($entity_init, $this->field_name, $langcode, array(1, 2, 3));
- }
-
-
- * Tests fields with no 'edit' access.
- */
- function testFieldFormAccess() {
-
- $field = $this->field_single;
- $field_name = $field['field_name'];
- $instance = $this->instance;
- $instance['field_name'] = $field_name;
- field_create_field($field);
- field_create_instance($instance);
-
-
- $field_no_access = array(
- 'field_name' => 'field_no_edit_access',
- 'type' => 'test_field',
- );
- $field_name_no_access = $field_no_access['field_name'];
- $instance_no_access = array(
- 'field_name' => $field_name_no_access,
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'default_value' => array(0 => array('value' => 99)),
- );
- field_create_field($field_no_access);
- field_create_instance($instance_no_access);
-
- $langcode = LANGUAGE_NONE;
-
-
-
- $entity_type = 'test_entity';
- $entity = field_test_create_entity(0, 0, $this->instance['bundle']);
-
- $form = array();
- $form_state = form_state_defaults();
- field_attach_form($entity_type, $entity, $form, $form_state);
-
- $this->assertEqual($form[$field_name_no_access][$langcode][0]['value']['#entity_type'], $entity_type, 'The correct entity type is set in the field structure.');
- $this->assertFalse($form[$field_name_no_access]['#access'], 'Field #access is FALSE for the field without edit access.');
-
-
- $this->backdropGet('test-entity/add/test-bundle');
- $this->assertNoFieldByName("{$field_name_no_access}[$langcode][0][value]", '', 'Widget is not displayed if field access is denied.');
-
-
- $edit = array("{$field_name}[$langcode][0][value]" => 1);
- $this->backdropPost(NULL, $edit, t('Save'));
- preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
- $id = $match[1];
-
-
- $entity = field_test_entity_test_load($id);
- $this->assertEqual($entity->{$field_name_no_access}[$langcode][0]['value'], 99, 'Default value was saved for the field with no edit access.');
- $this->assertEqual($entity->{$field_name}[$langcode][0]['value'], 1, 'Entered value vas saved for the field with edit access.');
-
-
- $edit = array("{$field_name}[$langcode][0][value]" => 2, 'revision' => TRUE);
- $this->backdropPost('test-entity/manage/' . $id . '/edit', $edit, t('Save'));
-
-
- $entity = field_test_entity_test_load($id);
- $this->assertEqual($entity->{$field_name_no_access}[$langcode][0]['value'], 99, 'New revision has the expected value for the field with no edit access.');
- $this->assertEqual($entity->{$field_name}[$langcode][0]['value'], 2, 'New revision has the expected value for the field with edit access.');
-
-
- $entity = field_test_entity_test_load($id, $entity->ftvid);
- $this->assertEqual($entity->{$field_name_no_access}[$langcode][0]['value'], 99, 'New revision has the expected value for the field with no edit access.');
- $this->assertEqual($entity->{$field_name}[$langcode][0]['value'], 2, 'New revision has the expected value for the field with edit access.');
- }
-
-
- * Tests Field API form integration within a subform.
- */
- function testNestedFieldForm() {
-
- field_create_field($this->field_single);
- field_create_field($this->field_unlimited);
- $this->instance['field_name'] = 'field_single';
- $this->instance['label'] = 'Single field';
- field_create_instance($this->instance);
- $this->instance['field_name'] = 'field_unlimited';
- $this->instance['label'] = 'Unlimited field';
- field_create_instance($this->instance);
-
-
- $entity_1 = field_test_create_entity(1, 1);
- $entity_1->is_new = TRUE;
- $entity_1->field_single[LANGUAGE_NONE][] = array('value' => 0);
- $entity_1->field_unlimited[LANGUAGE_NONE][] = array('value' => 1);
- field_test_entity_save($entity_1);
-
- $entity_2 = field_test_create_entity(2, 2);
- $entity_2->is_new = TRUE;
- $entity_2->field_single[LANGUAGE_NONE][] = array('value' => 10);
- $entity_2->field_unlimited[LANGUAGE_NONE][] = array('value' => 11);
- field_test_entity_save($entity_2);
-
-
- $this->backdropGet('test-entity/nested/1/2');
- $this->assertFieldByName('field_single[und][0][value]', 0, 'Entity 1: field_single value appears correctly is the form.');
- $this->assertFieldByName('field_unlimited[und][0][value]', 1, 'Entity 1: field_unlimited value 0 appears correctly is the form.');
- $this->assertFieldByName('entity_2[field_single][und][0][value]', 10, 'Entity 2: field_single value appears correctly is the form.');
- $this->assertFieldByName('entity_2[field_unlimited][und][0][value]', 11, 'Entity 2: field_unlimited value 0 appears correctly is the form.');
-
-
- $edit = array(
- 'field_single[und][0][value]' => 1,
- 'field_unlimited[und][0][value]' => 2,
- 'field_unlimited[und][1][value]' => 3,
- 'entity_2[field_single][und][0][value]' => 11,
- 'entity_2[field_unlimited][und][0][value]' => 12,
- 'entity_2[field_unlimited][und][1][value]' => 13,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- field_cache_clear();
- $entity_1 = field_test_create_entity(1);
- $entity_2 = field_test_create_entity(2);
- $this->assertFieldValues($entity_1, 'field_single', LANGUAGE_NONE, array(1));
- $this->assertFieldValues($entity_1, 'field_unlimited', LANGUAGE_NONE, array(2, 3));
- $this->assertFieldValues($entity_2, 'field_single', LANGUAGE_NONE, array(11));
- $this->assertFieldValues($entity_2, 'field_unlimited', LANGUAGE_NONE, array(12, 13));
-
-
-
- $edit = array(
- 'field_unlimited[und][1][value]' => -1,
- );
- $this->backdropPost('test-entity/nested/1/2', $edit, t('Save'));
- $this->assertRaw(t('%label does not accept the value -1', array('%label' => 'Unlimited field')), 'Entity 1: the field validation error was reported.');
- $error_field = $this->xpath('//input[@id=:id and contains(@class, "error")]', array(':id' => 'edit-field-unlimited-und-1-value'));
- $this->assertTrue($error_field, 'Entity 1: the error was flagged on the correct element.');
- $edit = array(
- 'entity_2[field_unlimited][und][1][value]' => -1,
- );
- $this->backdropPost('test-entity/nested/1/2', $edit, t('Save'));
- $this->assertRaw(t('%label does not accept the value -1', array('%label' => 'Unlimited field')), 'Entity 2: the field validation error was reported.');
- $error_field = $this->xpath('//input[@id=:id and contains(@class, "error")]', array(':id' => 'edit-entity-2-field-unlimited-und-1-value'));
- $this->assertTrue($error_field, 'Entity 2: the error was flagged on the correct element.');
-
-
- $edit = array(
- 'field_unlimited[und][0][_weight]' => 0,
- 'field_unlimited[und][1][_weight]' => -1,
- 'entity_2[field_unlimited][und][0][_weight]' => 0,
- 'entity_2[field_unlimited][und][1][_weight]' => -1,
- );
- $this->backdropPost('test-entity/nested/1/2', $edit, t('Save'));
- field_cache_clear();
- $this->assertFieldValues($entity_1, 'field_unlimited', LANGUAGE_NONE, array(3, 2));
- $this->assertFieldValues($entity_2, 'field_unlimited', LANGUAGE_NONE, array(13, 12));
-
-
-
-
-
- $this->backdropGet('test-entity/nested/1/2');
- $this->backdropPostAJAX(NULL, array(), 'field_unlimited_add_more');
- $this->assertFieldByName('field_unlimited[und][0][value]', 3, 'Entity 1: field_unlimited value 0 appears correctly is the form.');
- $this->assertFieldByName('field_unlimited[und][1][value]', 2, 'Entity 1: field_unlimited value 1 appears correctly is the form.');
- $this->assertFieldByName('field_unlimited[und][2][value]', '', 'Entity 1: field_unlimited value 2 appears correctly is the form.');
- $this->assertFieldByName('field_unlimited[und][3][value]', '', 'Entity 1: an empty widget was added for field_unlimited value 3.');
-
- $edit = array(
- 'entity_2[field_unlimited][und][0][value]' => 13,
- 'entity_2[field_unlimited][und][1][value]' => 14,
- 'entity_2[field_unlimited][und][2][value]' => 15,
- );
- $this->backdropPostAJAX(NULL, $edit, 'entity_2_field_unlimited_add_more');
- $this->assertFieldByName('entity_2[field_unlimited][und][0][value]', 13, 'Entity 2: field_unlimited value 0 appears correctly is the form.');
- $this->assertFieldByName('entity_2[field_unlimited][und][1][value]', 14, 'Entity 2: field_unlimited value 1 appears correctly is the form.');
- $this->assertFieldByName('entity_2[field_unlimited][und][2][value]', 15, 'Entity 2: field_unlimited value 2 appears correctly is the form.');
- $this->assertFieldByName('entity_2[field_unlimited][und][3][value]', '', 'Entity 2: an empty widget was added for field_unlimited value 3.');
-
- $this->backdropPost(NULL, array(), t('Save'));
- field_cache_clear();
- $this->assertFieldValues($entity_1, 'field_unlimited', LANGUAGE_NONE, array(3, 2));
- $this->assertFieldValues($entity_2, 'field_unlimited', LANGUAGE_NONE, array(13, 14, 15));
- }
- }
-
- class FieldDisplayAPITestCase extends FieldTestCase {
- protected $field_name;
- protected $field;
- protected $instance;
-
- protected $cardinality;
- protected $label;
- protected $is_new;
- protected $values;
- protected $entity;
-
- function setUp() {
- parent::setUp('field_test');
-
-
- $this->field_name = 'test_field';
- $this->label = $this->randomName();
- $this->cardinality = 4;
-
- $this->field = array(
- 'field_name' => $this->field_name,
- 'type' => 'test_field',
- 'cardinality' => $this->cardinality,
- );
- $this->instance = array(
- 'field_name' => $this->field_name,
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'label' => $this->label,
- 'display' => array(
- 'default' => array(
- 'type' => 'field_test_default',
- 'settings' => array(
- 'test_formatter_setting' => $this->randomName(),
- ),
- ),
- 'teaser' => array(
- 'type' => 'field_test_default',
- 'settings' => array(
- 'test_formatter_setting' => $this->randomName(),
- ),
- ),
- ),
- );
- field_create_field($this->field);
- field_create_instance($this->instance);
-
-
- $this->values = $this->_generateTestFieldValues($this->cardinality);
- $this->entity = field_test_create_entity();
- $this->is_new = TRUE;
- $this->entity->{$this->field_name}[LANGUAGE_NONE] = $this->values;
- field_test_entity_save($this->entity);
- }
-
-
- * Test the field_view_field() function.
- */
- function testFieldViewField() {
-
- $output = field_view_field('test_entity', $this->entity, $this->field_name);
- $this->backdropSetContent(backdrop_render($output));
- $settings = field_info_formatter_settings('field_test_default');
- $setting = $settings['test_formatter_setting'];
- $this->assertNoText($this->label, 'Label was not displayed.');
- foreach ($this->values as $delta => $value) {
- $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
- }
-
-
- $display = array(
- 'label' => 'above',
- 'type' => 'field_test_multiple',
- 'settings' => array(
- 'test_formatter_setting_multiple' => $this->randomName(),
- 'alter' => TRUE,
- ),
- );
- $output = field_view_field('test_entity', $this->entity, $this->field_name, $display);
- $this->backdropSetContent(backdrop_render($output));
- $setting = $display['settings']['test_formatter_setting_multiple'];
- $this->assertText($this->label, 'Label was displayed.');
- $this->assertText('field_test_field_attach_view_alter', 'Alter fired, display passed.');
- $array = array();
- foreach ($this->values as $delta => $value) {
- $array[] = $delta . ':' . $value['value'];
- }
- $this->assertText($setting . '|' . implode('|', $array), 'Values were displayed with expected setting.');
-
-
- $display = array(
- 'label' => 'above',
- 'type' => 'field_test_with_prepare_view',
- 'settings' => array(
- 'test_formatter_setting_additional' => $this->randomName(),
- ),
- );
- $output = field_view_field('test_entity', $this->entity, $this->field_name, $display);
- $view = backdrop_render($output);
- $this->backdropSetContent($view);
- $setting = $display['settings']['test_formatter_setting_additional'];
- $this->assertText($this->label, 'Label was displayed.');
- $this->assertNoText('field_test_field_attach_view_alter', 'Alter not fired.');
- foreach ($this->values as $delta => $value) {
- $this->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
- }
-
-
-
- $output = field_view_field('test_entity', $this->entity, $this->field_name, 'teaser');
- $this->backdropSetContent(backdrop_render($output));
- $setting = $this->instance['display']['teaser']['settings']['test_formatter_setting'];
- $this->assertNoText($this->label, 'Label was not displayed.');
- foreach ($this->values as $delta => $value) {
- $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
- }
-
-
-
- $output = field_view_field('test_entity', $this->entity, $this->field_name, 'unknown_view_mode');
- $this->backdropSetContent(backdrop_render($output));
- $setting = $this->instance['display']['default']['settings']['test_formatter_setting'];
- $this->assertNoText($this->label, 'Label was not displayed.');
- foreach ($this->values as $delta => $value) {
- $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
- }
- }
-
-
- * Test the field_view_value() function.
- */
- function testFieldViewValue() {
-
- $settings = field_info_formatter_settings('field_test_default');
- $setting = $settings['test_formatter_setting'];
- foreach ($this->values as $delta => $value) {
- $item = $this->entity->{$this->field_name}[LANGUAGE_NONE][$delta];
- $output = field_view_value('test_entity', $this->entity, $this->field_name, $item);
- $this->backdropSetContent(backdrop_render($output));
- $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
- }
-
-
- $display = array(
- 'type' => 'field_test_multiple',
- 'settings' => array(
- 'test_formatter_setting_multiple' => $this->randomName(),
- ),
- );
- $setting = $display['settings']['test_formatter_setting_multiple'];
- foreach ($this->values as $delta => $value) {
- $item = $this->entity->{$this->field_name}[LANGUAGE_NONE][$delta];
- $output = field_view_value('test_entity', $this->entity, $this->field_name, $item, $display);
- $this->backdropSetContent(backdrop_render($output));
- $this->assertText($setting . '|0:' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
- }
-
-
- $display = array(
- 'type' => 'field_test_with_prepare_view',
- 'settings' => array(
- 'test_formatter_setting_additional' => $this->randomName(),
- ),
- );
- $setting = $display['settings']['test_formatter_setting_additional'];
- foreach ($this->values as $delta => $value) {
- $item = $this->entity->{$this->field_name}[LANGUAGE_NONE][$delta];
- $output = field_view_value('test_entity', $this->entity, $this->field_name, $item, $display);
- $this->backdropSetContent(backdrop_render($output));
- $this->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
- }
-
-
-
- $setting = $this->instance['display']['teaser']['settings']['test_formatter_setting'];
- foreach ($this->values as $delta => $value) {
- $item = $this->entity->{$this->field_name}[LANGUAGE_NONE][$delta];
- $output = field_view_value('test_entity', $this->entity, $this->field_name, $item, 'teaser');
- $this->backdropSetContent(backdrop_render($output));
- $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
- }
-
-
- $setting = $this->instance['display']['default']['settings']['test_formatter_setting'];
- foreach ($this->values as $delta => $value) {
- $item = $this->entity->{$this->field_name}[LANGUAGE_NONE][$delta];
- $output = field_view_value('test_entity', $this->entity, $this->field_name, $item, 'unknown_view_mode');
- $this->backdropSetContent(backdrop_render($output));
- $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
- }
- }
- }
-
- class FieldCrudTestCase extends FieldTestCase {
- protected $field_name;
- protected $field;
- protected $instance;
-
- function setUp() {
-
- parent::setUp('field_test', 'number');
- }
-
-
-
-
-
-
-
- * Test the creation of a field.
- */
- function testCreateField() {
- $field_definition = array(
- 'field_name' => 'field_2',
- 'type' => 'test_field',
- );
- field_test_memorize();
- $field_definition = field_create_field($field_definition);
- $mem = field_test_memorize();
- $this->assertIdentical($mem['field_test_field_create_field'][0][0], $field_definition, 'hook_field_create_field() called with correct arguments.');
-
-
- $config = config('field.field.' . $field_definition['field_name']);
- $config_data = $config->get();
-
-
- $this->assertEqual($config_data['field_name'], $field_definition['field_name'], 'The field name is properly saved.');
- $this->assertEqual($config_data['type'], $field_definition['type'], 'The field type is properly saved.');
-
-
- $this->assertEqual($config_data['cardinality'], 1, 'Cardinality defaults to 1.');
-
-
- $field_type = field_info_field_types($field_definition['type']);
- $this->assertIdentical($config_data['settings'], $field_type['settings'], 'Default field settings have been written.');
-
-
- $this->assertEqual($config_data['storage']['type'], 'field_sql_storage', 'The field type is properly saved.');
-
-
- try {
- field_create_field($field_definition);
- $this->fail(t('Cannot create two fields with the same name.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot create two fields with the same name.'));
- }
-
-
- try {
- $field_definition = array(
- 'field_name' => 'field_1',
- );
- field_create_field($field_definition);
- $this->fail(t('Cannot create a field with no type.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot create a field with no type.'));
- }
-
-
- try {
- $field_definition = array(
- 'type' => 'test_field'
- );
- field_create_field($field_definition);
- $this->fail(t('Cannot create an unnamed field.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot create an unnamed field.'));
- }
-
-
- try {
- $field_definition = array(
- 'field_name' => '2field_2',
- 'type' => 'test_field',
- );
- field_create_field($field_definition);
- $this->fail(t('Cannot create a field with a name starting with a digit.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot create a field with a name starting with a digit.'));
- }
-
-
- try {
- $field_definition = array(
- 'field_name' => 'field#_3',
- 'type' => 'test_field',
- );
- field_create_field($field_definition);
- $this->fail(t('Cannot create a field with a name containing an illegal character.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot create a field with a name containing an illegal character.'));
- }
-
-
- try {
- $field_definition = array(
- 'field_name' => '_12345678901234567890123456789012',
- 'type' => 'test_field',
- );
- field_create_field($field_definition);
- $this->fail(t('Cannot create a field with a name longer than 32 characters.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot create a field with a name longer than 32 characters.'));
- }
-
-
-
- try {
- $field_definition = array(
- 'type' => 'test_field',
- 'field_name' => 'ftvid',
- );
- field_create_field($field_definition);
- $this->fail(t('Cannot create a field bearing the name of an entity key.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot create a field bearing the name of an entity key.'));
- }
- }
-
-
- * Test failure to create a field.
- */
- function testCreateFieldFail() {
- $field_name = 'duplicate';
- $field_definition = array('field_name' => $field_name, 'type' => 'test_field', 'storage' => array('type' => 'field_test_storage_failure'));
-
-
- $config = config('field.field.' . $field_name);
- $this->assertTrue($config->isNew(), 'A field config file for the field does not exist.');
-
-
- try {
- field_create_field($field_definition);
- $this->assertTrue(FALSE, 'Field creation (correctly) fails.');
- }
- catch (Exception $e) {
- $this->assertTrue(TRUE, 'Field creation (correctly) fails.');
- }
-
-
- $config = config('field.field.' . $field_name);
- $this->assertTrue($config->isNew(), 'A field config file for the field does not exist.');
- }
-
-
- * Test reading back a field definition.
- */
- function testReadField() {
- $field_definition = array(
- 'field_name' => 'field_1',
- 'type' => 'test_field',
- );
- field_create_field($field_definition);
-
-
- $field = field_read_field($field_definition['field_name']);
- $this->assertTrue($field_definition < $field, 'The field was properly read.');
- }
-
-
- * Test the deletion of a field.
- */
- function testDeleteField() {
-
-
-
- $this->field = array('field_name' => 'field_1', 'type' => 'test_field');
- field_create_field($this->field);
- $another_field = array('field_name' => 'field_2', 'type' => 'test_field');
- field_create_field($another_field);
-
-
- $instance_definition = array(
- 'field_name' => $this->field['field_name'],
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'widget' => array(
- 'type' => 'test_field_widget',
- ),
- );
- field_create_instance($instance_definition);
- $another_instance_definition = $instance_definition;
- $another_instance_definition['field_name'] = $another_field['field_name'];
- field_create_instance($another_instance_definition);
-
-
- $field = field_read_field($this->field['field_name'], array('include_deleted' => TRUE));
- $this->assertTrue(!empty($field) && empty($field['deleted']), 'A new field is not marked for deletion.');
- field_delete_field($this->field['field_name']);
-
-
- $field = field_read_field($this->field['field_name'], array('include_deleted' => TRUE));
- $this->assertFalse($field, 'A deleted field is completely purged when it has no data.');
-
-
- $instance = field_read_instance('test_entity', $instance_definition['field_name'], $instance_definition['bundle'], array('include_deleted' => TRUE));
- $this->assertFalse($instance, 'An instance for a deleted field is completely purged when it has no data.');
-
-
- field_create_field($this->field);
- field_create_instance($instance_definition);
-
-
- $entity = field_test_create_entity(1, 1, $instance_definition['bundle']);
- $entity->{$this->field['field_name']}[LANGUAGE_NONE][0]['value'] = 1;
- field_attach_insert('test_entity', $entity);
-
-
- field_delete_field($this->field['field_name']);
-
-
-
- $field = field_read_field($this->field['field_name'], array('include_deleted' => TRUE));
- $this->assertTrue(!empty($field['deleted']), 'A deleted field is marked for deletion.');
-
-
-
- $instance = field_read_instance('test_entity', $instance_definition['field_name'], $instance_definition['bundle'], array('include_deleted' => TRUE));
- $this->assertTrue(!empty($instance['deleted']), 'An instance for a deleted field is marked for deletion.');
-
-
- $field = field_read_field($this->field['field_name']);
- $this->assertTrue(empty($field), 'A deleted field is not loaded by default.');
-
-
- $instance = field_read_instance('test_entity', $instance_definition['field_name'], $instance_definition['bundle']);
- $this->assertTrue(empty($instance), 'An instance for a deleted field is not loaded by default.');
-
-
- $another_field = field_read_field($another_field['field_name']);
- $this->assertTrue(!empty($another_field) && empty($another_field['deleted']), 'A non-deleted field is not marked for deletion.');
- $another_instance = field_read_instance('test_entity', $another_instance_definition['field_name'], $another_instance_definition['bundle']);
- $this->assertTrue(!empty($another_instance) && empty($another_instance['deleted']), 'An instance of a non-deleted field is not marked for deletion.');
-
-
-
- field_purge_batch(1);
-
- $field = field_read_field($this->field['field_name'], array('include_deleted' => TRUE));
- $this->assertFalse($field, 'Field is deleted after a batch run.');
- }
-
- function testUpdateNonExistentField() {
- $test_field = array('field_name' => 'does_not_exist', 'type' => 'number_decimal');
- try {
- field_update_field($test_field);
- $this->fail(t('Cannot update a field that does not exist.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot update a field that does not exist.'));
- }
- }
-
- function testUpdateFieldType() {
- $field = array('field_name' => 'field_type', 'type' => 'number_decimal');
- field_create_field($field);
-
- $test_field = array('field_name' => 'field_type', 'type' => 'number_integer');
- try {
- field_update_field($test_field);
- $this->fail(t('Cannot update a field to a different type.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot update a field to a different type.'));
- }
- }
-
-
- * Test updating a field.
- */
- function testUpdateField() {
-
-
-
- $cardinality = 4;
- $field_definition = array(
- 'field_name' => 'field_update',
- 'type' => 'test_field',
- 'cardinality' => $cardinality,
- );
- $field_definition = field_create_field($field_definition);
- $instance = array(
- 'field_name' => 'field_update',
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- );
- $instance = field_create_instance($instance);
-
- do {
-
- $id = $cardinality;
- $entity = field_test_create_entity($id, $id, $instance['bundle']);
-
- for ($i = 0; $i < 20; $i++) {
- $entity->field_update[LANGUAGE_NONE][$i]['value'] = $i;
- }
-
- field_attach_insert('test_entity', $entity);
-
- $entity = field_test_create_entity($id, $id, $instance['bundle']);
- field_attach_load('test_entity', array($id => $entity));
- $this->assertEqual(count($entity->field_update[LANGUAGE_NONE]), $field_definition['cardinality'], 'Cardinality is kept');
-
- for ($delta = 0; $delta < $cardinality; $delta++) {
- $this->assertEqual($entity->field_update[LANGUAGE_NONE][$delta]['value'], $delta, 'Value is kept');
- }
-
- $field_definition['cardinality'] = ++$cardinality;
- field_update_field($field_definition);
- } while ($cardinality < 6);
- }
-
-
- * Test field type modules forbidding an update.
- */
- function testUpdateFieldForbid() {
- $field = array('field_name' => 'forbidden', 'type' => 'test_field', 'settings' => array('changeable' => 0, 'unchangeable' => 0));
- $field = field_create_field($field);
- $field['settings']['changeable']++;
- try {
- field_update_field($field);
- $this->pass(t("A changeable setting can be updated."));
- }
- catch (FieldException $e) {
- $this->fail(t("An unchangeable setting cannot be updated."));
- }
- $field['settings']['unchangeable']++;
- try {
- field_update_field($field);
- $this->fail(t("An unchangeable setting can be updated."));
- }
- catch (FieldException $e) {
- $this->pass(t("An unchangeable setting cannot be updated."));
- }
- }
-
-
- * Test that fields are properly marked active or inactive.
- */
- function testActive() {
- $field_definition = array(
- 'field_name' => 'field_1',
- 'type' => 'test_field',
-
-
- 'storage' => array(
- 'type' => 'field_sql_storage',
- ),
- );
- field_create_field($field_definition);
-
-
-
-
-
- $this->_testActiveHelper($field_definition, array('field_test'));
- $this->_testActiveHelper($field_definition, array('field_sql_storage'));
- $this->_testActiveHelper($field_definition, array('field_test', 'field_sql_storage'));
- }
-
-
- * Helper function for testActive().
- *
- * Test dependency between a field and a set of modules.
- *
- * @param $field_definition
- * A field definition.
- * @param $modules
- * An aray of module names. The field will be tested to be inactive as long
- * as any of those modules is disabled.
- */
- function _testActiveHelper($field_definition, $modules) {
- $field_name = $field_definition['field_name'];
-
-
- $field = field_read_field($field_name);
- $this->assertTrue($field_definition <= $field, 'The field was properly read.');
-
- module_disable($modules, FALSE);
-
- $fields = field_read_fields(array('field_name' => $field_name), array('include_inactive' => TRUE));
- $this->assertTrue(isset($fields[$field_name]) && $field_definition < $field, 'The field is properly read when explicitly fetching inactive fields.');
-
-
-
- while ($modules) {
- $field = field_read_field($field_name);
- $this->assertTrue(empty($field), format_string('%modules disabled. The field is marked inactive.', array('%modules' => implode(', ', $modules))));
-
- $module = array_shift($modules);
- module_enable(array($module), FALSE);
- }
-
-
-
- $field = field_read_field($field_name);
- $this->assertTrue($field_definition <= $field, 'The field was was marked active.');
- }
- }
-
- class FieldInstanceCrudTestCase extends FieldTestCase {
- protected $field;
- protected $instance_definition;
-
- function setUp() {
- parent::setUp('field_test');
-
- $this->field = array(
- 'field_name' => backdrop_strtolower($this->randomName()),
- 'type' => 'test_field',
- );
- field_create_field($this->field);
- $this->instance_definition = array(
- 'field_name' => $this->field['field_name'],
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- );
- }
-
-
-
-
-
-
-
-
- * Test the creation of a field instance.
- */
- function testCreateFieldInstance() {
- field_create_instance($this->instance_definition);
-
-
- $field_name = $this->instance_definition['field_name'];
- $bundle = $this->instance_definition['bundle'];
- $entity_type = $this->instance_definition['entity_type'];
- $config_data = config('field.instance.' . $entity_type . '.' . $bundle . '.' . $field_name)->get();
-
- $field_type = field_info_field_types($this->field['type']);
- $widget_type = field_info_widget_types($field_type['default_widget']);
- $formatter_type = field_info_formatter_types($field_type['default_formatter']);
-
-
- $this->assertIdentical($config_data['required'], 0, 'Required defaults to false.');
- $this->assertIdentical($config_data['label'], $this->instance_definition['field_name'], 'Label defaults to field name.');
- $this->assertIdentical($config_data['description'], '', 'Description defaults to empty string.');
- $this->assertIdentical($config_data['widget']['type'], $field_type['default_widget'], 'Default widget has been written.');
- $this->assertTrue(isset($config_data['display']['default']), 'Display for "full" view_mode has been written.');
- $this->assertIdentical($config_data['display']['default']['type'], $field_type['default_formatter'], 'Default formatter for "full" view_mode has been written.');
-
-
- $this->assertIdentical($config_data['settings'], $field_type['instance_settings'] , 'Default instance settings have been written.');
- $this->assertIdentical($config_data['widget']['settings'], $widget_type['settings'] , 'Default widget settings have been written.');
- $this->assertIdentical($config_data['display']['default']['settings'], $formatter_type['settings'], 'Default formatter settings for "full" view_mode have been written.');
-
-
- try {
- field_create_instance($this->instance_definition);
- $this->fail(t('Cannot create two instances with the same field / bundle combination.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot create two instances with the same field / bundle combination.'));
- }
-
-
- try {
- $this->instance_definition['field_name'] = $this->randomName();
- field_create_instance($this->instance_definition);
- $this->fail(t('Cannot create an instance of a non-existing field.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot create an instance of a non-existing field.'));
- }
-
-
- $field_restricted = array(
- 'field_name' => backdrop_strtolower($this->randomName()),
- 'type' => 'test_field',
- 'entity_types' => array('test_cacheable_entity'),
- );
- field_create_field($field_restricted);
-
-
-
- try {
- $instance = $this->instance_definition;
- $instance['field_name'] = $field_restricted['field_name'];
- $instance['entity_type'] = 'test_cacheable_entity';
- field_create_instance($instance);
- $this->pass(t('Can create an instance on an entity type allowed by the field.'));
- }
- catch (FieldException $e) {
- $this->fail(t('Can create an instance on an entity type allowed by the field.'));
- }
-
-
-
- try {
- $instance = $this->instance_definition;
- $instance['field_name'] = $field_restricted['field_name'];
- field_create_instance($instance);
- $this->fail(t('Cannot create an instance on an entity type forbidden by the field.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot create an instance on an entity type forbidden by the field.'));
- }
-
-
- }
-
-
- * Test reading back an instance definition.
- */
- function testReadFieldInstance() {
- field_create_instance($this->instance_definition);
-
-
- $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
- $this->assertTrue($this->instance_definition < $instance, 'The field was properly read.');
- }
-
-
- * Test the update of a field instance.
- */
- function testUpdateFieldInstance() {
- field_create_instance($this->instance_definition);
- $field_type = field_info_field_types($this->field['type']);
-
-
- $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
- $instance['required'] = !$instance['required'];
- $instance['label'] = $this->randomName();
- $instance['description'] = $this->randomName();
- $instance['settings']['test_instance_setting'] = $this->randomName();
- $instance['widget']['settings']['test_widget_setting'] =$this->randomName();
- $instance['widget']['weight']++;
- $instance['display']['default']['settings']['test_formatter_setting'] = $this->randomName();
- $instance['display']['default']['weight']++;
- field_update_instance($instance);
-
- $instance_new = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
- $this->assertEqual($instance['required'], $instance_new['required'], '"required" change is saved');
- $this->assertEqual($instance['label'], $instance_new['label'], '"label" change is saved');
- $this->assertEqual($instance['description'], $instance_new['description'], '"description" change is saved');
- $this->assertEqual($instance['widget']['settings']['test_widget_setting'], $instance_new['widget']['settings']['test_widget_setting'], 'Widget setting change is saved');
- $this->assertEqual($instance['widget']['weight'], $instance_new['widget']['weight'], 'Widget weight change is saved');
- $this->assertEqual($instance['display']['default']['settings']['test_formatter_setting'], $instance_new['display']['default']['settings']['test_formatter_setting'], 'Formatter setting change is saved');
- $this->assertEqual($instance['display']['default']['weight'], $instance_new['display']['default']['weight'], 'Widget weight change is saved');
-
-
- $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
- $instance['widget']['type'] = 'test_field_widget_multiple';
- $instance['display']['default']['type'] = 'field_test_multiple';
- field_update_instance($instance);
-
- $instance_new = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
- $this->assertEqual($instance['widget']['type'], $instance_new['widget']['type'] , 'Widget type change is saved.');
- $settings = field_info_widget_settings($instance_new['widget']['type']);
- $this->assertIdentical($settings, array_intersect_key($instance_new['widget']['settings'], $settings) , 'Widget type change updates default settings.');
- $this->assertEqual($instance['display']['default']['type'], $instance_new['display']['default']['type'] , 'Formatter type change is saved.');
- $info = field_info_formatter_types($instance_new['display']['default']['type']);
- $settings = $info['settings'];
- $this->assertIdentical($settings, array_intersect_key($instance_new['display']['default']['settings'], $settings) , 'Changing formatter type updates default settings.');
-
-
- $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
- $instance['display']['teaser'] = array();
- field_update_instance($instance);
-
- $instance_new = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
- $this->assertTrue(isset($instance_new['display']['teaser']), 'Display for the new view_mode has been written.');
- $this->assertIdentical($instance_new['display']['teaser']['type'], $field_type['default_formatter'], 'Default formatter for the new view_mode has been written.');
- $info = field_info_formatter_types($instance_new['display']['teaser']['type']);
- $settings = $info['settings'];
- $this->assertIdentical($settings, $instance_new['display']['teaser']['settings'] , 'Default formatter settings for the new view_mode have been written.');
-
-
- }
-
-
- * Test the deletion of a field instance.
- */
- function testDeleteFieldInstance() {
-
-
-
-
-
-
- field_create_instance($this->instance_definition);
- $another_instance_definition = $this->instance_definition;
- $another_instance_definition['bundle'] .= '_another_bundle';
- field_create_instance($another_instance_definition);
-
-
- $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle'], array('include_deleted' => TRUE));
- $this->assertTrue(!empty($instance) && empty($instance['deleted']), 'A new field instance is not marked for deletion.');
- field_delete_instance($instance);
-
-
-
- $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle'], array('include_deleted' => TRUE));
- $this->assertTrue(!empty($instance['deleted']), 'A deleted field instance is marked for deletion.');
-
-
- $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
- $this->assertTrue(empty($instance), 'A deleted field instance is not loaded by default.');
-
-
- $another_instance = field_read_instance('test_entity', $another_instance_definition['field_name'], $another_instance_definition['bundle']);
- $this->assertTrue(!empty($another_instance) && empty($another_instance['deleted']), 'A non-deleted field instance is not marked for deletion.');
-
-
- field_delete_instance($another_instance);
- $field = field_read_field($another_instance['field_name'], array('include_deleted' => TRUE));
- $this->assertFalse($field, 'A deleted field is deleted its instances have been deleted.');
- }
- }
-
- * Unit test class for the multilanguage fields logic.
- *
- * The following tests will check the multilanguage logic of _field_invoke() and
- * that only the correct values are returned by field_available_languages().
- */
- class FieldTranslationsTestCase extends FieldTestCase {
- protected $field_name;
- protected $field;
- protected $instance;
- protected $entity_type;
-
- function setUp() {
- parent::setUp('locale', 'field_test');
-
- $this->field_name = backdrop_strtolower($this->randomName() . '_field_name');
-
- $this->entity_type = 'test_entity';
-
- $field = array(
- 'field_name' => $this->field_name,
- 'type' => 'test_field',
- 'cardinality' => 4,
- 'translatable' => TRUE,
- );
- field_create_field($field);
- $this->field = field_read_field($this->field_name);
-
- $instance = array(
- 'field_name' => $this->field_name,
- 'entity_type' => $this->entity_type,
- 'bundle' => 'test_bundle',
- );
- field_create_instance($instance);
- $this->instance = field_read_instance('test_entity', $this->field_name, 'test_bundle');
-
- require_once BACKDROP_ROOT . '/core/includes/locale.inc';
- for ($i = 0; $i < 3; ++$i) {
- $language = (object) array(
- 'langcode' => 'l' . $i,
- 'name' => $this->randomString(),
- 'native' => $this->randomString(),
- );
- language_save($language);
- }
- }
-
-
- * Ensures that only valid values are returned by field_available_languages().
- */
- function testFieldAvailableLanguages() {
-
- field_test_entity_info_translatable('test_entity', FALSE);
- $field = $this->field;
- $field['field_name'] .= '_untranslatable';
-
-
- field_test_entity_info_translatable('test_entity', TRUE);
-
-
- state_set('field_test_field_available_languages_alter', TRUE);
- $enabled_languages = field_content_languages();
- $available_languages = field_available_languages($this->entity_type, $this->field);
- foreach ($available_languages as $delta => $langcode) {
- if ($langcode != 'xx' && $langcode != 'en') {
- $this->assertTrue(in_array($langcode, $enabled_languages), format_string('%language is an enabled language.', array('%language' => $langcode)));
- }
- }
- $this->assertTrue(in_array('xx', $available_languages), format_string('%language was made available.', array('%language' => 'xx')));
- $this->assertFalse(in_array('en', $available_languages), format_string('%language was made unavailable.', array('%language' => 'en')));
-
-
- $this->field['translatable'] = FALSE;
- field_update_field($this->field);
- $available_languages = field_available_languages($this->entity_type, $this->field);
- $this->assertTrue(count($available_languages) == 1 && $available_languages[0] === LANGUAGE_NONE, 'For untranslatable fields only LANGUAGE_NONE is available.');
- }
-
-
- * Test the multilanguage logic of _field_invoke().
- */
- function testFieldInvoke() {
-
- field_test_entity_info_translatable('test_entity', TRUE);
-
- $entity_type = 'test_entity';
- $entity = field_test_create_entity(0, 0, $this->instance['bundle']);
-
-
-
- $values = array();
- $extra_languages = mt_rand(1, 4);
- $languages = $available_languages = field_available_languages($this->entity_type, $this->field);
- for ($i = 0; $i < $extra_languages; ++$i) {
- $languages[] = $this->randomName(2);
- }
-
-
- foreach ($languages as $langcode) {
- for ($delta = 0; $delta < $this->field['cardinality']; $delta++) {
- $values[$langcode][$delta]['value'] = mt_rand(1, 127);
- }
- }
- $entity->{$this->field_name} = $values;
-
- $results = _field_invoke('test_op', $entity_type, $entity);
- foreach ($results as $langcode => $result) {
- $hash = hash('sha256', serialize(array($entity_type, $entity, $this->field_name, $langcode, $values[$langcode])));
-
-
- $this->assertEqual($hash, $result, format_string('The result for %language is correctly stored.', array('%language' => $langcode)));
- }
-
- $this->assertEqual(count($results), count($available_languages), 'No unavailable language has been processed.');
- }
-
-
- * Test the multilanguage logic of _field_invoke_multiple().
- */
- function testFieldInvokeMultiple() {
-
- field_test_entity_info_translatable('test_entity', TRUE);
-
- $values = array();
- $options = array();
- $entities = array();
- $entity_type = 'test_entity';
- $entity_count = 5;
- $available_languages = field_available_languages($this->entity_type, $this->field);
- $display_language = NULL;
-
- for ($id = 1; $id <= $entity_count; ++$id) {
- $entity = field_test_create_entity($id, $id, $this->instance['bundle']);
- $languages = $available_languages;
-
-
-
- $extra_languages = mt_rand(1, 4);
- for ($i = 0; $i < $extra_languages; ++$i) {
- $languages[] = $this->randomName(2);
- }
-
-
- $language_count = count($languages);
- for ($i = 0; $i < $language_count; ++$i) {
- $langcode = $languages[$i];
-
-
-
- if ($i !== $id) {
- for ($delta = 0; $delta < $this->field['cardinality']; $delta++) {
- $values[$id][$langcode][$delta]['value'] = mt_rand(1, 127);
- }
- }
-
-
- elseif (!isset($display_language)) {
- $display_language = $langcode;
- }
- }
-
- $entity->{$this->field_name} = $values[$id];
- $entities[$id] = $entity;
-
-
- $options['language'][$id] = field_language($entity_type, $entity, NULL, $display_language);
- }
-
- $grouped_results = _field_invoke_multiple('test_op_multiple', $entity_type, $entities);
- foreach ($grouped_results as $id => $results) {
- foreach ($results as $langcode => $result) {
- if (isset($values[$id][$langcode])) {
- $hash = hash('sha256', serialize(array($entity_type, $entities[$id], $this->field_name, $langcode, $values[$id][$langcode])));
-
-
- $this->assertEqual($hash, $result, format_string('The result for entity %id/%language is correctly stored.', array('%id' => $id, '%language' => $langcode)));
- }
- }
- $this->assertEqual(count($results), count($available_languages), format_string('No unavailable language has been processed for entity %id.', array('%id' => $id)));
- }
-
- $null = NULL;
- $grouped_results = _field_invoke_multiple('test_op_multiple', $entity_type, $entities, $null, $null, $options);
- foreach ($grouped_results as $id => $results) {
- foreach ($results as $langcode => $result) {
- $this->assertTrue(isset($options['language'][$id]), format_string('The result language %language for entity %id was correctly suggested (display language: %display_language).', array('%id' => $id, '%language' => $langcode, '%display_language' => $display_language)));
- }
- }
- }
-
-
- * Test translatable fields storage/retrieval.
- */
- function testTranslatableFieldSaveLoad() {
-
- field_test_entity_info_translatable('node', TRUE);
- $entity_info = entity_get_info('node');
- $this->assertTrue(count($entity_info['translation']), 'Nodes are translatable.');
-
-
- field_test_entity_info_translatable('test_entity', TRUE);
- $eid = $evid = 1;
- $entity_type = 'test_entity';
- $entity = field_test_create_entity($eid, $evid, $this->instance['bundle']);
- $field_translations = array();
- $available_languages = field_available_languages($entity_type, $this->field);
- $this->assertTrue(count($available_languages) > 1, 'Field is translatable.');
- foreach ($available_languages as $langcode) {
- $field_translations[$langcode] = $this->_generateTestFieldValues($this->field['cardinality']);
- }
-
-
- $entity->{$this->field_name} = $field_translations;
- field_attach_insert($entity_type, $entity);
- unset($entity->{$this->field_name});
- field_attach_load($entity_type, array($eid => $entity));
-
-
- foreach ($field_translations as $langcode => $items) {
- $result = TRUE;
- foreach ($items as $delta => $item) {
- $result = $result && $item['value'] == $entity->{$this->field_name}[$langcode][$delta]['value'];
- }
- $this->assertTrue($result, format_string('%language translation correctly handled.', array('%language' => $langcode)));
- }
- }
-
-
- * Tests display language logic for translatable fields.
- */
- function testFieldDisplayLanguage() {
- $field_name = backdrop_strtolower($this->randomName() . '_field_name');
- $entity_type = 'test_entity';
-
-
-
- $field = array(
- 'field_name' => $field_name,
- 'type' => 'test_field',
- 'cardinality' => 2,
- 'translatable' => TRUE,
- );
- field_create_field($field);
-
- $instance = array(
- 'field_name' => $field['field_name'],
- 'entity_type' => $entity_type,
- 'bundle' => 'test_bundle',
- );
- field_create_instance($instance);
-
- $entity = field_test_create_entity(1, 1, $this->instance['bundle']);
- $instances = field_info_instances($entity_type, $this->instance['bundle']);
-
- $enabled_languages = field_content_languages();
- $languages = array();
-
-
-
- foreach ($instances as $instance) {
- $field_name = $instance['field_name'];
- $field = field_info_field($field_name);
- do {
-
-
- $langcode = $enabled_languages[mt_rand(1, count($enabled_languages) - 1)];
- }
- while (isset($languages[$langcode]));
- $languages[$langcode] = TRUE;
- $entity->{$field_name}[$langcode] = $this->_generateTestFieldValues($field['cardinality']);
- }
-
-
- field_test_entity_info_translatable($entity_type, FALSE);
- backdrop_static_reset('field_language');
- $requested_language = $enabled_languages[0];
- $display_language = field_language($entity_type, $entity, NULL, $requested_language);
- foreach ($instances as $instance) {
- $field_name = $instance['field_name'];
- $this->assertTrue($display_language[$field_name] == LANGUAGE_NONE, format_string('The display language for field %field_name is %language.', array('%field_name' => $field_name, '%language' => LANGUAGE_NONE)));
- }
-
-
- field_test_entity_info_translatable($entity_type, TRUE);
- backdrop_static_reset('field_language');
- $display_language = field_language($entity_type, $entity, NULL, $requested_language);
-
- foreach ($instances as $instance) {
- $field_name = $instance['field_name'];
- $langcode = $display_language[$field_name];
-
-
-
- $this->assertTrue(isset($entity->{$field_name}[$langcode]) && $langcode != $requested_language, format_string('The display language for the field %field_name is %language.', array('%field_name' => $field_name, '%language' => $langcode)));
- }
-
-
- backdrop_static_reset('field_language');
- $langcode = field_language($entity_type, $entity, $this->field_name, $requested_language);
- $this->assertTrue(isset($entity->{$this->field_name}[$langcode]) && $langcode != $requested_language, format_string('The display language for the (single) field %field_name is %language.', array('%field_name' => $field_name, '%language' => $langcode)));
-
-
- $entity->{$this->field_name}[$requested_language] = mt_rand(1, 127);
- backdrop_static_reset('field_language');
- $display_language = field_language($entity_type, $entity, $this->field_name, $requested_language);
- $this->assertEqual($display_language, $requested_language, 'Display language behave correctly when language fallback is disabled');
- }
-
-
- * Tests field translations when creating a new revision.
- */
- function testFieldFormTranslationRevisions() {
- $web_user = $this->backdropCreateUser(array('access field_test content', 'administer field_test content'));
- $this->backdropLogin($web_user);
-
-
- field_test_entity_info_translatable($this->entity_type, TRUE);
- $eid = 1;
- $entity = field_test_create_entity($eid, $eid, $this->instance['bundle']);
- $available_languages = array_flip(field_available_languages($this->entity_type, $this->field));
- unset($available_languages[LANGUAGE_NONE]);
- $field_name = $this->field['field_name'];
-
-
- $entity->is_new = TRUE;
- foreach ($available_languages as $langcode => $value) {
- $entity->{$field_name}[$langcode][0]['value'] = $value + 1;
- }
- field_test_entity_save($entity);
-
-
- $langcode = field_valid_language(NULL);
- $edit = array("{$field_name}[$langcode][0][value]" => $entity->{$field_name}[$langcode][0]['value'], 'revision' => TRUE);
- $this->backdropPost('test-entity/manage/' . $eid . '/edit', $edit, t('Save'));
-
-
- $this->checkTranslationRevisions($eid, $eid, $available_languages);
- $this->checkTranslationRevisions($eid, $eid + 1, $available_languages);
- }
-
-
- * Check if the field translation attached to the entity revision identified
- * by the passed arguments were correctly stored.
- */
- private function checkTranslationRevisions($eid, $evid, $available_languages) {
- $field_name = $this->field['field_name'];
- $entity = field_test_entity_test_load($eid, $evid);
- foreach ($available_languages as $langcode => $value) {
- $passed = isset($entity->{$field_name}[$langcode]) && $entity->{$field_name}[$langcode][0]['value'] == $value + 1;
- $this->assertTrue($passed, format_string('The @language translation for revision @revision was correctly stored', array('@language' => $langcode, '@revision' => $entity->ftvid)));
- }
- }
- }
-
- * Unit test class for field bulk delete and batch purge functionality.
- */
- class FieldBulkDeleteTestCase extends FieldTestCase {
- protected $entity_type;
- protected $fields;
- protected $instances;
- protected $bundles;
- protected $entities;
- protected $entities_by_bundles;
-
-
- * Converts the passed entities to partially created ones.
- *
- * This replicates the partial entities created in field_purge_data_batch(),
- * which only have the ids and the to be deleted field defined. *
- * @param $entities
- * An array of entities of type test_entity.
- * @param $field_name
- * A field name whose data should be copied from $entities into the returned
- * partial entities. * @return array
- * An array of partial entities corresponding to $entities.
- */
- protected function convertToPartialEntities($entities, $field_name) {
- $partial_entities = array(); foreach ($entities as $id => $entity) {
-
-
- $partial_entities[$id] = field_test_create_entity($entity->ftid, $entity->ftvid, $entity->fttype, $entity->ftlabel);
- unset($partial_entities[$id]->ftlabel);
- $partial_entities[$id]->$field_name = $entity->$field_name;
- }
- return $partial_entities; }
-
-
- * Tests that the expected hooks have been invoked on the expected entities.
- *
- * @param $expected_hooks
- * An array keyed by hook name, with one entry per expected invocation.
- * Each entry is the value of the "$entity" parameter the hook is expected
- * to have been passed.
- * @param $actual_hooks
- * The array of actual hook invocations recorded by field_test_memorize().
- */
- function checkHooksInvocations($expected_hooks, $actual_hooks) {
- foreach ($expected_hooks as $hook => $invocations) {
- $actual_invocations = $actual_hooks[$hook];
-
-
- $this->assertEqual(count($actual_invocations), count($invocations), "$hook() was called the expected number of times.");
-
-
- foreach ($invocations as $argument) {
- $found = FALSE;
- foreach ($actual_invocations as $actual_arguments) {
- if ($actual_arguments[1] == $argument) {
- $found = TRUE;
- break;
- }
- }
- $this->assertTrue($found, "$hook() was called on expected argument");
- }
- }
- }
-
- function setUp() {
- parent::setUp('field_test');
-
- $this->fields = array();
- $this->instances = array();
- $this->entities = array();
- $this->entities_by_bundles = array();
-
-
- $this->bundles = array('bb_1' => 'bb_1', 'bb_2' => 'bb_2');
- foreach ($this->bundles as $name => $desc) {
- field_test_create_bundle($name, $desc);
- }
-
-
- $field = array('field_name' => 'bf_1', 'type' => 'test_field', 'cardinality' => 1);
- $this->fields[] = field_create_field($field);
- $field = array('field_name' => 'bf_2', 'type' => 'test_field', 'cardinality' => 4);
- $this->fields[] = field_create_field($field);
-
-
-
- $id = 0;
- $this->entity_type = 'test_entity';
- foreach ($this->bundles as $bundle) {
- foreach ($this->fields as $field) {
- $instance = array(
- 'field_name' => $field['field_name'],
- 'entity_type' => $this->entity_type,
- 'bundle' => $bundle,
- 'widget' => array(
- 'type' => 'test_field_widget',
- )
- );
- $this->instances[] = field_create_instance($instance);
- }
-
- for ($i = 0; $i < 10; $i++) {
- $entity = field_test_create_entity($id, $id, $bundle);
- foreach ($this->fields as $field) {
- $entity->{$field['field_name']}[LANGUAGE_NONE] = $this->_generateTestFieldValues($field['cardinality']);
- }
-
- $this->entities[$id] = $entity;
-
- $this->entities_by_bundles[$bundle][$id] = $entity;
- field_attach_insert($this->entity_type, $entity);
- $id++;
- }
- }
- }
-
-
- * Verify that deleting an instance leaves the field data items in
- * the database and that the appropriate Field API functions can
- * operate on the deleted data and instance.
- *
- * This tests how EntityFieldQuery interacts with
- * field_delete_instance() and could be moved to FieldCrudTestCase,
- * but depends on this class's setUp().
- */
- function testDeleteFieldInstance() {
- $bundle = reset($this->bundles);
- $field = reset($this->fields);
-
-
- $query = new EntityFieldQuery();
- $found = $query
- ->fieldCondition($field)
- ->entityCondition('bundle', $bundle)
- ->execute();
- $this->assertEqual(count($found['test_entity']), 10, 'Correct number of entities found before deleting');
-
-
- $instance = field_info_instance($this->entity_type, $field['field_name'], $bundle);
- field_delete_instance($instance);
-
-
- $instances = field_read_instances(array('field_name' => $field['field_name'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1));
- $this->assertEqual(count($instances), 1, 'There is one deleted instance');
- $this->assertEqual($instances[0]['bundle'], $bundle, 'The deleted instance is for the correct bundle');
-
-
- $query = new EntityFieldQuery();
- $found = $query
- ->fieldCondition($field)
- ->entityCondition('bundle', $bundle)
- ->execute();
- $this->assertTrue(!isset($found['test_entity']), 'No entities found after deleting');
-
-
-
- $query = new EntityFieldQuery();
- $found = $query
- ->fieldCondition($field)
- ->entityCondition('bundle', $bundle)
- ->deleted(TRUE)
- ->execute();
- $entities = array();
- foreach ($found[$this->entity_type] as $ids) {
- $entities[$ids->entity_id] = _field_create_entity_from_ids($ids);
- }
- field_attach_load($this->entity_type, $entities, FIELD_LOAD_CURRENT, array('field_name' => $field['field_name'], 'deleted' => 1));
- $this->assertEqual(count($found['test_entity']), 10, 'Correct number of entities found after deleting');
- foreach ($entities as $id => $entity) {
- $this->assertEqual($this->entities[$id]->{$field['field_name']}, $entity->{$field['field_name']}, "Entity $id with deleted data loaded correctly");
- }
- }
-
-
- * Verify that field data items and instances are purged when an
- * instance is deleted.
- */
- function testPurgeInstance() {
-
- field_test_memorize();
-
- $bundle = reset($this->bundles);
- $field = reset($this->fields);
-
-
- $instance = field_info_instance($this->entity_type, $field['field_name'], $bundle);
- field_delete_instance($instance);
-
-
- $mem = field_test_memorize();
- $this->assertEqual(count($mem), 0, 'No field hooks were called');
-
- $batch_size = 2;
- for ($count = 8; $count >= 0; $count -= $batch_size) {
-
- field_purge_batch($batch_size);
-
-
- $query = new EntityFieldQuery();
- $found = $query
- ->fieldCondition($field)
- ->entityCondition('bundle', $bundle)
- ->deleted(TRUE)
- ->execute();
- $this->assertEqual($count ? count($found['test_entity']) : count($found), $count, 'Correct number of entities found after purging 2');
- }
-
-
-
-
-
-
- $actual_hooks = field_test_memorize();
- $hooks = array();
- $entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field['field_name']);
- foreach (array_chunk($entities, $batch_size, TRUE) as $chunk_entity) {
- $hooks['field_test_field_load'][] = $chunk_entity;
- }
- foreach ($entities as $entity) {
- $hooks['field_test_field_delete'][] = $entity;
- }
- $this->checkHooksInvocations($hooks, $actual_hooks);
-
-
- $instances = field_read_instances(array('field_name' => $field['field_name'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1));
- $this->assertEqual(count($instances), 0, 'The instance is gone');
-
-
- $fields = field_read_fields(array('field_name' => $field['field_name']), array('include_deleted' => 1, 'include_inactive' => 1));
- $this->assertTrue(isset($fields[$field['field_name']]), 'The field exists and is not deleted');
- }
-
-
- * Verify that fields are preserved and purged correctly as multiple
- * instances are deleted and purged.
- */
- function testPurgeField() {
-
- field_test_memorize();
-
- $field = reset($this->fields);
-
-
- $bundle = reset($this->bundles);
- $instance = field_info_instance($this->entity_type, $field['field_name'], $bundle);
- field_delete_instance($instance);
-
-
- $mem = field_test_memorize();
- $this->assertEqual(count($mem), 0, 'No field hooks were called.');
-
-
- field_purge_batch(10);
-
-
-
-
-
-
- $actual_hooks = field_test_memorize();
- $hooks = array();
- $entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field['field_name']);
- $hooks['field_test_field_load'][] = $entities;
- $hooks['field_test_field_delete'] = $entities;
- $this->checkHooksInvocations($hooks, $actual_hooks);
-
-
- $instances = field_read_instances(array('field_name' => $field['field_name'], 'bundle' => $bundle), array('include_deleted' => 1));
- $this->assertEqual(count($instances), 0, 'The first instance has been deleted.');
-
-
- $fields = field_read_fields(array('field_name' => $field['field_name']), array('include_deleted' => 1));
- $this->assertTrue(isset($fields[$field['field_name']]) && !$fields[$field['field_name']]['deleted'], 'The field exists and is not deleted because one instance still remains.');
-
-
- $bundle = next($this->bundles);
- $instance = field_info_instance($this->entity_type, $field['field_name'], $bundle);
- field_delete_instance($instance);
-
-
- field_purge_batch(10);
-
-
- $fields = field_read_fields(array('field_name' => $field['field_name']), array('include_deleted' => 1, 'include_inactive' => 1));
- $this->assertEqual(count($fields), 0, 'The field is purged.');
- }
- }
-
- * Test the field blocks and their display of different formatters.
- */
- class FieldBlockTestCase extends FieldTestCase {
- protected $profile = 'testing';
- protected $content_type;
- protected $field_name;
- protected $field;
- protected $instance;
- protected $admin_user;
- protected $node;
-
- function setUp() {
- parent::setUp(array('node', 'field', 'field_test'));
-
- $this->content_type = $this->backdropCreateContentType(array(
- 'type' => 'post',
- 'name' => 'Post',
- ));
- $this->admin_user = $this->backdropCreateUser(array('access content', 'administer content types', 'administer nodes', 'administer layouts'));
-
- $this->field_name = backdrop_strtolower($this->randomName() . '_field_name');
- $this->field = field_create_field(array(
- 'field_name' => $this->field_name,
- 'type' => 'test_field',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- ));
- $this->instance = field_create_instance(array(
- 'label' => $this->randomName(),
- 'field_name' => $this->field_name,
- 'entity_type' => 'node',
- 'bundle' => 'post',
- ));
-
-
- $this->node = $this->backdropCreateNode(array(
- 'type' => 'post',
- $this->field_name => array(LANGUAGE_NONE => array(
- array('value' => 1),
- array('value' => 2),
- )),
- ));
- }
-
-
- * Check special conditions around the main content block.
- */
- function testFieldBlock() {
- $this->backdropLogin($this->admin_user);
-
-
- $layout_path = 'node/%';
- $edit = array(
- 'title' => 'Post layout',
- 'name' => 'node_post',
- 'layout_template' => 'moscone_flipped',
- 'path' => $layout_path,
- );
-
- $this->backdropPost('admin/structure/layouts/add', $edit, t('Create layout'));
-
-
- $this->clickLink(t('Add block'), 3);
- $this->clickLink(t('Field: @label (@name)', array('@label' => $this->instance['label'], '@name' => $this->field_name)));
-
- $block_settings = array(
- 'formatter' => 'field_test_default',
- 'formatter_settings[test_formatter_setting]' => 'My custom formatter string',
- );
-
- $this->backdropPost(NULL, $block_settings, t('Add block'));
-
-
- $this->assertText('test_formatter_setting: My custom formatter string');
-
-
- $last_block = $this->xpath('(//*[@id="layout-edit-main"]//*[contains(@class,:region)]//*[@data-block-id])[last()]', array(
- ':region' => 'l-sidebar',
- ));
- $block_uuid = (string) $last_block[0]['data-block-id'];
-
-
- $this->backdropPost(NULL, array(), t('Save layout'));
-
-
- $this->backdropGet('node/' . $this->node->nid);
- $block_class = backdrop_clean_css_identifier('block-field-node-' . $this->field_name);
- $frontend_block = $this->xpath('(//*[contains(@class, :region)]//*[contains(@class, :block)])[last()]', array(
- ':region' => 'l-sidebar',
- ':block' => $block_class,
- ));
-
-
- $block_title = ($frontend_block[0]->xpath('.//h2'));
- $field_items = ($frontend_block[0]->xpath('.//*[contains(@class, "field-items")]/*[contains(@class, "field-item")]'));
- $this->assertIdentical((string) $block_title[0], $this->instance['label'], 'Block title displayed as field label.');
- $this->assertIdentical((string) $field_items[0], 'My custom formatter string|1', 'First item displayed with correct formatter settings.');
- $this->assertIdentical((string) $field_items[1], 'My custom formatter string|2', 'Second item displayed with correct formatter settings.');
-
-
- $this->backdropGet('admin/structure/layouts/manage/node_post/configure-block/editor/' . $block_uuid);
- $block_settings = array(
- 'title_display' => 'none',
- 'label' => 'above',
- );
- $this->backdropPost(NULL, $block_settings, t('Update block'));
- $this->backdropPost(NULL, array(), t('Save layout'));
-
-
- $this->backdropGet('node/' . $this->node->nid);
- $frontend_block = $this->xpath('(//*[contains(@class, :region)]//*[contains(@class, :block)])[last()]', array(
- ':region' => 'l-sidebar',
- ':block' => $block_class,
- ));
-
-
- $block_title = ($frontend_block[0]->xpath('.//h2'));
- $field_label = ($frontend_block[0]->xpath('.//*[contains(@class, "field-label-above")]/*[contains(@class, "field-label")]'));
- $this->assertIdentical(array(), $block_title, 'Block title not shown when set to "None".');
- $this->assertTrue(strpos($field_label[0], $this->instance['label']) !== FALSE, 'Block label shown above the field content in the block.');
- }
- }
-
- * Test field_get_value().
- */
- class FieldGetValueTestCase extends FieldTestCase {
- protected $profile = 'testing';
- protected $content_type;
- protected $field_name;
- protected $field;
- protected $instance;
- protected $admin_user;
- protected $node;
-
- function setUp() {
- parent::setUp(array('node', 'field', 'field_test', 'email'));
-
- $this->content_type = $this->backdropCreateContentType(array(
- 'type' => 'post',
- 'name' => 'Post',
- ));
- $this->admin_user = $this->backdropCreateUser(array('access content', 'administer content types', 'administer nodes', 'administer layouts'));
-
-
- $this->test_field_name = backdrop_strtolower($this->randomName() . '_field_name');
- $this->field = field_create_field(array(
- 'field_name' => $this->test_field_name,
- 'type' => 'test_field',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- ));
- $this->instance = field_create_instance(array(
- 'label' => $this->randomName(),
- 'field_name' => $this->test_field_name,
- 'entity_type' => 'node',
- 'bundle' => 'post',
- ));
-
-
- $this->test_email_field_name = backdrop_strtolower($this->randomName() . '_email_field_name');
- $this->field = field_create_field(array(
- 'field_name' => $this->test_email_field_name,
- 'settings' => array(),
- 'type' => 'email',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- ));
- $this->instance = field_create_instance(array(
- 'settings' => array(),
- 'label' => $this->randomName(),
- 'field_name' => $this->test_email_field_name,
- 'entity_type' => 'node',
- 'bundle' => 'post',
- ));
-
- $this->value1 = 11;
- $this->value2 = 22;
- $this->value_email1 = 'email1@example.com';
- $this->value_email2 = 'email2@example.com';
-
-
- $this->node = $this->backdropCreateNode(array(
- 'type' => 'post',
- $this->test_field_name => array(LANGUAGE_NONE => array(
- array('value' => $this->value1),
- array('value' => $this->value2),
- )),
- $this->test_email_field_name => array(LANGUAGE_NONE => array(
- array('email' => $this->value_email1),
- array('email' => $this->value_email2),
- )),
- ));
- }
-
-
- * Test field_get_value().
- */
- function testFieldGetValue() {
-
- $single_value = field_get_value($this->node, $this->test_field_name);
- $this->assertEqual($this->value1, $single_value, 'The first value in test_field is returned.');
-
-
- $single_value = $this->node->getFieldValue($this->test_field_name);
- $this->assertEqual($this->value1, $single_value, 'The first value in test_field is returned.');
-
-
- $multiple_value = field_get_values($this->node, $this->test_field_name, 'value', FALSE);
- $this->assertEqual(2, count($multiple_value), 'An array containing both test_field values is returned.');
- $this->assertEqual($this->value2, $multiple_value[1], 'Returned value at array position 1 is correct.');
-
-
- $multiple_value = $this->node->getFieldValues($this->test_field_name, 'value', FALSE);
- $this->assertEqual(2, count($multiple_value), 'An array containing both test_field values is returned.');
- $this->assertEqual($this->value2, $multiple_value[1], 'Returned value at array position 1 is correct.');
-
-
- $email_value = field_get_value($this->node, $this->test_email_field_name, 'email');
- $this->assertEqual($this->value_email1, $email_value, 'The correct item key is returned.');
-
-
- $invalid_key = field_get_value($this->node, $this->test_email_field_name, 'wrong_key');
- $this->assertNull($invalid_key, 'Null is returned with an invalid key.');
-
-
- $invalid_field = field_get_value($this->node, 'no_field');
- $this->assertNull($invalid_field, 'Null is returned with an invalid field name.');
-
-
- $invalid_key = field_get_values($this->node, $this->test_email_field_name, 'wrong_key');
- $this->assertEqual(array(), $invalid_key, 'Empty array is returned with an invalid field name.');
-
-
- $invalid_field = field_get_values($this->node, 'no_field');
- $this->assertEqual(array(), $invalid_field, 'Empty array is returned with an invalid field name.');
-
- }
- }
-
- * Test fields with token display mode.
- */
- class FieldTokenTestCase extends FieldTestCase {
- protected $profile = 'testing';
- protected $test_field_name;
- protected $test_date_field_name;
- protected $admin_user;
- protected $node;
-
- function setUp() {
- parent::setUp(array('node', 'field', 'field_ui', 'field_test', 'date', 'path'));
-
-
- $this->admin_user = $this->backdropCreateUser(array('access content', 'bypass node access', 'administer content types', 'administer nodes', 'administer fields', 'administer view modes'));
- $this->backdropLogin($this->admin_user);
-
- $this->content_type = $this->backdropCreateContentType(array(
- 'type' => 'post',
- 'name' => 'Post',
- ));
-
- $this->backdropGet('admin/structure/types/manage/post/display');
- list($enable_link) = $this->xpath('//tr[contains(@class, "view-mode--token")]//a');
- $enable_href_parts = backdrop_parse_url($enable_link['href']);
- $this->backdropGet($enable_href_parts['path'], $enable_href_parts);
-
-
- $this->test_field_name = backdrop_strtolower($this->randomName() . '_field_name');
- $field = array(
- 'field_name' => $this->test_field_name,
- 'type' => 'text',
- 'cardinality' => 1
- );
- $instance = array(
- 'label' => $this->randomName(),
- 'field_name' => $this->test_field_name,
- 'entity_type' => 'node',
- 'bundle' => 'post',
- );
- $instance['display'] = array(
- 'default' => array(
- 'label' => 'above',
- 'type' => 'text_default',
- 'settings' => array(
- ),
- 'module' => 'text',
- 'weight' => 0 ,
- ),
- 'token' => array(
- 'label' => 'hidden',
- 'type' => 'text_default',
- 'weight' => 0,
- 'settings' => array(
- ),
- 'module' => 'text',
- ),
- );
-
- field_create_field($field);
- field_create_instance($instance);
-
-
- $this->test_date_field_name = backdrop_strtolower($this->randomName() . '_field_name');
- $field = array(
- 'field_name' => $this->test_date_field_name,
- 'type' => 'date',
- 'cardinality' => 1,
- 'settings' => array(
- ),
- );
- $instance = array(
- 'entity_type' => 'node',
- 'field_name' => $this->test_date_field_name,
- 'label' => 'Test',
- 'bundle' => 'post',
- 'weight' => -5,
- 'widget' => array(
- 'type' => 'date_text',
- 'settings' => array(
- ),
- 'weight' => -5,
- ),
- 'settings' => array(
- 'default_value' => 'now',
- ),
- );
-
- $instance['display'] = array(
- 'default' => array(
- 'label' => 'above',
- 'type' => 'date_default',
- 'settings' => array(
- 'format_type' => 'long',
- ),
- 'module' => 'date',
- 'weight' => 0 ,
- ),
- 'token' => array(
- 'label' => 'hidden',
- 'type' => 'date_default',
- 'weight' => 0,
- 'settings' => array(
- 'format_type' => 'html_year',
- ),
- 'module' => 'date',
- ),
- );
-
- field_create_field($field);
- field_create_instance($instance);
- config('path.settings')
- ->set('node_post_pattern', 'posts/[node:' . $this->test_date_field_name . ']/[node:' . $this->test_field_name . ']/[node:title]')
- ->save();
- }
-
-
- * Test fields in token display mode.
- */
- function testFieldToken() {
- $edit = array();
- $edit['title'] = $this->randomName(8);
- $edit[$this->test_field_name . '[und][0][value]'] = 'foo';
- $edit[$this->test_date_field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30am';
-
-
- $this->backdropPost('node/add/post', $edit, t('Save'));
- $this->node = $this->backdropGetNodeByTitle($edit['title']);
-
- $this->backdropGet('node/' . $this->node->nid);
-
- $uri = entity_uri('node', $this->node);
- backdrop_clear_path_cache($uri['path']);
- $alias = backdrop_get_path_alias($uri['path'], LANGUAGE_NONE);
- $expected_alias = 'posts/2010/' . $edit[$this->test_field_name . '[und][0][value]'] . '/' . backdrop_strtolower($this->node->title);
- $this->assertIdentical($alias, $expected_alias, format_string("Alias for %source was %actual, expected %expected.", array('%source' => $uri['path'], '%actual' => $alias, '%expected' => $expected_alias)));
- }
- }