From 7ca0caf496a29466ef74abefa97a1617f4bd6545 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Tue, 18 Oct 2016 14:27:26 +0100
Subject: [PATCH] Issue #2818799 by claudiu.cristea: MigrateSkipRowException 
 being TRUE, does not skip silently as should

---
 .../modules/migrate/src/MigrateExecutable.php |  4 ++-
 .../migrate/process/TestSkipRowProcess.php    | 34 +++++++++++++++++++
 .../tests/src/Kernel/MigrateSkipRowTest.php   | 24 +++++++++++++
 3 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 core/modules/migrate/tests/modules/migrate_prepare_row_test/src/Plugin/migrate/process/TestSkipRowProcess.php

diff --git a/core/modules/migrate/src/MigrateExecutable.php b/core/modules/migrate/src/MigrateExecutable.php
index c51b278ad091..440ce8ea281b 100644
--- a/core/modules/migrate/src/MigrateExecutable.php
+++ b/core/modules/migrate/src/MigrateExecutable.php
@@ -229,7 +229,9 @@ public function import() {
         $save = FALSE;
       }
       catch (MigrateSkipRowException $e) {
-        $id_map->saveIdMapping($row, array(), MigrateIdMapInterface::STATUS_IGNORED);
+        if ($e->getSaveToMap()) {
+          $id_map->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_IGNORED);
+        }
         $save = FALSE;
       }
 
diff --git a/core/modules/migrate/tests/modules/migrate_prepare_row_test/src/Plugin/migrate/process/TestSkipRowProcess.php b/core/modules/migrate/tests/modules/migrate_prepare_row_test/src/Plugin/migrate/process/TestSkipRowProcess.php
new file mode 100644
index 000000000000..4822113b3254
--- /dev/null
+++ b/core/modules/migrate/tests/modules/migrate_prepare_row_test/src/Plugin/migrate/process/TestSkipRowProcess.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace Drupal\migrate_prepare_row_test\Plugin\migrate\process;
+
+use Drupal\migrate\MigrateExecutableInterface;
+use Drupal\migrate\MigrateSkipRowException;
+use Drupal\migrate\ProcessPluginBase;
+use Drupal\migrate\Row;
+
+/**
+ * Provides a testing process plugin that skips rows.
+ *
+ * @MigrateProcessPlugin(
+ *   id = "test_skip_row_process"
+ * )
+ */
+class TestSkipRowProcess extends ProcessPluginBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
+    // Test both options for save_to_map.
+    $data = $row->getSourceProperty('data');
+    if ($data == 'skip_and_record (use plugin)') {
+      throw new MigrateSkipRowException('', TRUE);
+    }
+    elseif ($data == 'skip_and_dont_record (use plugin)') {
+      throw new MigrateSkipRowException('', FALSE);
+    }
+    return $value;
+  }
+
+}
diff --git a/core/modules/migrate/tests/src/Kernel/MigrateSkipRowTest.php b/core/modules/migrate/tests/src/Kernel/MigrateSkipRowTest.php
index acad26d25a35..44df50178ed6 100644
--- a/core/modules/migrate/tests/src/Kernel/MigrateSkipRowTest.php
+++ b/core/modules/migrate/tests/src/Kernel/MigrateSkipRowTest.php
@@ -62,6 +62,30 @@ public function testPrepareRowSkip() {
     $map_row = $id_map_plugin->getRowBySource(['id' => 2]);
     $this->assertFalse($map_row);
 
+    // Insert a custom processor in the process flow.
+    $definition['process']['value'] = [
+      'source' => 'data',
+      'plugin' => 'test_skip_row_process',
+    ];
+    // Change data to avoid triggering again hook_migrate_prepare_row().
+    $definition['source']['data_rows'] = [
+      ['id' => '1', 'data' => 'skip_and_record (use plugin)'],
+      ['id' => '2', 'data' => 'skip_and_dont_record (use plugin)'],
+    ];
+    $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
+
+    $executable = new MigrateExecutable($migration, new MigrateMessage());
+    $result = $executable->import();
+    $this->assertEquals($result, MigrationInterface::RESULT_COMPLETED);
+
+    $id_map_plugin = $migration->getIdMap();
+
+    // The first row is recorded in the map as ignored.
+    $map_row = $id_map_plugin->getRowBySource(['id' => 1]);
+    $this->assertEquals(MigrateIdMapInterface::STATUS_IGNORED, $map_row['source_row_status']);
+    // The second row is not recorded in the map.
+    $map_row = $id_map_plugin->getRowBySource(['id' => 2]);
+    $this->assertFalse($map_row);
   }
 
 }
-- 
GitLab