Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / system / tests / src / Functional / Update / EntityUpdateToPublishableTest.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\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
8
9 /**
10  * Tests the upgrade path for making an entity publishable.
11  *
12  * @group Update
13  * @group legacy
14  */
15 class EntityUpdateToPublishableTest extends UpdatePathTestBase {
16
17   use EntityDefinitionTestTrait;
18   use DbUpdatesTrait;
19
20   /**
21    * The entity type manager service.
22    *
23    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
24    */
25   protected $entityTypeManager;
26
27   /**
28    * The entity definition update manager.
29    *
30    * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
31    */
32   protected $entityDefinitionUpdateManager;
33
34   /**
35    * The last installed schema repository service.
36    *
37    * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
38    */
39   protected $lastInstalledSchemaRepository;
40
41   /**
42    * The key-value collection for tracking installed storage schema.
43    *
44    * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
45    */
46   protected $installedStorageSchema;
47
48   /**
49    * The state service.
50    *
51    * @var \Drupal\Core\State\StateInterface
52    */
53   protected $state;
54
55   /**
56    * {@inheritdoc}
57    */
58   protected function setUp() {
59     parent::setUp();
60
61     $this->entityTypeManager = \Drupal::entityTypeManager();
62     $this->entityDefinitionUpdateManager = \Drupal::entityDefinitionUpdateManager();
63     $this->lastInstalledSchemaRepository = \Drupal::service('entity.last_installed_schema.repository');
64     $this->installedStorageSchema = \Drupal::keyValue('entity.storage_schema.sql');
65     $this->state = \Drupal::state();
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   protected function setDatabaseDumpFiles() {
72     $this->databaseDumpFiles = [
73       __DIR__ . '/../../../fixtures/update/drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz',
74 ];
75   }
76
77   /**
78    * Tests the conversion of an entity type to be publishable.
79    *
80    * @see entity_test_update_update_8400()
81    */
82   public function testConvertToPublishable() {
83     // Check that entity type is not publishable prior to running the update
84     // process.
85     $entity_test_update = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
86     $this->assertFalse($entity_test_update->getKey('published'));
87
88     // Make the entity type translatable and publishable.
89     $this->updateEntityTypeDefinition();
90
91     $this->enableUpdates('entity_test_update', 'entity_rev_pub_updates', 8400);
92     $this->runUpdates();
93
94     /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_test_update */
95     $entity_test_update = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
96     $this->assertEquals('status', $entity_test_update->getKey('published'));
97
98     /** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $storage */
99     $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update');
100     $this->assertCount(102, $storage->loadMultiple(), 'All test entities were found.');
101
102     // The test entity with ID 50 was created before Content Translation was
103     // enabled, which means it didn't have a 'content_translation_status' field.
104     // content_translation_update_8400() added values for that field which
105     // should now be reflected in the entity's 'status' field.
106     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
107     $entity = $storage->load(50);
108     $this->assertEquals(1, $entity->status->value);
109
110     $translation = $entity->getTranslation('ro');
111     $this->assertEquals(1, $translation->status->value);
112
113     // The test entity with ID 100 was created with Content Translation enabled
114     // and it should have the same values as entity 50.
115     $entity = $storage->load(100);
116     $this->assertEquals(1, $entity->status->value);
117
118     $translation = $entity->getTranslation('ro');
119     $this->assertEquals(1, $translation->status->value);
120
121     // The test entity 101 had 'content_translation_status' set to 0 for the
122     // English (source) language.
123     $entity = $storage->load(101);
124     $this->assertEquals(0, $entity->status->value);
125
126     $translation = $entity->getTranslation('ro');
127     $this->assertEquals(1, $translation->status->value);
128
129     // The test entity 102 had 'content_translation_status' set to 0 for the
130     // Romanian language.
131     $entity = $storage->load(102);
132     $this->assertEquals(1, $entity->status->value);
133
134     $translation = $entity->getTranslation('ro');
135     $this->assertEquals(0, $translation->status->value);
136   }
137
138   /**
139    * Updates the 'entity_test_update' entity type to translatable and
140    * publishable.
141    */
142   protected function updateEntityTypeDefinition() {
143     $entity_type = clone $this->entityTypeManager->getDefinition('entity_test_update');
144
145     $keys = $entity_type->getKeys();
146     $keys['published'] = 'status';
147     $entity_type->set('entity_keys', $keys);
148
149     $entity_type->set('translatable', TRUE);
150     $entity_type->set('data_table', 'entity_test_update_data');
151
152     $this->state->set('entity_test_update.entity_type', $entity_type);
153
154     // Add the status field to the entity type.
155     $status = BaseFieldDefinition::create('boolean')
156       ->setLabel(t('Publishing status'))
157       ->setDescription(t('A boolean indicating the published state.'))
158       ->setRevisionable(TRUE)
159       ->setTranslatable(TRUE)
160       ->setRequired(TRUE)
161       ->setDefaultValue(TRUE);
162
163     $this->state->set('entity_test_update.additional_base_field_definitions', [
164       'status' => $status,
165     ]);
166
167     $this->entityTypeManager->clearCachedDefinitions();
168   }
169
170 }