3 namespace Drupal\Tests\migrate\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\migrate\Plugin\MigrationInterface;
7 use Drupal\migrate\MigrateExecutable;
8 use Drupal\migrate\Plugin\MigrateIdMapInterface;
11 * Tests row skips triggered during hook_migrate_prepare_row().
15 class MigrateSkipRowTest extends KernelTestBase {
22 public static $modules = ['migrate', 'migrate_prepare_row_test'];
25 * Tests migration interruptions.
27 public function testPrepareRowSkip() {
28 // Run a simple little migration with two data rows which should be skipped
31 'migration_tags' => ['prepare_row test'],
33 'plugin' => 'embedded_data',
35 ['id' => '1', 'data' => 'skip_and_record'],
36 ['id' => '2', 'data' => 'skip_and_dont_record'],
39 'id' => ['type' => 'string'],
42 'process' => ['value' => 'data'],
45 'config_name' => 'migrate_test.settings',
47 'load' => ['plugin' => 'null'],
50 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
52 $executable = new MigrateExecutable($migration);
53 $result = $executable->import();
54 $this->assertEqual($result, MigrationInterface::RESULT_COMPLETED);
56 /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map_plugin */
57 $id_map_plugin = $migration->getIdMap();
58 // The first row is recorded in the map as ignored.
59 $map_row = $id_map_plugin->getRowBySource(['id' => 1]);
60 $this->assertEqual(MigrateIdMapInterface::STATUS_IGNORED, $map_row['source_row_status']);
61 // Check that no message has been logged for the first exception.
62 $messages = $id_map_plugin->getMessageIterator(['id' => 1])->fetchAll();
63 $this->assertEmpty($messages);
65 // The second row is not recorded in the map.
66 $map_row = $id_map_plugin->getRowBySource(['id' => 2]);
67 $this->assertFalse($map_row);
68 // Check that the correct message has been logged for the second exception.
69 $messages = $id_map_plugin->getMessageIterator(['id' => 2])->fetchAll();
70 $this->assertCount(1, $messages);
71 $message = reset($messages);
72 $this->assertEquals('skip_and_dont_record message', $message->message);
73 $this->assertEquals(MigrationInterface::MESSAGE_INFORMATIONAL, $message->level);
75 // Insert a custom processor in the process flow.
76 $definition['process']['value'] = [
78 'plugin' => 'test_skip_row_process',
80 // Change data to avoid triggering again hook_migrate_prepare_row().
81 $definition['source']['data_rows'] = [
82 ['id' => '1', 'data' => 'skip_and_record (use plugin)'],
83 ['id' => '2', 'data' => 'skip_and_dont_record (use plugin)'],
85 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
87 $executable = new MigrateExecutable($migration);
88 $result = $executable->import();
89 $this->assertEquals($result, MigrationInterface::RESULT_COMPLETED);
91 $id_map_plugin = $migration->getIdMap();
93 // The first row is recorded in the map as ignored.
94 $map_row = $id_map_plugin->getRowBySource(['id' => 1]);
95 $this->assertEquals(MigrateIdMapInterface::STATUS_IGNORED, $map_row['source_row_status']);
96 // The second row is not recorded in the map.
97 $map_row = $id_map_plugin->getRowBySource(['id' => 2]);
98 $this->assertFalse($map_row);