380c1e66eda4ddad346cce50d3cfe8c19bf0ad35
[yaffs-website] / web / core / modules / system / src / Tests / Entity / EntityDefinitionTestTrait.php
1 <?php
2
3 namespace Drupal\system\Tests\Entity;
4
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\entity_test\FieldStorageDefinition;
7
8 /**
9  * Provides some test methods used to update existing entity definitions.
10  */
11 trait EntityDefinitionTestTrait {
12
13   /**
14    * Enables a new entity type definition.
15    */
16   protected function enableNewEntityType() {
17     $this->state->set('entity_test_new', TRUE);
18     $this->entityManager->clearCachedDefinitions();
19     $this->entityDefinitionUpdateManager->applyUpdates();
20   }
21
22   /**
23    * Resets the entity type definition.
24    */
25   protected function resetEntityType() {
26     $this->state->set('entity_test_update.entity_type', NULL);
27     $this->entityManager->clearCachedDefinitions();
28     $this->entityDefinitionUpdateManager->applyUpdates();
29   }
30
31   /**
32    * Updates the 'entity_test_update' entity type to revisionable.
33    */
34   protected function updateEntityTypeToRevisionable() {
35     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
36
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');
41
42     $this->state->set('entity_test_update.entity_type', $entity_type);
43   }
44
45   /**
46    * Updates the 'entity_test_update' entity type not revisionable.
47    */
48   protected function updateEntityTypeToNotRevisionable() {
49     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
50
51     $keys = $entity_type->getKeys();
52     unset($keys['revision']);
53     $entity_type->set('entity_keys', $keys);
54     $entity_type->set('revision_table', NULL);
55
56     $this->state->set('entity_test_update.entity_type', $entity_type);
57   }
58
59   /**
60    * Updates the 'entity_test_update' entity type to translatable.
61    */
62   protected function updateEntityTypeToTranslatable() {
63     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
64
65     $entity_type->set('translatable', TRUE);
66     $entity_type->set('data_table', 'entity_test_update_data');
67
68     if ($entity_type->isRevisionable()) {
69       $entity_type->set('revision_data_table', 'entity_test_update_revision_data');
70     }
71
72     $this->state->set('entity_test_update.entity_type', $entity_type);
73   }
74
75   /**
76    * Updates the 'entity_test_update' entity type to not translatable.
77    */
78   protected function updateEntityTypeToNotTranslatable() {
79     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
80
81     $entity_type->set('translatable', FALSE);
82     $entity_type->set('data_table', NULL);
83
84     if ($entity_type->isRevisionable()) {
85       $entity_type->set('revision_data_table', NULL);
86     }
87
88     $this->state->set('entity_test_update.entity_type', $entity_type);
89   }
90
91   /**
92    * Updates the 'entity_test_update' entity type to revisionable and
93    * translatable.
94    */
95   protected function updateEntityTypeToRevisionableAndTranslatable() {
96     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
97
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');
105
106     $this->state->set('entity_test_update.entity_type', $entity_type);
107   }
108
109   /**
110    * Adds a new base field to the 'entity_test_update' entity type.
111    *
112    * @param string $type
113    *   (optional) The field type for the new field. Defaults to 'string'.
114    * @param string $entity_type_id
115    *   (optional) The entity type ID the base field should be attached to.
116    *   Defaults to 'entity_test_update'.
117    * @param bool $is_revisionable
118    *   (optional) If the base field should be revisionable or not. Defaults to
119    *   FALSE.
120    */
121   protected function addBaseField($type = 'string', $entity_type_id = 'entity_test_update', $is_revisionable = FALSE) {
122     $definitions['new_base_field'] = BaseFieldDefinition::create($type)
123       ->setName('new_base_field')
124       ->setRevisionable($is_revisionable)
125       ->setLabel(t('A new base field'));
126     $this->state->set($entity_type_id . '.additional_base_field_definitions', $definitions);
127   }
128
129   /**
130    * Adds a long-named base field to the 'entity_test_update' entity type.
131    */
132   protected function addLongNameBaseField() {
133     $key = 'entity_test_update.additional_base_field_definitions';
134     $definitions = $this->state->get($key, []);
135     $definitions['new_long_named_entity_reference_base_field'] = BaseFieldDefinition::create('entity_reference')
136       ->setName('new_long_named_entity_reference_base_field')
137       ->setLabel(t('A new long-named base field'))
138       ->setSetting('target_type', 'user')
139       ->setSetting('handler', 'default');
140     $this->state->set($key, $definitions);
141   }
142
143   /**
144    * Adds a new revisionable base field to the 'entity_test_update' entity type.
145    *
146    * @param string $type
147    *   (optional) The field type for the new field. Defaults to 'string'.
148    */
149   protected function addRevisionableBaseField($type = 'string') {
150     $definitions['new_base_field'] = BaseFieldDefinition::create($type)
151       ->setName('new_base_field')
152       ->setLabel(t('A new revisionable base field'))
153       ->setRevisionable(TRUE);
154     $this->state->set('entity_test_update.additional_base_field_definitions', $definitions);
155   }
156
157   /**
158    * Modifies the new base field from 'string' to 'text'.
159    */
160   protected function modifyBaseField() {
161     $this->addBaseField('text');
162   }
163
164   /**
165    * Promotes a field to an entity key.
166    */
167   protected function makeBaseFieldEntityKey() {
168     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
169     $entity_keys = $entity_type->getKeys();
170     $entity_keys['new_base_field'] = 'new_base_field';
171     $entity_type->set('entity_keys', $entity_keys);
172     $this->state->set('entity_test_update.entity_type', $entity_type);
173   }
174
175   /**
176    * Removes the new base field from the 'entity_test_update' entity type.
177    *
178    * @param string $entity_type_id
179    *   (optional) The entity type ID the base field should be attached to.
180    */
181   protected function removeBaseField($entity_type_id = 'entity_test_update') {
182     $this->state->delete($entity_type_id . '.additional_base_field_definitions');
183   }
184
185   /**
186    * Adds a single-field index to the base field.
187    */
188   protected function addBaseFieldIndex() {
189     $this->state->set('entity_test_update.additional_field_index.entity_test_update.new_base_field', TRUE);
190   }
191
192   /**
193    * Removes the index added in addBaseFieldIndex().
194    */
195   protected function removeBaseFieldIndex() {
196     $this->state->delete('entity_test_update.additional_field_index.entity_test_update.new_base_field');
197   }
198
199   /**
200    * Adds a new bundle field to the 'entity_test_update' entity type.
201    *
202    * @param string $type
203    *   (optional) The field type for the new field. Defaults to 'string'.
204    */
205   protected function addBundleField($type = 'string') {
206     $definitions['new_bundle_field'] = FieldStorageDefinition::create($type)
207       ->setName('new_bundle_field')
208       ->setLabel(t('A new bundle field'))
209       ->setTargetEntityTypeId('entity_test_update');
210     $this->state->set('entity_test_update.additional_field_storage_definitions', $definitions);
211     $this->state->set('entity_test_update.additional_bundle_field_definitions.test_bundle', $definitions);
212   }
213
214   /**
215    * Modifies the new bundle field from 'string' to 'text'.
216    */
217   protected function modifyBundleField() {
218     $this->addBundleField('text');
219   }
220
221   /**
222    * Removes the new bundle field from the 'entity_test_update' entity type.
223    */
224   protected function removeBundleField() {
225     $this->state->delete('entity_test_update.additional_field_storage_definitions');
226     $this->state->delete('entity_test_update.additional_bundle_field_definitions.test_bundle');
227   }
228
229   /**
230    * Adds an index to the 'entity_test_update' entity type's base table.
231    *
232    * @see \Drupal\entity_test\EntityTestStorageSchema::getEntitySchema()
233    */
234   protected function addEntityIndex() {
235     $indexes = [
236       'entity_test_update__new_index' => ['name', 'test_single_property'],
237     ];
238     $this->state->set('entity_test_update.additional_entity_indexes', $indexes);
239   }
240
241   /**
242    * Removes the index added in addEntityIndex().
243    */
244   protected function removeEntityIndex() {
245     $this->state->delete('entity_test_update.additional_entity_indexes');
246   }
247
248   /**
249    * Renames the base table to 'entity_test_update_new'.
250    */
251   protected function renameBaseTable() {
252     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
253
254     $entity_type->set('base_table', 'entity_test_update_new');
255
256     $this->state->set('entity_test_update.entity_type', $entity_type);
257   }
258
259   /**
260    * Renames the data table to 'entity_test_update_data_new'.
261    */
262   protected function renameDataTable() {
263     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
264
265     $entity_type->set('data_table', 'entity_test_update_data_new');
266
267     $this->state->set('entity_test_update.entity_type', $entity_type);
268   }
269
270   /**
271    * Renames the revision table to 'entity_test_update_revision_new'.
272    */
273   protected function renameRevisionBaseTable() {
274     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
275
276     $entity_type->set('revision_table', 'entity_test_update_revision_new');
277
278     $this->state->set('entity_test_update.entity_type', $entity_type);
279   }
280
281   /**
282    * Renames the revision data table to 'entity_test_update_revision_data_new'.
283    */
284   protected function renameRevisionDataTable() {
285     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
286
287     $entity_type->set('revision_data_table', 'entity_test_update_revision_data_new');
288
289     $this->state->set('entity_test_update.entity_type', $entity_type);
290   }
291
292   /**
293    * Removes the entity type.
294    */
295   protected function deleteEntityType() {
296     $this->state->set('entity_test_update.entity_type', 'null');
297   }
298
299 }