Upgraded drupal core with security updates
[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    */
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);
120   }
121
122   /**
123    * Adds a long-named base field to the 'entity_test_update' entity type.
124    */
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);
134   }
135
136   /**
137    * Adds a new revisionable base field to the 'entity_test_update' entity type.
138    *
139    * @param string $type
140    *   (optional) The field type for the new field. Defaults to 'string'.
141    */
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);
148   }
149
150   /**
151    * Modifies the new base field from 'string' to 'text'.
152    */
153   protected function modifyBaseField() {
154     $this->addBaseField('text');
155   }
156
157   /**
158    * Promotes a field to an entity key.
159    */
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);
166   }
167
168   /**
169    * Removes the new base field from the 'entity_test_update' entity type.
170    */
171   protected function removeBaseField() {
172     $this->state->delete('entity_test_update.additional_base_field_definitions');
173   }
174
175   /**
176    * Adds a single-field index to the base field.
177    */
178   protected function addBaseFieldIndex() {
179     $this->state->set('entity_test_update.additional_field_index.entity_test_update.new_base_field', TRUE);
180   }
181
182   /**
183    * Removes the index added in addBaseFieldIndex().
184    */
185   protected function removeBaseFieldIndex() {
186     $this->state->delete('entity_test_update.additional_field_index.entity_test_update.new_base_field');
187   }
188
189   /**
190    * Adds a new bundle field to the 'entity_test_update' entity type.
191    *
192    * @param string $type
193    *   (optional) The field type for the new field. Defaults to 'string'.
194    */
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);
202   }
203
204   /**
205    * Modifies the new bundle field from 'string' to 'text'.
206    */
207   protected function modifyBundleField() {
208     $this->addBundleField('text');
209   }
210
211   /**
212    * Removes the new bundle field from the 'entity_test_update' entity type.
213    */
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');
217   }
218
219   /**
220    * Adds an index to the 'entity_test_update' entity type's base table.
221    *
222    * @see \Drupal\entity_test\EntityTestStorageSchema::getEntitySchema()
223    */
224   protected function addEntityIndex() {
225     $indexes = [
226       'entity_test_update__new_index' => ['name', 'test_single_property'],
227     ];
228     $this->state->set('entity_test_update.additional_entity_indexes', $indexes);
229   }
230
231   /**
232    * Removes the index added in addEntityIndex().
233    */
234   protected function removeEntityIndex() {
235     $this->state->delete('entity_test_update.additional_entity_indexes');
236   }
237
238   /**
239    * Renames the base table to 'entity_test_update_new'.
240    */
241   protected function renameBaseTable() {
242     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
243
244     $entity_type->set('base_table', 'entity_test_update_new');
245
246     $this->state->set('entity_test_update.entity_type', $entity_type);
247   }
248
249   /**
250    * Renames the data table to 'entity_test_update_data_new'.
251    */
252   protected function renameDataTable() {
253     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
254
255     $entity_type->set('data_table', 'entity_test_update_data_new');
256
257     $this->state->set('entity_test_update.entity_type', $entity_type);
258   }
259
260   /**
261    * Renames the revision table to 'entity_test_update_revision_new'.
262    */
263   protected function renameRevisionBaseTable() {
264     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
265
266     $entity_type->set('revision_table', 'entity_test_update_revision_new');
267
268     $this->state->set('entity_test_update.entity_type', $entity_type);
269   }
270
271   /**
272    * Renames the revision data table to 'entity_test_update_revision_data_new'.
273    */
274   protected function renameRevisionDataTable() {
275     $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
276
277     $entity_type->set('revision_data_table', 'entity_test_update_revision_data_new');
278
279     $this->state->set('entity_test_update.entity_type', $entity_type);
280   }
281
282   /**
283    * Removes the entity type.
284    */
285   protected function deleteEntityType() {
286     $this->state->set('entity_test_update.entity_type', 'null');
287   }
288
289 }