Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Contains \Drupal\Tests\migrate\Unit\MigrateSourceTest.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 namespace Drupal\Tests\migrate\Unit;
|
Chris@0
|
9
|
Chris@0
|
10 use Drupal\Core\Cache\CacheBackendInterface;
|
Chris@0
|
11 use Drupal\Core\DependencyInjection\ContainerBuilder;
|
Chris@0
|
12 use Drupal\Core\Extension\ModuleHandlerInterface;
|
Chris@0
|
13 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
|
Chris@0
|
14 use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
|
Chris@0
|
15 use Drupal\migrate\MigrateException;
|
Chris@0
|
16 use Drupal\migrate\MigrateExecutable;
|
Chris@0
|
17 use Drupal\migrate\MigrateSkipRowException;
|
Chris@0
|
18 use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
|
Chris@0
|
19 use Drupal\migrate\Plugin\MigrateIdMapInterface;
|
Chris@0
|
20 use Drupal\migrate\Row;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\source\SourcePluginBase
|
Chris@0
|
24 * @group migrate
|
Chris@0
|
25 */
|
Chris@0
|
26 class MigrateSourceTest extends MigrateTestCase {
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Override the migration config.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @var array
|
Chris@0
|
32 */
|
Chris@0
|
33 protected $defaultMigrationConfiguration = [
|
Chris@0
|
34 'id' => 'test_migration',
|
Chris@0
|
35 'source' => [],
|
Chris@0
|
36 ];
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Test row data.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @var array
|
Chris@0
|
42 */
|
Chris@0
|
43 protected $row = ['test_sourceid1' => '1', 'timestamp' => 500];
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Test source ids.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @var array
|
Chris@0
|
49 */
|
Chris@0
|
50 protected $sourceIds = ['test_sourceid1' => 'test_sourceid1'];
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * The migration entity.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @var \Drupal\migrate\Plugin\MigrationInterface
|
Chris@0
|
56 */
|
Chris@0
|
57 protected $migration;
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * The migrate executable.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @var \Drupal\migrate\MigrateExecutable
|
Chris@0
|
63 */
|
Chris@0
|
64 protected $executable;
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * Gets the source plugin to test.
|
Chris@0
|
68 *
|
Chris@0
|
69 * @param array $configuration
|
Chris@0
|
70 * (optional) The source configuration. Defaults to an empty array.
|
Chris@0
|
71 * @param array $migrate_config
|
Chris@0
|
72 * (optional) The migration configuration to be used in
|
Chris@0
|
73 * parent::getMigration(). Defaults to an empty array.
|
Chris@0
|
74 * @param int $status
|
Chris@0
|
75 * (optional) The default status for the new rows to be imported. Defaults
|
Chris@0
|
76 * to MigrateIdMapInterface::STATUS_NEEDS_UPDATE.
|
Chris@0
|
77 *
|
Chris@0
|
78 * @return \Drupal\migrate\Plugin\MigrateSourceInterface
|
Chris@0
|
79 * A mocked source plugin.
|
Chris@0
|
80 */
|
Chris@0
|
81 protected function getSource($configuration = [], $migrate_config = [], $status = MigrateIdMapInterface::STATUS_NEEDS_UPDATE, $high_water_value = NULL) {
|
Chris@0
|
82 $container = new ContainerBuilder();
|
Chris@0
|
83 \Drupal::setContainer($container);
|
Chris@0
|
84
|
Chris@0
|
85 $key_value = $this->getMock(KeyValueStoreInterface::class);
|
Chris@0
|
86
|
Chris@0
|
87 $key_value_factory = $this->getMock(KeyValueFactoryInterface::class);
|
Chris@0
|
88 $key_value_factory
|
Chris@0
|
89 ->method('get')
|
Chris@0
|
90 ->with('migrate:high_water')
|
Chris@0
|
91 ->willReturn($key_value);
|
Chris@0
|
92 $container->set('keyvalue', $key_value_factory);
|
Chris@0
|
93
|
Chris@0
|
94 $container->set('cache.migrate', $this->getMock(CacheBackendInterface::class));
|
Chris@0
|
95
|
Chris@0
|
96 $this->migrationConfiguration = $this->defaultMigrationConfiguration + $migrate_config;
|
Chris@0
|
97 $this->migration = parent::getMigration();
|
Chris@0
|
98 $this->executable = $this->getMigrateExecutable($this->migration);
|
Chris@0
|
99
|
Chris@0
|
100 // Update the idMap for Source so the default is that the row has already
|
Chris@0
|
101 // been imported. This allows us to use the highwater mark to decide on the
|
Chris@0
|
102 // outcome of whether we choose to import the row.
|
Chris@0
|
103 $id_map_array = ['original_hash' => '', 'hash' => '', 'source_row_status' => $status];
|
Chris@0
|
104 $this->idMap
|
Chris@0
|
105 ->expects($this->any())
|
Chris@0
|
106 ->method('getRowBySource')
|
Chris@0
|
107 ->willReturn($id_map_array);
|
Chris@0
|
108
|
Chris@0
|
109 $constructor_args = [$configuration, 'd6_action', [], $this->migration];
|
Chris@0
|
110 $methods = ['getModuleHandler', 'fields', 'getIds', '__toString', 'prepareRow', 'initializeIterator'];
|
Chris@0
|
111 $source_plugin = $this->getMock(SourcePluginBase::class, $methods, $constructor_args);
|
Chris@0
|
112
|
Chris@0
|
113 $source_plugin
|
Chris@0
|
114 ->method('fields')
|
Chris@0
|
115 ->willReturn([]);
|
Chris@0
|
116 $source_plugin
|
Chris@0
|
117 ->method('getIds')
|
Chris@0
|
118 ->willReturn([]);
|
Chris@0
|
119 $source_plugin
|
Chris@0
|
120 ->method('__toString')
|
Chris@0
|
121 ->willReturn('');
|
Chris@0
|
122 $source_plugin
|
Chris@0
|
123 ->method('prepareRow')
|
Chris@0
|
124 ->willReturn(empty($migrate_config['prepare_row_false']));
|
Chris@0
|
125
|
Chris@0
|
126 $rows = [$this->row];
|
Chris@0
|
127 if (isset($configuration['high_water_property']) && isset($high_water_value)) {
|
Chris@0
|
128 $property = $configuration['high_water_property']['name'];
|
Chris@0
|
129 $rows = array_filter($rows, function (array $row) use ($property, $high_water_value) {
|
Chris@0
|
130 return $row[$property] >= $high_water_value;
|
Chris@0
|
131 });
|
Chris@0
|
132 }
|
Chris@0
|
133 $iterator = new \ArrayIterator($rows);
|
Chris@0
|
134
|
Chris@0
|
135 $source_plugin
|
Chris@0
|
136 ->method('initializeIterator')
|
Chris@0
|
137 ->willReturn($iterator);
|
Chris@0
|
138
|
Chris@0
|
139 $module_handler = $this->getMock(ModuleHandlerInterface::class);
|
Chris@0
|
140 $source_plugin
|
Chris@0
|
141 ->method('getModuleHandler')
|
Chris@0
|
142 ->willReturn($module_handler);
|
Chris@0
|
143
|
Chris@0
|
144 $this->migration
|
Chris@0
|
145 ->method('getSourcePlugin')
|
Chris@0
|
146 ->willReturn($source_plugin);
|
Chris@0
|
147
|
Chris@0
|
148 return $source_plugin;
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * @covers ::__construct
|
Chris@0
|
153 */
|
Chris@0
|
154 public function testHighwaterTrackChangesIncompatible() {
|
Chris@0
|
155 $source_config = ['track_changes' => TRUE, 'high_water_property' => ['name' => 'something']];
|
Chris@0
|
156 $this->setExpectedException(MigrateException::class);
|
Chris@0
|
157 $this->getSource($source_config);
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 /**
|
Chris@0
|
161 * Test that the source count is correct.
|
Chris@0
|
162 *
|
Chris@0
|
163 * @covers ::count
|
Chris@0
|
164 */
|
Chris@0
|
165 public function testCount() {
|
Chris@0
|
166 // Mock the cache to validate set() receives appropriate arguments.
|
Chris@0
|
167 $container = new ContainerBuilder();
|
Chris@0
|
168 $cache = $this->getMock(CacheBackendInterface::class);
|
Chris@0
|
169 $cache->expects($this->any())->method('set')
|
Chris@0
|
170 ->with($this->isType('string'), $this->isType('int'), $this->isType('int'));
|
Chris@0
|
171 $container->set('cache.migrate', $cache);
|
Chris@0
|
172 \Drupal::setContainer($container);
|
Chris@0
|
173
|
Chris@0
|
174 // Test that the basic count works.
|
Chris@0
|
175 $source = $this->getSource();
|
Chris@0
|
176 $this->assertEquals(1, $source->count());
|
Chris@0
|
177
|
Chris@0
|
178 // Test caching the count works.
|
Chris@0
|
179 $source = $this->getSource(['cache_counts' => TRUE]);
|
Chris@0
|
180 $this->assertEquals(1, $source->count());
|
Chris@0
|
181
|
Chris@0
|
182 // Test the skip argument.
|
Chris@0
|
183 $source = $this->getSource(['skip_count' => TRUE]);
|
Chris@0
|
184 $this->assertEquals(-1, $source->count());
|
Chris@0
|
185
|
Chris@0
|
186 $this->migrationConfiguration['id'] = 'test_migration';
|
Chris@0
|
187 $migration = $this->getMigration();
|
Chris@0
|
188 $source = new StubSourceGeneratorPlugin([], '', [], $migration);
|
Chris@0
|
189
|
Chris@0
|
190 // Test the skipCount property's default value.
|
Chris@0
|
191 $this->assertEquals(-1, $source->count());
|
Chris@0
|
192
|
Chris@0
|
193 // Test the count value using a generator.
|
Chris@0
|
194 $source = new StubSourceGeneratorPlugin(['skip_count' => FALSE], '', [], $migration);
|
Chris@0
|
195 $this->assertEquals(3, $source->count());
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 /**
|
Chris@0
|
199 * Test that the key can be set for the count cache.
|
Chris@0
|
200 *
|
Chris@0
|
201 * @covers ::count
|
Chris@0
|
202 */
|
Chris@0
|
203 public function testCountCacheKey() {
|
Chris@0
|
204 // Mock the cache to validate set() receives appropriate arguments.
|
Chris@0
|
205 $container = new ContainerBuilder();
|
Chris@0
|
206 $cache = $this->getMock(CacheBackendInterface::class);
|
Chris@0
|
207 $cache->expects($this->any())->method('set')
|
Chris@0
|
208 ->with('test_key', $this->isType('int'), $this->isType('int'));
|
Chris@0
|
209 $container->set('cache.migrate', $cache);
|
Chris@0
|
210 \Drupal::setContainer($container);
|
Chris@0
|
211
|
Chris@0
|
212 // Test caching the count with a configured key works.
|
Chris@0
|
213 $source = $this->getSource(['cache_counts' => TRUE, 'cache_key' => 'test_key']);
|
Chris@0
|
214 $this->assertEquals(1, $source->count());
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 /**
|
Chris@0
|
218 * Test that we don't get a row if prepareRow() is false.
|
Chris@0
|
219 */
|
Chris@0
|
220 public function testPrepareRowFalse() {
|
Chris@0
|
221 $source = $this->getSource([], ['prepare_row_false' => TRUE]);
|
Chris@0
|
222
|
Chris@0
|
223 $source->rewind();
|
Chris@0
|
224 $this->assertNull($source->current(), 'No row is available when prepareRow() is false.');
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 /**
|
Chris@0
|
228 * Test that $row->needsUpdate() works as expected.
|
Chris@0
|
229 */
|
Chris@0
|
230 public function testNextNeedsUpdate() {
|
Chris@0
|
231 $source = $this->getSource();
|
Chris@0
|
232
|
Chris@0
|
233 // $row->needsUpdate() === TRUE so we get a row.
|
Chris@0
|
234 $source->rewind();
|
Chris@0
|
235 $this->assertTrue(is_a($source->current(), 'Drupal\migrate\Row'), '$row->needsUpdate() is TRUE so we got a row.');
|
Chris@0
|
236
|
Chris@0
|
237 // Test that we don't get a row when the incoming row is marked as imported.
|
Chris@0
|
238 $source = $this->getSource([], [], MigrateIdMapInterface::STATUS_IMPORTED);
|
Chris@0
|
239 $source->rewind();
|
Chris@0
|
240 $this->assertNull($source->current(), 'Row was already imported, should be NULL');
|
Chris@0
|
241 }
|
Chris@0
|
242
|
Chris@0
|
243 /**
|
Chris@0
|
244 * Test that an outdated highwater mark does not cause a row to be imported.
|
Chris@0
|
245 */
|
Chris@0
|
246 public function testOutdatedHighwater() {
|
Chris@0
|
247 $configuration = [
|
Chris@0
|
248 'high_water_property' => [
|
Chris@0
|
249 'name' => 'timestamp',
|
Chris@0
|
250 ],
|
Chris@0
|
251 ];
|
Chris@0
|
252 $source = $this->getSource($configuration, [], MigrateIdMapInterface::STATUS_IMPORTED, $this->row['timestamp'] + 1);
|
Chris@0
|
253
|
Chris@0
|
254 // The current highwater mark is now higher than the row timestamp so no row
|
Chris@0
|
255 // is expected.
|
Chris@0
|
256 $source->rewind();
|
Chris@0
|
257 $this->assertNull($source->current(), 'Original highwater mark is higher than incoming row timestamp.');
|
Chris@0
|
258 }
|
Chris@0
|
259
|
Chris@0
|
260 /**
|
Chris@0
|
261 * Test that a highwater mark newer than our saved one imports a row.
|
Chris@0
|
262 *
|
Chris@0
|
263 * @throws \Exception
|
Chris@0
|
264 */
|
Chris@0
|
265 public function testNewHighwater() {
|
Chris@0
|
266 $configuration = [
|
Chris@0
|
267 'high_water_property' => [
|
Chris@0
|
268 'name' => 'timestamp',
|
Chris@0
|
269 ],
|
Chris@0
|
270 ];
|
Chris@0
|
271 // Set a highwater property field for source. Now we should have a row
|
Chris@0
|
272 // because the row timestamp is greater than the current highwater mark.
|
Chris@0
|
273 $source = $this->getSource($configuration, [], MigrateIdMapInterface::STATUS_IMPORTED, $this->row['timestamp'] - 1);
|
Chris@0
|
274
|
Chris@0
|
275 $source->rewind();
|
Chris@0
|
276 $this->assertInstanceOf(Row::class, $source->current(), 'Incoming row timestamp is greater than current highwater mark so we have a row.');
|
Chris@0
|
277 }
|
Chris@0
|
278
|
Chris@0
|
279 /**
|
Chris@0
|
280 * Test basic row preparation.
|
Chris@0
|
281 *
|
Chris@0
|
282 * @covers ::prepareRow
|
Chris@0
|
283 */
|
Chris@0
|
284 public function testPrepareRow() {
|
Chris@0
|
285 $this->migrationConfiguration['id'] = 'test_migration';
|
Chris@0
|
286
|
Chris@0
|
287 // Get a new migration with an id.
|
Chris@0
|
288 $migration = $this->getMigration();
|
Chris@0
|
289 $source = new StubSourcePlugin([], '', [], $migration);
|
Chris@0
|
290 $row = new Row();
|
Chris@0
|
291
|
Chris@0
|
292 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
|
Chris@0
|
293 $module_handler->invokeAll('migrate_prepare_row', [$row, $source, $migration])
|
Chris@0
|
294 ->willReturn([TRUE, TRUE])
|
Chris@0
|
295 ->shouldBeCalled();
|
Chris@0
|
296 $module_handler->invokeAll('migrate_' . $migration->id() . '_prepare_row', [$row, $source, $migration])
|
Chris@0
|
297 ->willReturn([TRUE, TRUE])
|
Chris@0
|
298 ->shouldBeCalled();
|
Chris@0
|
299 $source->setModuleHandler($module_handler->reveal());
|
Chris@0
|
300
|
Chris@0
|
301 // Ensure we don't log this to the mapping table.
|
Chris@0
|
302 $this->idMap->expects($this->never())
|
Chris@0
|
303 ->method('saveIdMapping');
|
Chris@0
|
304
|
Chris@0
|
305 $this->assertTrue($source->prepareRow($row));
|
Chris@0
|
306
|
Chris@0
|
307 // Track_changes...
|
Chris@0
|
308 $source = new StubSourcePlugin(['track_changes' => TRUE], '', [], $migration);
|
Chris@0
|
309 $row2 = $this->prophesize(Row::class);
|
Chris@0
|
310 $row2->rehash()
|
Chris@0
|
311 ->shouldBeCalled();
|
Chris@0
|
312 $module_handler->invokeAll('migrate_prepare_row', [$row2, $source, $migration])
|
Chris@0
|
313 ->willReturn([TRUE, TRUE])
|
Chris@0
|
314 ->shouldBeCalled();
|
Chris@0
|
315 $module_handler->invokeAll('migrate_' . $migration->id() . '_prepare_row', [$row2, $source, $migration])
|
Chris@0
|
316 ->willReturn([TRUE, TRUE])
|
Chris@0
|
317 ->shouldBeCalled();
|
Chris@0
|
318 $source->setModuleHandler($module_handler->reveal());
|
Chris@0
|
319 $this->assertTrue($source->prepareRow($row2->reveal()));
|
Chris@0
|
320 }
|
Chris@0
|
321
|
Chris@0
|
322 /**
|
Chris@0
|
323 * Test that global prepare hooks can skip rows.
|
Chris@0
|
324 *
|
Chris@0
|
325 * @covers ::prepareRow
|
Chris@0
|
326 */
|
Chris@0
|
327 public function testPrepareRowGlobalPrepareSkip() {
|
Chris@0
|
328 $this->migrationConfiguration['id'] = 'test_migration';
|
Chris@0
|
329
|
Chris@0
|
330 $migration = $this->getMigration();
|
Chris@0
|
331 $source = new StubSourcePlugin([], '', [], $migration);
|
Chris@0
|
332 $row = new Row();
|
Chris@0
|
333
|
Chris@0
|
334 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
|
Chris@0
|
335 // Return a failure from a prepare row hook.
|
Chris@0
|
336 $module_handler->invokeAll('migrate_prepare_row', [$row, $source, $migration])
|
Chris@0
|
337 ->willReturn([TRUE, FALSE, TRUE])
|
Chris@0
|
338 ->shouldBeCalled();
|
Chris@0
|
339 $module_handler->invokeAll('migrate_' . $migration->id() . '_prepare_row', [$row, $source, $migration])
|
Chris@0
|
340 ->willReturn([TRUE, TRUE])
|
Chris@0
|
341 ->shouldBeCalled();
|
Chris@0
|
342 $source->setModuleHandler($module_handler->reveal());
|
Chris@0
|
343
|
Chris@0
|
344 $this->idMap->expects($this->once())
|
Chris@0
|
345 ->method('saveIdMapping')
|
Chris@0
|
346 ->with($row, [], MigrateIdMapInterface::STATUS_IGNORED);
|
Chris@0
|
347
|
Chris@0
|
348 $this->assertFalse($source->prepareRow($row));
|
Chris@0
|
349 }
|
Chris@0
|
350
|
Chris@0
|
351 /**
|
Chris@0
|
352 * Test that migrate specific prepare hooks can skip rows.
|
Chris@0
|
353 *
|
Chris@0
|
354 * @covers ::prepareRow
|
Chris@0
|
355 */
|
Chris@0
|
356 public function testPrepareRowMigratePrepareSkip() {
|
Chris@0
|
357 $this->migrationConfiguration['id'] = 'test_migration';
|
Chris@0
|
358
|
Chris@0
|
359 $migration = $this->getMigration();
|
Chris@0
|
360 $source = new StubSourcePlugin([], '', [], $migration);
|
Chris@0
|
361 $row = new Row();
|
Chris@0
|
362
|
Chris@0
|
363 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
|
Chris@0
|
364 // Return a failure from a prepare row hook.
|
Chris@0
|
365 $module_handler->invokeAll('migrate_prepare_row', [$row, $source, $migration])
|
Chris@0
|
366 ->willReturn([TRUE, TRUE])
|
Chris@0
|
367 ->shouldBeCalled();
|
Chris@0
|
368 $module_handler->invokeAll('migrate_' . $migration->id() . '_prepare_row', [$row, $source, $migration])
|
Chris@0
|
369 ->willReturn([TRUE, FALSE, TRUE])
|
Chris@0
|
370 ->shouldBeCalled();
|
Chris@0
|
371 $source->setModuleHandler($module_handler->reveal());
|
Chris@0
|
372
|
Chris@0
|
373 $this->idMap->expects($this->once())
|
Chris@0
|
374 ->method('saveIdMapping')
|
Chris@0
|
375 ->with($row, [], MigrateIdMapInterface::STATUS_IGNORED);
|
Chris@0
|
376
|
Chris@0
|
377 $this->assertFalse($source->prepareRow($row));
|
Chris@0
|
378 }
|
Chris@0
|
379
|
Chris@0
|
380 /**
|
Chris@0
|
381 * Test that a skip exception during prepare hooks correctly skips.
|
Chris@0
|
382 *
|
Chris@0
|
383 * @covers ::prepareRow
|
Chris@0
|
384 */
|
Chris@0
|
385 public function testPrepareRowPrepareException() {
|
Chris@0
|
386 $this->migrationConfiguration['id'] = 'test_migration';
|
Chris@0
|
387
|
Chris@0
|
388 $migration = $this->getMigration();
|
Chris@0
|
389 $source = new StubSourcePlugin([], '', [], $migration);
|
Chris@0
|
390 $row = new Row();
|
Chris@0
|
391
|
Chris@0
|
392 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
|
Chris@0
|
393 // Return a failure from a prepare row hook.
|
Chris@0
|
394 $module_handler->invokeAll('migrate_prepare_row', [$row, $source, $migration])
|
Chris@0
|
395 ->willReturn([TRUE, TRUE])
|
Chris@0
|
396 ->shouldBeCalled();
|
Chris@0
|
397 $module_handler->invokeAll('migrate_' . $migration->id() . '_prepare_row', [$row, $source, $migration])
|
Chris@0
|
398 ->willThrow(new MigrateSkipRowException())
|
Chris@0
|
399 ->shouldBeCalled();
|
Chris@0
|
400 $source->setModuleHandler($module_handler->reveal());
|
Chris@0
|
401
|
Chris@0
|
402 // This will only be called on the first prepare because the second
|
Chris@0
|
403 // explicitly avoids it.
|
Chris@0
|
404 $this->idMap->expects($this->once())
|
Chris@0
|
405 ->method('saveIdMapping')
|
Chris@0
|
406 ->with($row, [], MigrateIdMapInterface::STATUS_IGNORED);
|
Chris@0
|
407 $this->assertFalse($source->prepareRow($row));
|
Chris@0
|
408
|
Chris@0
|
409 // Throw an exception the second time that avoids mapping.
|
Chris@0
|
410 $e = new MigrateSkipRowException('', FALSE);
|
Chris@0
|
411 $module_handler->invokeAll('migrate_' . $migration->id() . '_prepare_row', [$row, $source, $migration])
|
Chris@0
|
412 ->willThrow($e)
|
Chris@0
|
413 ->shouldBeCalled();
|
Chris@0
|
414 $this->assertFalse($source->prepareRow($row));
|
Chris@0
|
415 }
|
Chris@0
|
416
|
Chris@0
|
417 /**
|
Chris@0
|
418 * Test that cacheCounts, skipCount, trackChanges preserve their default
|
Chris@0
|
419 * values.
|
Chris@0
|
420 */
|
Chris@0
|
421 public function testDefaultPropertiesValues() {
|
Chris@0
|
422 $this->migrationConfiguration['id'] = 'test_migration';
|
Chris@0
|
423 $migration = $this->getMigration();
|
Chris@0
|
424 $source = new StubSourceGeneratorPlugin([], '', [], $migration);
|
Chris@0
|
425
|
Chris@0
|
426 // Test the default value of the skipCount Value;
|
Chris@0
|
427 $this->assertTrue($source->getSkipCount());
|
Chris@0
|
428 $this->assertTrue($source->getCacheCounts());
|
Chris@0
|
429 $this->assertTrue($source->getTrackChanges());
|
Chris@0
|
430 }
|
Chris@0
|
431
|
Chris@0
|
432 /**
|
Chris@0
|
433 * Gets a mock executable for the test.
|
Chris@0
|
434 *
|
Chris@0
|
435 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
|
Chris@0
|
436 * The migration entity.
|
Chris@0
|
437 *
|
Chris@0
|
438 * @return \Drupal\migrate\MigrateExecutable
|
Chris@0
|
439 * The migrate executable.
|
Chris@0
|
440 */
|
Chris@0
|
441 protected function getMigrateExecutable($migration) {
|
Chris@0
|
442 /** @var \Drupal\migrate\MigrateMessageInterface $message */
|
Chris@0
|
443 $message = $this->getMock('Drupal\migrate\MigrateMessageInterface');
|
Chris@0
|
444 /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */
|
Chris@0
|
445 $event_dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
Chris@0
|
446 return new MigrateExecutable($migration, $message, $event_dispatcher);
|
Chris@0
|
447 }
|
Chris@0
|
448
|
Chris@0
|
449 }
|
Chris@0
|
450
|
Chris@0
|
451 /**
|
Chris@0
|
452 * Stubbed source plugin for testing base class implementations.
|
Chris@0
|
453 */
|
Chris@0
|
454 class StubSourcePlugin extends SourcePluginBase {
|
Chris@0
|
455
|
Chris@0
|
456 /**
|
Chris@0
|
457 * Helper for setting internal module handler implementation.
|
Chris@0
|
458 *
|
Chris@0
|
459 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
460 * The module handler.
|
Chris@0
|
461 */
|
Chris@0
|
462 public function setModuleHandler(ModuleHandlerInterface $module_handler) {
|
Chris@0
|
463 $this->moduleHandler = $module_handler;
|
Chris@0
|
464 }
|
Chris@0
|
465
|
Chris@0
|
466 /**
|
Chris@0
|
467 * {@inheritdoc}
|
Chris@0
|
468 */
|
Chris@0
|
469 public function fields() {
|
Chris@0
|
470 return [];
|
Chris@0
|
471 }
|
Chris@0
|
472
|
Chris@0
|
473 /**
|
Chris@0
|
474 * {@inheritdoc}
|
Chris@0
|
475 */
|
Chris@0
|
476 public function __toString() {
|
Chris@0
|
477 return '';
|
Chris@0
|
478 }
|
Chris@0
|
479
|
Chris@0
|
480 /**
|
Chris@0
|
481 * {@inheritdoc}
|
Chris@0
|
482 */
|
Chris@0
|
483 public function getIds() {
|
Chris@0
|
484 return [];
|
Chris@0
|
485 }
|
Chris@0
|
486
|
Chris@0
|
487 /**
|
Chris@0
|
488 * {@inheritdoc}
|
Chris@0
|
489 */
|
Chris@0
|
490 protected function initializeIterator() {
|
Chris@0
|
491 return [];
|
Chris@0
|
492 }
|
Chris@0
|
493
|
Chris@0
|
494 }
|
Chris@0
|
495
|
Chris@0
|
496 /**
|
Chris@0
|
497 * Stubbed source plugin with a generator as iterator. Also it overwrites the
|
Chris@0
|
498 * $skipCount, $cacheCounts and $trackChanges properties.
|
Chris@0
|
499 */
|
Chris@0
|
500 class StubSourceGeneratorPlugin extends StubSourcePlugin {
|
Chris@0
|
501
|
Chris@0
|
502 /**
|
Chris@0
|
503 * {@inheritdoc}
|
Chris@0
|
504 */
|
Chris@0
|
505 protected $skipCount = TRUE;
|
Chris@0
|
506
|
Chris@0
|
507 /**
|
Chris@0
|
508 * {@inheritdoc}
|
Chris@0
|
509 */
|
Chris@0
|
510 protected $cacheCounts = TRUE;
|
Chris@0
|
511
|
Chris@0
|
512 /**
|
Chris@0
|
513 * {@inheritdoc}
|
Chris@0
|
514 */
|
Chris@0
|
515 protected $trackChanges = TRUE;
|
Chris@0
|
516
|
Chris@0
|
517 /**
|
Chris@0
|
518 * Return the skipCount value.
|
Chris@0
|
519 */
|
Chris@0
|
520 public function getSkipCount() {
|
Chris@0
|
521 return $this->skipCount;
|
Chris@0
|
522 }
|
Chris@0
|
523
|
Chris@0
|
524 /**
|
Chris@0
|
525 * Return the cacheCounts value.
|
Chris@0
|
526 */
|
Chris@0
|
527 public function getCacheCounts() {
|
Chris@0
|
528 return $this->cacheCounts;
|
Chris@0
|
529 }
|
Chris@0
|
530
|
Chris@0
|
531 /**
|
Chris@0
|
532 * Return the trackChanges value.
|
Chris@0
|
533 */
|
Chris@0
|
534 public function getTrackChanges() {
|
Chris@0
|
535 return $this->trackChanges;
|
Chris@0
|
536 }
|
Chris@0
|
537
|
Chris@0
|
538 /**
|
Chris@0
|
539 * {@inheritdoc}
|
Chris@0
|
540 */
|
Chris@0
|
541 protected function initializeIterator() {
|
Chris@0
|
542 $data = [
|
Chris@0
|
543 ['title' => 'foo'],
|
Chris@0
|
544 ['title' => 'bar'],
|
Chris@0
|
545 ['title' => 'iggy'],
|
Chris@0
|
546 ];
|
Chris@0
|
547 foreach ($data as $row) {
|
Chris@0
|
548 yield $row;
|
Chris@0
|
549 }
|
Chris@0
|
550 }
|
Chris@0
|
551
|
Chris@0
|
552 }
|