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