annotate core/modules/migrate/tests/src/Kernel/MigrateSkipRowTest.php @ 19:fa3358dc1485 tip

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