3 namespace Drupal\migrate_drupal_ui\Tests;
5 @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 use Drupal\simpletest\WebTestBase;
13 * Provides a base class for testing migration upgrades in the UI.
15 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use
16 * \Drupal\Tests\migrate_drupal_ui\Functional\MigrateUpgradeTestBase instead.
18 abstract class MigrateUpgradeTestBase extends WebTestBase {
19 use MigrationConfigurationTrait;
22 * Use the Standard profile to test help implementations of many core modules.
26 protected $profile = 'standard';
29 * The source database connection.
31 * @var \Drupal\Core\Database\Connection
33 protected $sourceDatabase;
40 public static $modules = [
42 'content_translation',
49 'modules_available_test',
55 protected function setUp() {
57 $this->createMigrationConnection();
58 $this->sourceDatabase = Database::getConnection('default', 'migrate_drupal_ui');
60 // Log in as user 1. Migrations in the UI can only be performed as user 1.
61 $this->drupalLogin($this->rootUser);
65 * Loads a database fixture into the source database connection.
68 * Path to the dump file.
70 protected function loadFixture($path) {
71 $default_db = Database::getConnection()->getKey();
72 Database::setActiveConnection($this->sourceDatabase->getKey());
74 if (substr($path, -3) == '.gz') {
75 $path = 'compress.zlib://' . $path;
79 Database::setActiveConnection($default_db);
83 * Changes the database connection to the prefixed one.
85 * @todo Remove when we don't use global. https://www.drupal.org/node/2552791
87 protected function createMigrationConnection() {
88 $connection_info = Database::getConnectionInfo('default')['default'];
89 if ($connection_info['driver'] === 'sqlite') {
90 // Create database file in the test site's public file directory so that
91 // \Drupal\simpletest\TestBase::restoreEnvironment() will delete this once
92 // the test is complete.
93 $file = $this->publicFilesDirectory . '/' . $this->testId . '-migrate.db.sqlite';
95 $connection_info['database'] = $file;
96 $connection_info['prefix'] = '';
99 $prefix = is_array($connection_info['prefix']) ? $connection_info['prefix']['default'] : $connection_info['prefix'];
100 // Simpletest uses fixed length prefixes. Create a new prefix for the
101 // source database. Adding to the end of the prefix ensures that
102 // \Drupal\simpletest\TestBase::restoreEnvironment() will remove the
103 // additional tables.
104 $connection_info['prefix'] = $prefix . '0';
107 Database::addConnectionInfo('migrate_drupal_ui', 'default', $connection_info);
113 protected function tearDown() {
114 Database::removeConnection('migrate_drupal_ui');
119 * Executes all steps of migrations upgrade.
121 public function testMigrateUpgrade() {
122 $connection_options = $this->sourceDatabase->getConnectionOptions();
123 $this->drupalGet('/upgrade');
124 $this->assertText('Upgrade a site by importing its database and files into a clean and empty new install of Drupal 8.');
126 $this->drupalPostForm(NULL, [], t('Continue'));
127 $this->assertText('Provide credentials for the database of the Drupal site you want to upgrade.');
128 $this->assertFieldByName('mysql[host]');
130 $driver = $connection_options['driver'];
131 $connection_options['prefix'] = $connection_options['prefix']['default'];
133 // Use the driver connection form to get the correct options out of the
134 // database settings. This supports all of the databases we test against.
135 $drivers = drupal_get_database_types();
136 $form = $drivers[$driver]->getFormOptions($connection_options);
137 $connection_options = array_intersect_key($connection_options, $form + $form['advanced_options']);
139 $driver => $connection_options,
140 'source_base_path' => $this->getSourceBasePath(),
142 if (count($drivers) !== 1) {
143 $edit['driver'] = $driver;
145 $edits = $this->translatePostValues($edit);
147 // Ensure submitting the form with invalid database credentials gives us a
149 $this->drupalPostForm(NULL, [$driver . '[database]' => 'wrong'] + $edits, t('Review upgrade'));
150 $this->assertText('Resolve the issue below to continue the upgrade.');
152 $this->drupalPostForm(NULL, $edits, t('Review upgrade'));
153 $this->assertResponse(200);
154 $this->assertText('Upgrade analysis report');
155 // Ensure we get errors about missing modules.
156 $this->assertText(t('Source module not found for module_no_annotation.'));
157 $this->assertText(t('Source module not found for modules_available_test.'));
158 $this->assertText(t('Destination module not found for modules_available_test'));
160 // Uninstall the module causing the missing module error messages.
161 $this->container->get('module_installer')->uninstall(['modules_available_test'], TRUE);
163 // Restart the upgrade process.
164 $this->drupalGet('/upgrade');
165 $this->assertText('Upgrade a site by importing its database and files into a clean and empty new install of Drupal 8.');
167 $this->drupalPostForm(NULL, [], t('Continue'));
168 $this->assertText('Provide credentials for the database of the Drupal site you want to upgrade.');
169 $this->assertFieldByName('mysql[host]');
171 $this->drupalPostForm(NULL, $edits, t('Review upgrade'));
172 $this->assertResponse(200);
173 $this->assertText('Upgrade analysis report');
174 // Ensure there are no errors about the missing modules.
175 $this->assertNoText(t('Source module not found for module_no_annotation.'));
176 $this->assertNoText(t('Source module not found for modules_available_test.'));
177 $this->assertNoText(t('Destination module not found for modules_available_test'));
178 // Check for any missing module errors.
179 $this->drupalPostForm(NULL, [], t('Perform upgrade'));
180 $this->assertText(t('Congratulations, you upgraded Drupal!'));
182 // Have to reset all the statics after migration to ensure entities are
186 $expected_counts = $this->getEntityCounts();
187 foreach (array_keys(\Drupal::entityTypeManager()
188 ->getDefinitions()) as $entity_type) {
189 $real_count = \Drupal::entityQuery($entity_type)->count()->execute();
190 $expected_count = isset($expected_counts[$entity_type]) ? $expected_counts[$entity_type] : 0;
191 $this->assertEqual($expected_count, $real_count, "Found $real_count $entity_type entities, expected $expected_count.");
194 $version_tag = 'Drupal ' . $this->getLegacyDrupalVersion($this->sourceDatabase);
195 $plugin_manager = \Drupal::service('plugin.manager.migration');
196 /** @var \Drupal\migrate\Plugin\Migration[] $all_migrations */
197 $all_migrations = $plugin_manager->createInstancesByTag($version_tag);
198 foreach ($all_migrations as $migration) {
199 $id_map = $migration->getIdMap();
200 foreach ($id_map as $source_id => $map) {
201 // Convert $source_id into a keyless array so that
202 // \Drupal\migrate\Plugin\migrate\id_map\Sql::getSourceHash() works as
204 $source_id_values = array_values(unserialize($source_id));
205 $row = $id_map->getRowBySource($source_id_values);
206 $destination = serialize($id_map->currentDestination());
207 $message = "Migration of $source_id to $destination as part of the {$migration->id()} migration. The source row status is " . $row['source_row_status'];
208 // A completed migration should have maps with
209 // MigrateIdMapInterface::STATUS_IGNORED or
210 // MigrateIdMapInterface::STATUS_IMPORTED.
211 if ($row['source_row_status'] == MigrateIdMapInterface::STATUS_FAILED || $row['source_row_status'] == MigrateIdMapInterface::STATUS_NEEDS_UPDATE) {
212 $this->fail($message);
215 $this->pass($message);
219 \Drupal::service('module_installer')->install(['forum']);
220 \Drupal::service('module_installer')->install(['book']);
224 * Gets the source base path for the concrete test.
227 * The source base path.
229 abstract protected function getSourceBasePath();
232 * Gets the expected number of entities per entity type after migration.
235 * An array of expected counts keyed by entity type ID.
237 abstract protected function getEntityCounts();