Upgraded drupal core with security updates
[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   protected $profile = 'standard';
25
26   /**
27    * The source database connection.
28    *
29    * @var \Drupal\Core\Database\Connection
30    */
31   protected $sourceDatabase;
32
33   /**
34    * Modules to enable.
35    *
36    * @var array
37    */
38   public static $modules = [
39     'language',
40     'content_translation',
41     'migrate_drupal_ui',
42     'telephone',
43     'aggregator',
44     'book',
45     'forum',
46     'statistics',
47   ];
48
49   /**
50    * {@inheritdoc}
51    */
52   protected function setUp() {
53     parent::setUp();
54     $this->createMigrationConnection();
55     $this->sourceDatabase = Database::getConnection('default', 'migrate_drupal_ui');
56
57     // Log in as user 1. Migrations in the UI can only be performed as user 1.
58     $this->drupalLogin($this->rootUser);
59   }
60
61   /**
62    * Loads a database fixture into the source database connection.
63    *
64    * @param string $path
65    *   Path to the dump file.
66    */
67   protected function loadFixture($path) {
68     $default_db = Database::getConnection()->getKey();
69     Database::setActiveConnection($this->sourceDatabase->getKey());
70
71     if (substr($path, -3) == '.gz') {
72       $path = 'compress.zlib://' . $path;
73     }
74     require $path;
75
76     Database::setActiveConnection($default_db);
77   }
78
79   /**
80    * Changes the database connection to the prefixed one.
81    *
82    * @todo Remove when we don't use global. https://www.drupal.org/node/2552791
83    */
84   protected function createMigrationConnection() {
85     $connection_info = Database::getConnectionInfo('default')['default'];
86     if ($connection_info['driver'] === 'sqlite') {
87       // Create database file in the test site's public file directory so that
88       // \Drupal\simpletest\TestBase::restoreEnvironment() will delete this once
89       // the test is complete.
90       $file = $this->publicFilesDirectory . '/' . $this->testId . '-migrate.db.sqlite';
91       touch($file);
92       $connection_info['database'] = $file;
93       $connection_info['prefix'] = '';
94     }
95     else {
96       $prefix = is_array($connection_info['prefix']) ? $connection_info['prefix']['default'] : $connection_info['prefix'];
97       // Simpletest uses fixed length prefixes. Create a new prefix for the
98       // source database. Adding to the end of the prefix ensures that
99       // \Drupal\simpletest\TestBase::restoreEnvironment() will remove the
100       // additional tables.
101       $connection_info['prefix'] = $prefix . '0';
102     }
103
104     Database::addConnectionInfo('migrate_drupal_ui', 'default', $connection_info);
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   protected function tearDown() {
111     Database::removeConnection('migrate_drupal_ui');
112     parent::tearDown();
113   }
114
115   /**
116    * Executes all steps of migrations upgrade.
117    */
118   public function testMigrateUpgrade() {
119     $connection_options = $this->sourceDatabase->getConnectionOptions();
120     $this->drupalGet('/upgrade');
121     $this->assertText('Upgrade a site by importing it into a clean and empty new install of Drupal 8. You will lose any existing configuration once you import your site into it. See the online documentation for Drupal site upgrades for more detailed information.');
122
123     $this->drupalPostForm(NULL, [], t('Continue'));
124     $this->assertText('Provide credentials for the database of the Drupal site you want to upgrade.');
125     $this->assertFieldByName('mysql[host]');
126
127     $driver = $connection_options['driver'];
128     $connection_options['prefix'] = $connection_options['prefix']['default'];
129
130     // Use the driver connection form to get the correct options out of the
131     // database settings. This supports all of the databases we test against.
132     $drivers = drupal_get_database_types();
133     $form = $drivers[$driver]->getFormOptions($connection_options);
134     $connection_options = array_intersect_key($connection_options, $form + $form['advanced_options']);
135     $edit = [
136       $driver => $connection_options,
137       'source_base_path' => $this->getSourceBasePath(),
138     ];
139     if (count($drivers) !== 1) {
140       $edit['driver'] = $driver;
141     }
142     $edits = $this->translatePostValues($edit);
143
144     // Ensure submitting the form with invalid database credentials gives us a
145     // nice warning.
146     $this->drupalPostForm(NULL, [$driver . '[database]' => 'wrong'] + $edits, t('Review upgrade'));
147     $this->assertText('Resolve the issue below to continue the upgrade.');
148
149     $this->drupalPostForm(NULL, $edits, t('Review upgrade'));
150     $this->assertResponse(200);
151     $this->assertText('Are you sure?');
152     $this->drupalPostForm(NULL, [], t('Perform upgrade'));
153     $this->assertText(t('Congratulations, you upgraded Drupal!'));
154
155     // Have to reset all the statics after migration to ensure entities are
156     // loadable.
157     $this->resetAll();
158
159     $expected_counts = $this->getEntityCounts();
160     foreach (array_keys(\Drupal::entityTypeManager()
161       ->getDefinitions()) as $entity_type) {
162       $real_count = \Drupal::entityQuery($entity_type)->count()->execute();
163       $expected_count = isset($expected_counts[$entity_type]) ? $expected_counts[$entity_type] : 0;
164       $this->assertEqual($expected_count, $real_count, "Found $real_count $entity_type entities, expected $expected_count.");
165     }
166
167     $version_tag = 'Drupal ' . $this->getLegacyDrupalVersion($this->sourceDatabase);
168     $plugin_manager = \Drupal::service('plugin.manager.migration');
169     /** @var \Drupal\migrate\Plugin\Migration[] $all_migrations */
170     $all_migrations = $plugin_manager->createInstancesByTag($version_tag);
171     foreach ($all_migrations as $migration) {
172       $id_map = $migration->getIdMap();
173       foreach ($id_map as $source_id => $map) {
174         // Convert $source_id into a keyless array so that
175         // \Drupal\migrate\Plugin\migrate\id_map\Sql::getSourceHash() works as
176         // expected.
177         $source_id_values = array_values(unserialize($source_id));
178         $row = $id_map->getRowBySource($source_id_values);
179         $destination = serialize($id_map->currentDestination());
180         $message = "Migration of $source_id to $destination as part of the {$migration->id()} migration. The source row status is " . $row['source_row_status'];
181         // A completed migration should have maps with
182         // MigrateIdMapInterface::STATUS_IGNORED or
183         // MigrateIdMapInterface::STATUS_IMPORTED.
184         if ($row['source_row_status'] == MigrateIdMapInterface::STATUS_FAILED || $row['source_row_status'] == MigrateIdMapInterface::STATUS_NEEDS_UPDATE) {
185           $this->fail($message);
186         }
187         else {
188           $this->pass($message);
189         }
190       }
191     }
192     \Drupal::service('module_installer')->install(['forum']);
193     \Drupal::service('module_installer')->install(['book']);
194   }
195
196   /**
197    * Gets the source base path for the concrete test.
198    *
199    * @return string
200    *   The source base path.
201    */
202   abstract protected function getSourceBasePath();
203
204   /**
205    * Gets the expected number of entities per entity type after migration.
206    *
207    * @return int[]
208    *   An array of expected counts keyed by entity type ID.
209    */
210   abstract protected function getEntityCounts();
211
212 }