Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\FunctionalTests\Update;
|
Chris@0
|
4
|
Chris@0
|
5 use Behat\Mink\Driver\GoutteDriver;
|
Chris@0
|
6 use Behat\Mink\Mink;
|
Chris@0
|
7 use Behat\Mink\Selector\SelectorsHandler;
|
Chris@0
|
8 use Behat\Mink\Session;
|
Chris@0
|
9 use Drupal\Component\Utility\Crypt;
|
Chris@0
|
10 use Drupal\Core\Test\TestRunnerKernel;
|
Chris@0
|
11 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
12 use Drupal\Tests\HiddenFieldSelector;
|
Chris@0
|
13 use Drupal\Tests\SchemaCheckTestTrait;
|
Chris@0
|
14 use Drupal\Core\Database\Database;
|
Chris@0
|
15 use Drupal\Core\DependencyInjection\ContainerBuilder;
|
Chris@0
|
16 use Drupal\Core\Language\Language;
|
Chris@0
|
17 use Drupal\Core\Url;
|
Chris@0
|
18 use Drupal\user\Entity\User;
|
Chris@0
|
19 use Symfony\Component\DependencyInjection\Reference;
|
Chris@0
|
20 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Provides a base class for writing an update test.
|
Chris@0
|
24 *
|
Chris@0
|
25 * To write an update test:
|
Chris@0
|
26 * - Write the hook_update_N() implementations that you are testing.
|
Chris@0
|
27 * - Create one or more database dump files, which will set the database to the
|
Chris@0
|
28 * "before updates" state. Normally, these will add some configuration data to
|
Chris@0
|
29 * the database, set up some tables/fields, etc.
|
Chris@0
|
30 * - Create a class that extends this class.
|
Chris@0
|
31 * - In your setUp() method, point the $this->databaseDumpFiles variable to the
|
Chris@0
|
32 * database dump files, and then call parent::setUp() to run the base setUp()
|
Chris@0
|
33 * method in this class.
|
Chris@0
|
34 * - In your test method, call $this->runUpdates() to run the necessary updates,
|
Chris@0
|
35 * and then use test assertions to verify that the result is what you expect.
|
Chris@0
|
36 * - In order to test both with a "bare" database dump as well as with a
|
Chris@0
|
37 * database dump filled with content, extend your update path test class with
|
Chris@0
|
38 * a new test class that overrides the bare database dump. Refer to
|
Chris@0
|
39 * UpdatePathTestBaseFilledTest for an example.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @ingroup update_api
|
Chris@0
|
42 *
|
Chris@0
|
43 * @see hook_update_N()
|
Chris@0
|
44 */
|
Chris@0
|
45 abstract class UpdatePathTestBase extends BrowserTestBase {
|
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 $request = Request::createFromGlobals();
|
Chris@0
|
156
|
Chris@0
|
157 // Boot up Drupal into a state where calling the database API is possible.
|
Chris@0
|
158 // This is used to initialize the database system, so we can load the dump
|
Chris@0
|
159 // files.
|
Chris@0
|
160 $autoloader = require $this->root . '/autoload.php';
|
Chris@0
|
161 $kernel = TestRunnerKernel::createFromRequest($request, $autoloader);
|
Chris@0
|
162 $kernel->loadLegacyIncludes();
|
Chris@0
|
163
|
Chris@0
|
164 $this->changeDatabasePrefix();
|
Chris@0
|
165 $this->runDbTasks();
|
Chris@0
|
166 // Allow classes to set database dump files.
|
Chris@0
|
167 $this->setDatabaseDumpFiles();
|
Chris@0
|
168
|
Chris@0
|
169 // We are going to set a missing zlib requirement property for usage
|
Chris@0
|
170 // during the performUpgrade() and tearDown() methods. Also set that the
|
Chris@0
|
171 // tests failed.
|
Chris@0
|
172 if (!$this->zlibInstalled) {
|
Chris@0
|
173 parent::setUp();
|
Chris@0
|
174 return;
|
Chris@0
|
175 }
|
Chris@0
|
176 // Set the update url. This must be set here rather than in
|
Chris@0
|
177 // self::__construct() or the old URL generator will leak additional test
|
Chris@0
|
178 // sites.
|
Chris@0
|
179 $this->updateUrl = Url::fromRoute('system.db_update');
|
Chris@0
|
180
|
Chris@0
|
181 $this->setupBaseUrl();
|
Chris@0
|
182
|
Chris@0
|
183 // Install Drupal test site.
|
Chris@0
|
184 $this->prepareEnvironment();
|
Chris@0
|
185 $this->installDrupal();
|
Chris@0
|
186
|
Chris@0
|
187 // Add the config directories to settings.php.
|
Chris@0
|
188 drupal_install_config_directories();
|
Chris@0
|
189
|
Chris@0
|
190 // Set the container. parent::rebuildAll() would normally do this, but this
|
Chris@0
|
191 // not safe to do here, because the database has not been updated yet.
|
Chris@0
|
192 $this->container = \Drupal::getContainer();
|
Chris@0
|
193
|
Chris@0
|
194 $this->replaceUser1();
|
Chris@0
|
195
|
Chris@0
|
196 require_once \Drupal::root() . '/core/includes/update.inc';
|
Chris@0
|
197
|
Chris@0
|
198 // Setup Mink.
|
Chris@0
|
199 $session = $this->initMink();
|
Chris@0
|
200
|
Chris@0
|
201 $cookies = $this->extractCookiesFromRequest(\Drupal::request());
|
Chris@0
|
202 foreach ($cookies as $cookie_name => $values) {
|
Chris@0
|
203 foreach ($values as $value) {
|
Chris@0
|
204 $session->setCookie($cookie_name, $value);
|
Chris@0
|
205 }
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 // Set up the browser test output file.
|
Chris@0
|
209 $this->initBrowserOutputFile();
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * {@inheritdoc}
|
Chris@0
|
214 */
|
Chris@0
|
215 public function installDrupal() {
|
Chris@0
|
216 $this->initUserSession();
|
Chris@0
|
217 $this->prepareSettings();
|
Chris@0
|
218 $this->doInstall();
|
Chris@0
|
219 $this->initSettings();
|
Chris@0
|
220
|
Chris@0
|
221 $request = Request::createFromGlobals();
|
Chris@0
|
222 $container = $this->initKernel($request);
|
Chris@0
|
223 $this->initConfig($container);
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 /**
|
Chris@0
|
227 * {@inheritdoc}
|
Chris@0
|
228 */
|
Chris@0
|
229 protected function doInstall() {
|
Chris@0
|
230 $this->runDbTasks();
|
Chris@0
|
231 // Allow classes to set database dump files.
|
Chris@0
|
232 $this->setDatabaseDumpFiles();
|
Chris@0
|
233
|
Chris@0
|
234 // Load the database(s).
|
Chris@0
|
235 foreach ($this->databaseDumpFiles as $file) {
|
Chris@0
|
236 if (substr($file, -3) == '.gz') {
|
Chris@0
|
237 $file = "compress.zlib://$file";
|
Chris@0
|
238 }
|
Chris@0
|
239 require $file;
|
Chris@0
|
240 }
|
Chris@0
|
241 }
|
Chris@0
|
242
|
Chris@0
|
243 /**
|
Chris@0
|
244 * {@inheritdoc}
|
Chris@0
|
245 */
|
Chris@0
|
246 protected function initMink() {
|
Chris@0
|
247 $driver = $this->getDefaultDriverInstance();
|
Chris@0
|
248
|
Chris@0
|
249 if ($driver instanceof GoutteDriver) {
|
Chris@0
|
250 // Turn off curl timeout. Having a timeout is not a problem in a normal
|
Chris@0
|
251 // test running, but it is a problem when debugging. Also, disable SSL
|
Chris@0
|
252 // peer verification so that testing under HTTPS always works.
|
Chris@0
|
253 /** @var \GuzzleHttp\Client $client */
|
Chris@0
|
254 $client = $this->container->get('http_client_factory')->fromOptions([
|
Chris@0
|
255 'timeout' => NULL,
|
Chris@0
|
256 'verify' => FALSE,
|
Chris@0
|
257 ]);
|
Chris@0
|
258
|
Chris@0
|
259 // Inject a Guzzle middleware to generate debug output for every request
|
Chris@0
|
260 // performed in the test.
|
Chris@0
|
261 $handler_stack = $client->getConfig('handler');
|
Chris@0
|
262 $handler_stack->push($this->getResponseLogHandler());
|
Chris@0
|
263
|
Chris@0
|
264 $driver->getClient()->setClient($client);
|
Chris@0
|
265 }
|
Chris@0
|
266
|
Chris@0
|
267 $selectors_handler = new SelectorsHandler([
|
Chris@0
|
268 'hidden_field_selector' => new HiddenFieldSelector()
|
Chris@0
|
269 ]);
|
Chris@0
|
270 $session = new Session($driver, $selectors_handler);
|
Chris@0
|
271 $this->mink = new Mink();
|
Chris@0
|
272 $this->mink->registerSession('default', $session);
|
Chris@0
|
273 $this->mink->setDefaultSessionName('default');
|
Chris@0
|
274 $this->registerSessions();
|
Chris@0
|
275
|
Chris@0
|
276 return $session;
|
Chris@0
|
277 }
|
Chris@0
|
278
|
Chris@0
|
279 /**
|
Chris@0
|
280 * Set database dump files to be used.
|
Chris@0
|
281 */
|
Chris@0
|
282 abstract protected function setDatabaseDumpFiles();
|
Chris@0
|
283
|
Chris@0
|
284 /**
|
Chris@0
|
285 * Add settings that are missed since the installer isn't run.
|
Chris@0
|
286 */
|
Chris@0
|
287 protected function prepareSettings() {
|
Chris@0
|
288 parent::prepareSettings();
|
Chris@0
|
289
|
Chris@0
|
290 // Remember the profile which was used.
|
Chris@0
|
291 $settings['settings']['install_profile'] = (object) [
|
Chris@0
|
292 'value' => $this->installProfile,
|
Chris@0
|
293 'required' => TRUE,
|
Chris@0
|
294 ];
|
Chris@0
|
295 // Generate a hash salt.
|
Chris@0
|
296 $settings['settings']['hash_salt'] = (object) [
|
Chris@0
|
297 'value' => Crypt::randomBytesBase64(55),
|
Chris@0
|
298 'required' => TRUE,
|
Chris@0
|
299 ];
|
Chris@0
|
300
|
Chris@0
|
301 // Since the installer isn't run, add the database settings here too.
|
Chris@0
|
302 $settings['databases']['default'] = (object) [
|
Chris@0
|
303 'value' => Database::getConnectionInfo(),
|
Chris@0
|
304 'required' => TRUE,
|
Chris@0
|
305 ];
|
Chris@0
|
306
|
Chris@0
|
307 $this->writeSettings($settings);
|
Chris@0
|
308 }
|
Chris@0
|
309
|
Chris@0
|
310 /**
|
Chris@0
|
311 * Helper function to run pending database updates.
|
Chris@0
|
312 */
|
Chris@0
|
313 protected function runUpdates() {
|
Chris@0
|
314 if (!$this->zlibInstalled) {
|
Chris@0
|
315 $this->fail('Missing zlib requirement for update tests.');
|
Chris@0
|
316 return FALSE;
|
Chris@0
|
317 }
|
Chris@0
|
318 // The site might be broken at the time so logging in using the UI might
|
Chris@0
|
319 // not work, so we use the API itself.
|
Chris@0
|
320 drupal_rewrite_settings([
|
Chris@0
|
321 'settings' => [
|
Chris@0
|
322 'update_free_access' => (object) [
|
Chris@0
|
323 'value' => TRUE,
|
Chris@0
|
324 'required' => TRUE,
|
Chris@0
|
325 ],
|
Chris@0
|
326 ],
|
Chris@0
|
327 ]);
|
Chris@0
|
328
|
Chris@0
|
329 $this->drupalGet($this->updateUrl);
|
Chris@0
|
330 $this->clickLink(t('Continue'));
|
Chris@0
|
331
|
Chris@0
|
332 $this->doSelectionTest();
|
Chris@0
|
333 // Run the update hooks.
|
Chris@0
|
334 $this->clickLink(t('Apply pending updates'));
|
Chris@0
|
335 $this->checkForMetaRefresh();
|
Chris@0
|
336
|
Chris@0
|
337 // Ensure there are no failed updates.
|
Chris@0
|
338 if ($this->checkFailedUpdates) {
|
Chris@0
|
339 $this->assertNoRaw('<strong>' . t('Failed:') . '</strong>');
|
Chris@0
|
340
|
Chris@0
|
341 // Ensure that there are no pending updates.
|
Chris@0
|
342 foreach (['update', 'post_update'] as $update_type) {
|
Chris@0
|
343 switch ($update_type) {
|
Chris@0
|
344 case 'update':
|
Chris@0
|
345 $all_updates = update_get_update_list();
|
Chris@0
|
346 break;
|
Chris@0
|
347 case 'post_update':
|
Chris@0
|
348 $all_updates = \Drupal::service('update.post_update_registry')->getPendingUpdateInformation();
|
Chris@0
|
349 break;
|
Chris@0
|
350 }
|
Chris@0
|
351 foreach ($all_updates as $module => $updates) {
|
Chris@0
|
352 if (!empty($updates['pending'])) {
|
Chris@0
|
353 foreach (array_keys($updates['pending']) as $update_name) {
|
Chris@0
|
354 $this->fail("The $update_name() update function from the $module module did not run.");
|
Chris@0
|
355 }
|
Chris@0
|
356 }
|
Chris@0
|
357 }
|
Chris@0
|
358 }
|
Chris@0
|
359 // Reset the static cache of drupal_get_installed_schema_version() so that
|
Chris@0
|
360 // more complex update path testing works.
|
Chris@0
|
361 drupal_static_reset('drupal_get_installed_schema_version');
|
Chris@0
|
362
|
Chris@0
|
363 // The config schema can be incorrect while the update functions are being
|
Chris@0
|
364 // executed. But once the update has been completed, it needs to be valid
|
Chris@0
|
365 // again. Assert the schema of all configuration objects now.
|
Chris@0
|
366 $names = $this->container->get('config.storage')->listAll();
|
Chris@0
|
367 /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */
|
Chris@0
|
368 $typed_config = $this->container->get('config.typed');
|
Chris@0
|
369 $typed_config->clearCachedDefinitions();
|
Chris@0
|
370 foreach ($names as $name) {
|
Chris@0
|
371 $config = $this->config($name);
|
Chris@0
|
372 $this->assertConfigSchema($typed_config, $name, $config->get());
|
Chris@0
|
373 }
|
Chris@0
|
374
|
Chris@0
|
375 // Ensure that the update hooks updated all entity schema.
|
Chris@0
|
376 $needs_updates = \Drupal::entityDefinitionUpdateManager()->needsUpdates();
|
Chris@0
|
377 if ($needs_updates) {
|
Chris@0
|
378 foreach (\Drupal::entityDefinitionUpdateManager()->getChangeSummary() as $entity_type_id => $summary) {
|
Chris@0
|
379 $entity_type_label = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getLabel();
|
Chris@0
|
380 foreach ($summary as $message) {
|
Chris@0
|
381 $this->fail("$entity_type_label: $message");
|
Chris@0
|
382 }
|
Chris@0
|
383 }
|
Chris@0
|
384 // The above calls to `fail()` should prevent this from ever being
|
Chris@0
|
385 // called, but it is here in case something goes really wrong.
|
Chris@0
|
386 $this->assertFalse($needs_updates, 'After all updates ran, entity schema is up to date.');
|
Chris@0
|
387 }
|
Chris@0
|
388 }
|
Chris@0
|
389 }
|
Chris@0
|
390
|
Chris@0
|
391 /**
|
Chris@0
|
392 * Runs the install database tasks for the driver used by the test runner.
|
Chris@0
|
393 */
|
Chris@0
|
394 protected function runDbTasks() {
|
Chris@0
|
395 // Create a minimal container so that t() works.
|
Chris@0
|
396 // @see install_begin_request()
|
Chris@0
|
397 $container = new ContainerBuilder();
|
Chris@0
|
398 $container->setParameter('language.default_values', Language::$defaultValues);
|
Chris@0
|
399 $container
|
Chris@0
|
400 ->register('language.default', 'Drupal\Core\Language\LanguageDefault')
|
Chris@0
|
401 ->addArgument('%language.default_values%');
|
Chris@0
|
402 $container
|
Chris@0
|
403 ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
|
Chris@0
|
404 ->addArgument(new Reference('language.default'));
|
Chris@0
|
405 \Drupal::setContainer($container);
|
Chris@0
|
406
|
Chris@0
|
407 require_once __DIR__ . '/../../../../includes/install.inc';
|
Chris@0
|
408 $connection = Database::getConnection();
|
Chris@0
|
409 $errors = db_installer_object($connection->driver())->runTasks();
|
Chris@0
|
410 if (!empty($errors)) {
|
Chris@0
|
411 $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
|
Chris@0
|
412 }
|
Chris@0
|
413 }
|
Chris@0
|
414
|
Chris@0
|
415 /**
|
Chris@0
|
416 * Replace User 1 with the user created here.
|
Chris@0
|
417 */
|
Chris@0
|
418 protected function replaceUser1() {
|
Chris@0
|
419 /** @var \Drupal\user\UserInterface $account */
|
Chris@0
|
420 // @todo: Saving the account before the update is problematic.
|
Chris@0
|
421 // https://www.drupal.org/node/2560237
|
Chris@0
|
422 $account = User::load(1);
|
Chris@0
|
423 $account->setPassword($this->rootUser->pass_raw);
|
Chris@0
|
424 $account->setEmail($this->rootUser->getEmail());
|
Chris@0
|
425 $account->setUsername($this->rootUser->getUsername());
|
Chris@0
|
426 $account->save();
|
Chris@0
|
427 }
|
Chris@0
|
428
|
Chris@0
|
429 /**
|
Chris@0
|
430 * Tests the selection page.
|
Chris@0
|
431 */
|
Chris@0
|
432 protected function doSelectionTest() {
|
Chris@0
|
433 // No-op. Tests wishing to do test the selection page or the general
|
Chris@0
|
434 // update.php environment before running update.php can override this method
|
Chris@0
|
435 // and implement their required tests.
|
Chris@0
|
436 }
|
Chris@0
|
437
|
Chris@0
|
438 }
|