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