40d38867f0c8c1e400ca8b75879af33342f3bd18
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / ParamConverter / EntityConverterLatestRevisionTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\ParamConverter;
4
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;
9
10 /**
11  * Tests the entity converter when the "load_latest_revision" flag is set.
12  *
13  * @group ParamConverter
14  * @coversDefaultClass \Drupal\Core\ParamConverter\EntityConverter
15  */
16 class EntityConverterLatestRevisionTest extends KernelTestBase {
17
18   /**
19    * Modules to install.
20    *
21    * @var array
22    */
23   public static $modules = [
24     'entity_test',
25     'user',
26     'language',
27     'system',
28   ];
29
30   /**
31    * The entity converter service.
32    *
33    * @var \Drupal\Core\ParamConverter\EntityConverter
34    */
35   protected $converter;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42
43     $this->installEntitySchema('user');
44     $this->installEntitySchema('entity_test_mulrev');
45     $this->installEntitySchema('entity_test');
46     $this->installConfig(['system', 'language']);
47
48     $this->converter = $this->container->get('paramconverter.entity');
49
50     ConfigurableLanguage::createFromLangcode('de')->save();
51   }
52
53   /**
54    * Tests with no matching entity.
55    */
56   public function testNoEntity() {
57     $converted = $this->converter->convert(1, [
58       'load_latest_revision' => TRUE,
59       'type' => 'entity:entity_test_mulrev',
60     ], 'foo', []);
61     $this->assertEquals(NULL, $converted);
62   }
63
64   /**
65    * Tests with no pending revision.
66    */
67   public function testEntityNoPendingRevision() {
68     $entity = EntityTestMulRev::create();
69     $entity->save();
70
71     $converted = $this->converter->convert(1, [
72       'load_latest_revision' => TRUE,
73       'type' => 'entity:entity_test_mulrev',
74     ], 'foo', []);
75     $this->assertEquals($entity->getLoadedRevisionId(), $converted->getLoadedRevisionId());
76   }
77
78   /**
79    * Tests with a pending revision.
80    */
81   public function testEntityWithPendingRevision() {
82     $entity = EntityTestMulRev::create();
83     $entity->save();
84
85     $entity->isDefaultRevision(FALSE);
86     $entity->setNewRevision(TRUE);
87     $entity->save();
88
89     $converted = $this->converter->convert(1, [
90       'load_latest_revision' => TRUE,
91       'type' => 'entity:entity_test_mulrev',
92     ], 'foo', []);
93
94     $this->assertEquals($entity->getLoadedRevisionId(), $converted->getLoadedRevisionId());
95   }
96
97   /**
98    * Tests with a translated pending revision.
99    */
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();
104
105     // Create a new English entity.
106     $entity = EntityTestMulRev::create();
107     $entity->save();
108
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();
116
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();
120
121     // The default loaded language is still 'en'.
122     EntityTestMulRev::load($entity->id());
123     $this->assertEquals('en', $entity->language()->getId());
124
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',
129     ], 'foo', []);
130     $this->assertEquals('de', $converted->language()->getId());
131     $this->assertEquals($translated_entity->getLoadedRevisionId(), $converted->getLoadedRevisionId());
132
133     // Revert back to English as default language.
134     $this->config('system.site')->set('default_langcode', 'en')->save();
135
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',
140     ], 'foo', []);
141     $this->assertEquals('en', $converted->language()->getId());
142     $this->assertEquals($entity->getLoadedRevisionId(), $converted->getLoadedRevisionId());
143   }
144
145   /**
146    * Tests that pending revisions are loaded only when needed.
147    */
148   public function testOptimizedConvert() {
149     $entity = EntityTestMulRev::create();
150     $entity->save();
151
152     // Populate static cache for the current entity.
153     $entity = EntityTestMulRev::load($entity->id());
154
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())
162       ->execute();
163
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',
169     ], 'foo', []);
170     $this->assertEquals($entity->getLoadedRevisionId(), $converted->getLoadedRevisionId());
171   }
172
173   /**
174    * Test the latest revision flag and non-revisionable entities.
175    */
176   public function testConvertNonRevisionableEntityType() {
177     $entity = EntityTest::create();
178     $entity->save();
179
180     $converted = $this->converter->convert(1, [
181       'load_latest_revision' => TRUE,
182       'type' => 'entity:entity_test',
183     ], 'foo', []);
184
185     $this->assertEquals($entity->id(), $converted->id());
186   }
187
188 }