View source
<?php
namespace Drupal\field\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\field\Entity\FieldConfig;
class FieldAttachStorageTest extends FieldUnitTestBase {
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test_rev');
}
function testFieldAttachSaveLoad() {
$entity_type = 'entity_test_rev';
$this
->createFieldWithStorage('', $entity_type);
$cardinality = $this->fieldTestData->field_storage
->getCardinality();
$values = array();
$entity = entity_create($entity_type);
for ($revision_id = 0; $revision_id < 3; $revision_id++) {
$current_values = $this
->_generateTestFieldValues($cardinality + 1);
$entity->{$this->fieldTestData->field_name}
->setValue($current_values);
$entity
->setNewRevision();
$entity
->save();
$entity_id = $entity
->id();
$current_revision = $entity
->getRevisionId();
$values[$current_revision] = $current_values;
}
$storage = $this->container
->get('entity.manager')
->getStorage($entity_type);
$storage
->resetCache();
$entity = $storage
->load($entity_id);
$this
->assertEqual(count($entity->{$this->fieldTestData->field_name}), $cardinality, 'Current revision: expected number of values');
for ($delta = 0; $delta < $cardinality; $delta++) {
$this
->assertEqual($entity->{$this->fieldTestData->field_name}[$delta]->value, $values[$current_revision][$delta]['value'], format_string('Current revision: expected value %delta was found.', array(
'%delta' => $delta,
)));
}
foreach (array_keys($values) as $revision_id) {
$entity = $storage
->loadRevision($revision_id);
$this
->assertEqual(count($entity->{$this->fieldTestData->field_name}), $cardinality, format_string('Revision %revision_id: expected number of values.', array(
'%revision_id' => $revision_id,
)));
for ($delta = 0; $delta < $cardinality; $delta++) {
$this
->assertEqual($entity->{$this->fieldTestData->field_name}[$delta]->value, $values[$revision_id][$delta]['value'], format_string('Revision %revision_id: expected value %delta was found.', array(
'%revision_id' => $revision_id,
'%delta' => $delta,
)));
}
}
}
function testFieldAttachLoadMultiple() {
$entity_type = 'entity_test_rev';
$bundles = array(
1 => 'test_bundle_1',
2 => 'test_bundle_2',
);
entity_test_create_bundle($bundles[1]);
entity_test_create_bundle($bundles[2]);
$field_bundles_map = array(
1 => array(
1,
2,
),
2 => array(
1,
),
3 => array(
2,
),
);
for ($i = 1; $i <= 3; $i++) {
$field_names[$i] = 'field_' . $i;
$field_storage = entity_create('field_storage_config', array(
'field_name' => $field_names[$i],
'entity_type' => $entity_type,
'type' => 'test_field',
));
$field_storage
->save();
$field_ids[$i] = $field_storage
->uuid();
foreach ($field_bundles_map[$i] as $bundle) {
entity_create('field_config', array(
'field_name' => $field_names[$i],
'entity_type' => $entity_type,
'bundle' => $bundles[$bundle],
))
->save();
}
}
foreach ($bundles as $index => $bundle) {
$entities[$index] = entity_create($entity_type, array(
'id' => $index,
'revision_id' => $index,
'type' => $bundle,
));
$entity = clone $entities[$index];
foreach ($field_names as $field_name) {
if (!$entity
->hasField($field_name)) {
continue;
}
$values[$index][$field_name] = mt_rand(1, 127);
$entity->{$field_name}
->setValue(array(
'value' => $values[$index][$field_name],
));
}
$entity
->enforceIsnew();
$entity
->save();
}
$controller = \Drupal::entityManager()
->getStorage($entity
->getEntityTypeId());
$controller
->resetCache();
$entities = $controller
->loadMultiple();
foreach ($entities as $index => $entity) {
foreach ($field_names as $field_name) {
if (!$entity
->hasField($field_name)) {
continue;
}
$this
->assertEqual($entity->{$field_name}->value, $values[$index][$field_name], format_string('Entity %index: expected value was found.', array(
'%index' => $index,
)));
}
}
}
function testFieldAttachSaveEmptyData() {
$entity_type = 'entity_test';
$this
->createFieldWithStorage('', $entity_type);
$entity_init = entity_create($entity_type, array(
'id' => 1,
));
$entity = clone $entity_init;
$entity->{$this->fieldTestData->field_name} = NULL;
$entity
->enforceIsNew();
$entity = $this
->entitySaveReload($entity);
$this
->assertTrue($entity->{$this->fieldTestData->field_name}
->isEmpty(), 'Insert: NULL field results in no value saved');
$entity_init
->enforceIsNew(FALSE);
$entity = clone $entity_init;
$values = $this
->_generateTestFieldValues(1);
$entity->{$this->fieldTestData->field_name} = $values;
$entity = $this
->entitySaveReload($entity);
$this
->assertEqual($entity->{$this->fieldTestData->field_name}
->getValue(), $values, 'Field data saved');
$entity = clone $entity_init;
$entity->{$this->fieldTestData->field_name} = NULL;
$entity = $this
->entitySaveReload($entity);
$this
->assertTrue($entity->{$this->fieldTestData->field_name}
->isEmpty(), 'Update: NULL field removes existing values');
$entity = clone $entity_init;
$values = $this
->_generateTestFieldValues(1);
$entity->{$this->fieldTestData->field_name} = $values;
$entity = $this
->entitySaveReload($entity);
$this
->assertEqual($entity->{$this->fieldTestData->field_name}
->getValue(), $values, 'Field data saved');
$entity = clone $entity_init;
$entity->{$this->fieldTestData->field_name} = array();
$entity = $this
->entitySaveReload($entity);
$this
->assertTrue($entity->{$this->fieldTestData->field_name}
->isEmpty(), 'Update: empty array removes existing values');
}
function testFieldAttachSaveEmptyDataDefaultValue() {
$entity_type = 'entity_test_rev';
$this
->createFieldWithStorage('', $entity_type);
$this->fieldTestData->field
->set('default_value_callback', 'field_test_default_value');
$this->fieldTestData->field
->save();
$entity_init = entity_create($entity_type, array(
'id' => 1,
'revision_id' => 1,
));
$default = field_test_default_value($entity_init, $this->fieldTestData->field);
$this
->assertEqual($entity_init->{$this->fieldTestData->field_name}
->getValue(), $default, 'Default field value correctly populated.');
$entity = clone $entity_init;
$entity->{$this->fieldTestData->field_name} = NULL;
$entity
->enforceIsNew();
$entity = $this
->entitySaveReload($entity);
$this
->assertTrue($entity->{$this->fieldTestData->field_name}
->isEmpty(), 'Insert: NULL field results in no value saved');
$value = array(
array(
'value' => $default[0]['value'] - mt_rand(1, 127),
),
);
$entity = entity_create($entity_type, array(
'type' => $entity_init
->bundle(),
$this->fieldTestData->field_name => $value,
));
$this
->assertEqual($entity->{$this->fieldTestData->field_name}
->getValue(), $value, 'Prepopulated field value correctly maintained.');
}
function testFieldAttachDelete() {
$entity_type = 'entity_test_rev';
$this
->createFieldWithStorage('', $entity_type);
$cardinality = $this->fieldTestData->field_storage
->getCardinality();
$entity = entity_create($entity_type, array(
'type' => $this->fieldTestData->field
->getTargetBundle(),
));
$vids = array();
$values = $this
->_generateTestFieldValues($cardinality);
$entity->{$this->fieldTestData->field_name} = $values;
$entity
->save();
$vids[] = $entity
->getRevisionId();
$entity
->setNewRevision();
$entity
->save();
$vids[] = $entity
->getRevisionId();
$entity
->setNewRevision();
$entity
->save();
$vids[] = $entity
->getRevisionId();
$controller = $this->container
->get('entity.manager')
->getStorage($entity
->getEntityTypeId());
$controller
->resetCache();
foreach ($vids as $vid) {
$revision = $controller
->loadRevision($vid);
$this
->assertEqual(count($revision->{$this->fieldTestData->field_name}), $cardinality, "The test entity revision {$vid} has {$cardinality} values.");
}
$controller
->deleteRevision($vids[1]);
$controller
->resetCache();
foreach (array(
0,
2,
) as $key) {
$vid = $vids[$key];
$revision = $controller
->loadRevision($vid);
$this
->assertEqual(count($revision->{$this->fieldTestData->field_name}), $cardinality, "The test entity revision {$vid} has {$cardinality} values.");
}
$controller
->resetCache();
$current = $controller
->load($entity
->id());
$this
->assertEqual(count($current->{$this->fieldTestData->field_name}), $cardinality, "The test entity current revision has {$cardinality} values.");
$entity
->delete();
$controller
->resetCache();
foreach (array(
0,
1,
2,
) as $vid) {
$revision = $controller
->loadRevision($vid);
$this
->assertFalse($revision);
}
$this
->assertFalse($controller
->load($entity
->id()));
}
function testEntityCreateBundle() {
$entity_type = 'entity_test_rev';
$this
->createFieldWithStorage('', $entity_type);
$cardinality = $this->fieldTestData->field_storage
->getCardinality();
$new_bundle = 'test_bundle_' . Unicode::strtolower($this
->randomMachineName());
entity_test_create_bundle($new_bundle, NULL, $entity_type);
$this->fieldTestData->field_definition['bundle'] = $new_bundle;
entity_create('field_config', $this->fieldTestData->field_definition)
->save();
$entity = entity_create($entity_type, array(
'type' => $this->fieldTestData->field
->getTargetBundle(),
));
$values = $this
->_generateTestFieldValues($cardinality);
$entity->{$this->fieldTestData->field_name} = $values;
$entity = $this
->entitySaveReload($entity);
$this
->assertEqual(count($entity->{$this->fieldTestData->field_name}), $cardinality, "Data is retrieved for the new bundle");
}
function testEntityDeleteBundle() {
$entity_type = 'entity_test_rev';
$this
->createFieldWithStorage('', $entity_type);
$new_bundle = 'test_bundle_' . Unicode::strtolower($this
->randomMachineName());
entity_test_create_bundle($new_bundle, NULL, $entity_type);
$this->fieldTestData->field_definition['bundle'] = $new_bundle;
entity_create('field_config', $this->fieldTestData->field_definition)
->save();
$field_name = Unicode::strtolower($this
->randomMachineName() . '_field_name');
$field_storage = array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'type' => 'test_field',
'cardinality' => 1,
);
entity_create('field_storage_config', $field_storage)
->save();
$field = array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $this->fieldTestData->field
->getTargetBundle(),
'label' => $this
->randomMachineName() . '_label',
'description' => $this
->randomMachineName() . '_description',
'weight' => mt_rand(0, 127),
);
entity_create('field_config', $field)
->save();
$entity = entity_create($entity_type, array(
'type' => $this->fieldTestData->field
->getTargetBundle(),
));
$values = $this
->_generateTestFieldValues($this->fieldTestData->field_storage
->getCardinality());
$entity->{$this->fieldTestData->field_name} = $values;
$entity->{$field_name} = $this
->_generateTestFieldValues(1);
$entity = $this
->entitySaveReload($entity);
$this
->assertEqual(count($entity->{$this->fieldTestData->field_name}), 4, 'First field got loaded');
$this
->assertEqual(count($entity->{$field_name}), 1, 'Second field got loaded');
entity_test_delete_bundle($this->fieldTestData->field
->getTargetBundle(), $entity_type);
$controller = $this->container
->get('entity.manager')
->getStorage($entity
->getEntityTypeId());
$controller
->resetCache();
$entity = $controller
->load($entity
->id());
$this
->assertTrue(empty($entity->{$this->fieldTestData->field_name}), 'No data for first field');
$this
->assertTrue(empty($entity->{$field_name}), 'No data for second field');
$this
->assertFalse(FieldConfig::load('entity_test.' . $this->fieldTestData->field
->getTargetBundle() . '.' . $this->fieldTestData->field_name), "First field is deleted");
$this
->assertFalse(FieldConfig::load('entity_test.' . $field['bundle'] . '.' . $field_name), "Second field is deleted");
}
}