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 high water property of SqlBase when rows marked for update.
142 public function testHighWaterUpdate() {
143 // Assert all of the nodes have been imported.
144 $this->assertNodeExists('Item 1');
145 $this->assertNodeExists('Item 2');
146 $this->assertNodeExists('Item 3');
148 // Update Item 1 setting its high_water_property to value that is below
149 // current high water mark.
150 $this->sourceDatabase->update('high_water_node')
152 'title' => 'Item 1 updated',
155 ->condition('title', 'Item 1')
158 // Update Item 2 setting its high_water_property to value equal to
159 // current high water mark.
160 $this->sourceDatabase->update('high_water_node')
162 'title' => 'Item 2 updated',
165 ->condition('title', 'Item 2')
168 // Update Item 3 setting its high_water_property to value that is above
169 // current high water mark.
170 $this->sourceDatabase->update('high_water_node')
172 'title' => 'Item 3 updated',
175 ->condition('title', 'Item 3')
178 // Set all rows as needing an update.
179 $id_map = $this->getMigration('high_water_test')->getIdMap();
180 $id_map->prepareUpdate();
182 $this->executeMigration('high_water_test');
184 // Item with lower highwater should be updated.
185 $this->assertNodeExists('Item 1 updated');
186 $this->assertNodeDoesNotExist('Item 1');
188 // Item with equal highwater should be updated.
189 $this->assertNodeExists('Item 2 updated');
190 $this->assertNodeDoesNotExist('Item 2');
192 // Item with greater highwater should be updated.
193 $this->assertNodeExists('Item 3 updated');
194 $this->assertNodeDoesNotExist('Item 3');
198 * Assert that node with given title exists.
200 * @param string $title
203 protected function assertNodeExists($title) {
204 self::assertTrue($this->nodeExists($title));
208 * Assert that node with given title does not exist.
210 * @param string $title
213 protected function assertNodeDoesNotExist($title) {
214 self::assertFalse($this->nodeExists($title));
218 * Checks if node with given title exists.
220 * @param string $title
225 protected function nodeExists($title) {
226 $query = \Drupal::entityQuery('node');
228 ->condition('title', $title)
232 return !empty($result);