Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / migrate / tests / src / Kernel / MigrateSkipRowTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\migrate\Plugin\MigrationInterface;
7 use Drupal\migrate\MigrateExecutable;
8 use Drupal\migrate\Plugin\MigrateIdMapInterface;
9
10 /**
11  * Tests row skips triggered during hook_migrate_prepare_row().
12  *
13  * @group migrate
14  */
15 class MigrateSkipRowTest extends KernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['migrate', 'migrate_prepare_row_test'];
23
24   /**
25    * Tests migration interruptions.
26    */
27   public function testPrepareRowSkip() {
28     // Run a simple little migration with two data rows which should be skipped
29     // in different ways.
30     $definition = [
31       'migration_tags' => ['prepare_row test'],
32       'source' => [
33         'plugin' => 'embedded_data',
34         'data_rows' => [
35           ['id' => '1', 'data' => 'skip_and_record'],
36           ['id' => '2', 'data' => 'skip_and_dont_record'],
37         ],
38         'ids' => [
39           'id' => ['type' => 'string'],
40         ],
41       ],
42       'process' => ['value' => 'data'],
43       'destination' => [
44         'plugin' => 'config',
45         'config_name' => 'migrate_test.settings',
46       ],
47       'load' => ['plugin' => 'null'],
48     ];
49
50     $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
51
52     $executable = new MigrateExecutable($migration);
53     $result = $executable->import();
54     $this->assertEqual($result, MigrationInterface::RESULT_COMPLETED);
55
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);
64
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);
74
75     // Insert a custom processor in the process flow.
76     $definition['process']['value'] = [
77       'source' => 'data',
78       'plugin' => 'test_skip_row_process',
79     ];
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)'],
84     ];
85     $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
86
87     $executable = new MigrateExecutable($migration);
88     $result = $executable->import();
89     $this->assertEquals($result, MigrationInterface::RESULT_COMPLETED);
90
91     $id_map_plugin = $migration->getIdMap();
92
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);
99   }
100
101 }