ed30d4b56f4d3ab4d6241feaf378527bc1fa3917
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityBundleFieldTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 /**
6  * Tests adding a custom bundle field.
7  *
8  * @group Entity
9  */
10 class EntityBundleFieldTest extends EntityKernelTestBase {
11
12   /**
13    * Modules to enable.
14    *
15    * @var array
16    */
17   public static $modules = ['entity_schema_test'];
18
19   /**
20    * The module handler.
21    *
22    * @var \Drupal\Core\Extension\ModuleHandlerInterface
23    */
24   protected $moduleHandler;
25
26   /**
27    * The database connection used.
28    *
29    * @var \Drupal\Core\Database\Connection
30    */
31   protected $database;
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp() {
37     parent::setUp();
38     $this->installSchema('user', ['users_data']);
39     $this->moduleHandler = $this->container->get('module_handler');
40     $this->database = $this->container->get('database');
41   }
42
43   /**
44    * Tests making use of a custom bundle field.
45    */
46   public function testCustomBundleFieldUsage() {
47     entity_test_create_bundle('custom');
48
49     // Check that an entity with bundle entity_test does not have the custom
50     // field.
51     $storage = $this->entityManager->getStorage('entity_test');
52     $entity = $storage->create([
53       'type' => 'entity_test',
54     ]);
55     $this->assertFalse($entity->hasField('custom_bundle_field'));
56
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([
60       'type' => 'custom',
61     ]);
62     $this->assertTrue($entity->hasField('custom_bundle_field'));
63
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']]);
67
68     $entity->custom_bundle_field->value = 'swanky';
69     $entity->save();
70     $storage->resetCache();
71     $entity = $storage->load($entity->id());
72     $this->assertEqual($entity->custom_bundle_field->value, 'swanky', 'Entity was saved correctly');
73
74     $entity->custom_bundle_field->value = 'cozy';
75     $entity->save();
76     $storage->resetCache();
77     $entity = $storage->load($entity->id());
78     $this->assertEqual($entity->custom_bundle_field->value, 'cozy', 'Entity was updated correctly.');
79
80     $entity->delete();
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')
85       ->fields('f')
86       ->condition('f.entity_id', $entity->id())
87       ->execute();
88     $this->assertFalse($result->fetchAssoc(), 'Field data has been deleted');
89
90     // Create another entity to test that values are marked as deleted when a
91     // bundle is deleted.
92     $entity = $storage->create(['type' => 'custom', 'custom_bundle_field' => 'new']);
93     $entity->save();
94     entity_test_delete_bundle('custom');
95
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)
100       ->countQuery()
101       ->execute();
102     $this->assertEqual(1, $result->fetchField(), 'Field data has been deleted');
103
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']));
107
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');
111   }
112
113 }