Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Functional / Entity / Update / UpdateApiEntityDefinitionUpdateTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Entity\Update;
4
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\system\Functional\Update\DbUpdatesTrait;
8
9 /**
10  * Tests performing entity updates through the Update API.
11  *
12  * @group Entity
13  */
14 class UpdateApiEntityDefinitionUpdateTest extends BrowserTestBase {
15
16   use DbUpdatesTrait;
17
18   /**
19    * {@inheritdoc}
20    */
21   protected static $modules = ['entity_test'];
22
23   /**
24    * The entity manager.
25    *
26    * @var \Drupal\Core\Entity\EntityManagerInterface
27    */
28   protected $entityManager;
29
30   /**
31    * The entity definition update manager.
32    *
33    * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
34    */
35   protected $updatesManager;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42
43     $this->entityManager = $this->container->get('entity.manager');
44     $this->updatesManager = $this->container->get('entity.definition_update_manager');
45
46     $admin = $this->drupalCreateUser([], FALSE, TRUE);
47     $this->drupalLogin($admin);
48   }
49
50   /**
51    * Tests that individual updates applied sequentially work as expected.
52    */
53   public function testSingleUpdates() {
54     // Create a test entity.
55     $user_ids = [mt_rand(), mt_rand()];
56     $entity = EntityTest::create(['name' => $this->randomString(), 'user_id' => $user_ids]);
57     $entity->save();
58
59     // Check that only a single value is stored for 'user_id'.
60     $entity = $this->reloadEntity($entity);
61     $this->assertEqual(count($entity->user_id), 1);
62     $this->assertEqual($entity->user_id->target_id, $user_ids[0]);
63
64     // Make 'user_id' multiple by applying updates.
65     $this->enableUpdates('entity_test', 'entity_definition_updates', 8001);
66     $this->applyUpdates();
67
68     // Ensure the 'entity_test__user_id' table got created.
69     $this->assertTrue(\Drupal::database()->schema()->tableExists('entity_test__user_id'));
70
71     // Check that data was correctly migrated.
72     $entity = $this->reloadEntity($entity);
73     $this->assertEqual(count($entity->user_id), 1);
74     $this->assertEqual($entity->user_id->target_id, $user_ids[0]);
75
76     // Store multiple data and check it is correctly stored.
77     $entity->user_id = $user_ids;
78     $entity->save();
79     $entity = $this->reloadEntity($entity);
80     $this->assertEqual(count($entity->user_id), 2);
81     $this->assertEqual($entity->user_id[0]->target_id, $user_ids[0]);
82     $this->assertEqual($entity->user_id[1]->target_id, $user_ids[1]);
83
84     // Make 'user_id' single again by applying updates.
85     $this->enableUpdates('entity_test', 'entity_definition_updates', 8002);
86     $this->applyUpdates();
87
88     // Check that data was correctly migrated/dropped.
89     $entity = $this->reloadEntity($entity);
90     $this->assertEqual(count($entity->user_id), 1);
91     $this->assertEqual($entity->user_id->target_id, $user_ids[0]);
92
93     // Check that only a single value is stored for 'user_id' again.
94     $entity->user_id = $user_ids;
95     $entity->save();
96     $entity = $this->reloadEntity($entity);
97     $this->assertEqual(count($entity->user_id), 1);
98     $this->assertEqual($entity->user_id[0]->target_id, $user_ids[0]);
99   }
100
101   /**
102    * Tests that multiple updates applied in bulk work as expected.
103    */
104   public function testMultipleUpdates() {
105     // Create a test entity.
106     $user_ids = [mt_rand(), mt_rand()];
107     $entity = EntityTest::create(['name' => $this->randomString(), 'user_id' => $user_ids]);
108     $entity->save();
109
110     // Check that only a single value is stored for 'user_id'.
111     $entity = $this->reloadEntity($entity);
112     $this->assertEqual(count($entity->user_id), 1);
113     $this->assertEqual($entity->user_id->target_id, $user_ids[0]);
114
115     // Make 'user_id' multiple and then single again by applying updates.
116     $this->enableUpdates('entity_test', 'entity_definition_updates', 8002);
117     $this->applyUpdates();
118
119     // Check that data was correctly migrated back and forth.
120     $entity = $this->reloadEntity($entity);
121     $this->assertEqual(count($entity->user_id), 1);
122     $this->assertEqual($entity->user_id->target_id, $user_ids[0]);
123
124     // Check that only a single value is stored for 'user_id' again.
125     $entity->user_id = $user_ids;
126     $entity->save();
127     $entity = $this->reloadEntity($entity);
128     $this->assertEqual(count($entity->user_id), 1);
129     $this->assertEqual($entity->user_id[0]->target_id, $user_ids[0]);
130   }
131
132   /**
133    * Tests that entity updates are correctly reported in the status report page.
134    */
135   public function testStatusReport() {
136     // Create a test entity.
137     $entity = EntityTest::create(['name' => $this->randomString(), 'user_id' => mt_rand()]);
138     $entity->save();
139
140     // Check that the status report initially displays no error.
141     $this->drupalGet('admin/reports/status');
142     $this->assertNoRaw('Out of date');
143     $this->assertNoRaw('Mismatched entity and/or field definitions');
144
145     // Enable an entity update and check that we have a dedicated status report
146     // item.
147     $this->container->get('state')->set('entity_test.remove_name_field', TRUE);
148     $this->drupalGet('admin/reports/status');
149     $this->assertNoRaw('Out of date');
150     $this->assertRaw('Mismatched entity and/or field definitions');
151
152     // Enable a db update and check that now the entity update status report
153     // item is no longer displayed. We assume an update function will fix the
154     // mismatch.
155     $this->enableUpdates('entity_test', 'status_report', 8001);
156     $this->drupalGet('admin/reports/status');
157     $this->assertRaw('Out of date');
158     $this->assertRaw('Mismatched entity and/or field definitions');
159
160     // Apply db updates and check that entity updates were not applied.
161     $this->applyUpdates();
162     $this->drupalGet('admin/reports/status');
163     $this->assertNoRaw('Out of date');
164     $this->assertRaw('Mismatched entity and/or field definitions');
165
166     // Apply the entity updates and check that the entity update status report
167     // item is no longer displayed.
168     $this->updatesManager->applyUpdates();
169     $this->drupalGet('admin/reports/status');
170     $this->assertNoRaw('Out of date');
171     $this->assertNoRaw('Mismatched entity and/or field definitions');
172   }
173
174   /**
175    * Reloads the specified entity.
176    *
177    * @param \Drupal\entity_test\Entity\EntityTest $entity
178    *   An entity object.
179    *
180    * @return \Drupal\entity_test\Entity\EntityTest
181    *   The reloaded entity object.
182    */
183   protected function reloadEntity(EntityTest $entity) {
184     $this->entityManager->useCaches(FALSE);
185     $this->entityManager->getStorage('entity_test')->resetCache([$entity->id()]);
186     return EntityTest::load($entity->id());
187   }
188
189 }