Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\migrate\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Tests migration high water property.
|
Chris@0
|
7 *
|
Chris@0
|
8 * @group migrate
|
Chris@0
|
9 */
|
Chris@0
|
10 class HighWaterTest extends MigrateTestBase {
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * {@inheritdoc}
|
Chris@0
|
14 */
|
Chris@0
|
15 public static $modules = [
|
Chris@0
|
16 'system',
|
Chris@0
|
17 'user',
|
Chris@0
|
18 'node',
|
Chris@0
|
19 'migrate',
|
Chris@0
|
20 'migrate_high_water_test',
|
Chris@0
|
21 'field',
|
Chris@0
|
22 ];
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * {@inheritdoc}
|
Chris@0
|
26 */
|
Chris@0
|
27 protected function setUp() {
|
Chris@0
|
28 parent::setUp();
|
Chris@0
|
29 // Create source test table.
|
Chris@0
|
30 $this->sourceDatabase->schema()->createTable('high_water_node', [
|
Chris@0
|
31 'fields' => [
|
Chris@0
|
32 'id' => [
|
Chris@0
|
33 'description' => 'Serial',
|
Chris@0
|
34 'type' => 'serial',
|
Chris@0
|
35 'unsigned' => TRUE,
|
Chris@0
|
36 'not null' => TRUE,
|
Chris@0
|
37 ],
|
Chris@0
|
38 'changed' => [
|
Chris@0
|
39 'description' => 'Highwater',
|
Chris@0
|
40 'type' => 'int',
|
Chris@0
|
41 'unsigned' => TRUE,
|
Chris@0
|
42 ],
|
Chris@0
|
43 'title' => [
|
Chris@0
|
44 'description' => 'Title',
|
Chris@0
|
45 'type' => 'varchar',
|
Chris@0
|
46 'length' => 128,
|
Chris@0
|
47 'not null' => TRUE,
|
Chris@0
|
48 'default' => '',
|
Chris@0
|
49 ],
|
Chris@0
|
50 ],
|
Chris@0
|
51 'primary key' => [
|
Chris@0
|
52 'id',
|
Chris@0
|
53 ],
|
Chris@0
|
54 'description' => 'Contains nodes to import',
|
Chris@0
|
55 ]);
|
Chris@0
|
56
|
Chris@0
|
57 // Add 3 items to source table.
|
Chris@0
|
58 $this->sourceDatabase->insert('high_water_node')
|
Chris@0
|
59 ->fields([
|
Chris@0
|
60 'title',
|
Chris@0
|
61 'changed',
|
Chris@0
|
62 ])
|
Chris@0
|
63 ->values([
|
Chris@0
|
64 'title' => 'Item 1',
|
Chris@0
|
65 'changed' => 1,
|
Chris@0
|
66 ])
|
Chris@0
|
67 ->values([
|
Chris@0
|
68 'title' => 'Item 2',
|
Chris@0
|
69 'changed' => 2,
|
Chris@0
|
70 ])
|
Chris@0
|
71 ->values([
|
Chris@0
|
72 'title' => 'Item 3',
|
Chris@0
|
73 'changed' => 3,
|
Chris@0
|
74 ])
|
Chris@0
|
75 ->execute();
|
Chris@0
|
76
|
Chris@0
|
77 $this->installEntitySchema('node');
|
Chris@0
|
78 $this->installEntitySchema('user');
|
Chris@0
|
79 $this->installSchema('node', 'node_access');
|
Chris@0
|
80
|
Chris@0
|
81 $this->executeMigration('high_water_test');
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * Tests high water property of SqlBase.
|
Chris@0
|
86 */
|
Chris@0
|
87 public function testHighWater() {
|
Chris@0
|
88 // Assert all of the nodes have been imported.
|
Chris@0
|
89 $this->assertNodeExists('Item 1');
|
Chris@0
|
90 $this->assertNodeExists('Item 2');
|
Chris@0
|
91 $this->assertNodeExists('Item 3');
|
Chris@0
|
92
|
Chris@0
|
93 // Update Item 1 setting its high_water_property to value that is below
|
Chris@0
|
94 // current high water mark.
|
Chris@0
|
95 $this->sourceDatabase->update('high_water_node')
|
Chris@0
|
96 ->fields([
|
Chris@0
|
97 'title' => 'Item 1 updated',
|
Chris@0
|
98 'changed' => 2,
|
Chris@0
|
99 ])
|
Chris@0
|
100 ->condition('title', 'Item 1')
|
Chris@0
|
101 ->execute();
|
Chris@0
|
102
|
Chris@0
|
103 // Update Item 2 setting its high_water_property to value equal to
|
Chris@0
|
104 // current high water mark.
|
Chris@0
|
105 $this->sourceDatabase->update('high_water_node')
|
Chris@0
|
106 ->fields([
|
Chris@0
|
107 'title' => 'Item 2 updated',
|
Chris@0
|
108 'changed' => 3,
|
Chris@0
|
109 ])
|
Chris@0
|
110 ->condition('title', 'Item 2')
|
Chris@0
|
111 ->execute();
|
Chris@0
|
112
|
Chris@0
|
113 // Update Item 3 setting its high_water_property to value that is above
|
Chris@0
|
114 // current high water mark.
|
Chris@0
|
115 $this->sourceDatabase->update('high_water_node')
|
Chris@0
|
116 ->fields([
|
Chris@0
|
117 'title' => 'Item 3 updated',
|
Chris@0
|
118 'changed' => 4,
|
Chris@0
|
119 ])
|
Chris@0
|
120 ->condition('title', 'Item 3')
|
Chris@0
|
121 ->execute();
|
Chris@0
|
122
|
Chris@0
|
123 // Execute migration again.
|
Chris@0
|
124 $this->executeMigration('high_water_test');
|
Chris@0
|
125
|
Chris@0
|
126 // Item with lower highwater should not be updated.
|
Chris@0
|
127 $this->assertNodeExists('Item 1');
|
Chris@0
|
128 $this->assertNodeDoesNotExist('Item 1 updated');
|
Chris@0
|
129
|
Chris@0
|
130 // Item with equal highwater should not be updated.
|
Chris@0
|
131 $this->assertNodeExists('Item 2');
|
Chris@0
|
132 $this->assertNodeDoesNotExist('Item 2 updated');
|
Chris@0
|
133
|
Chris@0
|
134 // Item with greater highwater should be updated.
|
Chris@0
|
135 $this->assertNodeExists('Item 3 updated');
|
Chris@0
|
136 $this->assertNodeDoesNotExist('Item 3');
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@17
|
140 * Tests that the high water value can be 0.
|
Chris@17
|
141 */
|
Chris@17
|
142 public function testZeroHighwater() {
|
Chris@17
|
143 // Assert all of the nodes have been imported.
|
Chris@17
|
144 $this->assertNodeExists('Item 1');
|
Chris@17
|
145 $this->assertNodeExists('Item 2');
|
Chris@17
|
146 $this->assertNodeExists('Item 3');
|
Chris@17
|
147 $migration = $this->container->get('plugin.manager.migration')->CreateInstance('high_water_test', []);
|
Chris@17
|
148 $source = $migration->getSourcePlugin();
|
Chris@17
|
149 $source->rewind();
|
Chris@17
|
150 $count = 0;
|
Chris@17
|
151 while ($source->valid()) {
|
Chris@17
|
152 $count++;
|
Chris@17
|
153 $source->next();
|
Chris@17
|
154 }
|
Chris@17
|
155
|
Chris@17
|
156 // Expect no rows as everything is below the high water mark.
|
Chris@17
|
157 $this->assertSame(0, $count);
|
Chris@17
|
158
|
Chris@17
|
159 // Test resetting the high water mark to 0.
|
Chris@17
|
160 $this->container->get('keyvalue')->get('migrate:high_water')->set('high_water_test', 0);
|
Chris@17
|
161 $migration = $this->container->get('plugin.manager.migration')->CreateInstance('high_water_test', []);
|
Chris@17
|
162 $source = $migration->getSourcePlugin();
|
Chris@17
|
163 $source->rewind();
|
Chris@17
|
164 $count = 0;
|
Chris@17
|
165 while ($source->valid()) {
|
Chris@17
|
166 $count++;
|
Chris@17
|
167 $source->next();
|
Chris@17
|
168 }
|
Chris@17
|
169 $this->assertSame(3, $count);
|
Chris@17
|
170 }
|
Chris@17
|
171
|
Chris@17
|
172 /**
|
Chris@17
|
173 * Tests that deleting the high water value causes all rows to be reimported.
|
Chris@17
|
174 */
|
Chris@17
|
175 public function testNullHighwater() {
|
Chris@17
|
176 // Assert all of the nodes have been imported.
|
Chris@17
|
177 $this->assertNodeExists('Item 1');
|
Chris@17
|
178 $this->assertNodeExists('Item 2');
|
Chris@17
|
179 $this->assertNodeExists('Item 3');
|
Chris@17
|
180 $migration = $this->container->get('plugin.manager.migration')->CreateInstance('high_water_test', []);
|
Chris@17
|
181 $source = $migration->getSourcePlugin();
|
Chris@17
|
182 $source->rewind();
|
Chris@17
|
183 $count = 0;
|
Chris@17
|
184 while ($source->valid()) {
|
Chris@17
|
185 $count++;
|
Chris@17
|
186 $source->next();
|
Chris@17
|
187 }
|
Chris@17
|
188
|
Chris@17
|
189 // Expect no rows as everything is below the high water mark.
|
Chris@17
|
190 $this->assertSame(0, $count);
|
Chris@17
|
191
|
Chris@17
|
192 // Test resetting the high water mark.
|
Chris@17
|
193 $this->container->get('keyvalue')->get('migrate:high_water')->delete('high_water_test');
|
Chris@17
|
194 $migration = $this->container->get('plugin.manager.migration')->CreateInstance('high_water_test', []);
|
Chris@17
|
195 $source = $migration->getSourcePlugin();
|
Chris@17
|
196 $source->rewind();
|
Chris@17
|
197 $count = 0;
|
Chris@17
|
198 while ($source->valid()) {
|
Chris@17
|
199 $count++;
|
Chris@17
|
200 $source->next();
|
Chris@17
|
201 }
|
Chris@17
|
202 $this->assertSame(3, $count);
|
Chris@17
|
203 }
|
Chris@17
|
204
|
Chris@17
|
205 /**
|
Chris@0
|
206 * Tests high water property of SqlBase when rows marked for update.
|
Chris@0
|
207 */
|
Chris@0
|
208 public function testHighWaterUpdate() {
|
Chris@0
|
209 // Assert all of the nodes have been imported.
|
Chris@0
|
210 $this->assertNodeExists('Item 1');
|
Chris@0
|
211 $this->assertNodeExists('Item 2');
|
Chris@0
|
212 $this->assertNodeExists('Item 3');
|
Chris@0
|
213
|
Chris@0
|
214 // Update Item 1 setting its high_water_property to value that is below
|
Chris@0
|
215 // current high water mark.
|
Chris@0
|
216 $this->sourceDatabase->update('high_water_node')
|
Chris@0
|
217 ->fields([
|
Chris@0
|
218 'title' => 'Item 1 updated',
|
Chris@0
|
219 'changed' => 2,
|
Chris@0
|
220 ])
|
Chris@0
|
221 ->condition('title', 'Item 1')
|
Chris@0
|
222 ->execute();
|
Chris@0
|
223
|
Chris@0
|
224 // Update Item 2 setting its high_water_property to value equal to
|
Chris@0
|
225 // current high water mark.
|
Chris@0
|
226 $this->sourceDatabase->update('high_water_node')
|
Chris@0
|
227 ->fields([
|
Chris@0
|
228 'title' => 'Item 2 updated',
|
Chris@0
|
229 'changed' => 3,
|
Chris@0
|
230 ])
|
Chris@0
|
231 ->condition('title', 'Item 2')
|
Chris@0
|
232 ->execute();
|
Chris@0
|
233
|
Chris@0
|
234 // Update Item 3 setting its high_water_property to value that is above
|
Chris@0
|
235 // current high water mark.
|
Chris@0
|
236 $this->sourceDatabase->update('high_water_node')
|
Chris@0
|
237 ->fields([
|
Chris@0
|
238 'title' => 'Item 3 updated',
|
Chris@0
|
239 'changed' => 4,
|
Chris@0
|
240 ])
|
Chris@0
|
241 ->condition('title', 'Item 3')
|
Chris@0
|
242 ->execute();
|
Chris@0
|
243
|
Chris@0
|
244 // Set all rows as needing an update.
|
Chris@0
|
245 $id_map = $this->getMigration('high_water_test')->getIdMap();
|
Chris@0
|
246 $id_map->prepareUpdate();
|
Chris@0
|
247
|
Chris@0
|
248 $this->executeMigration('high_water_test');
|
Chris@0
|
249
|
Chris@0
|
250 // Item with lower highwater should be updated.
|
Chris@0
|
251 $this->assertNodeExists('Item 1 updated');
|
Chris@0
|
252 $this->assertNodeDoesNotExist('Item 1');
|
Chris@0
|
253
|
Chris@0
|
254 // Item with equal highwater should be updated.
|
Chris@0
|
255 $this->assertNodeExists('Item 2 updated');
|
Chris@0
|
256 $this->assertNodeDoesNotExist('Item 2');
|
Chris@0
|
257
|
Chris@0
|
258 // Item with greater highwater should be updated.
|
Chris@0
|
259 $this->assertNodeExists('Item 3 updated');
|
Chris@0
|
260 $this->assertNodeDoesNotExist('Item 3');
|
Chris@0
|
261 }
|
Chris@0
|
262
|
Chris@0
|
263 /**
|
Chris@0
|
264 * Assert that node with given title exists.
|
Chris@0
|
265 *
|
Chris@0
|
266 * @param string $title
|
Chris@0
|
267 * Title of the node.
|
Chris@0
|
268 */
|
Chris@0
|
269 protected function assertNodeExists($title) {
|
Chris@0
|
270 self::assertTrue($this->nodeExists($title));
|
Chris@0
|
271 }
|
Chris@0
|
272
|
Chris@0
|
273 /**
|
Chris@0
|
274 * Assert that node with given title does not exist.
|
Chris@0
|
275 *
|
Chris@0
|
276 * @param string $title
|
Chris@0
|
277 * Title of the node.
|
Chris@0
|
278 */
|
Chris@0
|
279 protected function assertNodeDoesNotExist($title) {
|
Chris@0
|
280 self::assertFalse($this->nodeExists($title));
|
Chris@0
|
281 }
|
Chris@0
|
282
|
Chris@0
|
283 /**
|
Chris@0
|
284 * Checks if node with given title exists.
|
Chris@0
|
285 *
|
Chris@0
|
286 * @param string $title
|
Chris@0
|
287 * Title of the node.
|
Chris@0
|
288 *
|
Chris@0
|
289 * @return bool
|
Chris@0
|
290 */
|
Chris@0
|
291 protected function nodeExists($title) {
|
Chris@0
|
292 $query = \Drupal::entityQuery('node');
|
Chris@0
|
293 $result = $query
|
Chris@0
|
294 ->condition('title', $title)
|
Chris@0
|
295 ->range(0, 1)
|
Chris@0
|
296 ->execute();
|
Chris@0
|
297
|
Chris@0
|
298 return !empty($result);
|
Chris@0
|
299 }
|
Chris@0
|
300
|
Chris@0
|
301 }
|