3 namespace Drupal\KernelTests\Core\ParamConverter;
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\entity_test\Entity\EntityTestMulRev;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\language\Entity\ConfigurableLanguage;
11 * Tests the entity converter when the "load_latest_revision" flag is set.
13 * @group ParamConverter
14 * @coversDefaultClass \Drupal\Core\ParamConverter\EntityConverter
16 class EntityConverterLatestRevisionTest extends KernelTestBase {
23 public static $modules = [
31 * The entity converter service.
33 * @var \Drupal\Core\ParamConverter\EntityConverter
40 protected function setUp() {
43 $this->installEntitySchema('user');
44 $this->installEntitySchema('entity_test_mulrev');
45 $this->installEntitySchema('entity_test');
46 $this->installConfig(['system', 'language']);
48 $this->converter = $this->container->get('paramconverter.entity');
50 ConfigurableLanguage::createFromLangcode('de')->save();
54 * Tests with no matching entity.
56 public function testNoEntity() {
57 $converted = $this->converter->convert(1, [
58 'load_latest_revision' => TRUE,
59 'type' => 'entity:entity_test_mulrev',
61 $this->assertEquals(NULL, $converted);
65 * Tests with no pending revision.
67 public function testEntityNoPendingRevision() {
68 $entity = EntityTestMulRev::create();
71 $converted = $this->converter->convert(1, [
72 'load_latest_revision' => TRUE,
73 'type' => 'entity:entity_test_mulrev',
75 $this->assertEquals($entity->getLoadedRevisionId(), $converted->getLoadedRevisionId());
79 * Tests with a pending revision.
81 public function testEntityWithPendingRevision() {
82 $entity = EntityTestMulRev::create();
85 $entity->isDefaultRevision(FALSE);
86 $entity->setNewRevision(TRUE);
89 $converted = $this->converter->convert(1, [
90 'load_latest_revision' => TRUE,
91 'type' => 'entity:entity_test_mulrev',
94 $this->assertEquals($entity->getLoadedRevisionId(), $converted->getLoadedRevisionId());
98 * Tests with a translated pending revision.
100 public function testWithTranslatedPendingRevision() {
101 // Enable translation for test entities.
102 $this->container->get('state')->set('entity_test.translation', TRUE);
103 $this->container->get('entity_type.bundle.info')->clearCachedBundles();
105 // Create a new English entity.
106 $entity = EntityTestMulRev::create();
109 // Create a translated pending revision.
110 $entity_type_id = 'entity_test_mulrev';
111 /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
112 $storage = $this->container->get('entity_type.manager')->getStorage($entity_type_id);
113 /** @var \Drupal\Core\Entity\ContentEntityInterface $translated_entity */
114 $translated_entity = $storage->createRevision($entity->addTranslation('de'), FALSE);
115 $translated_entity->save();
117 // Change the site language so the converters will attempt to load entities
118 // with language 'de'.
119 $this->config('system.site')->set('default_langcode', 'de')->save();
121 // The default loaded language is still 'en'.
122 EntityTestMulRev::load($entity->id());
123 $this->assertEquals('en', $entity->language()->getId());
125 // The converter will load the latest revision in the correct language.
126 $converted = $this->converter->convert(1, [
127 'load_latest_revision' => TRUE,
128 'type' => 'entity:entity_test_mulrev',
130 $this->assertEquals('de', $converted->language()->getId());
131 $this->assertEquals($translated_entity->getLoadedRevisionId(), $converted->getLoadedRevisionId());
133 // Revert back to English as default language.
134 $this->config('system.site')->set('default_langcode', 'en')->save();
136 // The converter will load the latest revision in the correct language.
137 $converted = $this->converter->convert(1, [
138 'load_latest_revision' => TRUE,
139 'type' => 'entity:entity_test_mulrev',
141 $this->assertEquals('en', $converted->language()->getId());
142 $this->assertEquals($entity->getLoadedRevisionId(), $converted->getLoadedRevisionId());
146 * Tests that pending revisions are loaded only when needed.
148 public function testOptimizedConvert() {
149 $entity = EntityTestMulRev::create();
152 // Populate static cache for the current entity.
153 $entity = EntityTestMulRev::load($entity->id());
155 // Delete the base table entry for the current entity, however, since the
156 // storage will query the revision table to get the latest revision, the
157 // logic handling pending revisions will work correctly anyway.
158 /** @var \Drupal\Core\Database\Connection $database */
159 $database = $this->container->get('database');
160 $database->delete('entity_test_mulrev')
161 ->condition('id', $entity->id())
164 // If optimization works, converting a default revision should not trigger
165 // a storage load, thus making the following assertion pass.
166 $converted = $this->converter->convert(1, [
167 'load_latest_revision' => TRUE,
168 'type' => 'entity:entity_test_mulrev',
170 $this->assertEquals($entity->getLoadedRevisionId(), $converted->getLoadedRevisionId());
174 * Test the latest revision flag and non-revisionable entities.
176 public function testConvertNonRevisionableEntityType() {
177 $entity = EntityTest::create();
180 $converted = $this->converter->convert(1, [
181 'load_latest_revision' => TRUE,
182 'type' => 'entity:entity_test',
185 $this->assertEquals($entity->id(), $converted->id());