Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\migrate\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\KernelTests\KernelTestBase;
|
Chris@0
|
6 use Drupal\migrate\MigrateMessage;
|
Chris@0
|
7 use Drupal\migrate\Plugin\MigrationInterface;
|
Chris@0
|
8 use Drupal\migrate\MigrateExecutable;
|
Chris@0
|
9 use Drupal\migrate\Plugin\MigrateIdMapInterface;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Tests row skips triggered during hook_migrate_prepare_row().
|
Chris@0
|
13 *
|
Chris@0
|
14 * @group migrate
|
Chris@0
|
15 */
|
Chris@0
|
16 class MigrateSkipRowTest extends KernelTestBase {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Modules to enable.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var array
|
Chris@0
|
22 */
|
Chris@0
|
23 public static $modules = ['migrate', 'migrate_prepare_row_test'];
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Tests migration interruptions.
|
Chris@0
|
27 */
|
Chris@0
|
28 public function testPrepareRowSkip() {
|
Chris@0
|
29 // Run a simple little migration with two data rows which should be skipped
|
Chris@0
|
30 // in different ways.
|
Chris@0
|
31 $definition = [
|
Chris@0
|
32 'migration_tags' => ['prepare_row test'],
|
Chris@0
|
33 'source' => [
|
Chris@0
|
34 'plugin' => 'embedded_data',
|
Chris@0
|
35 'data_rows' => [
|
Chris@0
|
36 ['id' => '1', 'data' => 'skip_and_record'],
|
Chris@0
|
37 ['id' => '2', 'data' => 'skip_and_dont_record'],
|
Chris@0
|
38 ],
|
Chris@0
|
39 'ids' => [
|
Chris@0
|
40 'id' => ['type' => 'string'],
|
Chris@0
|
41 ],
|
Chris@0
|
42 ],
|
Chris@0
|
43 'process' => ['value' => 'data'],
|
Chris@0
|
44 'destination' => [
|
Chris@0
|
45 'plugin' => 'config',
|
Chris@0
|
46 'config_name' => 'migrate_test.settings',
|
Chris@0
|
47 ],
|
Chris@0
|
48 'load' => ['plugin' => 'null'],
|
Chris@0
|
49 ];
|
Chris@0
|
50
|
Chris@0
|
51 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
|
Chris@0
|
52
|
Chris@0
|
53 $executable = new MigrateExecutable($migration, new MigrateMessage());
|
Chris@0
|
54 $result = $executable->import();
|
Chris@0
|
55 $this->assertEqual($result, MigrationInterface::RESULT_COMPLETED);
|
Chris@0
|
56
|
Chris@0
|
57 /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map_plugin */
|
Chris@0
|
58 $id_map_plugin = $migration->getIdMap();
|
Chris@0
|
59 // The first row is recorded in the map as ignored.
|
Chris@0
|
60 $map_row = $id_map_plugin->getRowBySource(['id' => 1]);
|
Chris@0
|
61 $this->assertEqual(MigrateIdMapInterface::STATUS_IGNORED, $map_row['source_row_status']);
|
Chris@0
|
62 // Check that no message has been logged for the first exception.
|
Chris@0
|
63 $messages = $id_map_plugin->getMessageIterator(['id' => 1])->fetchAll();
|
Chris@0
|
64 $this->assertEmpty($messages);
|
Chris@0
|
65
|
Chris@0
|
66 // The second row is not recorded in the map.
|
Chris@0
|
67 $map_row = $id_map_plugin->getRowBySource(['id' => 2]);
|
Chris@0
|
68 $this->assertFalse($map_row);
|
Chris@0
|
69 // Check that the correct message has been logged for the second exception.
|
Chris@0
|
70 $messages = $id_map_plugin->getMessageIterator(['id' => 2])->fetchAll();
|
Chris@0
|
71 $this->assertCount(1, $messages);
|
Chris@0
|
72 $message = reset($messages);
|
Chris@0
|
73 $this->assertEquals('skip_and_dont_record message', $message->message);
|
Chris@0
|
74 $this->assertEquals(MigrationInterface::MESSAGE_INFORMATIONAL, $message->level);
|
Chris@0
|
75
|
Chris@0
|
76 // Insert a custom processor in the process flow.
|
Chris@0
|
77 $definition['process']['value'] = [
|
Chris@0
|
78 'source' => 'data',
|
Chris@0
|
79 'plugin' => 'test_skip_row_process',
|
Chris@0
|
80 ];
|
Chris@0
|
81 // Change data to avoid triggering again hook_migrate_prepare_row().
|
Chris@0
|
82 $definition['source']['data_rows'] = [
|
Chris@0
|
83 ['id' => '1', 'data' => 'skip_and_record (use plugin)'],
|
Chris@0
|
84 ['id' => '2', 'data' => 'skip_and_dont_record (use plugin)'],
|
Chris@0
|
85 ];
|
Chris@0
|
86 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
|
Chris@0
|
87
|
Chris@0
|
88 $executable = new MigrateExecutable($migration, new MigrateMessage());
|
Chris@0
|
89 $result = $executable->import();
|
Chris@0
|
90 $this->assertEquals($result, MigrationInterface::RESULT_COMPLETED);
|
Chris@0
|
91
|
Chris@0
|
92 $id_map_plugin = $migration->getIdMap();
|
Chris@0
|
93
|
Chris@0
|
94 // The first row is recorded in the map as ignored.
|
Chris@0
|
95 $map_row = $id_map_plugin->getRowBySource(['id' => 1]);
|
Chris@0
|
96 $this->assertEquals(MigrateIdMapInterface::STATUS_IGNORED, $map_row['source_row_status']);
|
Chris@0
|
97 // The second row is not recorded in the map.
|
Chris@0
|
98 $map_row = $id_map_plugin->getRowBySource(['id' => 2]);
|
Chris@0
|
99 $this->assertFalse($map_row);
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 }
|