e39adee22f2ab60514c583a5f1229d3bd0587f70
[yaffs-website] / web / core / modules / migrate_drupal_ui / src / Tests / MigrateUpgradeTestBase.php
1 <?php
2
3 namespace Drupal\migrate_drupal_ui\Tests;
4
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);
6
7 use Drupal\Core\Database\Database;
8 use Drupal\migrate\Plugin\MigrateIdMapInterface;
9 use Drupal\migrate_drupal\MigrationConfigurationTrait;
10 use Drupal\simpletest\WebTestBase;
11
12 /**
13  * Provides a base class for testing migration upgrades in the UI.
14  *
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.
17  */
18 abstract class MigrateUpgradeTestBase extends WebTestBase {
19   use MigrationConfigurationTrait;
20
21   /**
22    * Use the Standard profile to test help implementations of many core modules.
23    *
24    * @var string
25    */
26   protected $profile = 'standard';
27
28   /**
29    * The source database connection.
30    *
31    * @var \Drupal\Core\Database\Connection
32    */
33   protected $sourceDatabase;
34
35   /**
36    * Modules to enable.
37    *
38    * @var array
39    */
40   public static $modules = [
41     'language',
42     'content_translation',
43     'migrate_drupal_ui',
44     'telephone',
45     'aggregator',
46     'book',
47     'forum',
48     'statistics',
49     'modules_available_test',
50   ];
51
52   /**
53    * {@inheritdoc}
54    */
55   protected function setUp() {
56     parent::setUp();
57     $this->createMigrationConnection();
58     $this->sourceDatabase = Database::getConnection('default', 'migrate_drupal_ui');
59
60     // Log in as user 1. Migrations in the UI can only be performed as user 1.
61     $this->drupalLogin($this->rootUser);
62   }
63
64   /**
65    * Loads a database fixture into the source database connection.
66    *
67    * @param string $path
68    *   Path to the dump file.
69    */
70   protected function loadFixture($path) {
71     $default_db = Database::getConnection()->getKey();
72     Database::setActiveConnection($this->sourceDatabase->getKey());
73
74     if (substr($path, -3) == '.gz') {
75       $path = 'compress.zlib://' . $path;
76     }
77     require $path;
78
79     Database::setActiveConnection($default_db);
80   }
81
82   /**
83    * Changes the database connection to the prefixed one.
84    *
85    * @todo Remove when we don't use global. https://www.drupal.org/node/2552791
86    */
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';
94       touch($file);
95       $connection_info['database'] = $file;
96       $connection_info['prefix'] = '';
97     }
98     else {
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';
105     }
106
107     Database::addConnectionInfo('migrate_drupal_ui', 'default', $connection_info);
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   protected function tearDown() {
114     Database::removeConnection('migrate_drupal_ui');
115     parent::tearDown();
116   }
117
118   /**
119    * Executes all steps of migrations upgrade.
120    */
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.');
125
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]');
129
130     $driver = $connection_options['driver'];
131     $connection_options['prefix'] = $connection_options['prefix']['default'];
132
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']);
138     $edit = [
139       $driver => $connection_options,
140       'source_base_path' => $this->getSourceBasePath(),
141     ];
142     if (count($drivers) !== 1) {
143       $edit['driver'] = $driver;
144     }
145     $edits = $this->translatePostValues($edit);
146
147     // Ensure submitting the form with invalid database credentials gives us a
148     // nice warning.
149     $this->drupalPostForm(NULL, [$driver . '[database]' => 'wrong'] + $edits, t('Review upgrade'));
150     $this->assertText('Resolve the issue below to continue the upgrade.');
151
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'));
159
160     // Uninstall the module causing the missing module error messages.
161     $this->container->get('module_installer')->uninstall(['modules_available_test'], TRUE);
162
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.');
166
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]');
170
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!'));
181
182     // Have to reset all the statics after migration to ensure entities are
183     // loadable.
184     $this->resetAll();
185
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.");
192     }
193
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
203         // expected.
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);
213         }
214         else {
215           $this->pass($message);
216         }
217       }
218     }
219     \Drupal::service('module_installer')->install(['forum']);
220     \Drupal::service('module_installer')->install(['book']);
221   }
222
223   /**
224    * Gets the source base path for the concrete test.
225    *
226    * @return string
227    *   The source base path.
228    */
229   abstract protected function getSourceBasePath();
230
231   /**
232    * Gets the expected number of entities per entity type after migration.
233    *
234    * @return int[]
235    *   An array of expected counts keyed by entity type ID.
236    */
237   abstract protected function getEntityCounts();
238
239 }