3 namespace Drupal\Tests\system\Functional\Entity\Update;
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\system\Functional\Update\DbUpdatesTrait;
10 * Tests performing entity updates through the Update API.
14 class UpdateApiEntityDefinitionUpdateTest extends BrowserTestBase {
21 protected static $modules = ['entity_test'];
26 * @var \Drupal\Core\Entity\EntityManagerInterface
28 protected $entityManager;
31 * The entity definition update manager.
33 * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
35 protected $updatesManager;
40 protected function setUp() {
43 $this->entityManager = $this->container->get('entity.manager');
44 $this->updatesManager = $this->container->get('entity.definition_update_manager');
46 $admin = $this->drupalCreateUser([], FALSE, TRUE);
47 $this->drupalLogin($admin);
51 * Tests that individual updates applied sequentially work as expected.
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]);
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]);
64 // Make 'user_id' multiple by applying updates.
65 $this->enableUpdates('entity_test', 'entity_definition_updates', 8001);
66 $this->applyUpdates();
68 // Ensure the 'entity_test__user_id' table got created.
69 $this->assertTrue(\Drupal::database()->schema()->tableExists('entity_test__user_id'));
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]);
76 // Store multiple data and check it is correctly stored.
77 $entity->user_id = $user_ids;
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]);
84 // Make 'user_id' single again by applying updates.
85 $this->enableUpdates('entity_test', 'entity_definition_updates', 8002);
86 $this->applyUpdates();
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]);
93 // Check that only a single value is stored for 'user_id' again.
94 $entity->user_id = $user_ids;
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]);
102 * Tests that multiple updates applied in bulk work as expected.
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]);
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]);
115 // Make 'user_id' multiple and then single again by applying updates.
116 $this->enableUpdates('entity_test', 'entity_definition_updates', 8002);
117 $this->applyUpdates();
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]);
124 // Check that only a single value is stored for 'user_id' again.
125 $entity->user_id = $user_ids;
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]);
133 * Tests that entity updates are correctly reported in the status report page.
135 public function testStatusReport() {
136 // Create a test entity.
137 $entity = EntityTest::create(['name' => $this->randomString(), 'user_id' => mt_rand()]);
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');
145 // Enable an entity update and check that we have a dedicated status report
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');
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
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');
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');
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');
175 * Reloads the specified entity.
177 * @param \Drupal\entity_test\Entity\EntityTest $entity
180 * @return \Drupal\entity_test\Entity\EntityTest
181 * The reloaded entity object.
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());