3 namespace Drupal\system\Tests\Entity;
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\entity_test\FieldStorageDefinition;
9 * Provides some test methods used to update existing entity definitions.
11 trait EntityDefinitionTestTrait {
14 * Enables a new entity type definition.
16 protected function enableNewEntityType() {
17 $this->state->set('entity_test_new', TRUE);
18 $this->entityManager->clearCachedDefinitions();
19 $this->entityDefinitionUpdateManager->applyUpdates();
23 * Resets the entity type definition.
25 protected function resetEntityType() {
26 $this->state->set('entity_test_update.entity_type', NULL);
27 $this->entityManager->clearCachedDefinitions();
28 $this->entityDefinitionUpdateManager->applyUpdates();
32 * Updates the 'entity_test_update' entity type to revisionable.
34 protected function updateEntityTypeToRevisionable() {
35 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
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');
42 $this->state->set('entity_test_update.entity_type', $entity_type);
46 * Updates the 'entity_test_update' entity type not revisionable.
48 protected function updateEntityTypeToNotRevisionable() {
49 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
51 $keys = $entity_type->getKeys();
52 unset($keys['revision']);
53 $entity_type->set('entity_keys', $keys);
54 $entity_type->set('revision_table', NULL);
56 $this->state->set('entity_test_update.entity_type', $entity_type);
60 * Updates the 'entity_test_update' entity type to translatable.
62 protected function updateEntityTypeToTranslatable() {
63 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
65 $entity_type->set('translatable', TRUE);
66 $entity_type->set('data_table', 'entity_test_update_data');
68 if ($entity_type->isRevisionable()) {
69 $entity_type->set('revision_data_table', 'entity_test_update_revision_data');
72 $this->state->set('entity_test_update.entity_type', $entity_type);
76 * Updates the 'entity_test_update' entity type to not translatable.
78 protected function updateEntityTypeToNotTranslatable() {
79 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
81 $entity_type->set('translatable', FALSE);
82 $entity_type->set('data_table', NULL);
84 if ($entity_type->isRevisionable()) {
85 $entity_type->set('revision_data_table', NULL);
88 $this->state->set('entity_test_update.entity_type', $entity_type);
92 * Updates the 'entity_test_update' entity type to revisionable and
95 protected function updateEntityTypeToRevisionableAndTranslatable() {
96 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
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');
106 $this->state->set('entity_test_update.entity_type', $entity_type);
110 * Adds a new base field to the 'entity_test_update' entity type.
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
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);
130 * Adds a long-named base field to the 'entity_test_update' entity type.
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);
144 * Adds a new revisionable base field to the 'entity_test_update' entity type.
146 * @param string $type
147 * (optional) The field type for the new field. Defaults to 'string'.
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);
158 * Modifies the new base field from 'string' to 'text'.
160 protected function modifyBaseField() {
161 $this->addBaseField('text');
165 * Promotes a field to an entity key.
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);
176 * Removes the new base field from the 'entity_test_update' entity type.
178 * @param string $entity_type_id
179 * (optional) The entity type ID the base field should be attached to.
181 protected function removeBaseField($entity_type_id = 'entity_test_update') {
182 $this->state->delete($entity_type_id . '.additional_base_field_definitions');
186 * Adds a single-field index to the base field.
188 protected function addBaseFieldIndex() {
189 $this->state->set('entity_test_update.additional_field_index.entity_test_update.new_base_field', TRUE);
193 * Removes the index added in addBaseFieldIndex().
195 protected function removeBaseFieldIndex() {
196 $this->state->delete('entity_test_update.additional_field_index.entity_test_update.new_base_field');
200 * Adds a new bundle field to the 'entity_test_update' entity type.
202 * @param string $type
203 * (optional) The field type for the new field. Defaults to 'string'.
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);
215 * Modifies the new bundle field from 'string' to 'text'.
217 protected function modifyBundleField() {
218 $this->addBundleField('text');
222 * Removes the new bundle field from the 'entity_test_update' entity type.
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');
230 * Adds an index to the 'entity_test_update' entity type's base table.
232 * @see \Drupal\entity_test\EntityTestStorageSchema::getEntitySchema()
234 protected function addEntityIndex() {
236 'entity_test_update__new_index' => ['name', 'test_single_property'],
238 $this->state->set('entity_test_update.additional_entity_indexes', $indexes);
242 * Removes the index added in addEntityIndex().
244 protected function removeEntityIndex() {
245 $this->state->delete('entity_test_update.additional_entity_indexes');
249 * Renames the base table to 'entity_test_update_new'.
251 protected function renameBaseTable() {
252 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
254 $entity_type->set('base_table', 'entity_test_update_new');
256 $this->state->set('entity_test_update.entity_type', $entity_type);
260 * Renames the data table to 'entity_test_update_data_new'.
262 protected function renameDataTable() {
263 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
265 $entity_type->set('data_table', 'entity_test_update_data_new');
267 $this->state->set('entity_test_update.entity_type', $entity_type);
271 * Renames the revision table to 'entity_test_update_revision_new'.
273 protected function renameRevisionBaseTable() {
274 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
276 $entity_type->set('revision_table', 'entity_test_update_revision_new');
278 $this->state->set('entity_test_update.entity_type', $entity_type);
282 * Renames the revision data table to 'entity_test_update_revision_data_new'.
284 protected function renameRevisionDataTable() {
285 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
287 $entity_type->set('revision_data_table', 'entity_test_update_revision_data_new');
289 $this->state->set('entity_test_update.entity_type', $entity_type);
293 * Removes the entity type.
295 protected function deleteEntityType() {
296 $this->state->set('entity_test_update.entity_type', 'null');