3 namespace Drupal\system\Tests\Entity;
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\entity_test\FieldStorageDefinition;
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);
11 * Provides some test methods used to update existing entity definitions.
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.
16 * @see https://www.drupal.org/node/2946549
18 trait EntityDefinitionTestTrait {
21 * Enables a new entity type definition.
23 protected function enableNewEntityType() {
24 $this->state->set('entity_test_new', TRUE);
25 $this->entityManager->clearCachedDefinitions();
26 $this->entityDefinitionUpdateManager->applyUpdates();
30 * Resets the entity type definition.
32 protected function resetEntityType() {
33 $this->state->set('entity_test_update.entity_type', NULL);
34 $this->entityManager->clearCachedDefinitions();
35 $this->entityDefinitionUpdateManager->applyUpdates();
39 * Updates the 'entity_test_update' entity type to revisionable.
41 protected function updateEntityTypeToRevisionable() {
42 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
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');
49 $this->state->set('entity_test_update.entity_type', $entity_type);
53 * Updates the 'entity_test_update' entity type not revisionable.
55 protected function updateEntityTypeToNotRevisionable() {
56 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
58 $keys = $entity_type->getKeys();
59 unset($keys['revision']);
60 $entity_type->set('entity_keys', $keys);
61 $entity_type->set('revision_table', NULL);
63 $this->state->set('entity_test_update.entity_type', $entity_type);
67 * Updates the 'entity_test_update' entity type to translatable.
69 protected function updateEntityTypeToTranslatable() {
70 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
72 $entity_type->set('translatable', TRUE);
73 $entity_type->set('data_table', 'entity_test_update_data');
75 if ($entity_type->isRevisionable()) {
76 $entity_type->set('revision_data_table', 'entity_test_update_revision_data');
79 $this->state->set('entity_test_update.entity_type', $entity_type);
83 * Updates the 'entity_test_update' entity type to not translatable.
85 protected function updateEntityTypeToNotTranslatable() {
86 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
88 $entity_type->set('translatable', FALSE);
89 $entity_type->set('data_table', NULL);
91 if ($entity_type->isRevisionable()) {
92 $entity_type->set('revision_data_table', NULL);
95 $this->state->set('entity_test_update.entity_type', $entity_type);
99 * Updates the 'entity_test_update' entity type to revisionable and
102 protected function updateEntityTypeToRevisionableAndTranslatable() {
103 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
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');
113 $this->state->set('entity_test_update.entity_type', $entity_type);
117 * Adds a new base field to the 'entity_test_update' entity type.
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
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);
137 * Adds a long-named base field to the 'entity_test_update' entity type.
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);
151 * Adds a new revisionable base field to the 'entity_test_update' entity type.
153 * @param string $type
154 * (optional) The field type for the new field. Defaults to 'string'.
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);
165 * Modifies the new base field from 'string' to 'text'.
167 protected function modifyBaseField() {
168 $this->addBaseField('text');
172 * Promotes a field to an entity key.
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);
183 * Removes the new base field from the 'entity_test_update' entity type.
185 * @param string $entity_type_id
186 * (optional) The entity type ID the base field should be attached to.
188 protected function removeBaseField($entity_type_id = 'entity_test_update') {
189 $this->state->delete($entity_type_id . '.additional_base_field_definitions');
193 * Adds a single-field index to the base field.
195 protected function addBaseFieldIndex() {
196 $this->state->set('entity_test_update.additional_field_index.entity_test_update.new_base_field', TRUE);
200 * Removes the index added in addBaseFieldIndex().
202 protected function removeBaseFieldIndex() {
203 $this->state->delete('entity_test_update.additional_field_index.entity_test_update.new_base_field');
207 * Adds a new bundle field to the 'entity_test_update' entity type.
209 * @param string $type
210 * (optional) The field type for the new field. Defaults to 'string'.
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);
222 * Modifies the new bundle field from 'string' to 'text'.
224 protected function modifyBundleField() {
225 $this->addBundleField('text');
229 * Removes the new bundle field from the 'entity_test_update' entity type.
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');
237 * Adds an index to the 'entity_test_update' entity type's base table.
239 * @see \Drupal\entity_test\EntityTestStorageSchema::getEntitySchema()
241 protected function addEntityIndex() {
243 'entity_test_update__new_index' => ['name', 'test_single_property'],
245 $this->state->set('entity_test_update.additional_entity_indexes', $indexes);
249 * Removes the index added in addEntityIndex().
251 protected function removeEntityIndex() {
252 $this->state->delete('entity_test_update.additional_entity_indexes');
256 * Renames the base table to 'entity_test_update_new'.
258 protected function renameBaseTable() {
259 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
261 $entity_type->set('base_table', 'entity_test_update_new');
263 $this->state->set('entity_test_update.entity_type', $entity_type);
267 * Renames the data table to 'entity_test_update_data_new'.
269 protected function renameDataTable() {
270 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
272 $entity_type->set('data_table', 'entity_test_update_data_new');
274 $this->state->set('entity_test_update.entity_type', $entity_type);
278 * Renames the revision table to 'entity_test_update_revision_new'.
280 protected function renameRevisionBaseTable() {
281 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
283 $entity_type->set('revision_table', 'entity_test_update_revision_new');
285 $this->state->set('entity_test_update.entity_type', $entity_type);
289 * Renames the revision data table to 'entity_test_update_revision_data_new'.
291 protected function renameRevisionDataTable() {
292 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
294 $entity_type->set('revision_data_table', 'entity_test_update_revision_data_new');
296 $this->state->set('entity_test_update.entity_type', $entity_type);
300 * Removes the entity type.
302 protected function deleteEntityType() {
303 $this->state->set('entity_test_update.entity_type', 'null');