View source
<?php
namespace Drupal\Tests\migrate\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
class MigrateSkipRowTest extends KernelTestBase {
public static $modules = [
'migrate',
'migrate_prepare_row_test',
];
public function testPrepareRowSkip() {
$definition = [
'migration_tags' => [
'prepare_row test',
],
'source' => [
'plugin' => 'embedded_data',
'data_rows' => [
[
'id' => '1',
'data' => 'skip_and_record',
],
[
'id' => '2',
'data' => 'skip_and_do_not_record',
],
],
'ids' => [
'id' => [
'type' => 'string',
],
],
],
'process' => [
'value' => 'data',
],
'destination' => [
'plugin' => 'config',
'config_name' => 'migrate_test.settings',
],
'load' => [
'plugin' => 'null',
],
];
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($definition);
$executable = new MigrateExecutable($migration);
$result = $executable
->import();
$this
->assertEqual($result, MigrationInterface::RESULT_COMPLETED);
$id_map_plugin = $migration
->getIdMap();
$map_row = $id_map_plugin
->getRowBySource([
'id' => 1,
]);
$this
->assertEqual(MigrateIdMapInterface::STATUS_IGNORED, $map_row['source_row_status']);
$messages = $id_map_plugin
->getMessages([
'id' => 1,
])
->fetchAll();
$this
->assertEmpty($messages);
$map_row = $id_map_plugin
->getRowBySource([
'id' => 2,
]);
$this
->assertFalse($map_row);
$messages = $id_map_plugin
->getMessages([
'id' => 2,
])
->fetchAll();
$this
->assertCount(1, $messages);
$message = reset($messages);
$this
->assertEquals('skip_and_do_not_record message', $message->message);
$this
->assertEquals(MigrationInterface::MESSAGE_INFORMATIONAL, $message->level);
$definition['process']['value'] = [
'source' => 'data',
'plugin' => 'test_skip_row_process',
];
$definition['source']['data_rows'] = [
[
'id' => '1',
'data' => 'skip_and_record (use plugin)',
],
[
'id' => '2',
'data' => 'skip_and_do_not_record (use plugin)',
],
];
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($definition);
$executable = new MigrateExecutable($migration);
$result = $executable
->import();
$this
->assertEquals($result, MigrationInterface::RESULT_COMPLETED);
$id_map_plugin = $migration
->getIdMap();
$map_row = $id_map_plugin
->getRowBySource([
'id' => 1,
]);
$this
->assertEquals(MigrateIdMapInterface::STATUS_IGNORED, $map_row['source_row_status']);
$map_row = $id_map_plugin
->getRowBySource([
'id' => 2,
]);
$this
->assertFalse($map_row);
}
}