3 namespace Drupal\Tests\migrate\Kernel;
6 * Tests migration high water property.
10 class HighWaterTest extends MigrateTestBase {
15 public static $modules = [
20 'migrate_high_water_test',
27 protected function setUp() {
29 // Create source test table.
30 $this->sourceDatabase->schema()->createTable('high_water_node', [
33 'description' => 'Serial',
39 'description' => 'Highwater',
44 'description' => 'Title',
54 'description' => 'Contains nodes to import',
57 // Add 3 items to source table.
58 $this->sourceDatabase->insert('high_water_node')
77 $this->installEntitySchema('node');
78 $this->installEntitySchema('user');
79 $this->installSchema('node', 'node_access');
81 $this->executeMigration('high_water_test');
85 * Tests high water property of SqlBase.
87 public function testHighWater() {
88 // Assert all of the nodes have been imported.
89 $this->assertNodeExists('Item 1');
90 $this->assertNodeExists('Item 2');
91 $this->assertNodeExists('Item 3');
93 // Update Item 1 setting its high_water_property to value that is below
94 // current high water mark.
95 $this->sourceDatabase->update('high_water_node')
97 'title' => 'Item 1 updated',
100 ->condition('title', 'Item 1')
103 // Update Item 2 setting its high_water_property to value equal to
104 // current high water mark.
105 $this->sourceDatabase->update('high_water_node')
107 'title' => 'Item 2 updated',
110 ->condition('title', 'Item 2')
113 // Update Item 3 setting its high_water_property to value that is above
114 // current high water mark.
115 $this->sourceDatabase->update('high_water_node')
117 'title' => 'Item 3 updated',
120 ->condition('title', 'Item 3')
123 // Execute migration again.
124 $this->executeMigration('high_water_test');
126 // Item with lower highwater should not be updated.
127 $this->assertNodeExists('Item 1');
128 $this->assertNodeDoesNotExist('Item 1 updated');
130 // Item with equal highwater should not be updated.
131 $this->assertNodeExists('Item 2');
132 $this->assertNodeDoesNotExist('Item 2 updated');
134 // Item with greater highwater should be updated.
135 $this->assertNodeExists('Item 3 updated');
136 $this->assertNodeDoesNotExist('Item 3');
140 * Tests that the high water value can be 0.
142 public function testZeroHighwater() {
143 // Assert all of the nodes have been imported.
144 $this->assertNodeExists('Item 1');
145 $this->assertNodeExists('Item 2');
146 $this->assertNodeExists('Item 3');
147 $migration = $this->container->get('plugin.manager.migration')->CreateInstance('high_water_test', []);
148 $source = $migration->getSourcePlugin();
151 while ($source->valid()) {
156 // Expect no rows as everything is below the high water mark.
157 $this->assertSame(0, $count);
159 // Test resetting the high water mark to 0.
160 $this->container->get('keyvalue')->get('migrate:high_water')->set('high_water_test', 0);
161 $migration = $this->container->get('plugin.manager.migration')->CreateInstance('high_water_test', []);
162 $source = $migration->getSourcePlugin();
165 while ($source->valid()) {
169 $this->assertSame(3, $count);
173 * Tests that deleting the high water value causes all rows to be reimported.
175 public function testNullHighwater() {
176 // Assert all of the nodes have been imported.
177 $this->assertNodeExists('Item 1');
178 $this->assertNodeExists('Item 2');
179 $this->assertNodeExists('Item 3');
180 $migration = $this->container->get('plugin.manager.migration')->CreateInstance('high_water_test', []);
181 $source = $migration->getSourcePlugin();
184 while ($source->valid()) {
189 // Expect no rows as everything is below the high water mark.
190 $this->assertSame(0, $count);
192 // Test resetting the high water mark.
193 $this->container->get('keyvalue')->get('migrate:high_water')->delete('high_water_test');
194 $migration = $this->container->get('plugin.manager.migration')->CreateInstance('high_water_test', []);
195 $source = $migration->getSourcePlugin();
198 while ($source->valid()) {
202 $this->assertSame(3, $count);
206 * Tests high water property of SqlBase when rows marked for update.
208 public function testHighWaterUpdate() {
209 // Assert all of the nodes have been imported.
210 $this->assertNodeExists('Item 1');
211 $this->assertNodeExists('Item 2');
212 $this->assertNodeExists('Item 3');
214 // Update Item 1 setting its high_water_property to value that is below
215 // current high water mark.
216 $this->sourceDatabase->update('high_water_node')
218 'title' => 'Item 1 updated',
221 ->condition('title', 'Item 1')
224 // Update Item 2 setting its high_water_property to value equal to
225 // current high water mark.
226 $this->sourceDatabase->update('high_water_node')
228 'title' => 'Item 2 updated',
231 ->condition('title', 'Item 2')
234 // Update Item 3 setting its high_water_property to value that is above
235 // current high water mark.
236 $this->sourceDatabase->update('high_water_node')
238 'title' => 'Item 3 updated',
241 ->condition('title', 'Item 3')
244 // Set all rows as needing an update.
245 $id_map = $this->getMigration('high_water_test')->getIdMap();
246 $id_map->prepareUpdate();
248 $this->executeMigration('high_water_test');
250 // Item with lower highwater should be updated.
251 $this->assertNodeExists('Item 1 updated');
252 $this->assertNodeDoesNotExist('Item 1');
254 // Item with equal highwater should be updated.
255 $this->assertNodeExists('Item 2 updated');
256 $this->assertNodeDoesNotExist('Item 2');
258 // Item with greater highwater should be updated.
259 $this->assertNodeExists('Item 3 updated');
260 $this->assertNodeDoesNotExist('Item 3');
264 * Assert that node with given title exists.
266 * @param string $title
269 protected function assertNodeExists($title) {
270 self::assertTrue($this->nodeExists($title));
274 * Assert that node with given title does not exist.
276 * @param string $title
279 protected function assertNodeDoesNotExist($title) {
280 self::assertFalse($this->nodeExists($title));
284 * Checks if node with given title exists.
286 * @param string $title
291 protected function nodeExists($title) {
292 $query = \Drupal::entityQuery('node');
294 ->condition('title', $title)
298 return !empty($result);