comparison core/modules/migrate/tests/src/Kernel/HighWaterTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\migrate\Kernel;
4
5 /**
6 * Tests migration high water property.
7 *
8 * @group migrate
9 */
10 class HighWaterTest extends MigrateTestBase {
11
12 /**
13 * {@inheritdoc}
14 */
15 public static $modules = [
16 'system',
17 'user',
18 'node',
19 'migrate',
20 'migrate_high_water_test',
21 'field',
22 ];
23
24 /**
25 * {@inheritdoc}
26 */
27 protected function setUp() {
28 parent::setUp();
29 // Create source test table.
30 $this->sourceDatabase->schema()->createTable('high_water_node', [
31 'fields' => [
32 'id' => [
33 'description' => 'Serial',
34 'type' => 'serial',
35 'unsigned' => TRUE,
36 'not null' => TRUE,
37 ],
38 'changed' => [
39 'description' => 'Highwater',
40 'type' => 'int',
41 'unsigned' => TRUE,
42 ],
43 'title' => [
44 'description' => 'Title',
45 'type' => 'varchar',
46 'length' => 128,
47 'not null' => TRUE,
48 'default' => '',
49 ],
50 ],
51 'primary key' => [
52 'id',
53 ],
54 'description' => 'Contains nodes to import',
55 ]);
56
57 // Add 3 items to source table.
58 $this->sourceDatabase->insert('high_water_node')
59 ->fields([
60 'title',
61 'changed',
62 ])
63 ->values([
64 'title' => 'Item 1',
65 'changed' => 1,
66 ])
67 ->values([
68 'title' => 'Item 2',
69 'changed' => 2,
70 ])
71 ->values([
72 'title' => 'Item 3',
73 'changed' => 3,
74 ])
75 ->execute();
76
77 $this->installEntitySchema('node');
78 $this->installEntitySchema('user');
79 $this->installSchema('node', 'node_access');
80
81 $this->executeMigration('high_water_test');
82 }
83
84 /**
85 * Tests high water property of SqlBase.
86 */
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');
92
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')
96 ->fields([
97 'title' => 'Item 1 updated',
98 'changed' => 2,
99 ])
100 ->condition('title', 'Item 1')
101 ->execute();
102
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')
106 ->fields([
107 'title' => 'Item 2 updated',
108 'changed' => 3,
109 ])
110 ->condition('title', 'Item 2')
111 ->execute();
112
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')
116 ->fields([
117 'title' => 'Item 3 updated',
118 'changed' => 4,
119 ])
120 ->condition('title', 'Item 3')
121 ->execute();
122
123 // Execute migration again.
124 $this->executeMigration('high_water_test');
125
126 // Item with lower highwater should not be updated.
127 $this->assertNodeExists('Item 1');
128 $this->assertNodeDoesNotExist('Item 1 updated');
129
130 // Item with equal highwater should not be updated.
131 $this->assertNodeExists('Item 2');
132 $this->assertNodeDoesNotExist('Item 2 updated');
133
134 // Item with greater highwater should be updated.
135 $this->assertNodeExists('Item 3 updated');
136 $this->assertNodeDoesNotExist('Item 3');
137 }
138
139 /**
140 * Tests high water property of SqlBase when rows marked for update.
141 */
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');
147
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')
151 ->fields([
152 'title' => 'Item 1 updated',
153 'changed' => 2,
154 ])
155 ->condition('title', 'Item 1')
156 ->execute();
157
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')
161 ->fields([
162 'title' => 'Item 2 updated',
163 'changed' => 3,
164 ])
165 ->condition('title', 'Item 2')
166 ->execute();
167
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')
171 ->fields([
172 'title' => 'Item 3 updated',
173 'changed' => 4,
174 ])
175 ->condition('title', 'Item 3')
176 ->execute();
177
178 // Set all rows as needing an update.
179 $id_map = $this->getMigration('high_water_test')->getIdMap();
180 $id_map->prepareUpdate();
181
182 $this->executeMigration('high_water_test');
183
184 // Item with lower highwater should be updated.
185 $this->assertNodeExists('Item 1 updated');
186 $this->assertNodeDoesNotExist('Item 1');
187
188 // Item with equal highwater should be updated.
189 $this->assertNodeExists('Item 2 updated');
190 $this->assertNodeDoesNotExist('Item 2');
191
192 // Item with greater highwater should be updated.
193 $this->assertNodeExists('Item 3 updated');
194 $this->assertNodeDoesNotExist('Item 3');
195 }
196
197 /**
198 * Assert that node with given title exists.
199 *
200 * @param string $title
201 * Title of the node.
202 */
203 protected function assertNodeExists($title) {
204 self::assertTrue($this->nodeExists($title));
205 }
206
207 /**
208 * Assert that node with given title does not exist.
209 *
210 * @param string $title
211 * Title of the node.
212 */
213 protected function assertNodeDoesNotExist($title) {
214 self::assertFalse($this->nodeExists($title));
215 }
216
217 /**
218 * Checks if node with given title exists.
219 *
220 * @param string $title
221 * Title of the node.
222 *
223 * @return bool
224 */
225 protected function nodeExists($title) {
226 $query = \Drupal::entityQuery('node');
227 $result = $query
228 ->condition('title', $title)
229 ->range(0, 1)
230 ->execute();
231
232 return !empty($result);
233 }
234
235 }