From 0b45cbb70fde40978c9012ac8dfbba32aec8f223 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Fri, 3 Mar 2017 15:02:40 +0000
Subject: [PATCH] Issue #2850984 by maxocub: Fix path alias migration of
 translated nodes [D7]

---
 .../migrate_drupal/tests/fixtures/drupal7.php | 24 +++++++++
 .../path/migration_templates/d7_url_alias.yml | 12 +++++
 .../Kernel/Migrate/d7/MigrateUrlAliasTest.php | 52 ++++++++++++++++++-
 3 files changed, 86 insertions(+), 2 deletions(-)

diff --git a/core/modules/migrate_drupal/tests/fixtures/drupal7.php b/core/modules/migrate_drupal/tests/fixtures/drupal7.php
index ea4ecdac1d26..6d6cc4989866 100644
--- a/core/modules/migrate_drupal/tests/fixtures/drupal7.php
+++ b/core/modules/migrate_drupal/tests/fixtures/drupal7.php
@@ -43135,6 +43135,30 @@
   'alias' => 'term33',
   'language' => 'und',
 ))
+->values(array(
+  'pid' => '2',
+  'source' => 'node/2',
+  'alias' => 'deep-space-9',
+  'language' => 'en',
+))
+->values(array(
+  'pid' => '3',
+  'source' => 'node/3',
+  'alias' => 'deep-space-9-is',
+  'language' => 'is',
+))
+->values(array(
+  'pid' => '4',
+  'source' => 'node/4',
+  'alias' => 'firefly-is',
+  'language' => 'is',
+))
+->values(array(
+  'pid' => '5',
+  'source' => 'node/5',
+  'alias' => 'firefly',
+  'language' => 'en',
+))
 ->execute();
 
 $connection->schema()->createTable('users', array(
diff --git a/core/modules/path/migration_templates/d7_url_alias.yml b/core/modules/path/migration_templates/d7_url_alias.yml
index a3ddec1ad0f4..713a34676f7f 100644
--- a/core/modules/path/migration_templates/d7_url_alias.yml
+++ b/core/modules/path/migration_templates/d7_url_alias.yml
@@ -18,5 +18,17 @@ process:
       - constants/slash
       - alias
   langcode: language
+  node_translation:
+    -
+      plugin: explode
+      source: source
+      delimiter: /
+    -
+      plugin: extract
+      index:
+        - 1
+    -
+      plugin: migration
+      migration: d7_node_translation
 destination:
   plugin: url_alias
diff --git a/core/modules/path/tests/src/Kernel/Migrate/d7/MigrateUrlAliasTest.php b/core/modules/path/tests/src/Kernel/Migrate/d7/MigrateUrlAliasTest.php
index 88c102b4e580..18452fedf194 100644
--- a/core/modules/path/tests/src/Kernel/Migrate/d7/MigrateUrlAliasTest.php
+++ b/core/modules/path/tests/src/Kernel/Migrate/d7/MigrateUrlAliasTest.php
@@ -14,14 +14,33 @@ class MigrateUrlAliasTest extends MigrateDrupal7TestBase {
   /**
    * {@inheritdoc}
    */
-  public static $modules = ['path'];
+  public static $modules = [
+    'content_translation',
+    'language',
+    'node',
+    'path',
+    'text',
+  ];
 
   /**
    * {@inheritdoc}
    */
   protected function setUp() {
     parent::setUp();
-    $this->executeMigration('d7_url_alias');
+
+    $this->installEntitySchema('node');
+    $this->installConfig('node');
+    $this->installSchema('node', ['node_access']);
+
+    $this->executeMigrations([
+      'language',
+      'd7_user_role',
+      'd7_user',
+      'd7_node_type',
+      'd7_node',
+      'd7_node_translation',
+      'd7_url_alias',
+    ]);
   }
 
   /**
@@ -38,4 +57,33 @@ public function testUrlAlias() {
     $this->assertIdentical('und', $path['langcode']);
   }
 
+  /**
+   * Test the URL alias migration with translated nodes.
+   */
+  public function testUrlAliasWithTranslatedNodes() {
+    $alias_storage = $this->container->get('path.alias_storage');
+
+    // Alias for the 'The thing about Deep Space 9' node in English.
+    $path = $alias_storage->load(['alias' => '/deep-space-9']);
+    $this->assertSame('/node/2', $path['source']);
+    $this->assertSame('en', $path['langcode']);
+
+    // Alias for the 'The thing about Deep Space 9' Icelandic translation,
+    // which should now point to node/2 instead of node/3.
+    $path = $alias_storage->load(['alias' => '/deep-space-9-is']);
+    $this->assertSame('/node/2', $path['source']);
+    $this->assertSame('is', $path['langcode']);
+
+    // Alias for the 'The thing about Firefly' node in Icelandic.
+    $path = $alias_storage->load(['alias' => '/firefly-is']);
+    $this->assertSame('/node/4', $path['source']);
+    $this->assertSame('is', $path['langcode']);
+
+    // Alias for the 'The thing about Firefly' English translation,
+    // which should now point to node/4 instead of node/5.
+    $path = $alias_storage->load(['alias' => '/firefly']);
+    $this->assertSame('/node/4', $path['source']);
+    $this->assertSame('en', $path['langcode']);
+  }
+
 }
-- 
GitLab