Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system\Tests\Update;
|
Chris@0
|
4
|
Chris@0
|
5 @trigger_error(__NAMESPACE__ . '\UpdatePathTestBase is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use \Drupal\FunctionalTests\Update\UpdatePathTestBase instead. See https://www.drupal.org/node/2896640.', E_USER_DEPRECATED);
|
Chris@0
|
6
|
Chris@0
|
7 use Drupal\Component\Utility\Crypt;
|
Chris@0
|
8 use Drupal\Tests\SchemaCheckTestTrait;
|
Chris@0
|
9 use Drupal\Core\Database\Database;
|
Chris@0
|
10 use Drupal\Core\DependencyInjection\ContainerBuilder;
|
Chris@0
|
11 use Drupal\Core\Language\Language;
|
Chris@0
|
12 use Drupal\Core\Url;
|
Chris@0
|
13 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
14 use Drupal\user\Entity\User;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Reference;
|
Chris@0
|
16 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Provides a base class for writing an update test.
|
Chris@0
|
20 *
|
Chris@0
|
21 * To write an update test:
|
Chris@0
|
22 * - Write the hook_update_N() implementations that you are testing.
|
Chris@0
|
23 * - Create one or more database dump files, which will set the database to the
|
Chris@0
|
24 * "before updates" state. Normally, these will add some configuration data to
|
Chris@0
|
25 * the database, set up some tables/fields, etc.
|
Chris@0
|
26 * - Create a class that extends this class.
|
Chris@0
|
27 * - In your setUp() method, point the $this->databaseDumpFiles variable to the
|
Chris@0
|
28 * database dump files, and then call parent::setUp() to run the base setUp()
|
Chris@0
|
29 * method in this class.
|
Chris@0
|
30 * - In your test method, call $this->runUpdates() to run the necessary updates,
|
Chris@0
|
31 * and then use test assertions to verify that the result is what you expect.
|
Chris@0
|
32 * - In order to test both with a "bare" database dump as well as with a
|
Chris@0
|
33 * database dump filled with content, extend your update path test class with
|
Chris@0
|
34 * a new test class that overrides the bare database dump. Refer to
|
Chris@0
|
35 * UpdatePathTestBaseFilledTest for an example.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @ingroup update_api
|
Chris@0
|
38 *
|
Chris@0
|
39 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
|
Chris@0
|
40 * Use \Drupal\FunctionalTests\Update\UpdatePathTestBase.
|
Chris@0
|
41 * @see https://www.drupal.org/node/2896640
|
Chris@0
|
42 *
|
Chris@0
|
43 * @see hook_update_N()
|
Chris@0
|
44 */
|
Chris@0
|
45 abstract class UpdatePathTestBase extends WebTestBase {
|
Chris@0
|
46
|
Chris@0
|
47 use SchemaCheckTestTrait;
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Modules to enable after the database is loaded.
|
Chris@0
|
51 */
|
Chris@0
|
52 protected static $modules = [];
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * The file path(s) to the dumped database(s) to load into the child site.
|
Chris@0
|
56 *
|
Chris@0
|
57 * The file system/tests/fixtures/update/drupal-8.bare.standard.php.gz is
|
Chris@0
|
58 * normally included first -- this sets up the base database from a bare
|
Chris@0
|
59 * standard Drupal installation.
|
Chris@0
|
60 *
|
Chris@0
|
61 * The file system/tests/fixtures/update/drupal-8.filled.standard.php.gz
|
Chris@0
|
62 * can also be used in case we want to test with a database filled with
|
Chris@0
|
63 * content, and with all core modules enabled.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @var array
|
Chris@0
|
66 */
|
Chris@0
|
67 protected $databaseDumpFiles = [];
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * The install profile used in the database dump file.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @var string
|
Chris@0
|
73 */
|
Chris@0
|
74 protected $installProfile = 'standard';
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Flag that indicates whether the child site has been updated.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @var bool
|
Chris@0
|
80 */
|
Chris@0
|
81 protected $upgradedSite = FALSE;
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * Array of errors triggered during the update process.
|
Chris@0
|
85 *
|
Chris@0
|
86 * @var array
|
Chris@0
|
87 */
|
Chris@0
|
88 protected $upgradeErrors = [];
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * Array of modules loaded when the test starts.
|
Chris@0
|
92 *
|
Chris@0
|
93 * @var array
|
Chris@0
|
94 */
|
Chris@0
|
95 protected $loadedModules = [];
|
Chris@0
|
96
|
Chris@0
|
97 /**
|
Chris@0
|
98 * Flag to indicate whether zlib is installed or not.
|
Chris@0
|
99 *
|
Chris@0
|
100 * @var bool
|
Chris@0
|
101 */
|
Chris@0
|
102 protected $zlibInstalled = TRUE;
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * Flag to indicate whether there are pending updates or not.
|
Chris@0
|
106 *
|
Chris@0
|
107 * @var bool
|
Chris@0
|
108 */
|
Chris@0
|
109 protected $pendingUpdates = TRUE;
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * The update URL.
|
Chris@0
|
113 *
|
Chris@0
|
114 * @var string
|
Chris@0
|
115 */
|
Chris@0
|
116 protected $updateUrl;
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * Disable strict config schema checking.
|
Chris@0
|
120 *
|
Chris@0
|
121 * The schema is verified at the end of running the update.
|
Chris@0
|
122 *
|
Chris@0
|
123 * @var bool
|
Chris@0
|
124 */
|
Chris@0
|
125 protected $strictConfigSchema = FALSE;
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * Fail the test if there are failed updates.
|
Chris@0
|
129 *
|
Chris@0
|
130 * @var bool
|
Chris@0
|
131 */
|
Chris@0
|
132 protected $checkFailedUpdates = TRUE;
|
Chris@0
|
133
|
Chris@0
|
134 /**
|
Chris@0
|
135 * Constructs an UpdatePathTestCase object.
|
Chris@0
|
136 *
|
Chris@0
|
137 * @param $test_id
|
Chris@0
|
138 * (optional) The ID of the test. Tests with the same id are reported
|
Chris@0
|
139 * together.
|
Chris@0
|
140 */
|
Chris@0
|
141 public function __construct($test_id = NULL) {
|
Chris@0
|
142 parent::__construct($test_id);
|
Chris@0
|
143 $this->zlibInstalled = function_exists('gzopen');
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * Overrides WebTestBase::setUp() for update testing.
|
Chris@0
|
148 *
|
Chris@0
|
149 * The main difference in this method is that rather than performing the
|
Chris@0
|
150 * installation via the installer, a database is loaded. Additional work is
|
Chris@0
|
151 * then needed to set various things such as the config directories and the
|
Chris@0
|
152 * container that would normally be done via the installer.
|
Chris@0
|
153 */
|
Chris@0
|
154 protected function setUp() {
|
Chris@0
|
155 $this->runDbTasks();
|
Chris@0
|
156 // Allow classes to set database dump files.
|
Chris@0
|
157 $this->setDatabaseDumpFiles();
|
Chris@0
|
158
|
Chris@0
|
159 // We are going to set a missing zlib requirement property for usage
|
Chris@0
|
160 // during the performUpgrade() and tearDown() methods. Also set that the
|
Chris@0
|
161 // tests failed.
|
Chris@0
|
162 if (!$this->zlibInstalled) {
|
Chris@0
|
163 parent::setUp();
|
Chris@0
|
164 return;
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 // Set the update url. This must be set here rather than in
|
Chris@0
|
168 // self::__construct() or the old URL generator will leak additional test
|
Chris@0
|
169 // sites.
|
Chris@0
|
170 $this->updateUrl = Url::fromRoute('system.db_update');
|
Chris@0
|
171
|
Chris@0
|
172 // These methods are called from parent::setUp().
|
Chris@0
|
173 $this->setBatch();
|
Chris@0
|
174 $this->initUserSession();
|
Chris@0
|
175 $this->prepareSettings();
|
Chris@0
|
176
|
Chris@0
|
177 // Load the database(s).
|
Chris@0
|
178 foreach ($this->databaseDumpFiles as $file) {
|
Chris@0
|
179 if (substr($file, -3) == '.gz') {
|
Chris@0
|
180 $file = "compress.zlib://$file";
|
Chris@0
|
181 }
|
Chris@0
|
182 require $file;
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 $this->initSettings();
|
Chris@0
|
186 $request = Request::createFromGlobals();
|
Chris@0
|
187 $container = $this->initKernel($request);
|
Chris@0
|
188 $this->initConfig($container);
|
Chris@0
|
189
|
Chris@0
|
190 // Add the config directories to settings.php.
|
Chris@0
|
191 drupal_install_config_directories();
|
Chris@0
|
192
|
Chris@0
|
193 // Restore the original Simpletest batch.
|
Chris@0
|
194 $this->restoreBatch();
|
Chris@0
|
195
|
Chris@0
|
196 // Set the container. parent::rebuildAll() would normally do this, but this
|
Chris@0
|
197 // not safe to do here, because the database has not been updated yet.
|
Chris@0
|
198 $this->container = \Drupal::getContainer();
|
Chris@0
|
199
|
Chris@0
|
200 $this->replaceUser1();
|
Chris@0
|
201
|
Chris@0
|
202 require_once \Drupal::root() . '/core/includes/update.inc';
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 /**
|
Chris@0
|
206 * Set database dump files to be used.
|
Chris@0
|
207 */
|
Chris@0
|
208 abstract protected function setDatabaseDumpFiles();
|
Chris@0
|
209
|
Chris@0
|
210 /**
|
Chris@0
|
211 * Add settings that are missed since the installer isn't run.
|
Chris@0
|
212 */
|
Chris@0
|
213 protected function prepareSettings() {
|
Chris@0
|
214 parent::prepareSettings();
|
Chris@0
|
215
|
Chris@0
|
216 // Remember the profile which was used.
|
Chris@0
|
217 $settings['settings']['install_profile'] = (object) [
|
Chris@0
|
218 'value' => $this->installProfile,
|
Chris@0
|
219 'required' => TRUE,
|
Chris@0
|
220 ];
|
Chris@0
|
221 // Generate a hash salt.
|
Chris@0
|
222 $settings['settings']['hash_salt'] = (object) [
|
Chris@0
|
223 'value' => Crypt::randomBytesBase64(55),
|
Chris@0
|
224 'required' => TRUE,
|
Chris@0
|
225 ];
|
Chris@0
|
226
|
Chris@0
|
227 // Since the installer isn't run, add the database settings here too.
|
Chris@0
|
228 $settings['databases']['default'] = (object) [
|
Chris@0
|
229 'value' => Database::getConnectionInfo(),
|
Chris@0
|
230 'required' => TRUE,
|
Chris@0
|
231 ];
|
Chris@0
|
232
|
Chris@0
|
233 $this->writeSettings($settings);
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 /**
|
Chris@0
|
237 * Helper function to run pending database updates.
|
Chris@0
|
238 */
|
Chris@0
|
239 protected function runUpdates() {
|
Chris@0
|
240 if (!$this->zlibInstalled) {
|
Chris@0
|
241 $this->fail('Missing zlib requirement for update tests.');
|
Chris@0
|
242 return FALSE;
|
Chris@0
|
243 }
|
Chris@0
|
244 // The site might be broken at the time so logging in using the UI might
|
Chris@0
|
245 // not work, so we use the API itself.
|
Chris@0
|
246 drupal_rewrite_settings([
|
Chris@0
|
247 'settings' => [
|
Chris@0
|
248 'update_free_access' => (object) [
|
Chris@0
|
249 'value' => TRUE,
|
Chris@0
|
250 'required' => TRUE,
|
Chris@0
|
251 ],
|
Chris@0
|
252 ],
|
Chris@0
|
253 ]);
|
Chris@0
|
254
|
Chris@0
|
255 $this->drupalGet($this->updateUrl);
|
Chris@0
|
256 $this->clickLink(t('Continue'));
|
Chris@0
|
257
|
Chris@0
|
258 $this->doSelectionTest();
|
Chris@0
|
259 // Run the update hooks.
|
Chris@0
|
260 $this->clickLink(t('Apply pending updates'));
|
Chris@0
|
261
|
Chris@0
|
262 // Ensure there are no failed updates.
|
Chris@0
|
263 if ($this->checkFailedUpdates) {
|
Chris@0
|
264 $this->assertNoRaw('<strong>' . t('Failed:') . '</strong>');
|
Chris@0
|
265
|
Chris@0
|
266 // Ensure that there are no pending updates.
|
Chris@0
|
267 foreach (['update', 'post_update'] as $update_type) {
|
Chris@0
|
268 switch ($update_type) {
|
Chris@0
|
269 case 'update':
|
Chris@0
|
270 $all_updates = update_get_update_list();
|
Chris@0
|
271 break;
|
Chris@0
|
272 case 'post_update':
|
Chris@0
|
273 $all_updates = \Drupal::service('update.post_update_registry')->getPendingUpdateInformation();
|
Chris@0
|
274 break;
|
Chris@0
|
275 }
|
Chris@0
|
276 foreach ($all_updates as $module => $updates) {
|
Chris@0
|
277 if (!empty($updates['pending'])) {
|
Chris@0
|
278 foreach (array_keys($updates['pending']) as $update_name) {
|
Chris@0
|
279 $this->fail("The $update_name() update function from the $module module did not run.");
|
Chris@0
|
280 }
|
Chris@0
|
281 }
|
Chris@0
|
282 }
|
Chris@0
|
283 }
|
Chris@0
|
284 // Reset the static cache of drupal_get_installed_schema_version() so that
|
Chris@0
|
285 // more complex update path testing works.
|
Chris@0
|
286 drupal_static_reset('drupal_get_installed_schema_version');
|
Chris@0
|
287
|
Chris@0
|
288 // The config schema can be incorrect while the update functions are being
|
Chris@0
|
289 // executed. But once the update has been completed, it needs to be valid
|
Chris@0
|
290 // again. Assert the schema of all configuration objects now.
|
Chris@0
|
291 $names = $this->container->get('config.storage')->listAll();
|
Chris@0
|
292 /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */
|
Chris@0
|
293 $typed_config = $this->container->get('config.typed');
|
Chris@0
|
294 $typed_config->clearCachedDefinitions();
|
Chris@0
|
295 foreach ($names as $name) {
|
Chris@0
|
296 $config = $this->config($name);
|
Chris@0
|
297 $this->assertConfigSchema($typed_config, $name, $config->get());
|
Chris@0
|
298 }
|
Chris@0
|
299
|
Chris@0
|
300 // Ensure that the update hooks updated all entity schema.
|
Chris@0
|
301 $needs_updates = \Drupal::entityDefinitionUpdateManager()->needsUpdates();
|
Chris@0
|
302 $this->assertFalse($needs_updates, 'After all updates ran, entity schema is up to date.');
|
Chris@0
|
303 if ($needs_updates) {
|
Chris@0
|
304 foreach (\Drupal::entityDefinitionUpdateManager()
|
Chris@0
|
305 ->getChangeSummary() as $entity_type_id => $summary) {
|
Chris@0
|
306 foreach ($summary as $message) {
|
Chris@0
|
307 $this->fail($message);
|
Chris@0
|
308 }
|
Chris@0
|
309 }
|
Chris@0
|
310 }
|
Chris@0
|
311 }
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 /**
|
Chris@0
|
315 * Runs the install database tasks for the driver used by the test runner.
|
Chris@0
|
316 */
|
Chris@0
|
317 protected function runDbTasks() {
|
Chris@0
|
318 // Create a minimal container so that t() works.
|
Chris@0
|
319 // @see install_begin_request()
|
Chris@0
|
320 $container = new ContainerBuilder();
|
Chris@0
|
321 $container->setParameter('language.default_values', Language::$defaultValues);
|
Chris@0
|
322 $container
|
Chris@0
|
323 ->register('language.default', 'Drupal\Core\Language\LanguageDefault')
|
Chris@0
|
324 ->addArgument('%language.default_values%');
|
Chris@0
|
325 $container
|
Chris@0
|
326 ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
|
Chris@0
|
327 ->addArgument(new Reference('language.default'));
|
Chris@0
|
328 \Drupal::setContainer($container);
|
Chris@0
|
329
|
Chris@0
|
330 require_once __DIR__ . '/../../../../../includes/install.inc';
|
Chris@0
|
331 $connection = Database::getConnection();
|
Chris@0
|
332 $errors = db_installer_object($connection->driver())->runTasks();
|
Chris@0
|
333 if (!empty($errors)) {
|
Chris@0
|
334 $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
|
Chris@0
|
335 }
|
Chris@0
|
336 }
|
Chris@0
|
337
|
Chris@0
|
338 /**
|
Chris@0
|
339 * Replace User 1 with the user created here.
|
Chris@0
|
340 */
|
Chris@0
|
341 protected function replaceUser1() {
|
Chris@0
|
342 /** @var \Drupal\user\UserInterface $account */
|
Chris@0
|
343 // @todo: Saving the account before the update is problematic.
|
Chris@0
|
344 // https://www.drupal.org/node/2560237
|
Chris@0
|
345 $account = User::load(1);
|
Chris@0
|
346 $account->setPassword($this->rootUser->pass_raw);
|
Chris@0
|
347 $account->setEmail($this->rootUser->getEmail());
|
Chris@18
|
348 $account->setUsername($this->rootUser->getAccountName());
|
Chris@0
|
349 $account->save();
|
Chris@0
|
350 }
|
Chris@0
|
351
|
Chris@0
|
352 /**
|
Chris@0
|
353 * Tests the selection page.
|
Chris@0
|
354 */
|
Chris@0
|
355 protected function doSelectionTest() {
|
Chris@0
|
356 // No-op. Tests wishing to do test the selection page or the general
|
Chris@0
|
357 // update.php environment before running update.php can override this method
|
Chris@0
|
358 // and implement their required tests.
|
Chris@0
|
359 }
|
Chris@0
|
360
|
Chris@0
|
361 }
|