From eac02c746114e6c672cc26bbfd6e5e3145b5b52b Mon Sep 17 00:00:00 2001
From: webchick <drupal@webchick.net>
Date: Tue, 29 Sep 2015 08:51:01 -0700
Subject: [PATCH] Issue #2570541 by mikeryan, phenaproxima: Track simple
 configuration migrations like normal migrations

---
 .../migrate/src/Plugin/migrate/destination/Config.php     | 6 ++++--
 .../migrate/tests/src/Unit/MigrateSqlSourceTestCase.php   | 7 +++++++
 .../migrate/tests/src/Unit/destination/ConfigTest.php     | 6 +++++-
 .../migrate_drupal/src/Plugin/migrate/source/Variable.php | 8 ++++++--
 .../migrate_drupal/tests/src/Unit/source/VariableTest.php | 1 +
 .../search/src/Tests/Migrate/d6/MigrateSearchPageTest.php | 6 ++++--
 6 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/core/modules/migrate/src/Plugin/migrate/destination/Config.php b/core/modules/migrate/src/Plugin/migrate/destination/Config.php
index 7cc737e0d741..748f1a8d2f41 100644
--- a/core/modules/migrate/src/Plugin/migrate/destination/Config.php
+++ b/core/modules/migrate/src/Plugin/migrate/destination/Config.php
@@ -1,4 +1,5 @@
 <?php
+
 /**
  * @file
  * Contains \Drupal\migrate\Plugin\migrate\destination\Config.
@@ -78,7 +79,7 @@ public function import(Row $row, array $old_destination_id_values = array()) {
       }
     }
     $this->config->save();
-    return TRUE;
+    return [$this->config->getName()];
   }
 
   /**
@@ -92,7 +93,8 @@ public function fields(MigrationInterface $migration = NULL) {
    * {@inheritdoc}
    */
   public function getIds() {
-    return array();
+    $ids['config_name']['type'] = 'string';
+    return $ids;
   }
 
   /**
diff --git a/core/modules/migrate/tests/src/Unit/MigrateSqlSourceTestCase.php b/core/modules/migrate/tests/src/Unit/MigrateSqlSourceTestCase.php
index e644a04603d1..0f4aad3f5d77 100644
--- a/core/modules/migrate/tests/src/Unit/MigrateSqlSourceTestCase.php
+++ b/core/modules/migrate/tests/src/Unit/MigrateSqlSourceTestCase.php
@@ -121,6 +121,13 @@ public function testSourceCount() {
     $this->assertEquals($this->source->count(), $this->expectedCount);
   }
 
+  /**
+   * Test the source defines a valid ID.
+   */
+  public function testSourceId() {
+    $this->assertNotEmpty($this->source->getIds());
+  }
+
   /**
    * @param \Drupal\migrate\Row $row
    * @param string $key
diff --git a/core/modules/migrate/tests/src/Unit/destination/ConfigTest.php b/core/modules/migrate/tests/src/Unit/destination/ConfigTest.php
index 6b7dcd7cbeb5..363e4f16e027 100644
--- a/core/modules/migrate/tests/src/Unit/destination/ConfigTest.php
+++ b/core/modules/migrate/tests/src/Unit/destination/ConfigTest.php
@@ -37,6 +37,9 @@ public function testImport() {
     }
     $config->expects($this->once())
       ->method('save');
+    $config->expects($this->once())
+      ->method('getName')
+      ->willReturn('d8_config');
     $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
     $config_factory->expects($this->once())
       ->method('getEditable')
@@ -49,7 +52,8 @@ public function testImport() {
       ->method('getRawDestination')
       ->will($this->returnValue($source));
     $destination = new Config(array('config_name' => 'd8_config'), 'd8_config', array('pluginId' => 'd8_config'), $migration, $config_factory);
-    $destination->import($row);
+    $destination_id = $destination->import($row);
+    $this->assertEquals($destination_id, ['d8_config']);
   }
 
 }
diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php
index bdaf98164ba9..64e33a55c4cc 100644
--- a/core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php
+++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php
@@ -53,7 +53,10 @@ protected function initializeIterator() {
    *   Only those values are returned that are actually in the database.
    */
   protected function values() {
-    return array_map('unserialize', $this->prepareQuery()->execute()->fetchAllKeyed());
+    // Create an ID field so we can record migration in the map table.
+    // Arbitrarily, use the first variable name.
+    $values['id'] = reset($this->variables);
+    return $values + array_map('unserialize', $this->prepareQuery()->execute()->fetchAllKeyed());
   }
 
   /**
@@ -84,7 +87,8 @@ public function query() {
    * {@inheritdoc}
    */
   public function getIds() {
-    return array();
+    $ids['id']['type'] = 'string';
+    return $ids;
   }
 
 }
diff --git a/core/modules/migrate_drupal/tests/src/Unit/source/VariableTest.php b/core/modules/migrate_drupal/tests/src/Unit/source/VariableTest.php
index 51409bf4b4fd..0dd6d0689de7 100644
--- a/core/modules/migrate_drupal/tests/src/Unit/source/VariableTest.php
+++ b/core/modules/migrate_drupal/tests/src/Unit/source/VariableTest.php
@@ -32,6 +32,7 @@ class VariableTest extends MigrateSqlSourceTestCase {
 
   protected $expectedResults = array(
     array(
+      'id' => 'foo',
       'foo' => 1,
       'bar' => FALSE,
     ),
diff --git a/core/modules/search/src/Tests/Migrate/d6/MigrateSearchPageTest.php b/core/modules/search/src/Tests/Migrate/d6/MigrateSearchPageTest.php
index 6db13e8242ff..3f4710094e48 100644
--- a/core/modules/search/src/Tests/Migrate/d6/MigrateSearchPageTest.php
+++ b/core/modules/search/src/Tests/Migrate/d6/MigrateSearchPageTest.php
@@ -55,13 +55,15 @@ public function testSearchPage() {
       ->condition('name', 'node_rank_comments')
       ->execute();
 
+    /** @var \Drupal\migrate\Entity\MigrationInterface $migration */
     $migration = \Drupal::entityManager()
       ->getStorage('migration')
       ->loadUnchanged('d6_search_page');
+    // Indicate we're rerunning a migration that's already run.
+    $migration->getIdMap()->prepareUpdate();
     $this->executeMigration($migration);
 
-    $search_page = SearchPage::load($id);
-    $configuration = $search_page->getPlugin()->getConfiguration();
+    $configuration = SearchPage::load($id)->getPlugin()->getConfiguration();
     $this->assertIdentical(4, $configuration['rankings']['comments']);
   }
 
-- 
GitLab