3 namespace Drupal\Tests\system\Functional\Update;
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\FunctionalTests\Update\UpdatePathTestBase;
7 use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
10 * Tests the upgrade path for making an entity publishable.
15 class EntityUpdateToPublishableTest extends UpdatePathTestBase {
17 use EntityDefinitionTestTrait;
21 * The entity type manager service.
23 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
25 protected $entityTypeManager;
28 * The entity definition update manager.
30 * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
32 protected $entityDefinitionUpdateManager;
35 * The last installed schema repository service.
37 * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
39 protected $lastInstalledSchemaRepository;
42 * The key-value collection for tracking installed storage schema.
44 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
46 protected $installedStorageSchema;
51 * @var \Drupal\Core\State\StateInterface
58 protected function setUp() {
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();
71 protected function setDatabaseDumpFiles() {
72 $this->databaseDumpFiles = [
73 __DIR__ . '/../../../fixtures/update/drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz',
78 * Tests the conversion of an entity type to be publishable.
80 * @see entity_test_update_update_8400()
82 public function testConvertToPublishable() {
83 // Check that entity type is not publishable prior to running the update
85 $entity_test_update = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
86 $this->assertFalse($entity_test_update->getKey('published'));
88 // Make the entity type translatable and publishable.
89 $this->updateEntityTypeDefinition();
91 $this->enableUpdates('entity_test_update', 'entity_rev_pub_updates', 8400);
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'));
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.');
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);
110 $translation = $entity->getTranslation('ro');
111 $this->assertEquals(1, $translation->status->value);
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);
118 $translation = $entity->getTranslation('ro');
119 $this->assertEquals(1, $translation->status->value);
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);
126 $translation = $entity->getTranslation('ro');
127 $this->assertEquals(1, $translation->status->value);
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);
134 $translation = $entity->getTranslation('ro');
135 $this->assertEquals(0, $translation->status->value);
139 * Updates the 'entity_test_update' entity type to translatable and
142 protected function updateEntityTypeDefinition() {
143 $entity_type = clone $this->entityTypeManager->getDefinition('entity_test_update');
145 $keys = $entity_type->getKeys();
146 $keys['published'] = 'status';
147 $entity_type->set('entity_keys', $keys);
149 $entity_type->set('translatable', TRUE);
150 $entity_type->set('data_table', 'entity_test_update_data');
152 $this->state->set('entity_test_update.entity_type', $entity_type);
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)
161 ->setDefaultValue(TRUE);
163 $this->state->set('entity_test_update.additional_base_field_definitions', [
167 $this->entityTypeManager->clearCachedDefinitions();