View source
<?php
namespace Drupal\Tests\field\Kernel;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Field\FieldException;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
class FieldCrudTest extends FieldKernelTestBase {
protected $fieldStorage;
protected $fieldStorageDefinition;
protected $fieldDefinition;
public function setUp() {
parent::setUp();
$this->fieldStorageDefinition = [
'field_name' => mb_strtolower($this
->randomMachineName()),
'entity_type' => 'entity_test',
'type' => 'test_field',
];
$this->fieldStorage = FieldStorageConfig::create($this->fieldStorageDefinition);
$this->fieldStorage
->save();
$this->fieldDefinition = [
'field_name' => $this->fieldStorage
->getName(),
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
];
}
public function testCreateField() {
$field = FieldConfig::create($this->fieldDefinition);
$field
->save();
$field = FieldConfig::load($field
->id());
$this
->assertEquals('TRUE', $field
->getSetting('field_setting_from_config_data'));
$this
->assertNull($field
->getSetting('config_data_from_field_setting'));
$config = $this
->config('field.field.' . $field
->id())
->get();
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
$this
->assertTrue($config['settings']['config_data_from_field_setting']);
$this
->assertTrue(!isset($config['settings']['field_setting_from_config_data']));
unset($config['settings']['config_data_from_field_setting']);
$this
->assertEqual($config['required'], FALSE, 'Required defaults to false.');
$this
->assertIdentical($config['label'], $this->fieldDefinition['field_name'], 'Label defaults to field name.');
$this
->assertIdentical($config['description'], '', 'Description defaults to empty string.');
$this
->assertEqual($config['settings'], $field_type_manager
->getDefaultFieldSettings($this->fieldStorageDefinition['type']), 'Default field settings have been written.');
$this
->assertEqual($config['field_type'], $this->fieldStorageDefinition['type']);
try {
FieldConfig::create($this->fieldDefinition)
->save();
$this
->fail('Cannot create two fields with the same field / bundle combination.');
} catch (EntityStorageException $e) {
}
try {
$this->fieldDefinition['field_name'] = $this
->randomMachineName();
FieldConfig::create($this->fieldDefinition)
->save();
$this
->fail('Cannot create a field with a non-existing storage.');
} catch (FieldException $e) {
}
}
public function testFieldPropertyConstraints() {
$field = FieldConfig::create($this->fieldDefinition);
$field
->save();
$field_name = $this->fieldStorage
->getName();
\Drupal::state()
->set('field_test_constraint', $field_name);
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
$this
->doFieldPropertyConstraintsTests();
\Drupal::getContainer()
->set('entity_field.manager', NULL);
$this
->doFieldPropertyConstraintsTests();
}
protected function doFieldPropertyConstraintsTests() {
$field_name = $this->fieldStorage
->getName();
$entity = EntityTest::create();
$entity
->set($field_name, 1);
$violations = $entity
->validate();
$this
->assertCount(0, $violations, 'No violations found when in-range value passed.');
$entity
->set($field_name, -2);
$violations = $entity
->validate();
$this
->assertCount(2, $violations, 'Two violations found when using a null and outside the range value.');
$this
->assertEquals($field_name . '.0.value', $violations[0]
->getPropertyPath());
$this
->assertEquals(t('%name does not accept the value @value.', [
'%name' => $field_name,
'@value' => -2,
]), $violations[0]
->getMessage());
$this
->assertEquals($field_name . '.0.value', $violations[1]
->getPropertyPath());
$this
->assertEquals(t('This value should be %limit or more.', [
'%limit' => 0,
]), $violations[1]
->getMessage());
$entity
->set($field_name, 33);
$violations = $entity
->validate();
$this
->assertCount(1, $violations, 'Violations found when using value outside the range.');
$this
->assertEquals($field_name . '.0.value', $violations[0]
->getPropertyPath());
$this
->assertEquals(t('This value should be %limit or less.', [
'%limit' => 32,
]), $violations[0]
->getMessage());
}
public function testCreateFieldCustomStorage() {
$field_name = mb_strtolower($this
->randomMachineName());
\Drupal::state()
->set('field_test_custom_storage', $field_name);
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'test_field',
'custom_storage' => TRUE,
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_name' => $field_storage
->getName(),
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
]);
$field
->save();
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
$this
->assertFalse(\Drupal::database()
->schema()
->tableExists('entity_test__' . $field_storage
->getName()));
$entity = EntityTest::create([
'name' => $this
->randomString(),
$field_name => 'Test value',
]);
$this
->assertIdentical('Test value', $entity->{$field_name}->value, 'The test value is set on the field.');
$entity
->save();
$entity = EntityTest::load($entity
->id());
$this
->assertNull($entity->{$field_name}->value, 'The loaded entity field value is NULL.');
}
public function testReadField() {
FieldConfig::create($this->fieldDefinition)
->save();
$field = FieldConfig::load('entity_test.' . $this->fieldDefinition['bundle'] . '.' . $this->fieldDefinition['field_name']);
$this
->assertTrue($this->fieldDefinition['field_name'] == $field
->getName(), 'The field was properly read.');
$this
->assertTrue($this->fieldDefinition['entity_type'] == $field
->getTargetEntityTypeId(), 'The field was properly read.');
$this
->assertTrue($this->fieldDefinition['bundle'] == $field
->getTargetBundle(), 'The field was properly read.');
}
public function testUpdateField() {
FieldConfig::create($this->fieldDefinition)
->save();
$field = FieldConfig::load('entity_test.' . $this->fieldDefinition['bundle'] . '.' . $this->fieldDefinition['field_name']);
$field
->setRequired(!$field
->isRequired());
$field
->setLabel($this
->randomMachineName());
$field
->set('description', $this
->randomMachineName());
$field
->setSetting('test_field_setting', $this
->randomMachineName());
$field
->save();
$field_new = FieldConfig::load('entity_test.' . $this->fieldDefinition['bundle'] . '.' . $this->fieldDefinition['field_name']);
$this
->assertEqual($field
->isRequired(), $field_new
->isRequired(), '"required" change is saved');
$this
->assertEqual($field
->getLabel(), $field_new
->getLabel(), '"label" change is saved');
$this
->assertEqual($field
->getDescription(), $field_new
->getDescription(), '"description" change is saved');
}
public function testDeleteFieldNoData() {
FieldConfig::create($this->fieldDefinition)
->save();
$another_field_definition = $this->fieldDefinition;
$another_field_definition['bundle'] .= '_another_bundle';
entity_test_create_bundle($another_field_definition['bundle']);
FieldConfig::create($another_field_definition)
->save();
$field = current(\Drupal::entityTypeManager()
->getStorage('field_config')
->loadByProperties([
'entity_type' => 'entity_test',
'field_name' => $this->fieldDefinition['field_name'],
'bundle' => $this->fieldDefinition['bundle'],
'include_deleted' => TRUE,
]));
$this
->assertTrue(!empty($field) && empty($field->deleted), 'A new field is not marked for deletion.');
$field
->delete();
$fields = \Drupal::entityTypeManager()
->getStorage('field_config')
->loadByProperties([
'entity_type' => 'entity_test',
'field_name' => $this->fieldDefinition['field_name'],
'bundle' => $this->fieldDefinition['bundle'],
'include_deleted' => TRUE,
]);
$this
->assertCount(0, $fields, 'A deleted field is marked for deletion.');
$field = FieldConfig::load('entity_test.' . '.' . $this->fieldDefinition['bundle'] . '.' . $this->fieldDefinition['field_name']);
$this
->assertTrue(empty($field), 'Field was deleted');
$another_field = FieldConfig::load('entity_test.' . $another_field_definition['bundle'] . '.' . $another_field_definition['field_name']);
$this
->assertTrue(!empty($another_field) && !$another_field
->isDeleted(), 'A non-deleted field is not marked for deletion.');
}
public function testDeleteFieldCrossDeletion() {
$field_definition_2 = $this->fieldDefinition;
$field_definition_2['bundle'] .= '_another_bundle';
entity_test_create_bundle($field_definition_2['bundle']);
$field_storage = $this->fieldStorage;
FieldConfig::create($this->fieldDefinition)
->save();
FieldConfig::create($field_definition_2)
->save();
$field_storage
->delete();
$this
->assertNull(FieldConfig::loadByName('entity_test', $this->fieldDefinition['bundle'], $field_storage
->getName()));
$this
->assertNull(FieldConfig::loadByName('entity_test', $field_definition_2['bundle'], $field_storage
->getName()));
$field_storage = FieldStorageConfig::create($this->fieldStorageDefinition);
$field_storage
->save();
$field = FieldConfig::create($this->fieldDefinition);
$field
->save();
$field_2 = FieldConfig::create($field_definition_2);
$field_2
->save();
$field
->delete();
$this
->assertNotEmpty(FieldStorageConfig::loadByName('entity_test', $field_storage
->getName()));
$field_2
->delete();
$this
->assertNull(FieldStorageConfig::loadByName('entity_test', $field_storage
->getName()));
$field_storage = FieldStorageConfig::create($this->fieldStorageDefinition);
$field_storage
->save();
$field = FieldConfig::create($this->fieldDefinition);
$field
->save();
$field_2 = FieldConfig::create($field_definition_2);
$field_2
->save();
$this->container
->get('entity_type.manager')
->getStorage('field_config')
->delete([
$field,
$field_2,
]);
$this
->assertNull(FieldStorageConfig::loadByName('entity_test', $field_storage
->getName()));
}
}