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