Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / ContentEntityNonRevisionableFieldTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 use Drupal\entity_test\Entity\EntityTestMulRev;
6 use Drupal\entity_test\Entity\EntityTestRev;
7 use Drupal\language\Entity\ConfigurableLanguage;
8
9 /**
10  * Tests non-revisionable fields on revisionable (and translatable) entities.
11  *
12  * @group Entity
13  */
14 class ContentEntityNonRevisionableFieldTest extends EntityKernelTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['language'];
22
23   /**
24    * The EntityTestMulRev entity type storage.
25    *
26    * @var \Drupal\Core\Entity\EntityStorageInterface
27    */
28   protected $mulRev;
29
30   /**
31    * The EntityTestRev entity type storage.
32    *
33    * @var \Drupal\Core\Entity\EntityStorageInterface
34    */
35   protected $rev;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42
43     // Enable an additional language.
44     ConfigurableLanguage::createFromLangcode('de')->save();
45
46     $this->installEntitySchema('entity_test_mulrev');
47     $this->installEntitySchema('entity_test_rev');
48     $this->mulRev = $this->entityManager->getStorage('entity_test_mulrev');
49     $this->rev = $this->entityManager->getStorage('entity_test_rev');
50   }
51
52   /**
53    * Tests non-revisionable fields on revisionable and translatable entities.
54    */
55   public function testMulNonRevisionableField() {
56     $user1 = $this->createUser();
57     $user2 = $this->createUser();
58
59     // Create a test entity.
60     $entity = EntityTestMulRev::create([
61       'name' => $this->randomString(),
62       'user_id' => $user1->id(),
63       'language' => 'en',
64       'non_rev_field' => 'Huron',
65     ]);
66     $entity->save();
67
68     // Create a test entity.
69     $entity2 = EntityTestMulRev::create([
70       'name' => $this->randomString(),
71       'user_id' => $user1->id(),
72       'language' => 'en',
73       'non_rev_field' => 'Michigan',
74     ]);
75     $entity2->save();
76
77     $this->assertEquals('Huron', $entity->get('non_rev_field')->value, 'Huron found on entity 1');
78     $this->assertEquals('Michigan', $entity2->get('non_rev_field')->value, 'Michigan found on entity 2');
79
80     $entity->setNewRevision();
81     $entity->setOwner($user2);
82     $entity->save();
83     $entity2->setNewRevision();
84     $entity2->setOwner($user2);
85     $entity2->save();
86     $this->assertEquals($user2->id(), $entity->getOwner()->id(), 'User 2 found on entity 1');
87     $this->assertEquals($user2->id(), $entity2->getOwner()->id(), 'User 2 found on entity 2');
88
89     $entity->addTranslation('de');
90     $entity->save();
91     $entity2->addTranslation('de');
92     $entity2->save();
93
94     $expected_revision_ids = [
95       4 => 2,
96       3 => 1,
97       2 => 2,
98       1 => 1,
99     ];
100     $revision_ids = $this->mulRev->getQuery()
101       ->allRevisions()
102       ->sort('revision_id', 'DESC')
103       ->execute();
104     $this->assertEquals($expected_revision_ids, $revision_ids, 'Revision ids found');
105
106     $expected_non_rev_field_revision_ids = [
107       3 => 1,
108       1 => 1,
109     ];
110     $non_rev_field_revision_ids = $this->mulRev->getQuery()
111       ->allRevisions()
112       ->condition('non_rev_field', 'Huron')
113       ->sort('revision_id', 'DESC')
114       ->execute();
115     $this->assertEquals($expected_non_rev_field_revision_ids, $non_rev_field_revision_ids, 'Revision ids found');
116   }
117
118   /**
119    * Tests non-revisionable fields on revisionable entities.
120    */
121   public function testNonRevisionableField() {
122     $user1 = $this->createUser();
123     $user2 = $this->createUser();
124
125     // Create a test entity.
126     $entity = EntityTestRev::create([
127       'name' => $this->randomString(),
128       'user_id' => $user1->id(),
129       'non_rev_field' => 'Superior',
130     ]);
131     $entity->save();
132
133     // Create a test entity.
134     $entity2 = EntityTestRev::create([
135       'name' => $this->randomString(),
136       'user_id' => $user1->id(),
137       'non_rev_field' => 'Ontario',
138     ]);
139     $entity2->save();
140
141     $this->assertEquals('Superior', $entity->get('non_rev_field')->value, 'Superior found on entity 1');
142     $this->assertEquals('Ontario', $entity2->get('non_rev_field')->value, 'Ontario found on entity 2');
143
144     $entity->setNewRevision();
145     $entity->setOwner($user2);
146     $entity->save();
147     $entity2->setNewRevision();
148     $entity2->setOwner($user2);
149     $entity2->save();
150     $this->assertEquals($user2->id(), $entity->getOwner()->id(), 'User 2 found on entity 1');
151     $this->assertEquals($user2->id(), $entity2->getOwner()->id(), 'User 2 found on entity 2');
152
153     $expected_revision_ids = [
154       4 => 2,
155       3 => 1,
156       2 => 2,
157       1 => 1,
158     ];
159     $revision_ids = $this->rev->getQuery()
160       ->allRevisions()
161       ->sort('revision_id', 'DESC')
162       ->execute();
163     $this->assertEquals($expected_revision_ids, $revision_ids, 'Revision ids found');
164
165     $expected_non_rev_field_revision_ids = [
166       3 => 1,
167       1 => 1,
168     ];
169     $non_rev_field_revision_ids = $this->rev->getQuery()
170       ->allRevisions()
171       ->condition('non_rev_field', 'Superior')
172       ->sort('revision_id', 'DESC')
173       ->execute();
174     $this->assertEquals($expected_non_rev_field_revision_ids, $non_rev_field_revision_ids, 'Revision ids found');
175   }
176
177   /**
178    * Tests multi column non revisionable base field for revisionable entity.
179    */
180   public function testMultiColumnNonRevisionableBaseField() {
181     \Drupal::state()->set('entity_test.multi_column', TRUE);
182     \Drupal::entityDefinitionUpdateManager()->applyUpdates();
183     // Refresh the storage.
184     $this->mulRev = $this->entityManager->getStorage('entity_test_mulrev');
185     $user1 = $this->createUser();
186
187     // Create a test entity.
188     $entity = EntityTestMulRev::create([
189       'name' => $this->randomString(),
190       'user_id' => $user1->id(),
191       'language' => 'en',
192       'non_rev_field' => 'Huron',
193       'description' => [
194         'shape' => 'shape',
195         'color' => 'color',
196       ],
197     ]);
198     $entity->save();
199     $entity = $this->mulRev->loadUnchanged($entity->id());
200     $expected = [
201       [
202         'shape' => 'shape',
203         'color' => 'color',
204       ],
205     ];
206     $this->assertEquals('Huron', $entity->get('non_rev_field')->value, 'Huron found on entity 1');
207     $this->assertEquals($expected, $entity->description->getValue());
208   }
209
210 }