MigrateUpgradeTestBase.php 8.92 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\migrate_drupal_ui\Tests;

5 6
@trigger_error('\Drupal\migrate_drupal_ui\Tests\MigrateUpgradeTestBase is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use \Drupal\Tests\migrate_drupal_ui\Functional\MigrateUpgradeTestBase instead.', E_USER_DEPRECATED);

7
use Drupal\Core\Database\Database;
8
use Drupal\migrate\Plugin\MigrateIdMapInterface;
9
use Drupal\migrate_drupal\MigrationConfigurationTrait;
10 11 12 13
use Drupal\simpletest\WebTestBase;

/**
 * Provides a base class for testing migration upgrades in the UI.
14 15 16
 *
 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use
 *   \Drupal\Tests\migrate_drupal_ui\Functional\MigrateUpgradeTestBase instead.
17 18
 */
abstract class MigrateUpgradeTestBase extends WebTestBase {
19
  use MigrationConfigurationTrait;
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  /**
   * Use the Standard profile to test help implementations of many core modules.
   */
  protected $profile = 'standard';

  /**
   * The source database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $sourceDatabase;

  /**
   * Modules to enable.
   *
   * @var array
   */
38 39 40 41 42 43 44 45 46
  public static $modules = [
    'language',
    'content_translation',
    'migrate_drupal_ui',
    'telephone',
    'aggregator',
    'book',
    'forum',
    'statistics',
47
    'modules_available_test',
48
  ];
49 50 51 52 53 54 55 56 57

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->createMigrationConnection();
    $this->sourceDatabase = Database::getConnection('default', 'migrate_drupal_ui');

58
    // Log in as user 1. Migrations in the UI can only be performed as user 1.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    $this->drupalLogin($this->rootUser);
  }

  /**
   * Loads a database fixture into the source database connection.
   *
   * @param string $path
   *   Path to the dump file.
   */
  protected function loadFixture($path) {
    $default_db = Database::getConnection()->getKey();
    Database::setActiveConnection($this->sourceDatabase->getKey());

    if (substr($path, -3) == '.gz') {
      $path = 'compress.zlib://' . $path;
    }
    require $path;

    Database::setActiveConnection($default_db);
  }

  /**
   * Changes the database connection to the prefixed one.
   *
   * @todo Remove when we don't use global. https://www.drupal.org/node/2552791
   */
  protected function createMigrationConnection() {
    $connection_info = Database::getConnectionInfo('default')['default'];
    if ($connection_info['driver'] === 'sqlite') {
      // Create database file in the test site's public file directory so that
      // \Drupal\simpletest\TestBase::restoreEnvironment() will delete this once
      // the test is complete.
      $file = $this->publicFilesDirectory . '/' . $this->testId . '-migrate.db.sqlite';
      touch($file);
      $connection_info['database'] = $file;
      $connection_info['prefix'] = '';
    }
    else {
      $prefix = is_array($connection_info['prefix']) ? $connection_info['prefix']['default'] : $connection_info['prefix'];
      // Simpletest uses fixed length prefixes. Create a new prefix for the
      // source database. Adding to the end of the prefix ensures that
      // \Drupal\simpletest\TestBase::restoreEnvironment() will remove the
      // additional tables.
      $connection_info['prefix'] = $prefix . '0';
    }

    Database::addConnectionInfo('migrate_drupal_ui', 'default', $connection_info);
  }

  /**
   * {@inheritdoc}
   */
  protected function tearDown() {
    Database::removeConnection('migrate_drupal_ui');
    parent::tearDown();
  }

  /**
   * Executes all steps of migrations upgrade.
   */
119
  public function testMigrateUpgrade() {
120 121
    $connection_options = $this->sourceDatabase->getConnectionOptions();
    $this->drupalGet('/upgrade');
122
    $this->assertText('Upgrade a site by importing its database and files into a clean and empty new install of Drupal 8.');
123 124 125 126 127 128 129 130 131 132 133 134 135

    $this->drupalPostForm(NULL, [], t('Continue'));
    $this->assertText('Provide credentials for the database of the Drupal site you want to upgrade.');
    $this->assertFieldByName('mysql[host]');

    $driver = $connection_options['driver'];
    $connection_options['prefix'] = $connection_options['prefix']['default'];

    // Use the driver connection form to get the correct options out of the
    // database settings. This supports all of the databases we test against.
    $drivers = drupal_get_database_types();
    $form = $drivers[$driver]->getFormOptions($connection_options);
    $connection_options = array_intersect_key($connection_options, $form + $form['advanced_options']);
136
    $edit = [
137 138
      $driver => $connection_options,
      'source_base_path' => $this->getSourceBasePath(),
139 140 141 142 143 144 145 146 147 148
    ];
    if (count($drivers) !== 1) {
      $edit['driver'] = $driver;
    }
    $edits = $this->translatePostValues($edit);

    // Ensure submitting the form with invalid database credentials gives us a
    // nice warning.
    $this->drupalPostForm(NULL, [$driver . '[database]' => 'wrong'] + $edits, t('Review upgrade'));
    $this->assertText('Resolve the issue below to continue the upgrade.');
149 150 151 152

    $this->drupalPostForm(NULL, $edits, t('Review upgrade'));
    $this->assertResponse(200);
    $this->assertText('Are you sure?');
153 154 155 156 157 158 159 160 161 162
    // Ensure we get errors about missing modules.
    $this->assertText(t('Source module not found for module_no_annotation.'));
    $this->assertText(t('Source module not found for modules_available_test.'));
    $this->assertText(t('Destination module not found for modules_available_test'));

    // Uninstall the module causing the missing module error messages.
    $this->container->get('module_installer')->uninstall(['modules_available_test'], TRUE);

    // Restart the upgrade process.
    $this->drupalGet('/upgrade');
163
    $this->assertText('Upgrade a site by importing its database and files into a clean and empty new install of Drupal 8.');
164 165 166 167 168 169 170 171 172 173 174 175 176

    $this->drupalPostForm(NULL, [], t('Continue'));
    $this->assertText('Provide credentials for the database of the Drupal site you want to upgrade.');
    $this->assertFieldByName('mysql[host]');

    $this->drupalPostForm(NULL, $edits, t('Review upgrade'));
    $this->assertResponse(200);
    $this->assertText('Are you sure?');
    // Ensure there are no errors about the missing modules.
    $this->assertNoText(t('Source module not found for module_no_annotation.'));
    $this->assertNoText(t('Source module not found for modules_available_test.'));
    $this->assertNoText(t('Destination module not found for modules_available_test'));
    // Check for any missing module errors.
177 178 179 180 181 182 183 184
    $this->drupalPostForm(NULL, [], t('Perform upgrade'));
    $this->assertText(t('Congratulations, you upgraded Drupal!'));

    // Have to reset all the statics after migration to ensure entities are
    // loadable.
    $this->resetAll();

    $expected_counts = $this->getEntityCounts();
185 186 187
    foreach (array_keys(\Drupal::entityTypeManager()
      ->getDefinitions()) as $entity_type) {
      $real_count = \Drupal::entityQuery($entity_type)->count()->execute();
188 189 190
      $expected_count = isset($expected_counts[$entity_type]) ? $expected_counts[$entity_type] : 0;
      $this->assertEqual($expected_count, $real_count, "Found $real_count $entity_type entities, expected $expected_count.");
    }
191 192 193 194 195 196 197 198 199 200 201 202 203 204

    $version_tag = 'Drupal ' . $this->getLegacyDrupalVersion($this->sourceDatabase);
    $plugin_manager = \Drupal::service('plugin.manager.migration');
    /** @var \Drupal\migrate\Plugin\Migration[] $all_migrations */
    $all_migrations = $plugin_manager->createInstancesByTag($version_tag);
    foreach ($all_migrations as $migration) {
      $id_map = $migration->getIdMap();
      foreach ($id_map as $source_id => $map) {
        // Convert $source_id into a keyless array so that
        // \Drupal\migrate\Plugin\migrate\id_map\Sql::getSourceHash() works as
        // expected.
        $source_id_values = array_values(unserialize($source_id));
        $row = $id_map->getRowBySource($source_id_values);
        $destination = serialize($id_map->currentDestination());
205
        $message = "Migration of $source_id to $destination as part of the {$migration->id()} migration. The source row status is " . $row['source_row_status'];
206 207 208 209 210 211 212 213 214 215 216
        // A completed migration should have maps with
        // MigrateIdMapInterface::STATUS_IGNORED or
        // MigrateIdMapInterface::STATUS_IMPORTED.
        if ($row['source_row_status'] == MigrateIdMapInterface::STATUS_FAILED || $row['source_row_status'] == MigrateIdMapInterface::STATUS_NEEDS_UPDATE) {
          $this->fail($message);
        }
        else {
          $this->pass($message);
        }
      }
    }
217
    \Drupal::service('module_installer')->install(['forum']);
218
    \Drupal::service('module_installer')->install(['book']);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  }

  /**
   * Gets the source base path for the concrete test.
   *
   * @return string
   *   The source base path.
   */
  abstract protected function getSourceBasePath();

  /**
   * Gets the expected number of entities per entity type after migration.
   *
   * @return int[]
   *   An array of expected counts keyed by entity type ID.
   */
  abstract protected function getEntityCounts();

}