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

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