3 namespace Drupal\Tests\system\Functional\Update;
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\FunctionalTests\Update\UpdatePathTestBase;
7 use Drupal\system\Tests\Entity\EntityDefinitionTestTrait;
10 * Tests the upgrade path for making an entity revisionable and publishable.
12 * @see https://www.drupal.org/node/2841291
16 class EntityUpdateToRevisionableAndPublishableTest extends UpdatePathTestBase {
18 use EntityDefinitionTestTrait;
22 * The entity type manager service.
24 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
26 protected $entityTypeManager;
29 * The entity definition update manager.
31 * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
33 protected $entityDefinitionUpdateManager;
36 * The last installed schema repository service.
38 * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
40 protected $lastInstalledSchemaRepository;
43 * The key-value collection for tracking installed storage schema.
45 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
47 protected $installedStorageSchema;
52 * @var \Drupal\Core\State\StateInterface
59 protected function setUp() {
62 $this->entityTypeManager = \Drupal::entityTypeManager();
63 $this->entityDefinitionUpdateManager = \Drupal::entityDefinitionUpdateManager();
64 $this->lastInstalledSchemaRepository = \Drupal::service('entity.last_installed_schema.repository');
65 $this->installedStorageSchema = \Drupal::keyValue('entity.storage_schema.sql');
66 $this->state = \Drupal::state();
72 protected function setDatabaseDumpFiles() {
73 $this->databaseDumpFiles = [
74 __DIR__ . '/../../../fixtures/update/drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz',
75 __DIR__ . '/../../../fixtures/update/drupal-8.entity-test-schema-converter-enabled.php',
80 * Tests the conversion of an entity type to revisionable and publishable.
82 * @see entity_test_update_update_8400()
84 public function testConvertToRevisionableAndPublishable() {
85 // Check that entity type is not revisionable nor publishable prior to
86 // running the update process.
87 $entity_test_update = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
88 $this->assertFalse($entity_test_update->isRevisionable());
89 $this->assertFalse($entity_test_update->getKey('published'));
91 // Make the entity type revisionable, translatable and publishable.
92 $this->updateEntityTypeDefinition();
94 $this->enableUpdates('entity_test_update', 'entity_rev_pub_updates', 8400);
97 /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_test_update */
98 $entity_test_update = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
99 $this->assertTrue($entity_test_update->isRevisionable());
100 $this->assertEqual('status', $entity_test_update->getKey('published'));
102 /** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $storage */
103 $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update');
104 $this->assertEqual(count($storage->loadMultiple()), 102, 'All test entities were found.');
106 // The conversion to revisionable is already tested by
107 // \Drupal\system\Tests\Entity\Update\SqlContentEntityStorageSchemaConverterTest::testMakeRevisionable()
108 // so we only need to check that some special cases are handled.
109 // All the checks implemented here are taking into consideration the special
110 // conditions in which the test database was created.
111 // @see _entity_test_update_create_test_entities()
113 // The test entity with ID 50 was created before Content Translation was
114 // enabled, which means it didn't have a 'content_translation_status' field.
115 // content_translation_update_8400() added values for that field which
116 // should now be reflected in the entity's 'status' field.
117 /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
118 $revision = $storage->loadRevision(50);
119 $this->assertEqual(1, $revision->status->value);
121 $translation = $revision->getTranslation('ro');
122 $this->assertEqual(1, $translation->status->value);
124 // The test entity with ID 100 was created with Content Translation enabled
125 // and it should have the same values as entity 50.
126 $revision = $storage->loadRevision(100);
127 $this->assertEqual(1, $revision->status->value);
129 $translation = $revision->getTranslation('ro');
130 $this->assertEqual(1, $translation->status->value);
132 // The test entity 101 had 'content_translation_status' set to 0 for the
133 // English (source) language.
134 $revision = $storage->loadRevision(101);
135 $this->assertEqual(0, $revision->status->value);
137 $translation = $revision->getTranslation('ro');
138 $this->assertEqual(1, $translation->status->value);
140 // The test entity 102 had 'content_translation_status' set to 0 for the
141 // Romanian language.
142 $revision = $storage->loadRevision(102);
143 $this->assertEqual(1, $revision->status->value);
145 $translation = $revision->getTranslation('ro');
146 $this->assertEqual(0, $translation->status->value);
150 * Updates the 'entity_test_update' entity type to revisionable,
151 * translatable, publishable and adds revision metadata keys.
153 protected function updateEntityTypeDefinition() {
154 $entity_type = clone $this->entityTypeManager->getDefinition('entity_test_update');
156 $keys = $entity_type->getKeys();
157 $keys['revision'] = 'revision_id';
158 $keys['published'] = 'status';
159 $entity_type->set('entity_keys', $keys);
161 $revision_metadata_keys = [
162 'revision_user' => 'revision_user',
163 'revision_created' => 'revision_created',
164 'revision_log_message' => 'revision_log_message',
165 'revision_default' => 'revision_default',
167 $entity_type->set('revision_metadata_keys', $revision_metadata_keys);
169 $entity_type->set('translatable', TRUE);
170 $entity_type->set('data_table', 'entity_test_update_data');
171 $entity_type->set('revision_table', 'entity_test_update_revision');
172 $entity_type->set('revision_data_table', 'entity_test_update_revision_data');
174 $this->state->set('entity_test_update.entity_type', $entity_type);
176 // Also add the status and revision metadata base fields to the entity type.
177 $status = BaseFieldDefinition::create('boolean')
178 ->setLabel(t('Publishing status'))
179 ->setDescription(t('A boolean indicating the published state.'))
180 ->setRevisionable(TRUE)
181 ->setTranslatable(TRUE)
183 ->setDefaultValue(TRUE);
185 $revision_created = BaseFieldDefinition::create('created')
186 ->setLabel(t('Revision create time'))
187 ->setDescription(t('The time that the current revision was created.'))
188 ->setRevisionable(TRUE);
190 $revision_user = BaseFieldDefinition::create('entity_reference')
191 ->setLabel(t('Revision user'))
192 ->setDescription(t('The user ID of the author of the current revision.'))
193 ->setSetting('target_type', 'user')
194 ->setRevisionable(TRUE);
196 $revision_log_message = BaseFieldDefinition::create('string_long')
197 ->setLabel(t('Revision log message'))
198 ->setDescription(t('Briefly describe the changes you have made.'))
199 ->setRevisionable(TRUE)
200 ->setDefaultValue('')
201 ->setDisplayOptions('form', [
202 'type' => 'string_textarea',
209 $this->state->set('entity_test_update.additional_base_field_definitions', [
211 'revision_created' => $revision_created,
212 'revision_user' => $revision_user,
213 'revision_log_message' => $revision_log_message,
216 $this->entityTypeManager->clearCachedDefinitions();