/** * Test the creation of a field storage. */ function testCreate() { $field_storage_definition = array('field_name' => 'field_2', 'entity_type' => 'entity_test', 'type' => 'test_field'); field_test_memorize(); $field_storage = FieldStorageConfig::create($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 { FieldStorageConfig::create($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'); FieldStorageConfig::create($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'); FieldStorageConfig::create($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'); FieldStorageConfig::create($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'); FieldStorageConfig::create($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'); FieldStorageConfig::create($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'); FieldStorageConfig::create($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'); FieldStorageConfig::create($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.')); } }
/** * {@inheritdoc} */ public function delete() { // Reports that delete() method is executed for testing purposes. field_test_memorize('field_test_field_delete', array($this->getEntity())); }
/** * Verify that field storages are preserved and purged correctly as multiple * fields are deleted and purged. */ function testPurgeFieldStorage() { // Start recording hook invocations. field_test_memorize(); $field_storage = reset($this->fieldStorages); $field_name = $field_storage->getName(); // Delete the first field. $bundle = reset($this->bundles); $field = FieldConfig::loadByName($this->entityTypeId, $bundle, $field_name); $field->delete(); // Assert that FieldItemInterface::delete() was not called yet. $mem = field_test_memorize(); $this->assertEqual(count($mem), 0, 'No field hooks were called.'); // Purge the data. field_purge_batch(10); // Check hooks invocations. // FieldItemInterface::delete() should have been called once for each entity in the // bundle. $actual_hooks = field_test_memorize(); $hooks = array(); $entities = $this->entitiesByBundles[$bundle]; foreach ($entities as $id => $entity) { $hooks['field_test_field_delete'][] = $entity; } $this->checkHooksInvocations($hooks, $actual_hooks); // The field still exists, deleted. $fields = entity_load_multiple_by_properties('field_config', array('uuid' => $field->uuid(), 'include_deleted' => TRUE)); $this->assertTrue(isset($fields[$field->uuid()]) && $fields[$field->uuid()]->isDeleted(), 'The field exists and is deleted'); // Purge again to purge the field. field_purge_batch(0); // The field is gone. $fields = entity_load_multiple_by_properties('field_config', array('uuid' => $field->uuid(), 'include_deleted' => TRUE)); $this->assertEqual(count($fields), 0, 'The field is purged.'); // The field storage still exists, not deleted. $storages = entity_load_multiple_by_properties('field_storage_config', array('uuid' => $field_storage->uuid(), 'include_deleted' => TRUE)); $this->assertTrue(isset($storages[$field_storage->uuid()]) && !$storages[$field_storage->uuid()]->isDeleted(), 'The field storage exists and is not deleted'); // Delete the second field. $bundle = next($this->bundles); $field = FieldConfig::loadByName($this->entityTypeId, $bundle, $field_name); $field->delete(); // Assert that FieldItemInterface::delete() was not called yet. $mem = field_test_memorize(); $this->assertEqual(count($mem), 0, 'No field hooks were called.'); // Purge the data. field_purge_batch(10); // Check hooks invocations (same as above, for the 2nd bundle). $actual_hooks = field_test_memorize(); $hooks = array(); $entities = $this->entitiesByBundles[$bundle]; foreach ($entities as $id => $entity) { $hooks['field_test_field_delete'][] = $entity; } $this->checkHooksInvocations($hooks, $actual_hooks); // The field and the storage still exist, deleted. $fields = entity_load_multiple_by_properties('field_config', array('uuid' => $field->uuid(), 'include_deleted' => TRUE)); $this->assertTrue(isset($fields[$field->uuid()]) && $fields[$field->uuid()]->isDeleted(), 'The field exists and is deleted'); $storages = entity_load_multiple_by_properties('field_storage_config', array('uuid' => $field_storage->uuid(), 'include_deleted' => TRUE)); $this->assertTrue(isset($storages[$field_storage->uuid()]) && $storages[$field_storage->uuid()]->isDeleted(), 'The field storage exists and is deleted'); // Purge again to purge the field and the storage. field_purge_batch(0); // The field and the storage are gone. $fields = entity_load_multiple_by_properties('field_config', array('uuid' => $field->uuid(), 'include_deleted' => TRUE)); $this->assertEqual(count($fields), 0, 'The field is purged.'); $storages = entity_load_multiple_by_properties('field_storage_config', array('uuid' => $field_storage->uuid(), 'include_deleted' => TRUE)); $this->assertEqual(count($storages), 0, 'The field storage is purged.'); }