3 namespace Drupal\KernelTests\Core\Entity;
6 * Tests adding a custom bundle field.
10 class EntityBundleFieldTest extends EntityKernelTestBase {
17 public static $modules = ['entity_schema_test'];
22 * @var \Drupal\Core\Extension\ModuleHandlerInterface
24 protected $moduleHandler;
27 * The database connection used.
29 * @var \Drupal\Core\Database\Connection
36 protected function setUp() {
38 $this->installSchema('user', ['users_data']);
39 $this->moduleHandler = $this->container->get('module_handler');
40 $this->database = $this->container->get('database');
44 * Tests making use of a custom bundle field.
46 public function testCustomBundleFieldUsage() {
47 entity_test_create_bundle('custom');
49 // Check that an entity with bundle entity_test does not have the custom
51 $storage = $this->entityManager->getStorage('entity_test');
52 $entity = $storage->create([
53 'type' => 'entity_test',
55 $this->assertFalse($entity->hasField('custom_bundle_field'));
57 // Check that the custom bundle has the defined custom field and check
58 // saving and deleting of custom field data.
59 $entity = $storage->create([
62 $this->assertTrue($entity->hasField('custom_bundle_field'));
64 // Ensure that the field exists in the field map.
65 $field_map = \Drupal::entityManager()->getFieldMap();
66 $this->assertEqual($field_map['entity_test']['custom_bundle_field'], ['type' => 'string', 'bundles' => ['custom' => 'custom']]);
68 $entity->custom_bundle_field->value = 'swanky';
70 $storage->resetCache();
71 $entity = $storage->load($entity->id());
72 $this->assertEqual($entity->custom_bundle_field->value, 'swanky', 'Entity was saved correctly');
74 $entity->custom_bundle_field->value = 'cozy';
76 $storage->resetCache();
77 $entity = $storage->load($entity->id());
78 $this->assertEqual($entity->custom_bundle_field->value, 'cozy', 'Entity was updated correctly.');
81 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
82 $table_mapping = $storage->getTableMapping();
83 $table = $table_mapping->getDedicatedDataTableName($entity->getFieldDefinition('custom_bundle_field'));
84 $result = $this->database->select($table, 'f')
86 ->condition('f.entity_id', $entity->id())
88 $this->assertFalse($result->fetchAssoc(), 'Field data has been deleted');
90 // Create another entity to test that values are marked as deleted when a
92 $entity = $storage->create(['type' => 'custom', 'custom_bundle_field' => 'new']);
94 entity_test_delete_bundle('custom');
96 $table = $table_mapping->getDedicatedDataTableName($entity->getFieldDefinition('custom_bundle_field'));
97 $result = $this->database->select($table, 'f')
98 ->condition('f.entity_id', $entity->id())
99 ->condition('deleted', 1)
102 $this->assertEqual(1, $result->fetchField(), 'Field data has been deleted');
104 // Ensure that the field no longer exists in the field map.
105 $field_map = \Drupal::entityManager()->getFieldMap();
106 $this->assertFalse(isset($field_map['entity_test']['custom_bundle_field']));
108 // @todo Test field purge and table deletion once supported. See
109 // https://www.drupal.org/node/2282119.
110 // $this->assertFalse($this->database->schema()->tableExists($table), 'Custom field table was deleted');