Issue by pcambra, aspilicious, andypost, swentel: Drop procedural usage of fields in field module.

8.0.x
Dries 2013-06-21 15:23:16 -04:00
parent 81b2685cb6
commit 55651cf3a4
22 changed files with 632 additions and 550 deletions

View File

@ -1587,7 +1587,7 @@ function field_entity_bundle_delete($entity_type, $bundle) {
// entity types or bundles.
$instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle), array('include_inactive' => TRUE));
foreach ($instances as $instance) {
field_delete_instance($instance);
$instance->delete();
}
// Clear the cache.

View File

@ -24,89 +24,6 @@ use Drupal\field\FieldException;
* the Field API.
*/
/**
* Creates a field.
*
* This function does not bind the field to any bundle; use
* field_create_instance() for that.
*
* @param array $field
* A field definition. The field_name and type properties are required.
* Other properties, if omitted, will be given the following default values:
* - cardinality: 1
* - locked: FALSE
* - indexes: the field-type indexes, specified by the field type's
* hook_field_schema(). The indexes specified in $field are added
* to those default indexes. It is possible to override the
* definition of a field-type index by providing an index with the
* same name, or to remove it by redefining it as an empty array
* of columns. Overriding field-type indexes should be done
* carefully, for it might seriously affect the site's performance.
* - settings: each omitted setting is given the default value defined in
* hook_field_info().
* - storage:
* - type: the storage backend specified in the
* 'field.settings.default_storage' configuration.
* - settings: each omitted setting is given the default value specified in
* hook_field_storage_info().
*
* @return \Drupal\field\Plugin\Core\Entity\Field
* The field entity.
*
* @throws Drupal\field\FieldException
*
* @deprecated as of Drupal 8.0. Use
* entity_create('field_entity', $definition)->save().
*
* See: @link field Field API data structures @endlink.
*/
function field_create_field(array $field) {
$field = entity_create('field_entity', $field);
$field->save();
return $field;
}
/**
* Updates a field.
*
* Any module may forbid any update for any reason. For example, the
* field's storage module might forbid an update if it would change
* the storage schema while data for the field exists. A field type
* module might forbid an update if it would change existing data's
* semantics, or if there are external dependencies on field settings
* that cannot be updated.
*
* @param mixed $field
* Either the \Drupal\field\Plugin\Core\Entity\Field object to update, or a
* field array structure. If the latter, $field['field_name'] must provided;
* it identifies the field that will be updated to match this structure. Any
* other properties of the field that are not specified in $field will be left
* unchanged, so it is not necessary to pass in a fully populated $field
* structure.
*
* @throws Drupal\field\FieldException
*
* @deprecated as of Drupal 8.0. Use $field->save().
*
* @see field_create_field()
*/
function field_update_field($field) {
// Module developers can still pass in an array of properties.
if (is_array($field)) {
$field_loaded = entity_load('field_entity', $field['field_name']);
if (empty($field_loaded)) {
throw new FieldException('Attempt to update a non-existent field.');
}
// Merge incoming values.
foreach ($field as $key => $value) {
$field_loaded[$key] = $value;
}
$field = $field_loaded;
}
$field->save();
}
/**
* Reads a single field record directly from the database.
*
@ -161,103 +78,6 @@ function field_read_fields($conditions = array(), $include_additional = array())
return entity_load_multiple_by_properties('field_entity', $conditions);
}
/**
* Marks a field and its instances and data for deletion.
*
* @param $field_name
* The field name to delete.
*
* @deprecated as of Drupal 8.0. Use $field->delete().
*/
function field_delete_field($field_name) {
if ($field = field_info_field($field_name)) {
$field->delete();
}
}
/**
* Creates an instance of a field, binding it to a bundle.
*
* @param array $instance
* A field instance definition array. The field_name, entity_type and
* bundle properties are required. Other properties, if omitted,
* will be given the following default values:
* - label: the field name
* - description: empty string
* - required: FALSE
* - default_value_function: empty string
* - settings: each omitted setting is given the default value specified in
* hook_field_info().
* - widget:
* - type: the default widget specified in hook_field_info().
* - settings: each omitted setting is given the default value specified in
* hook_field_widget_info().
*
* @return \Drupal\field\Plugin\Core\Entity\FieldInstance
* The field instance entity.
*
* @throws Drupal\field\FieldException
*
* @deprecated as of Drupal 8.0. Use
* entity_create('field_instance', $definition)->save().
*
* See: @link field Field API data structures @endlink.
*/
function field_create_instance(array $instance) {
$instance = entity_create('field_instance', $instance);
$instance->save();
return $instance;
}
/**
* Updates an instance of a field.
*
* @param mixed $instance
* Either the \Drupal\field\Plugin\Core\Entity\FieldInstance to update, or an
* associative array representing an instance structure. If the latter, the
* required keys and values are:
* - entity_type: The type of the entity the field is attached to.
* - bundle: The bundle this field belongs to.
* - field_name: The name of an existing field.
* The other array elements represent properties of the instance, and all
* properties must be specified or their default values will be used (except
* internal-use properties, which are assigned automatically). To avoid losing
* the previously stored properties of the instance when making a change,
* first load the instance with field_info_instance(), then override the
* values you want to override, and finally save using this function. Example:
* @code
* // Fetch an instance info array.
* $instance_info = field_info_instance($entity_type, $field_name, $bundle_name);
* // Change a single property in the instance definition.
* $instance_info['definition']['required'] = TRUE;
* // Write the changed definition back.
* field_update_instance($instance_info['definition']);
* @endcode
*
* @throws Drupal\field\FieldException
*
* @deprecated as of Drupal 8.0. Use $instance->save().
*
* @see field_info_instance()
* @see field_create_instance()
*/
function field_update_instance($instance) {
// Module developers can still pass in an array of properties.
if (is_array($instance)) {
$instance_loaded = entity_load('field_instance', $instance['entity_type'] . '.' . $instance['bundle'] . '.' . $instance['field_name']);
if (empty($instance_loaded)) {
throw new FieldException('Attempt to update a non-existent instance.');
}
// Merge incoming values.
foreach ($instance as $key => $value) {
$instance_loaded[$key] = $value;
}
$instance = $instance_loaded;
}
$instance->save();
}
/**
* Reads a single instance record from the database.
*
@ -316,22 +136,6 @@ function field_read_instances($conditions = array(), $include_additional = array
return entity_load_multiple_by_properties('field_instance', $conditions);
}
/**
* Marks a field instance and its data for deletion.
*
* @param \Drupal\field\Plugin\Core\Entity\FieldInstance $instance
* The field instance.
* @param $field_cleanup
* If TRUE, the field will be deleted as well if its last instance is being
* deleted. If FALSE, it is the caller's responsibility to handle the case of
* fields left without instances. Defaults to TRUE.
*
* @deprecated as of Drupal 8.0. Use $instance->delete().
*/
function field_delete_instance(FieldInstance $instance, $field_cleanup = TRUE) {
$instance->delete($field_cleanup);
}
/**
* @} End of "defgroup field_crud".
*/

View File

@ -75,8 +75,7 @@ require_once __DIR__ . '/field.form.inc';
* field_sql_storage.module, stores field data in the local SQL database.
*
* - @link field_purge Field API bulk data deletion @endlink: Cleans up after
* bulk deletion operations such as field_delete_field() and
* field_delete_instance().
* bulk deletion operations such as deletion of field or field_instance.
*
* - @link field_language Field language API @endlink: Provides native
* multilingual support for the Field API.

View File

@ -37,7 +37,7 @@ class ActiveTest extends FieldTestBase {
'type' => 'field_sql_storage',
),
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
// Test disabling and enabling:
// - the field type module,

View File

@ -16,7 +16,40 @@ use Drupal\Core\Language\Language;
*/
class BulkDeleteTest extends FieldUnitTestBase {
protected $field;
/**
* The fields to use in this test.
*
* @var array
*/
protected $fields;
/**
* The entities to use in this test.
*
* @var array
*/
protected $entities;
/**
* The entities to use in this test, keyed by bundle.
*
* @var array
*/
protected $entities_by_bundles;
/**
* The bundles for the entities used in this test.
*
* @var array
*/
protected $bundles;
/**
* The entity type to be used in the test classes.
*
* @var array
*/
protected $entity_type = 'test_entity';
public static function getInfo() {
return array(
@ -37,8 +70,7 @@ class BulkDeleteTest extends FieldUnitTestBase {
* @param $field_name
* A field name whose data should be copied from $entities into the returned
* partial entities.
* @return
* An array of partial entities corresponding to $entities.
* @return array An array of partial entities corresponding to $entities.
*/
protected function convertToPartialEntities($entities, $field_name) {
$partial_entities = array();
@ -93,7 +125,6 @@ class BulkDeleteTest extends FieldUnitTestBase {
parent::setUp();
$this->fields = array();
$this->instances = array();
$this->entities = array();
$this->entities_by_bundles = array();
@ -104,29 +135,37 @@ class BulkDeleteTest extends FieldUnitTestBase {
}
// Create two fields.
$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);
$field = entity_create('field_entity', array(
'field_name' => 'bf_1',
'type' => 'test_field',
'cardinality' => 1
));
$field->save();
$this->fields[] = $field;
$field = entity_create('field_entity', array(
'field_name' => 'bf_2',
'type' => 'test_field',
'cardinality' => 4
));
$field->save();
$this->fields[] = $field;
// For each bundle, create an instance of each field, and 10
// entities with values for each field.
$id = 1;
$this->entity_type = 'test_entity';
foreach ($this->bundles as $bundle) {
foreach ($this->fields as $field) {
$instance = array(
'field_name' => $field['field_name'],
entity_create('field_instance', array(
'field_name' => $field->id(),
'entity_type' => $this->entity_type,
'bundle' => $bundle,
);
$this->instances[] = field_create_instance($instance);
))->save();
}
for ($i = 0; $i < 10; $i++) {
$entity = field_test_create_entity($id, $id, $bundle);
foreach ($this->fields as $field) {
$entity->{$field['field_name']}[Language::LANGCODE_NOT_SPECIFIED] = $this->_generateTestFieldValues($field['cardinality']);
$entity->{$field->id()}[Language::LANGCODE_NOT_SPECIFIED] = $this->_generateTestFieldValues($field->cardinality);
}
$entity->save();
$id++;
@ -144,14 +183,13 @@ class BulkDeleteTest extends FieldUnitTestBase {
* 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().
* This tests how EntityFieldQuery interacts with field instance deletion and
* could be moved to FieldCrudTestCase, but depends on this class's setUp().
*/
function testDeleteFieldInstance() {
$bundle = reset($this->bundles);
$field = reset($this->fields);
$field_name = $field['field_name'];
$field_name = $field->id();
$factory = \Drupal::service('entity.query');
// There are 10 entities of this bundle.
@ -161,11 +199,11 @@ class BulkDeleteTest extends FieldUnitTestBase {
$this->assertEqual(count($found), 10, 'Correct number of entities found before deleting');
// Delete the instance.
$instance = field_info_instance($this->entity_type, $field['field_name'], $bundle);
field_delete_instance($instance);
$instance = field_info_instance($this->entity_type, $field->id(), $bundle);
$instance->delete();
// The instance still exists, deleted.
$instances = field_read_instances(array('field_id' => $field['uuid'], 'deleted' => TRUE), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
$instances = field_read_instances(array('field_id' => $field->uuid, 'deleted' => TRUE), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
$this->assertEqual(count($instances), 1, 'There is one deleted instance');
$this->assertEqual($instances[0]['bundle'], $bundle, 'The deleted instance is for the correct bundle');
@ -192,10 +230,10 @@ class BulkDeleteTest extends FieldUnitTestBase {
$ids->entity_id = $entity_id;
$entities[$entity_id] = _field_create_entity_from_ids($ids);
}
field_attach_load($this->entity_type, $entities, FIELD_LOAD_CURRENT, array('field_id' => $field['uuid'], 'deleted' => TRUE));
field_attach_load($this->entity_type, $entities, FIELD_LOAD_CURRENT, array('field_id' => $field->uuid, 'deleted' => TRUE));
$this->assertEqual(count($found), 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");
$this->assertEqual($this->entities[$id]->{$field->id()}, $entity->{$field->id()}, "Entity $id with deleted data loaded correctly");
}
}
@ -211,8 +249,8 @@ class BulkDeleteTest extends FieldUnitTestBase {
$field = reset($this->fields);
// Delete the instance.
$instance = field_info_instance($this->entity_type, $field['field_name'], $bundle);
field_delete_instance($instance);
$instance = field_info_instance($this->entity_type, $field->id(), $bundle);
$instance->delete();
// No field hooks were called.
$mem = field_test_memorize();
@ -226,7 +264,7 @@ class BulkDeleteTest extends FieldUnitTestBase {
// There are $count deleted entities left.
$found = \Drupal::entityQuery('test_entity')
->condition('fttype', $bundle)
->condition($field['field_name'] . '.deleted', 1)
->condition($field->id() . '.deleted', 1)
->execute();
$this->assertEqual(count($found), $count, 'Correct number of entities found after purging 2');
}
@ -238,7 +276,7 @@ class BulkDeleteTest extends FieldUnitTestBase {
// bundle.
$actual_hooks = field_test_memorize();
$hooks = array();
$entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field['field_name']);
$entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field->id());
foreach (array_chunk($entities, $batch_size, TRUE) as $chunk_entity) {
$hooks['field_test_field_load'][] = $chunk_entity;
}
@ -248,19 +286,19 @@ class BulkDeleteTest extends FieldUnitTestBase {
$this->checkHooksInvocations($hooks, $actual_hooks);
// The instance still exists, deleted.
$instances = field_read_instances(array('field_id' => $field['uuid'], 'deleted' => TRUE), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
$instances = field_read_instances(array('field_id' => $field->uuid, 'deleted' => TRUE), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
$this->assertEqual(count($instances), 1, 'There is one deleted instance');
// Purge the instance.
field_purge_batch($batch_size);
// The instance is gone.
$instances = field_read_instances(array('field_id' => $field['uuid'], 'deleted' => TRUE), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
$instances = field_read_instances(array('field_id' => $field->uuid, 'deleted' => TRUE), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
$this->assertEqual(count($instances), 0, 'The instance is gone');
// The field still exists, not deleted, because it has a second instance.
$fields = field_read_fields(array('uuid' => $field['uuid']), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
$this->assertTrue(isset($fields[$field['uuid']]), 'The field exists and is not deleted');
$fields = field_read_fields(array('uuid' => $field->uuid), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
$this->assertTrue(isset($fields[$field->uuid]), 'The field exists and is not deleted');
}
/**
@ -275,8 +313,8 @@ class BulkDeleteTest extends FieldUnitTestBase {
// Delete the first instance.
$bundle = reset($this->bundles);
$instance = field_info_instance($this->entity_type, $field['field_name'], $bundle);
field_delete_instance($instance);
$instance = field_info_instance($this->entity_type, $field->id(), $bundle);
$instance->delete();
// Assert that hook_field_delete() was not called yet.
$mem = field_test_memorize();
@ -292,7 +330,7 @@ class BulkDeleteTest extends FieldUnitTestBase {
// bundle.
$actual_hooks = field_test_memorize();
$hooks = array();
$entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field['field_name']);
$entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field->id());
$hooks['field_test_field_load'][] = $entities;
$hooks['field_test_field_delete'] = $entities;
$this->checkHooksInvocations($hooks, $actual_hooks);
@ -301,13 +339,13 @@ class BulkDeleteTest extends FieldUnitTestBase {
field_purge_batch(0);
// The field still exists, not deleted.
$fields = field_read_fields(array('uuid' => $field['uuid']), array('include_deleted' => TRUE));
$this->assertTrue(isset($fields[$field['uuid']]) && !$fields[$field['uuid']]->deleted, 'The field exists and is not deleted');
$fields = field_read_fields(array('uuid' => $field->uuid), array('include_deleted' => TRUE));
$this->assertTrue(isset($fields[$field->uuid]) && !$fields[$field->uuid]->deleted, 'The field exists and is not deleted');
// Delete the second instance.
$bundle = next($this->bundles);
$instance = field_info_instance($this->entity_type, $field['field_name'], $bundle);
field_delete_instance($instance);
$instance = field_info_instance($this->entity_type, $field->id(), $bundle);
$instance->delete();
// Assert that hook_field_delete() was not called yet.
$mem = field_test_memorize();
@ -319,20 +357,21 @@ class BulkDeleteTest extends FieldUnitTestBase {
// Check hooks invocations (same as above, for the 2nd bundle).
$actual_hooks = field_test_memorize();
$hooks = array();
$entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field['field_name']);
$entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field->id());
$hooks['field_test_field_load'][] = $entities;
$hooks['field_test_field_delete'] = $entities;
$this->checkHooksInvocations($hooks, $actual_hooks);
// The field still exists, deleted.
$fields = field_read_fields(array('uuid' => $field['uuid']), array('include_deleted' => TRUE));
$this->assertTrue(isset($fields[$field['uuid']]) && $fields[$field['uuid']]->deleted, 'The field exists and is deleted');
$fields = field_read_fields(array('uuid' => $field->uuid), array('include_deleted' => TRUE));
$this->assertTrue(isset($fields[$field->uuid]) && $fields[$field->uuid]->deleted, 'The field exists and is deleted');
// Purge again to purge the instance and the field.
field_purge_batch(0);
// The field is gone.
$fields = field_read_fields(array('uuid' => $field['uuid']), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
$fields = field_read_fields(array('uuid' => $field->uuid), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
$this->assertEqual(count($fields), 0, 'The field is purged.');
}
}

View File

@ -41,7 +41,8 @@ class CrudTest extends FieldUnitTestBase {
'type' => 'test_field',
);
field_test_memorize();
$field = field_create_field($field_definition);
$field = entity_create('field_entity', $field_definition);
$field->save();
$mem = field_test_memorize();
$this->assertIdentical($mem['field_test_field_create_field'][0][0]['field_name'], $field_definition['field_name'], 'hook_field_create_field() called with correct arguments.');
$this->assertIdentical($mem['field_test_field_create_field'][0][0]['type'], $field_definition['type'], 'hook_field_create_field() called with correct arguments.');
@ -67,7 +68,7 @@ class CrudTest extends FieldUnitTestBase {
// Guarantee that the name is unique.
try {
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$this->fail(t('Cannot create two fields with the same name.'));
}
catch (FieldException $e) {
@ -79,7 +80,7 @@ class CrudTest extends FieldUnitTestBase {
$field_definition = array(
'field_name' => 'field_1',
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$this->fail(t('Cannot create a field with no type.'));
}
catch (FieldException $e) {
@ -91,7 +92,7 @@ class CrudTest extends FieldUnitTestBase {
$field_definition = array(
'type' => 'test_field'
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$this->fail(t('Cannot create an unnamed field.'));
}
catch (FieldException $e) {
@ -104,7 +105,7 @@ class CrudTest extends FieldUnitTestBase {
'field_name' => '2field_2',
'type' => 'test_field',
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$this->fail(t('Cannot create a field with a name starting with a digit.'));
}
catch (FieldException $e) {
@ -117,7 +118,7 @@ class CrudTest extends FieldUnitTestBase {
'field_name' => 'field#_3',
'type' => 'test_field',
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$this->fail(t('Cannot create a field with a name containing an illegal character.'));
}
catch (FieldException $e) {
@ -130,7 +131,7 @@ class CrudTest extends FieldUnitTestBase {
'field_name' => '_12345678901234567890123456789012',
'type' => 'test_field',
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$this->fail(t('Cannot create a field with a name longer than 32 characters.'));
}
catch (FieldException $e) {
@ -144,7 +145,7 @@ class CrudTest extends FieldUnitTestBase {
'type' => 'test_field',
'field_name' => 'ftvid',
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$this->fail(t('Cannot create a field bearing the name of an entity key.'));
}
catch (FieldException $e) {
@ -165,7 +166,7 @@ class CrudTest extends FieldUnitTestBase {
// Try to create the field.
try {
$field = field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$this->assertTrue(FALSE, 'Field creation (correctly) fails.');
}
catch (\Exception $e) {
@ -185,7 +186,7 @@ class CrudTest extends FieldUnitTestBase {
'field_name' => 'field_1',
'type' => 'test_field',
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
// Read the field back.
$field = field_read_field($field_definition['field_name']);
@ -200,7 +201,7 @@ class CrudTest extends FieldUnitTestBase {
'field_name' => 'field_1',
'type' => 'test_field',
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
// Check that 'single column' criteria works.
$fields = field_read_fields(array('field_name' => $field_definition['field_name']));
@ -218,7 +219,7 @@ class CrudTest extends FieldUnitTestBase {
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
);
field_create_instance($instance_definition);
entity_create('field_instance', $instance_definition)->save();
}
/**
@ -230,7 +231,7 @@ class CrudTest extends FieldUnitTestBase {
'field_name' => 'field_1',
'type' => 'test_field',
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$field = field_read_field($field_definition['field_name']);
$schema = $field->getSchema();
$expected_indexes = array('value' => array('value'));
@ -245,7 +246,7 @@ class CrudTest extends FieldUnitTestBase {
'value' => array(),
),
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$field = field_read_field($field_definition['field_name']);
$schema = $field->getSchema();
$expected_indexes = array('value' => array());
@ -260,7 +261,7 @@ class CrudTest extends FieldUnitTestBase {
'value_2' => array('value'),
),
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$field = field_read_field($field_definition['field_name']);
$schema = $field->getSchema();
$expected_indexes = array('value' => array('value'), 'value_2' => array('value'));
@ -275,9 +276,9 @@ class CrudTest extends FieldUnitTestBase {
// Create two fields (so we can test that only one is deleted).
$this->field = array('field_name' => 'field_1', 'type' => 'test_field');
field_create_field($this->field);
entity_create('field_entity', $this->field)->save();
$this->another_field = array('field_name' => 'field_2', 'type' => 'test_field');
field_create_field($this->another_field);
entity_create('field_entity', $this->another_field)->save();
// Create instances for each.
$this->instance_definition = array(
@ -285,15 +286,15 @@ class CrudTest extends FieldUnitTestBase {
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
);
field_create_instance($this->instance_definition);
$this->another_instance_definition = $this->instance_definition;
$this->another_instance_definition['field_name'] = $this->another_field['field_name'];
field_create_instance($this->another_instance_definition);
entity_create('field_instance', $this->instance_definition)->save();
$another_instance_definition = $this->instance_definition;
$another_instance_definition['field_name'] = $this->another_field['field_name'];
entity_create('field_instance', $another_instance_definition)->save();
// Test that the first field is not deleted, and then delete it.
$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_info_field($this->field['field_name'])->delete();
// Make sure that the field is marked as deleted when it is specifically
// loaded.
@ -316,13 +317,13 @@ class CrudTest extends FieldUnitTestBase {
// Make sure the other field (and its field instance) are not deleted.
$another_field = field_read_field($this->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', $this->another_instance_definition['field_name'], $this->another_instance_definition['bundle']);
$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.');
// Try to create a new field the same name as a deleted field and
// write data into it.
field_create_field($this->field);
field_create_instance($this->instance_definition);
entity_create('field_entity', $this->field)->save();
entity_create('field_instance', $this->instance_definition)->save();
$field = field_read_field($this->field['field_name']);
$this->assertTrue(!empty($field) && empty($field['deleted']), 'A new field with a previously used name is created.');
$instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
@ -346,12 +347,13 @@ class CrudTest extends FieldUnitTestBase {
}
function testUpdateFieldType() {
$field = array('field_name' => 'field_type', 'type' => 'number_decimal');
$field = field_create_field($field);
$field_definition = array('field_name' => 'field_type', 'type' => 'number_decimal');
$field = entity_create('field_entity', $field_definition);
$field->save();
$test_field = array('field_name' => 'field_type', 'type' => 'number_integer');
try {
field_update_field($test_field);
$field->type = 'number_integer';
$field->save();
$this->fail(t('Cannot update a field to a different type.'));
}
catch (FieldException $e) {
@ -367,21 +369,23 @@ class CrudTest extends FieldUnitTestBase {
// respected. Since cardinality enforcement is consistent across database
// systems, it makes a good test case.
$cardinality = 4;
$field = field_create_field(array(
$field = entity_create('field_entity', array(
'field_name' => 'field_update',
'type' => 'test_field',
'cardinality' => $cardinality,
));
$instance = field_create_instance(array(
$field->save();
$instance = entity_create('field_instance', array(
'field_name' => 'field_update',
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
));
$instance->save();
do {
// We need a unique ID for our entity. $cardinality will do.
$id = $cardinality;
$entity = field_test_create_entity($id, $id, $instance['bundle']);
$entity = field_test_create_entity($id, $id, $instance->bundle);
// Fill in the entity with more values than $cardinality.
for ($i = 0; $i < 20; $i++) {
$entity->field_update[Language::LANGCODE_NOT_SPECIFIED][$i]['value'] = $i;
@ -389,16 +393,16 @@ class CrudTest extends FieldUnitTestBase {
// Save the entity.
field_attach_insert($entity);
// Load back and assert there are $cardinality number of values.
$entity = field_test_create_entity($id, $id, $instance['bundle']);
$entity = field_test_create_entity($id, $id, $instance->bundle);
field_attach_load('test_entity', array($id => $entity));
$this->assertEqual(count($entity->field_update[Language::LANGCODE_NOT_SPECIFIED]), $field['cardinality'], 'Cardinality is kept');
$this->assertEqual(count($entity->field_update[Language::LANGCODE_NOT_SPECIFIED]), $field->cardinality, 'Cardinality is kept');
// Now check the values themselves.
for ($delta = 0; $delta < $cardinality; $delta++) {
$this->assertEqual($entity->field_update[Language::LANGCODE_NOT_SPECIFIED][$delta]['value'], $delta, 'Value is kept');
}
// Increase $cardinality and set the field cardinality to the new value.
$field['cardinality'] = ++$cardinality;
field_update_field($field);
$field->cardinality = ++$cardinality;
$field->save();
} while ($cardinality < 6);
}
@ -406,19 +410,25 @@ class CrudTest extends FieldUnitTestBase {
* 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']++;
$field = entity_create('field_entity', array(
'field_name' => 'forbidden',
'type' => 'test_field',
'settings' => array(
'changeable' => 0,
'unchangeable' => 0
)));
$field->save();
$field->settings['changeable']++;
try {
field_update_field($field);
$field->save();
$this->pass(t("A changeable setting can be updated."));
}
catch (FieldException $e) {
$this->fail(t("An unchangeable setting cannot be updated."));
}
$field['settings']['unchangeable']++;
$field->settings['unchangeable']++;
try {
field_update_field($field);
$field->save();
$this->fail(t("An unchangeable setting can be updated."));
}
catch (FieldException $e) {

View File

@ -11,6 +11,48 @@ use Drupal\Core\Language\Language;
class DisplayApiTest extends FieldUnitTestBase {
/**
* The field name to use in this test.
*
* @var string
*/
protected $field_name;
/**
* The field label to use in this test.
*
* @var string
*/
protected $label;
/**
* The field cardinality to use in this test.
*
* @var number
*/
protected $cardinality;
/**
* The field display options to use in this test.
*
* @var array
*/
protected $display_options;
/**
* The field display options to use in this test.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;
/**
* An array of random values, in the format expected for field values.
*
* @var array
*/
protected $values;
public static function getInfo() {
return array(
'name' => 'Field Display API tests',
@ -27,12 +69,12 @@ class DisplayApiTest extends FieldUnitTestBase {
$this->label = $this->randomName();
$this->cardinality = 4;
$this->field = array(
$field = array(
'field_name' => $this->field_name,
'type' => 'test_field',
'cardinality' => $this->cardinality,
);
$this->instance = array(
$instance = array(
'field_name' => $this->field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
@ -54,14 +96,14 @@ class DisplayApiTest extends FieldUnitTestBase {
),
);
field_create_field($this->field);
field_create_instance($this->instance);
entity_create('field_entity', $field)->save();
entity_create('field_instance', $instance)->save();
// Create a display for the default view mode.
entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
entity_get_display($instance['entity_type'], $instance['bundle'], 'default')
->setComponent($this->field_name, $this->display_options['default'])
->save();
// Create a display for the teaser view mode.
entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'teaser')
entity_get_display($instance['entity_type'], $instance['bundle'], 'teaser')
->setComponent($this->field_name, $this->display_options['teaser'])
->save();

View File

@ -19,6 +19,20 @@ class FieldAccessTest extends FieldTestBase {
*/
public static $modules = array('node', 'field_test');
/**
* Node entity to use in this test.
*
* @var \Drupal\node\Plugin\Core\Entity\Node
*/
protected $node;
/**
* Field value to test display on nodes.
*
* @var string
*/
protected $test_view_field_value;
public static function getInfo() {
return array(
'name' => 'Field access tests',
@ -34,32 +48,32 @@ class FieldAccessTest extends FieldTestBase {
$this->drupalLogin($web_user);
// Create content type.
$this->content_type_info = $this->drupalCreateContentType();
$this->content_type = $this->content_type_info->type;
$content_type_info = $this->drupalCreateContentType();
$content_type = $content_type_info->type;
$this->field = array(
$field = array(
'field_name' => 'test_view_field',
'type' => 'text',
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
entity_create('field_entity', $field)->save();
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => 'node',
'bundle' => $this->content_type,
'bundle' => $content_type,
);
field_create_instance($this->instance);
entity_create('field_instance', $instance)->save();
// Assign display properties for the 'default' and 'teaser' view modes.
foreach (array('default', 'teaser') as $view_mode) {
entity_get_display('node', $this->content_type, $view_mode)
->setComponent($this->field['field_name'])
entity_get_display('node', $content_type, $view_mode)
->setComponent($field['field_name'])
->save();
}
// Create test node.
$this->test_view_field_value = 'This is some text';
$settings = array();
$settings['type'] = $this->content_type;
$settings['type'] = $content_type;
$settings['title'] = 'Field view access test';
$settings['test_view_field'] = array(array('value' => $this->test_view_field_value));
$this->node = $this->drupalCreateNode($settings);

View File

@ -14,6 +14,21 @@ use Drupal\field\FieldValidationException;
* Unit test class for non-storage related field_attach_* functions.
*/
class FieldAttachOtherTest extends FieldUnitTestBase {
/**
* Field name to use in the test.
*
* @var string
*/
protected $field_name;
/**
* Field name to use in the test.
*
* @var string
*/
protected $field_name_2;
public static function getInfo() {
return array(
'name' => 'Field attach tests (other)',
@ -192,9 +207,10 @@ class FieldAttachOtherTest extends FieldUnitTestBase {
// hook_field_formatter_prepare_view().
field_test_create_bundle('test_bundle_2');
$formatter_setting = $this->randomName();
$this->instance2 = $this->instance;
$this->instance2['bundle'] = 'test_bundle_2';
field_create_instance($this->instance2);
$instance_definition = $this->instance_definition;
$instance_definition['bundle'] = 'test_bundle_2';
$this->instance2 = entity_create('field_instance', $instance_definition);
$this->instance2->save();
$display_2 = entity_get_display('test_entity', 'test_bundle_2', 'full')
->setComponent($this->field['field_name'], array(
@ -268,9 +284,9 @@ class FieldAttachOtherTest extends FieldUnitTestBase {
'fttype' => $this->instance['bundle'],
));
$cid = "field:$entity_type:{$entity_init->ftid}";
$instance = $this->instance;
$instance['entity_type'] = $entity_type;
field_create_instance($instance);
$instance_definition = $this->instance_definition;
$instance_definition['entity_type'] = $entity_type;
entity_create('field_instance', $instance_definition)->save();
// Check that no initial cache entry is present.
$this->assertFalse(cache('field')->get($cid), 'Cached: no initial cache entry');
@ -553,4 +569,5 @@ class FieldAttachOtherTest extends FieldUnitTestBase {
$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');
}
}

View File

@ -16,6 +16,21 @@ use Drupal\Core\Language\Language;
* all hook_field_attach_pre_{load,insert,update}() hooks.
*/
class FieldAttachStorageTest extends FieldUnitTestBase {
/**
* The field instance.
*
* @var \Drupal\field\Plugin\Core\Entity\FieldInstance
*/
protected $instance;
/**
* Field name to use in the test.
*
* @var string
*/
protected $field_name;
public static function getInfo() {
return array(
'name' => 'Field attach tests (storage-related)',
@ -39,7 +54,7 @@ class FieldAttachStorageTest extends FieldUnitTestBase {
// Configure the instance so that we test hook_field_load() (see
// field_test_field_load() in field_test.module).
$this->instance['settings']['test_hook_field_load'] = TRUE;
field_update_instance($this->instance);
$this->instance->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
$entity_type = 'test_entity';
@ -117,11 +132,11 @@ class FieldAttachStorageTest extends FieldUnitTestBase {
);
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 = entity_create('field_entity', array('field_name' => $field_names[$i], 'type' => 'test_field'));
$field->save();
$field_ids[$i] = $field['uuid'];
foreach ($field_bundles_map[$i] as $bundle) {
$instance = array(
entity_create('field_instance', array(
'field_name' => $field_names[$i],
'entity_type' => 'test_entity',
'bundle' => $bundles[$bundle],
@ -130,8 +145,7 @@ class FieldAttachStorageTest extends FieldUnitTestBase {
// (see field_test_field_load() in field_test.module).
'test_hook_field_load' => TRUE,
),
);
field_create_instance($instance);
))->save();
}
}
@ -191,13 +205,13 @@ class FieldAttachStorageTest extends FieldUnitTestBase {
),
);
foreach ($fields as $field) {
field_create_field($field);
entity_create('field_entity', $field)->save();
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
);
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
}
$entity_init = field_test_create_entity();
@ -226,22 +240,19 @@ class FieldAttachStorageTest extends FieldUnitTestBase {
*/
function testFieldStorageDetailsAlter() {
$field_name = 'field_test_change_my_details';
$field = array(
$field = entity_create('field_entity', array(
'field_name' => $field_name,
'type' => 'test_field',
'cardinality' => 4,
'storage' => array('type' => 'field_test_storage'),
);
$field = field_create_field($field);
$instance = array(
));
$field->save();
$instance = entity_create('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']);
$instance = field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
));
$instance->save();
// The storage details are indexed by a storage engine type.
$this->assertTrue(array_key_exists('drupal_variables', $field['storage_details']), 'The storage type is Drupal variables.');
@ -345,7 +356,7 @@ class FieldAttachStorageTest extends FieldUnitTestBase {
function testFieldAttachSaveMissingDataDefaultValue() {
// Add a default value function.
$this->instance['default_value_function'] = 'field_test_default_value';
field_update_instance($this->instance);
$this->instance->save();
// Verify that fields are populated with default values.
$entity_type = 'test_entity';
@ -444,8 +455,8 @@ class FieldAttachStorageTest extends FieldUnitTestBase {
field_test_create_bundle($new_bundle);
// Add an instance to that bundle.
$this->instance['bundle'] = $new_bundle;
field_create_instance($this->instance);
$this->instance_definition['bundle'] = $new_bundle;
entity_create('field_instance', $this->instance_definition)->save();
// Save an entity with data in the field.
$entity = field_test_create_entity(0, 0, $this->instance['bundle']);
@ -483,13 +494,13 @@ class FieldAttachStorageTest extends FieldUnitTestBase {
field_test_create_bundle($new_bundle);
// Add an instance to that bundle.
$this->instance['bundle'] = $new_bundle;
field_create_instance($this->instance);
$this->instance_definition['bundle'] = $new_bundle;
entity_create('field_instance', $this->instance_definition)->save();
// Create a second field for the test bundle
$field_name = drupal_strtolower($this->randomName() . '_field_name');
$field = array('field_name' => $field_name, 'type' => 'test_field', 'cardinality' => 1);
field_create_field($field);
entity_create('field_entity', $field)->save();
$instance = array(
'field_name' => $field_name,
'entity_type' => 'test_entity',
@ -498,7 +509,7 @@ class FieldAttachStorageTest extends FieldUnitTestBase {
'description' => $this->randomName() . '_description',
'weight' => mt_rand(0, 127),
);
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
// Save an entity with data for both fields
$entity = field_test_create_entity(0, 0, $this->instance['bundle']);
@ -527,4 +538,5 @@ class FieldAttachStorageTest extends FieldUnitTestBase {
$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");
}
}

View File

@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\field\Tests\FieldInfoTest.
* Contains \Drupal\field\Tests\FieldInfoTest.
*/
namespace Drupal\field\Tests;
@ -54,11 +54,11 @@ class FieldInfoTest extends FieldUnitTestBase {
// Create a field, verify it shows up.
$core_fields = field_info_fields();
$field = array(
$field = entity_create('field_entity', array(
'field_name' => drupal_strtolower($this->randomName()),
'type' => 'test_field',
);
field_create_field($field);
));
$field->save();
$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');
@ -72,7 +72,7 @@ class FieldInfoTest extends FieldUnitTestBase {
$this->assertEqual($fields[$field['field_name']]['active'], TRUE, 'info fields contains active 1');
// Create an instance, verify that it shows up
$instance = array(
$instance_definition = array(
'field_name' => $field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
@ -80,14 +80,15 @@ class FieldInfoTest extends FieldUnitTestBase {
'description' => $this->randomName(),
'weight' => mt_rand(0, 127),
);
field_create_instance($instance);
$instance = entity_create('field_instance', $instance_definition);
$instance->save();
$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');
$this->assertTrue($instance_definition < $instances[$instance['field_name']], 'Instance appears in info correctly');
// Test a valid entity type but an invalid bundle.
$instances = field_info_instances('test_entity', 'invalid_bundle');
@ -127,7 +128,8 @@ class FieldInfoTest extends FieldUnitTestBase {
'field_name' => 'field',
'type' => 'test_field',
);
$field = field_create_field($field_definition);
$field = entity_create('field_entity', $field_definition);
$field->save();
// Simulate a stored field definition missing a field setting (e.g. a
// third-party module adding a new field setting has been enabled, and
@ -153,13 +155,14 @@ class FieldInfoTest extends FieldUnitTestBase {
'field_name' => 'field',
'type' => 'test_field',
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$instance_definition = array(
'field_name' => $field_definition['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
);
$instance = field_create_instance($instance_definition);
$instance = entity_create('field_instance', $instance_definition);
$instance->save();
// Simulate a stored instance definition missing various settings (e.g. a
// third-party module adding instance or widget settings has been enabled,
@ -189,13 +192,13 @@ class FieldInfoTest extends FieldUnitTestBase {
'field_name' => 'field',
'type' => 'test_field',
);
field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$instance_definition = array(
'field_name' => 'field',
'entity_type' => 'comment',
'bundle' => 'comment_node_article',
);
field_create_instance($instance_definition);
entity_create('field_instance', $instance_definition)->save();
// Disable coment module. This clears field_info cache.
module_disable(array('comment'));
@ -224,7 +227,7 @@ class FieldInfoTest extends FieldUnitTestBase {
),
);
foreach ($fields as $field) {
field_create_field($field);
entity_create('field_entity', $field)->save();
}
// Create a couple instances.
@ -251,7 +254,7 @@ class FieldInfoTest extends FieldUnitTestBase {
),
);
foreach ($instances as $instance) {
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
}
$expected = array(
@ -308,11 +311,11 @@ class FieldInfoTest extends FieldUnitTestBase {
// Create a test field and ensure it's in the array returned by
// field_info_fields().
$field_name = drupal_strtolower($this->randomName());
$field = array(
$field = entity_create('field_entity', array(
'field_name' => $field_name,
'type' => 'test_field',
);
field_create_field($field);
));
$field->save();
$fields = field_info_fields();
$this->assertTrue(isset($fields[$field_name]), 'The test field is initially found in the array returned by field_info_fields().');
@ -330,11 +333,10 @@ class FieldInfoTest extends FieldUnitTestBase {
* Test that the widget definition functions work.
*/
function testWidgetDefinition() {
$widget_definition = field_info_widget_types('test_field_widget_multiple');
// Test if hook_field_widget_info_alter is beïng called.
$this->assertTrue(in_array('test_field', $widget_definition['field_types']), "The 'test_field_widget_multiple' widget is enabled for the 'test_field' field type in field_test_field_widget_info_alter().");
}
}

View File

@ -2,18 +2,36 @@
/**
* @file
* Definition of Drupal\field\Tests\FieldInstanceCrudTest.
* Contains \Drupal\field\Tests\FieldInstanceCrudTest.
*/
namespace Drupal\field\Tests;
use Drupal\field\FieldException;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
class FieldInstanceCrudTest extends FieldUnitTestBase {
/**
* The field entity.
*
* @var \Drupal\field\Plugin\Core\Entity\Field
*/
protected $field;
/**
* The field entity definition.
*
* @var array
*/
protected $field_definition;
/**
* The field instance entity definition.
*
* @var array
*/
protected $instance_definition;
public static function getInfo() {
return array(
'name' => 'Field instance CRUD tests',
@ -25,11 +43,12 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
function setUp() {
parent::setUp();
$this->field = array(
$this->field_definition = array(
'field_name' => drupal_strtolower($this->randomName()),
'type' => 'test_field',
);
field_create_field($this->field);
$this->field = entity_create('field_entity', $this->field_definition);
$this->field->save();
$this->instance_definition = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
@ -47,14 +66,15 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
* Test the creation of a field instance.
*/
function testCreateFieldInstance() {
$instance = field_create_instance($this->instance_definition);
$instance = entity_create('field_instance', $this->instance_definition);
$instance->save();
// Read the configuration. Check against raw configuration data rather than
// the loaded ConfigEntity, to be sure we check that the defaults are
// applied on write.
$config = \Drupal::config('field.instance.' . $instance->id())->get();
$field_type = field_info_field_types($this->field['type']);
$field_type = field_info_field_types($this->field_definition['type']);
// Check that default values are set.
$this->assertEqual($config['required'], FALSE, 'Required defaults to false.');
@ -66,7 +86,7 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
// Guarantee that the field/bundle combination is unique.
try {
field_create_instance($this->instance_definition);
entity_create('field_instance', $this->instance_definition)->save();
$this->fail(t('Cannot create two instances with the same field / bundle combination.'));
}
catch (FieldException $e) {
@ -76,7 +96,7 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
// Check that the specified field exists.
try {
$this->instance_definition['field_name'] = $this->randomName();
field_create_instance($this->instance_definition);
entity_create('field_instance', $this->instance_definition)->save();
$this->fail(t('Cannot create an instance of a non-existing field.'));
}
catch (FieldException $e) {
@ -84,20 +104,21 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
}
// Create a field restricted to a specific entity type.
$field_restricted = array(
$field_restricted_definition = array(
'field_name' => drupal_strtolower($this->randomName()),
'type' => 'test_field',
'entity_types' => array('test_cacheable_entity'),
);
field_create_field($field_restricted);
$field_restricted = entity_create('field_entity', $field_restricted_definition);
$field_restricted->save();
// Check that an instance can be added to an entity type allowed
// by the field.
try {
$instance = $this->instance_definition;
$instance['field_name'] = $field_restricted['field_name'];
$instance['field_name'] = $field_restricted_definition['field_name'];
$instance['entity_type'] = 'test_cacheable_entity';
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
$this->pass(t('Can create an instance on an entity type allowed by the field.'));
}
catch (FieldException $e) {
@ -108,8 +129,8 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
// forbidden by the field.
try {
$instance = $this->instance_definition;
$instance['field_name'] = $field_restricted['field_name'];
field_create_instance($instance);
$instance['field_name'] = $field_restricted_definition['field_name'];
entity_create('field_instance', $instance)->save();
$this->fail(t('Cannot create an instance on an entity type forbidden by the field.'));
}
catch (FieldException $e) {
@ -123,7 +144,7 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
* Test reading back an instance definition.
*/
function testReadFieldInstance() {
field_create_instance($this->instance_definition);
entity_create('field_instance', $this->instance_definition)->save();
// Read the instance back.
$instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
@ -136,7 +157,7 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
* Test the update of a field instance.
*/
function testUpdateFieldInstance() {
field_create_instance($this->instance_definition);
entity_create('field_instance', $this->instance_definition)->save();
// Check that basic changes are saved.
$instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
@ -144,7 +165,7 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
$instance['label'] = $this->randomName();
$instance['description'] = $this->randomName();
$instance['settings']['test_instance_setting'] = $this->randomName();
field_update_instance($instance);
$instance->save();
$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');
@ -164,15 +185,15 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
// Create two instances for the same field so we can test that only one
// is deleted.
field_create_instance($this->instance_definition);
$this->another_instance_definition = $this->instance_definition;
$this->another_instance_definition['bundle'] .= '_another_bundle';
$instance = field_create_instance($this->another_instance_definition);
entity_create('field_instance', $this->instance_definition)->save();
$another_instance_definition = $this->instance_definition;
$another_instance_definition['bundle'] .= '_another_bundle';
entity_create('field_instance', $another_instance_definition)->save();
// Test that the first instance is not deleted, and then delete it.
$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->delete();
// Make sure the instance is marked as deleted when the instance is
// specifically loaded.
@ -184,14 +205,15 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
$this->assertTrue(empty($instance), 'A deleted field instance is not loaded by default.');
// Make sure the other field instance is not deleted.
$another_instance = field_read_instance('test_entity', $this->another_instance_definition['field_name'], $this->another_instance_definition['bundle']);
$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.');
// Make sure the field is deleted when its last instance is deleted.
field_delete_instance($another_instance);
$another_instance->delete();
$deleted_fields = \Drupal::state()->get('field.field.deleted');
$this->assertTrue(isset($deleted_fields[$another_instance['field_id']]), 'A deleted field is marked for deletion.');
$field = field_read_field($another_instance['field_name']);
$this->assertFalse($field, 'The field marked to be deleted is not found anymore in the configuration.');
}
}

View File

@ -54,12 +54,13 @@ abstract class FieldUnitTestBase extends DrupalUnitTestBase {
$field = 'field' . $suffix;
$field_id = 'field_id' . $suffix;
$instance = 'instance' . $suffix;
$instance_definition = 'instance_definition' . $suffix;
$this->$field_name = drupal_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->$field = entity_create('field_entity', array('field_name' => $this->$field_name, 'type' => 'test_field', 'cardinality' => 4));
$this->$field->save();
$this->$field_id = $this->{$field}['uuid'];
$this->$instance = array(
$this->$instance_definition = array(
'field_name' => $this->$field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
@ -70,7 +71,8 @@ abstract class FieldUnitTestBase extends DrupalUnitTestBase {
'test_instance_setting' => $this->randomName(),
),
);
field_create_instance($this->$instance);
$this->$instance = entity_create('field_instance', $this->$instance_definition);
$this->$instance->save();
entity_get_form_display('test_entity', 'test_bundle', 'default')
->setComponent($this->$field_name, array(

View File

@ -19,6 +19,34 @@ class FormTest extends FieldTestBase {
*/
public static $modules = array('node', 'field_test', 'options');
/**
* An array of values defining a field single.
*
* @var array
*/
protected $field_single;
/**
* An array of values defining a field multiple.
*
* @var array
*/
protected $field_multiple;
/**
* An array of values defining a field with unlimited cardinality.
*
* @var array
*/
protected $field_unlimited;
/**
* An array of values defining a field instance.
*
* @var array
*/
protected $instance;
public static function getInfo() {
return array(
'name' => 'Field form tests',
@ -50,13 +78,13 @@ class FormTest extends FieldTestBase {
}
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);
$field = $this->field_single;
$field_name = $field['field_name'];
$this->instance['field_name'] = $field_name;
entity_create('field_entity', $field)->save();
entity_create('field_instance', $this->instance)->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->setComponent($field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
@ -66,93 +94,93 @@ class FormTest extends FieldTestBase {
// Create token value expected for description.
$token_description = check_plain(config('system.site')->get('name')) . '_description';
$this->assertText($token_description, 'Token replacement for description is displayed');
$this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', 'Widget is displayed');
$this->assertNoField("{$this->field_name}[$langcode][1][value]", 'No extraneous widget is displayed');
$this->assertFieldByName("{$field_name}[$langcode][0][value]", '', 'Widget is displayed');
$this->assertNoField("{$field_name}[$langcode][1][value]", 'No extraneous widget is displayed');
// Check that hook_field_widget_form_alter() does not believe this is the
// default value form.
$this->assertNoText('From hook_field_widget_form_alter(): Default form is true.', 'Not default value form in hook_field_widget_form_alter().');
// Submit with invalid value (field-level validation).
$edit = array("{$this->field_name}[$langcode][0][value]" => -1);
$edit = array("{$field_name}[$langcode][0][value]" => -1);
$this->drupalPost(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.');
// TODO : check that the correct field is flagged for error.
// Create an entity
$value = mt_rand(1, 127);
$edit = array("{$this->field_name}[$langcode][0][value]" => $value);
$edit = array("{$field_name}[$langcode][0][value]" => $value);
$this->drupalPost(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->assertEqual($entity->{$field_name}[$langcode][0]['value'], $value, 'Field value was saved');
// Display edit form.
$this->drupalGet('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');
$this->assertFieldByName("{$field_name}[$langcode][0][value]", $value, 'Widget is displayed with the correct default value');
$this->assertNoField("{$field_name}[$langcode][1][value]", 'No extraneous widget is displayed');
// Update the entity.
$value = mt_rand(1, 127);
$edit = array("{$this->field_name}[$langcode][0][value]" => $value);
$edit = array("{$field_name}[$langcode][0][value]" => $value);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated');
$this->container->get('plugin.manager.entity')->getStorageController('test_entity')->resetCache(array($id));
$entity = field_test_entity_test_load($id);
$this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was updated');
$this->assertEqual($entity->{$field_name}[$langcode][0]['value'], $value, 'Field value was updated');
// Empty the field.
$value = '';
$edit = array("{$this->field_name}[$langcode][0][value]" => $value);
$edit = array("{$field_name}[$langcode][0][value]" => $value);
$this->drupalPost('test-entity/manage/' . $id . '/edit', $edit, t('Save'));
$this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated');
$this->container->get('plugin.manager.entity')->getStorageController('test_entity')->resetCache(array($id));
$entity = field_test_entity_test_load($id);
$this->assertIdentical($entity->{$this->field_name}, array(), 'Field was emptied');
$this->assertIdentical($entity->{$field_name}, array(), 'Field was emptied');
}
/**
* Tests field widget default values on entity forms.
*/
function testFieldFormDefaultValue() {
$this->field = $this->field_single;
$this->field_name = $this->field['field_name'];
$this->instance['field_name'] = $this->field_name;
$field = $this->field_single;
$field_name = $field['field_name'];
$this->instance['field_name'] = $field_name;
$default = rand(1, 127);
$this->instance['default_value'] = array(array('value' => $default));
field_create_field($this->field);
field_create_instance($this->instance);
entity_create('field_entity', $field)->save();
entity_create('field_instance', $this->instance)->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->setComponent($field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Display creation form.
$this->drupalGet('test-entity/add/test_bundle');
// Test that the default value is displayed correctly.
$this->assertFieldByXpath("//input[@name='{$this->field_name}[$langcode][0][value]' and @value='$default']");
$this->assertFieldByXpath("//input[@name='{$field_name}[$langcode][0][value]' and @value='$default']");
// Try to submit an empty value.
$edit = array("{$this->field_name}[$langcode][0][value]" => '');
$edit = array("{$field_name}[$langcode][0][value]" => '');
$this->drupalPost(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->assertTrue(empty($entity->{$this->field_name}), 'Field is now empty.');
$this->assertTrue(empty($entity->{$field_name}), 'Field is now empty.');
}
function testFieldFormSingleRequired() {
$this->field = $this->field_single;
$this->field_name = $this->field['field_name'];
$this->instance['field_name'] = $this->field_name;
$field = $this->field_single;
$field_name = $field['field_name'];
$this->instance['field_name'] = $field_name;
$this->instance['required'] = TRUE;
field_create_field($this->field);
field_create_instance($this->instance);
entity_create('field_entity', $field)->save();
entity_create('field_instance', $this->instance)->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->setComponent($field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
@ -163,50 +191,50 @@ class FormTest extends FieldTestBase {
// Create an entity
$value = mt_rand(1, 127);
$edit = array("{$this->field_name}[$langcode][0][value]" => $value);
$edit = array("{$field_name}[$langcode][0][value]" => $value);
$this->drupalPost(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->assertEqual($entity->{$field_name}[$langcode][0]['value'], $value, 'Field value was saved');
// Edit with missing required value.
$value = '';
$edit = array("{$this->field_name}[$langcode][0][value]" => $value);
$edit = array("{$field_name}[$langcode][0][value]" => $value);
$this->drupalPost('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 testFieldFormMultiple() {
// $this->field = $this->field_multiple;
// $this->field_name = $this->field['field_name'];
// $this->instance['field_name'] = $this->field_name;
// field_create_field($this->field);
// field_create_instance($this->instance);
// $field_name = $this->field['field_name'];
// $this->instance['field_name'] = $field_name;
// entity_create('field_entity', $this->field)->save();
// entity_create('field_instance', $this->instance)->save();
// }
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);
$field = $this->field_unlimited;
$field_name = $field['field_name'];
$this->instance['field_name'] = $field_name;
entity_create('field_entity', $field)->save();
entity_create('field_instance', $this->instance)->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->setComponent($field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Display creation form -> 1 widget.
$this->drupalGet('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->assertFieldByName("{$field_name}[$langcode][0][value]", '', 'Widget 1 is displayed');
$this->assertNoField("{$field_name}[$langcode][1][value]", 'No extraneous widget is displayed');
// Press 'add more' button -> 2 widgets.
$this->drupalPost(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->assertFieldByName("{$field_name}[$langcode][0][value]", '', 'Widget 1 is displayed');
$this->assertFieldByName("{$field_name}[$langcode][1][value]", '', 'New widget is displayed');
$this->assertNoField("{$field_name}[$langcode][2][value]", 'No extraneous widget is displayed');
// TODO : check that non-field inpurs are preserved ('title')...
// Yet another time so that we can play with more values -> 3 widgets.
@ -224,8 +252,8 @@ class FormTest extends FieldTestBase {
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;
$edit["{$field_name}[$langcode][$delta][value]"] = $value;
$edit["{$field_name}[$langcode][$delta][_weight]"] = $weight;
// We'll need three slightly different formats to check the values.
$values[$delta] = $value;
$weights[$delta] = $weight;
@ -236,15 +264,15 @@ class FormTest extends FieldTestBase {
// Press 'add more' button -> 4 widgets
$this->drupalPost(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");
$this->assertFieldByName("{$field_name}[$langcode][$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value");
$this->assertFieldByName("{$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->assertFieldByName("{$field_name}[$langcode][$delta][value]", '', "New widget is displayed");
$this->assertFieldByName("{$field_name}[$langcode][$delta][_weight]", $delta, "New widget has the right weight");
$this->assertNoField("{$field_name}[$langcode][" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');
// Submit the form and create the entity.
$this->drupalPost(NULL, $edit, t('Save'));
@ -254,7 +282,7 @@ class FormTest extends FieldTestBase {
$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');
$this->assertIdentical($entity->{$field_name}[$langcode], $field_values, 'Field values were saved in the correct order');
// Display edit form: check that the expected number of widgets is
// displayed, with correct values change values, reorder, leave an empty
@ -270,31 +298,31 @@ class FormTest extends FieldTestBase {
*/
function testFieldFormMultivalueWithRequiredRadio() {
// Create a multivalue test field.
$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);
$field = $this->field_unlimited;
$field_name = $field['field_name'];
$this->instance['field_name'] = $field_name;
entity_create('field_entity', $field)->save();
entity_create('field_instance', $this->instance)->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->setComponent($field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Add a required radio field.
field_create_field(array(
entity_create('field_entity', array(
'field_name' => 'required_radio_test',
'type' => 'list_text',
'settings' => array(
'allowed_values' => array('yes' => 'yes', 'no' => 'no'),
),
));
))->save();
$instance = array(
'field_name' => 'required_radio_test',
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'required' => TRUE,
);
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')
->setComponent($instance['field_name'], array(
'type' => 'options_buttons',
@ -311,19 +339,19 @@ class FormTest extends FieldTestBase {
$this->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed.');
// Verify that the widget is added.
$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->assertFieldByName("{$field_name}[$langcode][0][value]", '', 'Widget 1 is displayed');
$this->assertFieldByName("{$field_name}[$langcode][1][value]", '', 'New widget is displayed');
$this->assertNoField("{$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);
$field = $this->field_unlimited;
$field_name = $field['field_name'];
$this->instance['field_name'] = $field_name;
entity_create('field_entity', $field)->save();
entity_create('field_instance', $this->instance)->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->setComponent($field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
@ -348,8 +376,8 @@ class FormTest extends FieldTestBase {
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;
$edit["{$field_name}[$langcode][$delta][value]"] = $value;
$edit["{$field_name}[$langcode][$delta][_weight]"] = $weight;
// We'll need three slightly different formats to check the values.
$values[$delta] = $value;
$weights[$delta] = $weight;
@ -358,19 +386,19 @@ class FormTest extends FieldTestBase {
}
// Press 'add more' button through Ajax, and place the expected HTML result
// as the tested content.
$commands = $this->drupalPostAJAX(NULL, $edit, $this->field_name . '_add_more');
$commands = $this->drupalPostAJAX(NULL, $edit, $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");
$this->assertFieldByName("{$field_name}[$langcode][$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value");
$this->assertFieldByName("{$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->assertFieldByName("{$field_name}[$langcode][$delta][value]", '', "New widget is displayed");
$this->assertFieldByName("{$field_name}[$langcode][$delta][_weight]", $delta, "New widget has the right weight");
$this->assertNoField("{$field_name}[$langcode][" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');
}
/**
@ -379,13 +407,13 @@ class FormTest extends FieldTestBase {
function testFieldFormMultipleWidget() {
// Create a field with fixed cardinality and an instance using a multiple
// widget.
$this->field = $this->field_multiple;
$this->field_name = $this->field['field_name'];
$this->instance['field_name'] = $this->field_name;
field_create_field($this->field);
field_create_instance($this->instance);
$field = $this->field_multiple;
$field_name = $field['field_name'];
$this->instance['field_name'] = $field_name;
entity_create('field_entity', $field)->save();
entity_create('field_instance', $this->instance)->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name, array(
->setComponent($field_name, array(
'type' => 'test_field_widget_multiple',
))
->save();
@ -393,28 +421,28 @@ class FormTest extends FieldTestBase {
// Display creation form.
$this->drupalGet('test-entity/add/test_bundle');
$this->assertFieldByName("{$this->field_name}[$langcode]", '', 'Widget is displayed.');
$this->assertFieldByName("{$field_name}[$langcode]", '', 'Widget is displayed.');
// Create entity with three values.
$edit = array("{$this->field_name}[$langcode]" => '1, 2, 3');
$edit = array("{$field_name}[$langcode]" => '1, 2, 3');
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
// Check that the values were saved.
$entity_init = field_test_create_entity($id);
$this->assertFieldValues($entity_init, $this->field_name, $langcode, array(1, 2, 3));
$this->assertFieldValues($entity_init, $field_name, $langcode, array(1, 2, 3));
// Display the form, check that the values are correctly filled in.
$this->drupalGet('test-entity/manage/' . $id . '/edit');
$this->assertFieldByName("{$this->field_name}[$langcode]", '1, 2, 3', 'Widget is displayed.');
$this->assertFieldByName("{$field_name}[$langcode]", '1, 2, 3', 'Widget is displayed.');
// Submit the form with more values than the field accepts.
$edit = array("{$this->field_name}[$langcode]" => '1, 2, 3, 4, 5');
$edit = array("{$field_name}[$langcode]" => '1, 2, 3, 4, 5');
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertRaw('this field cannot hold more than 4 values', 'Form validation failed.');
// Check that the field values were not submitted.
$this->assertFieldValues($entity_init, $this->field_name, $langcode, array(1, 2, 3));
$this->assertFieldValues($entity_init, $field_name, $langcode, array(1, 2, 3));
}
/**
@ -426,8 +454,8 @@ class FormTest extends FieldTestBase {
$field_name = $field['field_name'];
$instance = $this->instance;
$instance['field_name'] = $field_name;
field_create_field($field);
field_create_instance($instance);
entity_create('field_entity', $field)->save();
entity_create('field_instance', $instance)->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($field_name)
->save();
@ -444,8 +472,8 @@ class FormTest extends FieldTestBase {
'bundle' => 'test_bundle',
'default_value' => array(0 => array('value' => 99)),
);
field_create_field($field_no_access);
field_create_instance($instance_no_access);
entity_create('field_entity', $field_no_access)->save();
entity_create('field_instance', $instance_no_access)->save();
entity_get_form_display($instance_no_access['entity_type'], $instance_no_access['bundle'], 'default')
->setComponent($field_name_no_access)
->save();
@ -501,17 +529,17 @@ class FormTest extends FieldTestBase {
*/
function testNestedFieldForm() {
// Add two instances on the 'test_bundle'
field_create_field($this->field_single);
field_create_field($this->field_unlimited);
entity_create('field_entity', $this->field_single)->save();
entity_create('field_entity', $this->field_unlimited)->save();
$this->instance['field_name'] = 'field_single';
$this->instance['label'] = 'Single field';
field_create_instance($this->instance);
entity_create('field_instance', $this->instance)->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'])
->save();
$this->instance['field_name'] = 'field_unlimited';
$this->instance['label'] = 'Unlimited field';
field_create_instance($this->instance);
entity_create('field_instance', $this->instance)->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'])
->save();
@ -615,12 +643,13 @@ class FormTest extends FieldTestBase {
* Tests the Hidden widget.
*/
function testFieldFormHiddenWidget() {
$this->field = $this->field_single;
$this->field_name = $this->field['field_name'];
$this->instance['field_name'] = $this->field_name;
$field = $this->field_single;
$field_name = $field['field_name'];
$this->instance['field_name'] = $field_name;
$this->instance['default_value'] = array(0 => array('value' => 99));
field_create_field($this->field);
field_create_instance($this->instance);
entity_create('field_entity', $field)->save();
$this->instance = entity_create('field_instance', $this->instance);
$this->instance->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'], array(
'type' => 'hidden',
@ -633,18 +662,18 @@ class FormTest extends FieldTestBase {
// Create an entity and test that the default value is assigned correctly to
// the field that uses the hidden widget.
$this->assertNoField("{$this->field_name}[$langcode][0][value]", 'The hidden widget is not displayed');
$this->assertNoField("{$field_name}[$langcode][0][value]", 'The hidden widget is not displayed');
$this->drupalPost(NULL, array(), 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'], 99, 'Default value was saved');
$this->assertEqual($entity->{$field_name}[$langcode][0]['value'], 99, 'Default value was saved');
// Update the instance to remove the default value and switch to the
// default widget.
$this->instance['default_value'] = NULL;
field_update_instance($this->instance);
$this->instance->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'], array(
'type' => 'test_field_widget',
@ -653,16 +682,16 @@ class FormTest extends FieldTestBase {
// Display edit form.
$this->drupalGet('test-entity/manage/' . $id . '/edit');
$this->assertFieldByName("{$this->field_name}[$langcode][0][value]", 99, 'Widget is displayed with the correct default value');
$this->assertFieldByName("{$field_name}[$langcode][0][value]", 99, 'Widget is displayed with the correct default value');
// Update the entity.
$value = mt_rand(1, 127);
$edit = array("{$this->field_name}[$langcode][0][value]" => $value);
$edit = array("{$field_name}[$langcode][0][value]" => $value);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated');
entity_get_controller('test_entity')->resetCache(array($id));
$entity = field_test_entity_test_load($id);
$this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was updated');
$this->assertEqual($entity->{$field_name}[$langcode][0]['value'], $value, 'Field value was updated');
// Update the form display and switch to the Hidden widget again.
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
@ -678,7 +707,7 @@ class FormTest extends FieldTestBase {
// Check that the expected value has been carried over to the new revision.
entity_get_controller('test_entity')->resetCache(array($id));
$entity = field_test_entity_test_load($id);
$this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'New revision has the expected value for the field with the Hidden widget');
$this->assertEqual($entity->{$field_name}[$langcode][0]['value'], $value, 'New revision has the expected value for the field with the Hidden widget');
}
/**
@ -697,4 +726,5 @@ class FormTest extends FieldTestBase {
$this->assertEqual($values[$key][$column], $value, format_string('Value @value was saved correctly.', array('@value' => $value)));
}
}
}

View File

@ -22,6 +22,13 @@ class ShapeItemTest extends FieldUnitTestBase {
*/
public static $modules = array('field_test');
/**
* The name of the field to use in this test.
*
* @var string
*/
protected $field_name = 'field_shape';
public static function getInfo() {
return array(
'name' => 'Shape field item',
@ -34,17 +41,17 @@ class ShapeItemTest extends FieldUnitTestBase {
parent::setUp();
// Create an field field and instance for validation.
$this->field = array(
'field_name' => 'field_shape',
$field = array(
'field_name' => $this->field_name,
'type' => 'shape',
);
field_create_field($this->field);
$this->instance = array(
entity_create('field_entity', $field)->save();
$instance = array(
'entity_type' => 'entity_test',
'field_name' => 'field_shape',
'field_name' => $this->field_name,
'bundle' => 'entity_test',
);
field_create_instance($this->instance);
entity_create('field_instance', $instance)->save();
}
/**
@ -55,34 +62,34 @@ class ShapeItemTest extends FieldUnitTestBase {
$entity = entity_create('entity_test', array());
$shape = 'cube';
$color = 'blue';
$entity->field_shape->shape = $shape;
$entity->field_shape->color = $color;
$entity->{$this->field_name}->shape = $shape;
$entity->{$this->field_name}->color = $color;
$entity->name->value = $this->randomName();
$entity->save();
// Verify entity has been created properly.
$id = $entity->id();
$entity = entity_load('entity_test', $id);
$this->assertTrue($entity->field_shape instanceof FieldInterface, 'Field implements interface.');
$this->assertTrue($entity->field_shape[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_shape->shape, $shape);
$this->assertEqual($entity->field_shape->color, $color);
$this->assertEqual($entity->field_shape[0]->shape, $shape);
$this->assertEqual($entity->field_shape[0]->color, $color);
$this->assertTrue($entity->{$this->field_name} instanceof FieldInterface, 'Field implements interface.');
$this->assertTrue($entity->{$this->field_name}[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->{$this->field_name}->shape, $shape);
$this->assertEqual($entity->{$this->field_name}->color, $color);
$this->assertEqual($entity->{$this->field_name}[0]->shape, $shape);
$this->assertEqual($entity->{$this->field_name}[0]->color, $color);
// Verify changing the field value.
$new_shape = 'circle';
$new_color = 'red';
$entity->field_shape->shape = $new_shape;
$entity->field_shape->color = $new_color;
$this->assertEqual($entity->field_shape->shape, $new_shape);
$this->assertEqual($entity->field_shape->color, $new_color);
$entity->{$this->field_name}->shape = $new_shape;
$entity->{$this->field_name}->color = $new_color;
$this->assertEqual($entity->{$this->field_name}->shape, $new_shape);
$this->assertEqual($entity->{$this->field_name}->color, $new_color);
// Read changed entity and assert changed values.
$entity->save();
$entity = entity_load('entity_test', $id);
$this->assertEqual($entity->field_shape->shape, $new_shape);
$this->assertEqual($entity->field_shape->color, $new_color);
$this->assertEqual($entity->{$this->field_name}->shape, $new_shape);
$this->assertEqual($entity->{$this->field_name}->color, $new_color);
}
}

View File

@ -22,6 +22,13 @@ class TestItemTest extends FieldUnitTestBase {
*/
public static $modules = array('field_test');
/**
* The name of the field to use in this test.
*
* @var string
*/
protected $field_name = 'field_test';
public static function getInfo() {
return array(
'name' => 'Test field item',
@ -34,17 +41,17 @@ class TestItemTest extends FieldUnitTestBase {
parent::setUp();
// Create an field field and instance for validation.
$this->field = array(
'field_name' => 'field_test',
$field = array(
'field_name' => $this->field_name,
'type' => 'test_field',
);
field_create_field($this->field);
$this->instance = array(
entity_create('field_entity', $field)->save();
$instance = array(
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'field_name' => $this->field_name,
'bundle' => 'entity_test',
);
field_create_instance($this->instance);
entity_create('field_instance', $instance)->save();
}
/**
@ -61,20 +68,20 @@ class TestItemTest extends FieldUnitTestBase {
// Verify entity has been created properly.
$id = $entity->id();
$entity = entity_load('entity_test', $id);
$this->assertTrue($entity->field_test instanceof FieldInterface, 'Field implements interface.');
$this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_test->value, $value);
$this->assertEqual($entity->field_test[0]->value, $value);
$this->assertTrue($entity->{$this->field_name} instanceof FieldInterface, 'Field implements interface.');
$this->assertTrue($entity->{$this->field_name}[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->{$this->field_name}->value, $value);
$this->assertEqual($entity->{$this->field_name}[0]->value, $value);
// Verify changing the field value.
$new_value = rand(1, 10);
$entity->field_test->value = $new_value;
$this->assertEqual($entity->field_test->value, $new_value);
$this->assertEqual($entity->{$this->field_name}->value, $new_value);
// Read changed entity and assert changed values.
$entity->save();
$entity = entity_load('entity_test', $id);
$this->assertEqual($entity->field_test->value, $new_value);
$this->assertEqual($entity->{$this->field_name}->value, $new_value);
}
}

View File

@ -26,6 +26,49 @@ class TranslationTest extends FieldUnitTestBase {
*/
public static $modules = array('language', 'node');
/**
* The name of the field to use in this test.
*
* @var string
*/
protected $field_name;
/**
* The name of the entity type to use in this test.
*
* @var string
*/
protected $entity_type = 'test_entity';
/**
* An array defining the field to use in this test.
*
* @var array
*/
protected $field_definition;
/**
* An array defining the field instance to use in this test.
*
* @var array
*/
protected $instance_definition;
/**
* The field to use in this test.
*
* @var \Drupal\field\Plugin\Core\Entity\Field
*/
protected $field;
/**
* The field instance to use in this test.
*
* @var \Drupal\field\Plugin\Core\Entity\FieldInstance
*/
protected $instance;
public static function getInfo() {
return array(
'name' => 'Field translations tests',
@ -41,15 +84,13 @@ class TranslationTest extends FieldUnitTestBase {
$this->field_name = drupal_strtolower($this->randomName() . '_field_name');
$this->entity_type = 'test_entity';
$this->field_definition = array(
'field_name' => $this->field_name,
'type' => 'test_field',
'cardinality' => 4,
'translatable' => TRUE,
);
field_create_field($this->field_definition);
entity_create('field_entity', $this->field_definition)->save();
$this->field = field_read_field($this->field_name);
$this->instance_definition = array(
@ -57,7 +98,7 @@ class TranslationTest extends FieldUnitTestBase {
'entity_type' => $this->entity_type,
'bundle' => 'test_bundle',
);
field_create_instance($this->instance_definition);
entity_create('field_instance', $this->instance_definition)->save();
$this->instance = field_read_instance('test_entity', $this->field_name, 'test_bundle');
for ($i = 0; $i < 3; ++$i) {
@ -95,7 +136,7 @@ class TranslationTest extends FieldUnitTestBase {
// Test field_available_languages() behavior for untranslatable fields.
$this->field['translatable'] = FALSE;
field_update_field($this->field);
$this->field->save();
$available_langcodes = field_available_languages($this->entity_type, $this->field);
$this->assertTrue(count($available_langcodes) == 1 && $available_langcodes[0] === Language::LANGCODE_NOT_SPECIFIED, 'For untranslatable fields only Language::LANGCODE_NOT_SPECIFIED is available.');
}
@ -107,7 +148,6 @@ class TranslationTest extends FieldUnitTestBase {
// Enable field translations for the entity.
field_test_entity_info_translatable('test_entity', TRUE);
$entity_type = 'test_entity';
$entity = field_test_create_entity(0, 0, $this->instance['bundle']);
// Populate some extra languages to check if _field_invoke() correctly uses
@ -251,12 +291,13 @@ class TranslationTest extends FieldUnitTestBase {
$field_name_default = drupal_strtolower($this->randomName() . '_field_name');
$field_definition = $this->field_definition;
$field_definition['field_name'] = $field_name_default;
$field = field_create_field($field_definition);
entity_create('field_entity', $field_definition)->save();
$instance_definition = $this->instance_definition;
$instance_definition['field_name'] = $field_name_default;
$instance_definition['default_value'] = array(array('value' => rand(1, 127)));
$instance = field_create_instance($instance_definition);
$instance = entity_create('field_instance', $instance_definition);
$instance->save();
$translation_langcodes = array_slice($available_langcodes, 0, 2);
asort($translation_langcodes);
@ -310,14 +351,14 @@ class TranslationTest extends FieldUnitTestBase {
'cardinality' => 2,
'translatable' => TRUE,
);
field_create_field($field);
entity_create('field_entity', $field)->save();
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => $entity_type,
'bundle' => 'test_bundle',
);
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
$entity = field_test_create_entity(1, 1, $this->instance['bundle']);
$instances = field_info_instances($entity_type, $this->instance['bundle']);
@ -387,4 +428,5 @@ class TranslationTest extends FieldUnitTestBase {
$display_langcode = field_language($entity, $this->field_name, $requested_langcode);
$this->assertEqual($display_langcode, $requested_langcode, 'Display language behave correctly when language fallback is disabled');
}
}

View File

@ -21,6 +21,34 @@ class TranslationWebTest extends FieldTestBase {
*/
public static $modules = array('language', 'field_test');
/**
* The name of the field to use in this test.
*
* @var string
*/
protected $field_name;
/**
* The name of the entity type to use in this test.
*
* @var string
*/
protected $entity_type = 'test_entity';
/**
* The field to use in this test.
*
* @var \Drupal\field\Plugin\Core\Entity\Field
*/
protected $field;
/**
* The field instance to use in this test.
*
* @var \Drupal\field\Plugin\Core\Entity\FieldInstance
*/
protected $instance;
public static function getInfo() {
return array(
'name' => 'Field translations web tests',
@ -34,15 +62,13 @@ class TranslationWebTest extends FieldTestBase {
$this->field_name = drupal_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);
entity_create('field_entity', $field)->save();
$this->field = field_read_field($this->field_name);
$instance = array(
@ -50,7 +76,7 @@ class TranslationWebTest extends FieldTestBase {
'entity_type' => $this->entity_type,
'bundle' => 'test_bundle',
);
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
$this->instance = field_read_instance('test_entity', $this->field_name, 'test_bundle');
entity_get_form_display($this->entity_type, 'test_bundle', 'default')

View File

@ -36,7 +36,7 @@ class ApiDataTest extends FieldTestBase {
'entity_type' => 'node',
'bundle' => 'page',
);
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
// The second one will be attached to users only.
$instance = array(
@ -44,7 +44,7 @@ class ApiDataTest extends FieldTestBase {
'entity_type' => 'user',
'bundle' => 'user',
);
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
// The third will be attached to both nodes and users.
$instance = array(
@ -52,13 +52,13 @@ class ApiDataTest extends FieldTestBase {
'entity_type' => 'node',
'bundle' => 'page',
);
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
$instance = array(
'field_name' => $field_names[2],
'entity_type' => 'user',
'bundle' => 'user',
);
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
// Now create some example nodes/users for the view result.
for ($i = 0; $i < 5; $i++) {
@ -66,7 +66,7 @@ class ApiDataTest extends FieldTestBase {
'field_name_0' => array((array('value' => $this->randomName()))),
'field_name_2' => array((array('value' => $this->randomName()))),
);
$this->nodes[] = $this->drupalCreateNode($edit);
$nodes[] = $this->drupalCreateNode($edit);
}
$this->container->get('views.views_data')->clear();

View File

@ -35,12 +35,15 @@ abstract class FieldTestBase extends ViewTestBase {
/**
* Stores the field definitions used by the test.
*
* @var array
*/
public $fields;
/**
* Stores the instances of the fields. They have
* the same keys as the fields.
*
* @var array
*/
public $instances;
@ -58,7 +61,8 @@ abstract class FieldTestBase extends ViewTestBase {
$field_names[$i] = 'field_name_' . $i;
$field = array('field_name' => $field_names[$i], 'type' => 'text');
$this->fields[$i] = $field = field_create_field($field);
$this->fields[$i] = $field = entity_create('field_entity', $field);
$field->save();
}
return $field_names;
}
@ -70,7 +74,8 @@ abstract class FieldTestBase extends ViewTestBase {
'entity_type' => 'node',
'bundle' => 'page',
);
$this->instances[$key] = field_create_instance($instance);
$this->instances[$key] = entity_create('field_instance', $instance);
$this->instances[$key]->save();
}
}

View File

@ -47,9 +47,11 @@ class HandlerFieldFieldTest extends FieldTestBase {
$this->setUpFields(3);
// Setup a field with cardinality > 1.
$this->fields[3] = $field = field_create_field(array('field_name' => 'field_name_3', 'type' => 'text', 'cardinality' => FIELD_CARDINALITY_UNLIMITED));
$this->fields[3] = $field = entity_create('field_entity', array('field_name' => 'field_name_3', 'type' => 'text', 'cardinality' => FIELD_CARDINALITY_UNLIMITED));
$field->save();
// Setup a field that will have no value.
$this->fields[4] = $field = field_create_field(array('field_name' => 'field_name_4', 'type' => 'text', 'cardinality' => FIELD_CARDINALITY_UNLIMITED));
$this->fields[4] = $field = entity_create('field_entity', array('field_name' => 'field_name_4', 'type' => 'text', 'cardinality' => FIELD_CARDINALITY_UNLIMITED));
$field->save();
$this->setUpInstances();

View File

@ -147,7 +147,7 @@ function field_test_field_language_alter(&$display_langcode, $context) {
* field_test_memorize();
*
* // call some Field API functions that invoke field_test hooks
* $field = field_create_field(...);
* entity_create('field_entity', $field_definition)->save();
*
* // retrieve and reset the memorized hook call data
* $mem = field_test_memorize();