annotate core/modules/system/src/Tests/Entity/EntityDefinitionTestTrait.php @ 19:fa3358dc1485 tip

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