function FieldStorageCrudTest::testCreate in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/field/src/Tests/FieldStorageCrudTest.php \Drupal\field\Tests\FieldStorageCrudTest::testCreate()
Test the creation of a field storage.
File
- core/
modules/ field/ src/ Tests/ FieldStorageCrudTest.php, line 38 - Contains \Drupal\field\Tests\FieldStorageCrudTest.
Class
- FieldStorageCrudTest
- Tests field storage create, read, update, and delete.
Namespace
Drupal\field\TestsCode
function testCreate() {
$field_storage_definition = array(
'field_name' => 'field_2',
'entity_type' => 'entity_test',
'type' => 'test_field',
);
field_test_memorize();
$field_storage = entity_create('field_storage_config', $field_storage_definition);
$field_storage
->save();
$field_storage = FieldStorageConfig::load($field_storage
->id());
$this
->assertTrue($field_storage
->getSetting('storage_setting_from_config_data'));
$this
->assertNull($field_storage
->getSetting('config_data_from_storage_setting'));
$mem = field_test_memorize();
$this
->assertIdentical($mem['field_test_field_storage_config_create'][0][0]
->getName(), $field_storage_definition['field_name'], 'hook_entity_create() called with correct arguments.');
$this
->assertIdentical($mem['field_test_field_storage_config_create'][0][0]
->getType(), $field_storage_definition['type'], 'hook_entity_create() called with correct arguments.');
// 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.
$field_storage_config = $this
->config('field.storage.' . $field_storage
->id())
->get();
$this
->assertTrue($field_storage_config['settings']['config_data_from_storage_setting']);
$this
->assertTrue(!isset($field_storage_config['settings']['storage_setting_from_config_data']));
// Since we are working with raw configuration, this needs to be unset
// manually.
// @see Drupal\field_test\Plugin\Field\FieldType\TestItem::storageSettingsFromConfigData()
unset($field_storage_config['settings']['config_data_from_storage_setting']);
// Ensure that basic properties are preserved.
$this
->assertEqual($field_storage_config['field_name'], $field_storage_definition['field_name'], 'The field name is properly saved.');
$this
->assertEqual($field_storage_config['entity_type'], $field_storage_definition['entity_type'], 'The field entity type is properly saved.');
$this
->assertEqual($field_storage_config['id'], $field_storage_definition['entity_type'] . '.' . $field_storage_definition['field_name'], 'The field id is properly saved.');
$this
->assertEqual($field_storage_config['type'], $field_storage_definition['type'], 'The field type is properly saved.');
// Ensure that cardinality defaults to 1.
$this
->assertEqual($field_storage_config['cardinality'], 1, 'Cardinality defaults to 1.');
// Ensure that default settings are present.
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
$this
->assertEqual($field_storage_config['settings'], $field_type_manager
->getDefaultStorageSettings($field_storage_definition['type']), 'Default storage settings have been written.');
// Guarantee that the name is unique.
try {
entity_create('field_storage_config', $field_storage_definition)
->save();
$this
->fail(t('Cannot create two fields with the same name.'));
} catch (EntityStorageException $e) {
$this
->pass(t('Cannot create two fields with the same name.'));
}
// Check that field type is required.
try {
$field_storage_definition = array(
'field_name' => 'field_1',
'entity_type' => 'entity_type',
);
entity_create('field_storage_config', $field_storage_definition)
->save();
$this
->fail(t('Cannot create a field with no type.'));
} catch (FieldException $e) {
$this
->pass(t('Cannot create a field with no type.'));
}
// Check that field name is required.
try {
$field_storage_definition = array(
'type' => 'test_field',
'entity_type' => 'entity_test',
);
entity_create('field_storage_config', $field_storage_definition)
->save();
$this
->fail(t('Cannot create an unnamed field.'));
} catch (FieldException $e) {
$this
->pass(t('Cannot create an unnamed field.'));
}
// Check that entity type is required.
try {
$field_storage_definition = array(
'field_name' => 'test_field',
'type' => 'test_field',
);
entity_create('field_storage_config', $field_storage_definition)
->save();
$this
->fail('Cannot create a field without an entity type.');
} catch (FieldException $e) {
$this
->pass('Cannot create a field without an entity type.');
}
// Check that field name must start with a letter or _.
try {
$field_storage_definition = array(
'field_name' => '2field_2',
'entity_type' => 'entity_test',
'type' => 'test_field',
);
entity_create('field_storage_config', $field_storage_definition)
->save();
$this
->fail(t('Cannot create a field with a name starting with a digit.'));
} catch (FieldException $e) {
$this
->pass(t('Cannot create a field with a name starting with a digit.'));
}
// Check that field name must only contain lowercase alphanumeric or _.
try {
$field_storage_definition = array(
'field_name' => 'field#_3',
'entity_type' => 'entity_test',
'type' => 'test_field',
);
entity_create('field_storage_config', $field_storage_definition)
->save();
$this
->fail(t('Cannot create a field with a name containing an illegal character.'));
} catch (FieldException $e) {
$this
->pass(t('Cannot create a field with a name containing an illegal character.'));
}
// Check that field name cannot be longer than 32 characters long.
try {
$field_storage_definition = array(
'field_name' => '_12345678901234567890123456789012',
'entity_type' => 'entity_test',
'type' => 'test_field',
);
entity_create('field_storage_config', $field_storage_definition)
->save();
$this
->fail(t('Cannot create a field with a name longer than 32 characters.'));
} catch (FieldException $e) {
$this
->pass(t('Cannot create a field with a name longer than 32 characters.'));
}
// Check that field name can not be an entity key.
// "id" is known as an entity key from the "entity_test" type.
try {
$field_storage_definition = array(
'type' => 'test_field',
'field_name' => 'id',
'entity_type' => 'entity_test',
);
entity_create('field_storage_config', $field_storage_definition)
->save();
$this
->fail(t('Cannot create a field bearing the name of an entity key.'));
} catch (FieldException $e) {
$this
->pass(t('Cannot create a field bearing the name of an entity key.'));
}
}