annotate core/modules/system/src/Tests/Entity/EntityDefinitionTestTrait.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
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@0 114 */
Chris@0 115 protected function addBaseField($type = 'string') {
Chris@0 116 $definitions['new_base_field'] = BaseFieldDefinition::create($type)
Chris@0 117 ->setName('new_base_field')
Chris@0 118 ->setLabel(t('A new base field'));
Chris@0 119 $this->state->set('entity_test_update.additional_base_field_definitions', $definitions);
Chris@0 120 }
Chris@0 121
Chris@0 122 /**
Chris@0 123 * Adds a long-named base field to the 'entity_test_update' entity type.
Chris@0 124 */
Chris@0 125 protected function addLongNameBaseField() {
Chris@0 126 $key = 'entity_test_update.additional_base_field_definitions';
Chris@0 127 $definitions = $this->state->get($key, []);
Chris@0 128 $definitions['new_long_named_entity_reference_base_field'] = BaseFieldDefinition::create('entity_reference')
Chris@0 129 ->setName('new_long_named_entity_reference_base_field')
Chris@0 130 ->setLabel(t('A new long-named base field'))
Chris@0 131 ->setSetting('target_type', 'user')
Chris@0 132 ->setSetting('handler', 'default');
Chris@0 133 $this->state->set($key, $definitions);
Chris@0 134 }
Chris@0 135
Chris@0 136 /**
Chris@0 137 * Adds a new revisionable base field to the 'entity_test_update' entity type.
Chris@0 138 *
Chris@0 139 * @param string $type
Chris@0 140 * (optional) The field type for the new field. Defaults to 'string'.
Chris@0 141 */
Chris@0 142 protected function addRevisionableBaseField($type = 'string') {
Chris@0 143 $definitions['new_base_field'] = BaseFieldDefinition::create($type)
Chris@0 144 ->setName('new_base_field')
Chris@0 145 ->setLabel(t('A new revisionable base field'))
Chris@0 146 ->setRevisionable(TRUE);
Chris@0 147 $this->state->set('entity_test_update.additional_base_field_definitions', $definitions);
Chris@0 148 }
Chris@0 149
Chris@0 150 /**
Chris@0 151 * Modifies the new base field from 'string' to 'text'.
Chris@0 152 */
Chris@0 153 protected function modifyBaseField() {
Chris@0 154 $this->addBaseField('text');
Chris@0 155 }
Chris@0 156
Chris@0 157 /**
Chris@0 158 * Promotes a field to an entity key.
Chris@0 159 */
Chris@0 160 protected function makeBaseFieldEntityKey() {
Chris@0 161 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
Chris@0 162 $entity_keys = $entity_type->getKeys();
Chris@0 163 $entity_keys['new_base_field'] = 'new_base_field';
Chris@0 164 $entity_type->set('entity_keys', $entity_keys);
Chris@0 165 $this->state->set('entity_test_update.entity_type', $entity_type);
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * Removes the new base field from the 'entity_test_update' entity type.
Chris@0 170 */
Chris@0 171 protected function removeBaseField() {
Chris@0 172 $this->state->delete('entity_test_update.additional_base_field_definitions');
Chris@0 173 }
Chris@0 174
Chris@0 175 /**
Chris@0 176 * Adds a single-field index to the base field.
Chris@0 177 */
Chris@0 178 protected function addBaseFieldIndex() {
Chris@0 179 $this->state->set('entity_test_update.additional_field_index.entity_test_update.new_base_field', TRUE);
Chris@0 180 }
Chris@0 181
Chris@0 182 /**
Chris@0 183 * Removes the index added in addBaseFieldIndex().
Chris@0 184 */
Chris@0 185 protected function removeBaseFieldIndex() {
Chris@0 186 $this->state->delete('entity_test_update.additional_field_index.entity_test_update.new_base_field');
Chris@0 187 }
Chris@0 188
Chris@0 189 /**
Chris@0 190 * Adds a new bundle field to the 'entity_test_update' entity type.
Chris@0 191 *
Chris@0 192 * @param string $type
Chris@0 193 * (optional) The field type for the new field. Defaults to 'string'.
Chris@0 194 */
Chris@0 195 protected function addBundleField($type = 'string') {
Chris@0 196 $definitions['new_bundle_field'] = FieldStorageDefinition::create($type)
Chris@0 197 ->setName('new_bundle_field')
Chris@0 198 ->setLabel(t('A new bundle field'))
Chris@0 199 ->setTargetEntityTypeId('entity_test_update');
Chris@0 200 $this->state->set('entity_test_update.additional_field_storage_definitions', $definitions);
Chris@0 201 $this->state->set('entity_test_update.additional_bundle_field_definitions.test_bundle', $definitions);
Chris@0 202 }
Chris@0 203
Chris@0 204 /**
Chris@0 205 * Modifies the new bundle field from 'string' to 'text'.
Chris@0 206 */
Chris@0 207 protected function modifyBundleField() {
Chris@0 208 $this->addBundleField('text');
Chris@0 209 }
Chris@0 210
Chris@0 211 /**
Chris@0 212 * Removes the new bundle field from the 'entity_test_update' entity type.
Chris@0 213 */
Chris@0 214 protected function removeBundleField() {
Chris@0 215 $this->state->delete('entity_test_update.additional_field_storage_definitions');
Chris@0 216 $this->state->delete('entity_test_update.additional_bundle_field_definitions.test_bundle');
Chris@0 217 }
Chris@0 218
Chris@0 219 /**
Chris@0 220 * Adds an index to the 'entity_test_update' entity type's base table.
Chris@0 221 *
Chris@0 222 * @see \Drupal\entity_test\EntityTestStorageSchema::getEntitySchema()
Chris@0 223 */
Chris@0 224 protected function addEntityIndex() {
Chris@0 225 $indexes = [
Chris@0 226 'entity_test_update__new_index' => ['name', 'test_single_property'],
Chris@0 227 ];
Chris@0 228 $this->state->set('entity_test_update.additional_entity_indexes', $indexes);
Chris@0 229 }
Chris@0 230
Chris@0 231 /**
Chris@0 232 * Removes the index added in addEntityIndex().
Chris@0 233 */
Chris@0 234 protected function removeEntityIndex() {
Chris@0 235 $this->state->delete('entity_test_update.additional_entity_indexes');
Chris@0 236 }
Chris@0 237
Chris@0 238 /**
Chris@0 239 * Renames the base table to 'entity_test_update_new'.
Chris@0 240 */
Chris@0 241 protected function renameBaseTable() {
Chris@0 242 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
Chris@0 243
Chris@0 244 $entity_type->set('base_table', 'entity_test_update_new');
Chris@0 245
Chris@0 246 $this->state->set('entity_test_update.entity_type', $entity_type);
Chris@0 247 }
Chris@0 248
Chris@0 249 /**
Chris@0 250 * Renames the data table to 'entity_test_update_data_new'.
Chris@0 251 */
Chris@0 252 protected function renameDataTable() {
Chris@0 253 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
Chris@0 254
Chris@0 255 $entity_type->set('data_table', 'entity_test_update_data_new');
Chris@0 256
Chris@0 257 $this->state->set('entity_test_update.entity_type', $entity_type);
Chris@0 258 }
Chris@0 259
Chris@0 260 /**
Chris@0 261 * Renames the revision table to 'entity_test_update_revision_new'.
Chris@0 262 */
Chris@0 263 protected function renameRevisionBaseTable() {
Chris@0 264 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
Chris@0 265
Chris@0 266 $entity_type->set('revision_table', 'entity_test_update_revision_new');
Chris@0 267
Chris@0 268 $this->state->set('entity_test_update.entity_type', $entity_type);
Chris@0 269 }
Chris@0 270
Chris@0 271 /**
Chris@0 272 * Renames the revision data table to 'entity_test_update_revision_data_new'.
Chris@0 273 */
Chris@0 274 protected function renameRevisionDataTable() {
Chris@0 275 $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
Chris@0 276
Chris@0 277 $entity_type->set('revision_data_table', 'entity_test_update_revision_data_new');
Chris@0 278
Chris@0 279 $this->state->set('entity_test_update.entity_type', $entity_type);
Chris@0 280 }
Chris@0 281
Chris@0 282 /**
Chris@0 283 * Removes the entity type.
Chris@0 284 */
Chris@0 285 protected function deleteEntityType() {
Chris@0 286 $this->state->set('entity_test_update.entity_type', 'null');
Chris@0 287 }
Chris@0 288
Chris@0 289 }