From 699606e6036302783836884038007364b5ce6ab5 Mon Sep 17 00:00:00 2001
From: Gabor Hojtsy <gabor@hojtsy.hu>
Date: Mon, 8 May 2017 14:56:50 +0200
Subject: [PATCH] Issue #2597680 by stimalsina, neclimdul, paryank, Derimagia,
 timodwhit, Dries, vinothg, heddn, benjy, effulgentsia: Some migrations have
 warnings when node types don't exist

---
 .../d6_upload_entity_display.yml              |  9 ++++-
 .../d6_upload_entity_form_display.yml         |  9 ++++-
 .../d6/MigrateUploadEntityDisplayTest.php     | 24 ++++++++++++--
 .../d6/MigrateUploadEntityFormDisplayTest.php | 24 ++++++++++++--
 .../d6_vocabulary_entity_display.yml          |  9 ++++-
 .../d6_vocabulary_entity_form_display.yml     |  9 ++++-
 .../d6_vocabulary_field_instance.yml          |  9 ++++-
 .../d6/MigrateVocabularyEntityDisplayTest.php | 32 +++++++++++++++++-
 ...MigrateVocabularyEntityFormDisplayTest.php | 32 +++++++++++++++++-
 .../d6/MigrateVocabularyFieldInstanceTest.php | 33 +++++++++++++++++--
 10 files changed, 177 insertions(+), 13 deletions(-)

diff --git a/core/modules/file/migration_templates/d6_upload_entity_display.yml b/core/modules/file/migration_templates/d6_upload_entity_display.yml
index b097c88af429..576bb68c0e66 100644
--- a/core/modules/file/migration_templates/d6_upload_entity_display.yml
+++ b/core/modules/file/migration_templates/d6_upload_entity_display.yml
@@ -14,7 +14,14 @@ source:
       settings: {}
 process:
   entity_type: 'constants/entity_type'
-  bundle: node_type
+  bundle:
+    -
+      plugin: migration_lookup
+      migration: d6_node_type
+      source: node_type
+    -
+      plugin: skip_on_empty
+      method: row
   view_mode: 'constants/view_mode'
   field_name: 'constants/name'
   type: 'constants/type'
diff --git a/core/modules/file/migration_templates/d6_upload_entity_form_display.yml b/core/modules/file/migration_templates/d6_upload_entity_form_display.yml
index 75436db76129..055749a97ebc 100644
--- a/core/modules/file/migration_templates/d6_upload_entity_form_display.yml
+++ b/core/modules/file/migration_templates/d6_upload_entity_form_display.yml
@@ -15,7 +15,14 @@ source:
         progress_indicator: throbber
 process:
   entity_type: 'constants/entity_type'
-  bundle: node_type
+  bundle:
+    -
+      plugin: migration_lookup
+      migration: d6_node_type
+      source: node_type
+    -
+      plugin: skip_on_empty
+      method: row
   field_name: 'constants/name'
   form_mode: 'constants/form_mode'
   type: 'constants/type'
diff --git a/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateUploadEntityDisplayTest.php b/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateUploadEntityDisplayTest.php
index fc6cf455b840..200dc8617a33 100644
--- a/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateUploadEntityDisplayTest.php
+++ b/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateUploadEntityDisplayTest.php
@@ -23,13 +23,14 @@ class MigrateUploadEntityDisplayTest extends MigrateDrupal6TestBase {
   protected function setUp() {
     parent::setUp();
     $this->migrateFields();
-    $this->executeMigration('d6_upload_entity_display');
   }
 
   /**
-   * Tests the Drupal 6 upload settings to Drupal 8 entity display migration.
+   * Tests Drupal 6 upload settings to Drupal 8 entity display migration.
    */
   public function testUploadEntityDisplay() {
+    $this->executeMigration('d6_upload_entity_display');
+
     $display = EntityViewDisplay::load('node.page.default');
     $component = $display->getComponent('upload');
     $this->assertIdentical('file_default', $component['type']);
@@ -46,4 +47,23 @@ public function testUploadEntityDisplay() {
     $this->assertIdentical(['node', 'page', 'default', 'upload'], $this->getMigration('d6_upload_entity_display')->getIdMap()->lookupDestinationID(['page']));
   }
 
+  /**
+   * Tests that entity displays are ignored appropriately.
+   *
+   * Entity displays should be ignored when they belong to node types which
+   * were not migrated.
+   */
+  public function testSkipNonExistentNodeType() {
+    // The "story" node type is migrated by d6_node_type but we need to pretend
+    // that it didn't occur, so record that in the map table.
+    $this->mockFailure('d6_node_type', ['type' => 'story']);
+
+    // d6_upload_entity_display should skip over the "story" node type config
+    // because, according to the map table, it didn't occur.
+    $migration = $this->getMigration('d6_upload_entity_display');
+
+    $this->executeMigration($migration);
+    $this->assertNull($migration->getIdMap()->lookupDestinationIds(['node_type' => 'story'])[0][0]);
+  }
+
 }
diff --git a/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateUploadEntityFormDisplayTest.php b/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateUploadEntityFormDisplayTest.php
index 4055ffbb57dc..36e9f1da209c 100644
--- a/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateUploadEntityFormDisplayTest.php
+++ b/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateUploadEntityFormDisplayTest.php
@@ -23,13 +23,14 @@ class MigrateUploadEntityFormDisplayTest extends MigrateDrupal6TestBase {
   protected function setUp() {
     parent::setUp();
     $this->migrateFields();
-    $this->executeMigration('d6_upload_entity_form_display');
   }
 
   /**
-   * Tests the Drupal 6 upload settings to Drupal 8 entity form display migration.
+   * Tests Drupal 6 upload settings to Drupal 8 entity form display migration.
    */
   public function testUploadEntityFormDisplay() {
+    $this->executeMigration('d6_upload_entity_form_display');
+
     $display = EntityFormDisplay::load('node.page.default');
     $component = $display->getComponent('upload');
     $this->assertIdentical('file_generic', $component['type']);
@@ -46,4 +47,23 @@ public function testUploadEntityFormDisplay() {
     $this->assertIdentical(['node', 'page', 'default', 'upload'], $this->getMigration('d6_upload_entity_form_display')->getIdMap()->lookupDestinationID(['page']));
   }
 
+  /**
+   * Tests that entity displays are ignored appropriately.
+   *
+   * Entity displays should be ignored when they belong to node types which
+   * were not migrated.
+   */
+  public function testSkipNonExistentNodeType() {
+    // The "story" node type is migrated by d6_node_type but we need to pretend
+    // that it didn't occur, so record that in the map table.
+    $this->mockFailure('d6_node_type', ['type' => 'story']);
+
+    // d6_upload_entity_form_display should skip over the "story" node type
+    // config because according to the map table, it didn't occur.
+    $migration = $this->getMigration('d6_upload_entity_form_display');
+
+    $this->executeMigration($migration);
+    $this->assertNull($migration->getIdMap()->lookupDestinationIds(['node_type' => 'story'])[0][0]);
+  }
+
 }
diff --git a/core/modules/taxonomy/migration_templates/d6_vocabulary_entity_display.yml b/core/modules/taxonomy/migration_templates/d6_vocabulary_entity_display.yml
index 3c52a2fb04b1..8f154c3c8214 100644
--- a/core/modules/taxonomy/migration_templates/d6_vocabulary_entity_display.yml
+++ b/core/modules/taxonomy/migration_templates/d6_vocabulary_entity_display.yml
@@ -15,7 +15,14 @@ process:
   entity_type: 'constants/entity_type'
   view_mode: 'constants/view_mode'
   options: 'constants/options'
-  bundle: type
+  bundle:
+    -
+      plugin: migration_lookup
+      migration: d6_node_type
+      source: type
+    -
+      plugin: skip_on_empty
+      method: row
   field_name:
     plugin: migration_lookup
     migration: d6_taxonomy_vocabulary
diff --git a/core/modules/taxonomy/migration_templates/d6_vocabulary_entity_form_display.yml b/core/modules/taxonomy/migration_templates/d6_vocabulary_entity_form_display.yml
index 160f68c34c03..4ac6d646bca5 100644
--- a/core/modules/taxonomy/migration_templates/d6_vocabulary_entity_form_display.yml
+++ b/core/modules/taxonomy/migration_templates/d6_vocabulary_entity_form_display.yml
@@ -19,7 +19,14 @@ process:
       0: options_select
       1: entity_reference_autocomplete_tags
   options/weight: 'constants/options/weight'
-  bundle: type
+  bundle:
+    -
+      plugin: migration_lookup
+      migration: d6_node_type
+      source: type
+    -
+      plugin: skip_on_empty
+      method: row
   field_name:
     plugin: migration_lookup
     migration: d6_taxonomy_vocabulary
diff --git a/core/modules/taxonomy/migration_templates/d6_vocabulary_field_instance.yml b/core/modules/taxonomy/migration_templates/d6_vocabulary_field_instance.yml
index a3afd9b9458b..6fe5eed0175c 100644
--- a/core/modules/taxonomy/migration_templates/d6_vocabulary_field_instance.yml
+++ b/core/modules/taxonomy/migration_templates/d6_vocabulary_field_instance.yml
@@ -10,7 +10,14 @@ source:
     selection_handler: 'default:taxonomy_term'
 process:
   entity_type: 'constants/entity_type'
-  bundle: type
+  bundle:
+    -
+      plugin: migration_lookup
+      migration: d6_node_type
+      source: type
+    -
+      plugin: skip_on_empty
+      method: row
   field_name:
     -
       plugin: migration_lookup
diff --git a/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyEntityDisplayTest.php b/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyEntityDisplayTest.php
index a0f261e0d50a..f6fb85d74aea 100644
--- a/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyEntityDisplayTest.php
+++ b/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyEntityDisplayTest.php
@@ -22,13 +22,24 @@ class MigrateVocabularyEntityDisplayTest extends MigrateDrupal6TestBase {
    */
   protected function setUp() {
     parent::setUp();
-    $this->migrateTaxonomy();
+
+    // Execute Dependency Migrations.
+    $this->migrateContentTypes();
+    $this->installEntitySchema('taxonomy_term');
+    $this->executeMigrations([
+      'd6_node_type',
+      'd6_taxonomy_vocabulary',
+      'd6_vocabulary_field',
+      'd6_vocabulary_field_instance',
+    ]);
   }
 
   /**
    * Tests the Drupal 6 vocabulary-node type association to Drupal 8 migration.
    */
   public function testVocabularyEntityDisplay() {
+    $this->executeMigration('d6_vocabulary_entity_display');
+
     // Test that the field exists.
     $component = EntityViewDisplay::load('node.page.default')->getComponent('tags');
     $this->assertIdentical('entity_reference_label', $component['type']);
@@ -37,4 +48,23 @@ public function testVocabularyEntityDisplay() {
     $this->assertIdentical(['node', 'article', 'default', 'tags'], $this->getMigration('d6_vocabulary_entity_display')->getIdMap()->lookupDestinationID([4, 'article']));
   }
 
+  /**
+   * Tests that vocabulary displays are ignored appropriately.
+   *
+   * Vocabulary displays should be ignored when they belong to node types which
+   * were not migrated.
+   */
+  public function testSkipNonExistentNodeType() {
+    // The "story" node type is migrated by d6_node_type but we need to pretend
+    // that it didn't occur, so record that in the map table.
+    $this->mockFailure('d6_node_type', ['type' => 'story']);
+
+    // d6_vocabulary_entity_display should skip over the "story" node type
+    // config because, according to the map table, it didn't occur.
+    $migration = $this->getMigration('d6_vocabulary_entity_display');
+
+    $this->executeMigration($migration);
+    $this->assertNull($migration->getIdMap()->lookupDestinationIds(['type' => 'story'])[0][0]);
+  }
+
 }
diff --git a/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyEntityFormDisplayTest.php b/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyEntityFormDisplayTest.php
index 417105c54e11..93ac64369fac 100644
--- a/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyEntityFormDisplayTest.php
+++ b/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyEntityFormDisplayTest.php
@@ -22,13 +22,24 @@ class MigrateVocabularyEntityFormDisplayTest extends MigrateDrupal6TestBase {
    */
   protected function setUp() {
     parent::setUp();
-    $this->migrateTaxonomy();
+
+    // Execute Dependency Migrations.
+    $this->migrateContentTypes();
+    $this->installEntitySchema('taxonomy_term');
+    $this->executeMigrations([
+      'd6_taxonomy_vocabulary',
+      'd6_vocabulary_field',
+      'd6_vocabulary_field_instance',
+      'd6_vocabulary_entity_display',
+    ]);
   }
 
   /**
    * Tests the Drupal 6 vocabulary-node type association to Drupal 8 migration.
    */
   public function testVocabularyEntityFormDisplay() {
+    $this->executeMigration('d6_vocabulary_entity_form_display');
+
     // Test that the field exists.
     $component = EntityFormDisplay::load('node.page.default')->getComponent('tags');
     $this->assertIdentical('options_select', $component['type']);
@@ -42,4 +53,23 @@ public function testVocabularyEntityFormDisplay() {
     $this->assertIdentical($entity_form_display->getComponent('vocabulary_2_i_1_')['type'], 'entity_reference_autocomplete_tags');
   }
 
+  /**
+   * Tests that vocabulary displays are ignored appropriately.
+   *
+   * Vocabulary displays should be ignored when they belong to node types which
+   * were not migrated.
+   */
+  public function testSkipNonExistentNodeType() {
+    // The "story" node type is migrated by d6_node_type but we need to pretend
+    // that it didn't occur, so record that in the map table.
+    $this->mockFailure('d6_node_type', ['type' => 'story']);
+
+    // d6_vocabulary_entity_form_display should skip over the "story" node type
+    // config because, according to the map table, it didn't occur.
+    $migration = $this->getMigration('d6_vocabulary_entity_form_display');
+
+    $this->executeMigration($migration);
+    $this->assertNull($migration->getIdMap()->lookupDestinationIds(['type' => 'story'])[0][0]);
+  }
+
 }
diff --git a/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyFieldInstanceTest.php b/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyFieldInstanceTest.php
index 9264260ebfbb..bd5a497dd7f2 100644
--- a/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyFieldInstanceTest.php
+++ b/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateVocabularyFieldInstanceTest.php
@@ -22,13 +22,23 @@ class MigrateVocabularyFieldInstanceTest extends MigrateDrupal6TestBase {
    */
   protected function setUp() {
     parent::setUp();
-    $this->migrateTaxonomy();
+
+    // Execute Dependency Migrations.
+    $this->migrateContentTypes();
+    $this->installEntitySchema('taxonomy_term');
+    $this->executeMigrations([
+      'd6_node_type',
+      'd6_taxonomy_vocabulary',
+      'd6_vocabulary_field',
+    ]);
   }
 
   /**
    * Tests the Drupal 6 vocabulary-node type association to Drupal 8 migration.
    */
   public function testVocabularyFieldInstance() {
+    $this->executeMigration('d6_vocabulary_field_instance');
+
     // Test that the field exists.
     $field_id = 'node.article.tags';
     $field = FieldConfig::load($field_id);
@@ -50,10 +60,29 @@ public function testVocabularyFieldInstance() {
 
     $this->assertIdentical(['node', 'article', 'tags'], $this->getMigration('d6_vocabulary_field_instance')->getIdMap()->lookupDestinationID([4, 'article']));
 
-    // Test the the field vocabulary_1_i_0_
+    // Test the the field vocabulary_1_i_0_.
     $field_id = 'node.story.vocabulary_1_i_0_';
     $field = FieldConfig::load($field_id);
     $this->assertFalse($field->isRequired(), 'Field is not required');
   }
 
+  /**
+   * Tests that vocabulary field instances are ignored appropriately.
+   *
+   * Vocabulary field instances should be ignored when they belong to node
+   * types which were not migrated.
+   */
+  public function testSkipNonExistentNodeType() {
+    // The "story" node type is migrated by d6_node_type but we need to pretend
+    // that it didn't occur, so record that in the map table.
+    $this->mockFailure('d6_node_type', ['type' => 'story']);
+
+    // d6_vocabulary_field_instance should skip over the "story" node type
+    // config because, according to the map table, it didn't occur.
+    $migration = $this->getMigration('d6_vocabulary_field_instance');
+
+    $this->executeMigration($migration);
+    $this->assertNull($migration->getIdMap()->lookupDestinationIds(['type' => 'story'])[0][0]);
+  }
+
 }
-- 
GitLab