From 49b4f93382e4bdb0f99633b0180beacfacdb4804 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Mon, 12 May 2014 22:04:54 -0700
Subject: [PATCH] Issue #2264717 by penyaskito: Convert MigrateDrupal6Test into
 a reusable abstract class which can be reused for D7 and other migrations .
---
 .../Tests/MigrateFullDrupalTestBase.php       | 100 +++++++
 .../Tests/d6/MigrateDrupal6Test.php           | 244 ++++++++----------
 2 files changed, 201 insertions(+), 143 deletions(-)
 create mode 100644 core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/MigrateFullDrupalTestBase.php
diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/MigrateFullDrupalTestBase.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/MigrateFullDrupalTestBase.php
new file mode 100644
index 000000000000..341cce7ef8d2
--- /dev/null
+++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/MigrateFullDrupalTestBase.php
@@ -0,0 +1,100 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\migrate_drupal\Tests\MigrateFullDrupalTestBase.
+ */
+
+namespace Drupal\migrate_drupal\Tests;
+
+use Drupal\migrate\MigrateExecutable;
+use Drupal\simpletest\TestBase;
+
+/**
+ * Test helper for running a complete Drupal migration.
+ */
+abstract class MigrateFullDrupalTestBase extends MigrateDrupalTestBase {
+
+  /**
+   * Get the dump classes required for this migration test.
+   *
+   * @return array
+   *   The list of files containing dumps.
+   */
+  protected abstract function getDumps();
+
+  /**
+   * Get the test classes that needs to be run for this test.
+   *
+   * @return array
+   *   The list of test fully-classified class names.
+   */
+  protected abstract function getTestClassesList();
+
+  /**
+   * {@inheritdoc}
+   */
+  public function tearDown() {
+    // Move the results of every class under ours. This is solely for
+    // reporting, the filename will guide developers.
+    self::getDatabaseConnection()
+      ->update('simpletest')
+      ->fields(array('test_class' => get_class($this)))
+      ->condition('test_id', $this->testId)
+      ->execute();
+    parent::tearDown();
+  }
+
+
+  /**
+   * Test the complete Drupal migration.
+   */
+  public function testDrupal() {
+    $dumps = $this->getDumps();
+    $this->loadDumps($dumps);
+
+    $classes = $this->getTestClassesList();
+
+    // Run every migration in the order specified by the storage controller.
+    foreach (entity_load_multiple('migration', static::$migrations) as $migration) {
+      (new MigrateExecutable($migration, $this))->import();
+    }
+    foreach ($classes as $class) {
+      $test_object = new $class($this->testId);
+      $test_object->databasePrefix = $this->databasePrefix;
+      $test_object->container = $this->container;
+      // run() does a lot of setup and tear down work which we don't need:
+      // it would setup a new database connection and wouldn't find the
+      // Drupal dump. Also by skipping the setUp() methods there are no id
+      // mappings or entities prepared. The tests run against solely migrated
+      // data.
+      foreach (get_class_methods($test_object) as $method) {
+        if (strtolower(substr($method, 0, 4)) == 'test') {
+          // Insert a fail record. This will be deleted on completion to ensure
+          // that testing completed.
+          $method_info = new \ReflectionMethod($class, $method);
+          $caller = array(
+            'file' => $method_info->getFileName(),
+            'line' => $method_info->getStartLine(),
+            'function' => $class . '->' . $method . '()',
+          );
+          $completion_check_id = TestBase::insertAssert($this->testId, $class, FALSE, 'The test did not complete due to a fatal error.', 'Completion check', $caller);
+          // Run the test method.
+          try {
+            $test_object->$method();
+          }
+          catch (\Exception $e) {
+            $this->exceptionHandler($e);
+          }
+          // Remove the completion check record.
+          TestBase::deleteAssert($completion_check_id);
+        }
+      }
+      // Add the pass/fail/exception/debug results.
+      foreach ($this->results as $key => &$value) {
+        $value += $test_object->results[$key];
+      }
+    }
+  }
+
+}
diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateDrupal6Test.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateDrupal6Test.php
index 1e393a6affaf..adcf35b01a4d 100644
--- a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateDrupal6Test.php
+++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateDrupal6Test.php
@@ -7,14 +7,12 @@
 
 namespace Drupal\migrate_drupal\Tests\d6;
 
-use Drupal\migrate\MigrateExecutable;
-use Drupal\migrate_drupal\Tests\MigrateDrupalTestBase;
-use Drupal\simpletest\TestBase;
+use Drupal\migrate_drupal\Tests\MigrateFullDrupalTestBase;
 
 /**
  * Test the complete Drupal 6 migration.
  */
-class MigrateDrupal6Test extends MigrateDrupalTestBase {
+class MigrateDrupal6Test extends MigrateFullDrupalTestBase {
 
   /**
    * Modules to enable.
@@ -49,6 +47,94 @@ class MigrateDrupal6Test extends MigrateDrupalTestBase {
     'views',
   );
 
+  /**
+   * Migrations to run in the test.
+   *
+   * @var array
+   */
+  static $migrations = array(
+    'd6_action_settings',
+    'd6_aggregator_settings',
+    'd6_aggregator_feed',
+    'd6_aggregator_item',
+    'd6_block',
+    'd6_book_settings',
+    'd6_cck_field_values:*',
+    'd6_cck_field_revision:*',
+    'd6_comment',
+    'd6_comment_entity_display',
+    'd6_comment_entity_form_display',
+    'd6_comment_field',
+    'd6_comment_field_instance',
+    'd6_contact_category',
+    'd6_contact_settings',
+    'd6_custom_block',
+    'd6_date_formats',
+    'd6_dblog_settings',
+    'd6_field',
+    'd6_field_instance',
+    'd6_field_instance_widget_settings',
+    'd6_field_settings',
+    'd6_field_formatter_settings',
+    'd6_file_settings',
+    'd6_file',
+    'd6_filter_format',
+    'd6_forum_settings',
+    'd6_locale_settings',
+    'd6_menu_settings',
+    'd6_menu',
+    'd6_node_revision',
+    'd6_node',
+    'd6_node_settings',
+    'd6_node_type',
+    'd6_profile_values:user',
+    'd6_search_page',
+    'd6_search_settings',
+    'd6_simpletest_settings',
+    'd6_statistics_settings',
+    'd6_syslog_settings',
+    'd6_system_cron',
+    'd6_system_file',
+    'd6_system_filter',
+    'd6_system_image',
+    'd6_system_image_gd',
+    'd6_system_maintenance',
+    'd6_system_performance',
+    'd6_system_rss',
+    'd6_system_site',
+    'd6_system_theme',
+    'd6_taxonomy_settings',
+    'd6_taxonomy_term',
+    'd6_taxonomy_vocabulary',
+    'd6_term_node_revision:*',
+    'd6_term_node:*',
+    'd6_text_settings',
+    'd6_update_settings',
+    'd6_upload_entity_display',
+    'd6_upload_entity_form_display',
+    'd6_upload_field',
+    'd6_upload_field_instance',
+    'd6_upload',
+    'd6_url_alias',
+    'd6_user_mail',
+    'd6_user_profile_field_instance',
+    'd6_user_profile_entity_display',
+    'd6_user_profile_entity_form_display',
+    'd6_user_profile_field',
+    'd6_user_picture_entity_display',
+    'd6_user_picture_entity_form_display',
+    'd6_user_picture_field_instance',
+    'd6_user_picture_field',
+    'd6_user_picture_file',
+    'd6_user_role',
+    'd6_user',
+    'd6_view_modes',
+    'd6_vocabulary_entity_display',
+    'd6_vocabulary_entity_form_display',
+    'd6_vocabulary_field_instance',
+    'd6_vocabulary_field',
+  );
+
   /**
    * {@inheritdoc}
    */
@@ -63,22 +149,7 @@ public static function getInfo() {
   /**
    * {@inheritdoc}
    */
-  public function tearDown() {
-    // Move the results of every class under ours. This is solely for
-    // reporting, the filename will guide developers.
-    self::getDatabaseConnection()
-      ->update('simpletest')
-      ->fields(array('test_class' => get_class($this)))
-      ->condition('test_id', $this->testId)
-      ->execute();
-    parent::tearDown();
-  }
-
-
-  /**
-   * Test the complete Drupal 6 migration.
-   */
-  public function testDrupal6() {
+  protected function getDumps() {
     $tests_path = $this->getDumpDirectory();
     $dumps = array(
       $tests_path . '/Drupal6ActionSettings.php',
@@ -140,89 +211,14 @@ public function testDrupal6() {
       $tests_path . '/Drupal6UserRole.php',
       $tests_path . '/Drupal6VocabularyField.php',
     );
-    $this->loadDumps($dumps);
-    $migrations = array(
-      'd6_action_settings',
-      'd6_aggregator_settings',
-      'd6_aggregator_feed',
-      'd6_aggregator_item',
-      'd6_block',
-      'd6_book_settings',
-      'd6_cck_field_values:*',
-      'd6_cck_field_revision:*',
-      'd6_comment',
-      'd6_comment_entity_display',
-      'd6_comment_entity_form_display',
-      'd6_comment_field',
-      'd6_comment_field_instance',
-      'd6_contact_category',
-      'd6_contact_settings',
-      'd6_custom_block',
-      'd6_date_formats',
-      'd6_dblog_settings',
-      'd6_field',
-      'd6_field_instance',
-      'd6_field_instance_widget_settings',
-      'd6_field_settings',
-      'd6_field_formatter_settings',
-      'd6_file_settings',
-      'd6_file',
-      'd6_filter_format',
-      'd6_forum_settings',
-      'd6_locale_settings',
-      'd6_menu_settings',
-      'd6_menu',
-      'd6_node_revision',
-      'd6_node',
-      'd6_node_settings',
-      'd6_node_type',
-      'd6_profile_values:user',
-      'd6_search_page',
-      'd6_search_settings',
-      'd6_simpletest_settings',
-      'd6_statistics_settings',
-      'd6_syslog_settings',
-      'd6_system_cron',
-      'd6_system_file',
-      'd6_system_filter',
-      'd6_system_image',
-      'd6_system_image_gd',
-      'd6_system_maintenance',
-      'd6_system_performance',
-      'd6_system_rss',
-      'd6_system_site',
-      'd6_system_theme',
-      'd6_taxonomy_settings',
-      'd6_taxonomy_term',
-      'd6_taxonomy_vocabulary',
-      'd6_term_node_revision:*',
-      'd6_term_node:*',
-      'd6_text_settings',
-      'd6_update_settings',
-      'd6_upload_entity_display',
-      'd6_upload_entity_form_display',
-      'd6_upload_field',
-      'd6_upload_field_instance',
-      'd6_upload',
-      'd6_url_alias',
-      'd6_user_mail',
-      'd6_user_profile_field_instance',
-      'd6_user_profile_entity_display',
-      'd6_user_profile_entity_form_display',
-      'd6_user_profile_field',
-      'd6_user_picture_entity_display',
-      'd6_user_picture_entity_form_display',
-      'd6_user_picture_field_instance',
-      'd6_user_picture_field',
-      'd6_user_picture_file',
-      'd6_user_role',
-      'd6_user',
-      'd6_view_modes',
-      'd6_vocabulary_entity_display',
-      'd6_vocabulary_entity_form_display',
-      'd6_vocabulary_field_instance',
-      'd6_vocabulary_field',
-    );
+
+    return $dumps;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getTestClassesList() {
     $classes = array(
       __NAMESPACE__ . '\MigrateActionConfigsTest',
       __NAMESPACE__ . '\MigrateAggregatorConfigsTest',
@@ -305,46 +301,8 @@ public function testDrupal6() {
       __NAMESPACE__ . '\MigrateVocabularyFieldInstanceTest',
       __NAMESPACE__ . '\MigrateVocabularyFieldTest',
     );
-    // Run every migration in the order specified by the storage controller.
-    foreach (entity_load_multiple('migration', $migrations) as $migration) {
-      (new MigrateExecutable($migration, $this))->import();
-    }
-    foreach ($classes as $class) {
-      $test_object = new $class($this->testId);
-      $test_object->databasePrefix = $this->databasePrefix;
-      $test_object->container = $this->container;
-      // run() does a lot of setup and tear down work which we don't need:
-      // it would setup a new database connection and wouldn't find the
-      // Drupal 6 dump. Also by skipping the setUp() methods there are no id
-      // mappings or entities prepared. The tests run against solely migrated
-      // data.
-      foreach (get_class_methods($test_object) as $method) {
-        if (strtolower(substr($method, 0, 4)) == 'test') {
-          // Insert a fail record. This will be deleted on completion to ensure
-          // that testing completed.
-          $method_info = new \ReflectionMethod($class, $method);
-          $caller = array(
-            'file' => $method_info->getFileName(),
-            'line' => $method_info->getStartLine(),
-            'function' => $class . '->' . $method . '()',
-          );
-          $completion_check_id = TestBase::insertAssert($this->testId, $class, FALSE, 'The test did not complete due to a fatal error.', 'Completion check', $caller);
-          // Run the test method.
-          try {
-            $test_object->$method();
-          }
-          catch (\Exception $e) {
-            $this->exceptionHandler($e);
-          }
-          // Remove the completion check record.
-          TestBase::deleteAssert($completion_check_id);
-        }
-      }
-      // Add the pass/fail/exception/debug results.
-      foreach ($this->results as $key => &$value) {
-        $value += $test_object->results[$key];
-      }
-    }
+
+    return $classes;
   }
 
 }
-- 
GitLab