Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / system / tests / src / Functional / Update / EntityUpdateToRevisionableAndPublishableTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Update;
4
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\FunctionalTests\Update\UpdatePathTestBase;
7 use Drupal\system\Tests\Entity\EntityDefinitionTestTrait;
8
9 /**
10  * Tests the upgrade path for making an entity revisionable and publishable.
11  *
12  * @see https://www.drupal.org/node/2841291
13  *
14  * @group Update
15  */
16 class EntityUpdateToRevisionableAndPublishableTest extends UpdatePathTestBase {
17
18   use EntityDefinitionTestTrait;
19   use DbUpdatesTrait;
20
21   /**
22    * The entity type manager service.
23    *
24    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
25    */
26   protected $entityTypeManager;
27
28   /**
29    * The entity definition update manager.
30    *
31    * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
32    */
33   protected $entityDefinitionUpdateManager;
34
35   /**
36    * The last installed schema repository service.
37    *
38    * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
39    */
40   protected $lastInstalledSchemaRepository;
41
42   /**
43    * The key-value collection for tracking installed storage schema.
44    *
45    * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
46    */
47   protected $installedStorageSchema;
48
49   /**
50    * The state service.
51    *
52    * @var \Drupal\Core\State\StateInterface
53    */
54   protected $state;
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function setUp() {
60     parent::setUp();
61
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();
67   }
68
69   /**
70    * {@inheritdoc}
71    */
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',
76     ];
77   }
78
79   /**
80    * Tests the conversion of an entity type to revisionable and publishable.
81    *
82    * @see entity_test_update_update_8400()
83    */
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'));
90
91     // Make the entity type revisionable, translatable and publishable.
92     $this->updateEntityTypeDefinition();
93
94     $this->enableUpdates('entity_test_update', 'entity_rev_pub_updates', 8400);
95     $this->runUpdates();
96
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'));
101
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.');
105
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()
112
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);
120
121     $translation = $revision->getTranslation('ro');
122     $this->assertEqual(1, $translation->status->value);
123
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);
128
129     $translation = $revision->getTranslation('ro');
130     $this->assertEqual(1, $translation->status->value);
131
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);
136
137     $translation = $revision->getTranslation('ro');
138     $this->assertEqual(1, $translation->status->value);
139
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);
144
145     $translation = $revision->getTranslation('ro');
146     $this->assertEqual(0, $translation->status->value);
147   }
148
149   /**
150    * Updates the 'entity_test_update' entity type to revisionable,
151    * translatable, publishable and adds revision metadata keys.
152    */
153   protected function updateEntityTypeDefinition() {
154     $entity_type = clone $this->entityTypeManager->getDefinition('entity_test_update');
155
156     $keys = $entity_type->getKeys();
157     $keys['revision'] = 'revision_id';
158     $keys['published'] = 'status';
159     $entity_type->set('entity_keys', $keys);
160
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',
166     ];
167     $entity_type->set('revision_metadata_keys', $revision_metadata_keys);
168
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');
173
174     $this->state->set('entity_test_update.entity_type', $entity_type);
175
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)
182       ->setRequired(TRUE)
183       ->setDefaultValue(TRUE);
184
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);
189
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);
195
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',
203         'weight' => 25,
204         'settings' => [
205           'rows' => 4,
206         ],
207       ]);
208
209     $this->state->set('entity_test_update.additional_base_field_definitions', [
210       'status' => $status,
211       'revision_created' => $revision_created,
212       'revision_user' => $revision_user,
213       'revision_log_message' => $revision_log_message,
214     ]);
215
216     $this->entityTypeManager->clearCachedDefinitions();
217   }
218
219 }