3 namespace Drupal\system\Tests\Entity;
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\entity_test\FieldStorageDefinition;
9 * Provides some test methods used to update existing entity definitions.
11 trait EntityDefinitionTestTrait {
14 * Enables a new entity type definition.
16 protected function enableNewEntityType() {
17 $this->state->set('entity_test_new', TRUE);
18 $this->entityManager->clearCachedDefinitions();
19 $this->entityDefinitionUpdateManager->applyUpdates();
23 * Resets the entity type definition.
25 protected function resetEntityType() {
26 $this->state->set('entity_test_update.entity_type', NULL);
27 $this->entityManager->clearCachedDefinitions();
28 $this->entityDefinitionUpdateManager->applyUpdates();
32 * Updates the 'entity_test_update' entity type to revisionable.
34 protected function updateEntityTypeToRevisionable() {
35 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
37 $keys = $entity_type->getKeys();
38 $keys['revision'] = 'revision_id';
39 $entity_type->set('entity_keys', $keys);
40 $entity_type->set('revision_table', 'entity_test_update_revision');
42 $this->state->set('entity_test_update.entity_type', $entity_type);
46 * Updates the 'entity_test_update' entity type not revisionable.
48 protected function updateEntityTypeToNotRevisionable() {
49 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
51 $keys = $entity_type->getKeys();
52 unset($keys['revision']);
53 $entity_type->set('entity_keys', $keys);
54 $entity_type->set('revision_table', NULL);
56 $this->state->set('entity_test_update.entity_type', $entity_type);
60 * Updates the 'entity_test_update' entity type to translatable.
62 protected function updateEntityTypeToTranslatable() {
63 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
65 $entity_type->set('translatable', TRUE);
66 $entity_type->set('data_table', 'entity_test_update_data');
68 if ($entity_type->isRevisionable()) {
69 $entity_type->set('revision_data_table', 'entity_test_update_revision_data');
72 $this->state->set('entity_test_update.entity_type', $entity_type);
76 * Updates the 'entity_test_update' entity type to not translatable.
78 protected function updateEntityTypeToNotTranslatable() {
79 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
81 $entity_type->set('translatable', FALSE);
82 $entity_type->set('data_table', NULL);
84 if ($entity_type->isRevisionable()) {
85 $entity_type->set('revision_data_table', NULL);
88 $this->state->set('entity_test_update.entity_type', $entity_type);
92 * Updates the 'entity_test_update' entity type to revisionable and
95 protected function updateEntityTypeToRevisionableAndTranslatable() {
96 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
98 $keys = $entity_type->getKeys();
99 $keys['revision'] = 'revision_id';
100 $entity_type->set('entity_keys', $keys);
101 $entity_type->set('translatable', TRUE);
102 $entity_type->set('data_table', 'entity_test_update_data');
103 $entity_type->set('revision_table', 'entity_test_update_revision');
104 $entity_type->set('revision_data_table', 'entity_test_update_revision_data');
106 $this->state->set('entity_test_update.entity_type', $entity_type);
110 * Adds a new base field to the 'entity_test_update' entity type.
112 * @param string $type
113 * (optional) The field type for the new field. Defaults to 'string'.
115 protected function addBaseField($type = 'string') {
116 $definitions['new_base_field'] = BaseFieldDefinition::create($type)
117 ->setName('new_base_field')
118 ->setLabel(t('A new base field'));
119 $this->state->set('entity_test_update.additional_base_field_definitions', $definitions);
123 * Adds a long-named base field to the 'entity_test_update' entity type.
125 protected function addLongNameBaseField() {
126 $key = 'entity_test_update.additional_base_field_definitions';
127 $definitions = $this->state->get($key, []);
128 $definitions['new_long_named_entity_reference_base_field'] = BaseFieldDefinition::create('entity_reference')
129 ->setName('new_long_named_entity_reference_base_field')
130 ->setLabel(t('A new long-named base field'))
131 ->setSetting('target_type', 'user')
132 ->setSetting('handler', 'default');
133 $this->state->set($key, $definitions);
137 * Adds a new revisionable base field to the 'entity_test_update' entity type.
139 * @param string $type
140 * (optional) The field type for the new field. Defaults to 'string'.
142 protected function addRevisionableBaseField($type = 'string') {
143 $definitions['new_base_field'] = BaseFieldDefinition::create($type)
144 ->setName('new_base_field')
145 ->setLabel(t('A new revisionable base field'))
146 ->setRevisionable(TRUE);
147 $this->state->set('entity_test_update.additional_base_field_definitions', $definitions);
151 * Modifies the new base field from 'string' to 'text'.
153 protected function modifyBaseField() {
154 $this->addBaseField('text');
158 * Promotes a field to an entity key.
160 protected function makeBaseFieldEntityKey() {
161 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
162 $entity_keys = $entity_type->getKeys();
163 $entity_keys['new_base_field'] = 'new_base_field';
164 $entity_type->set('entity_keys', $entity_keys);
165 $this->state->set('entity_test_update.entity_type', $entity_type);
169 * Removes the new base field from the 'entity_test_update' entity type.
171 protected function removeBaseField() {
172 $this->state->delete('entity_test_update.additional_base_field_definitions');
176 * Adds a single-field index to the base field.
178 protected function addBaseFieldIndex() {
179 $this->state->set('entity_test_update.additional_field_index.entity_test_update.new_base_field', TRUE);
183 * Removes the index added in addBaseFieldIndex().
185 protected function removeBaseFieldIndex() {
186 $this->state->delete('entity_test_update.additional_field_index.entity_test_update.new_base_field');
190 * Adds a new bundle field to the 'entity_test_update' entity type.
192 * @param string $type
193 * (optional) The field type for the new field. Defaults to 'string'.
195 protected function addBundleField($type = 'string') {
196 $definitions['new_bundle_field'] = FieldStorageDefinition::create($type)
197 ->setName('new_bundle_field')
198 ->setLabel(t('A new bundle field'))
199 ->setTargetEntityTypeId('entity_test_update');
200 $this->state->set('entity_test_update.additional_field_storage_definitions', $definitions);
201 $this->state->set('entity_test_update.additional_bundle_field_definitions.test_bundle', $definitions);
205 * Modifies the new bundle field from 'string' to 'text'.
207 protected function modifyBundleField() {
208 $this->addBundleField('text');
212 * Removes the new bundle field from the 'entity_test_update' entity type.
214 protected function removeBundleField() {
215 $this->state->delete('entity_test_update.additional_field_storage_definitions');
216 $this->state->delete('entity_test_update.additional_bundle_field_definitions.test_bundle');
220 * Adds an index to the 'entity_test_update' entity type's base table.
222 * @see \Drupal\entity_test\EntityTestStorageSchema::getEntitySchema()
224 protected function addEntityIndex() {
226 'entity_test_update__new_index' => ['name', 'test_single_property'],
228 $this->state->set('entity_test_update.additional_entity_indexes', $indexes);
232 * Removes the index added in addEntityIndex().
234 protected function removeEntityIndex() {
235 $this->state->delete('entity_test_update.additional_entity_indexes');
239 * Renames the base table to 'entity_test_update_new'.
241 protected function renameBaseTable() {
242 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
244 $entity_type->set('base_table', 'entity_test_update_new');
246 $this->state->set('entity_test_update.entity_type', $entity_type);
250 * Renames the data table to 'entity_test_update_data_new'.
252 protected function renameDataTable() {
253 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
255 $entity_type->set('data_table', 'entity_test_update_data_new');
257 $this->state->set('entity_test_update.entity_type', $entity_type);
261 * Renames the revision table to 'entity_test_update_revision_new'.
263 protected function renameRevisionBaseTable() {
264 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
266 $entity_type->set('revision_table', 'entity_test_update_revision_new');
268 $this->state->set('entity_test_update.entity_type', $entity_type);
272 * Renames the revision data table to 'entity_test_update_revision_data_new'.
274 protected function renameRevisionDataTable() {
275 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
277 $entity_type->set('revision_data_table', 'entity_test_update_revision_data_new');
279 $this->state->set('entity_test_update.entity_type', $entity_type);
283 * Removes the entity type.
285 protected function deleteEntityType() {
286 $this->state->set('entity_test_update.entity_type', 'null');