3 namespace Drupal\Tests\system\Functional\Update;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests the update script access and functionality.
14 class UpdateScriptTest extends BrowserTestBase {
21 public static $modules = ['update_script_test', 'dblog', 'language'];
26 protected $dumpHeaders = TRUE;
29 * URL to the update.php script.
36 * A user with the necessary permissions to administer software updates.
38 * @var \Drupal\user\UserInterface
42 protected function setUp() {
44 $this->updateUrl = Url::fromRoute('system.db_update');
45 $this->updateUser = $this->drupalCreateUser(['administer software updates', 'access site in maintenance mode']);
46 \Drupal::service('entity.definition_update_manager')->applyUpdates();
50 * Tests access to the update script.
52 public function testUpdateAccess() {
53 // Try accessing update.php without the proper permission.
54 $regular_user = $this->drupalCreateUser();
55 $this->drupalLogin($regular_user);
56 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
57 $this->assertResponse(403);
59 // Check that a link to the update page is not accessible to regular users.
60 $this->drupalGet('/update-script-test/database-updates-menu-item');
61 $this->assertNoLink('Run database updates');
63 // Try accessing update.php as an anonymous user.
64 $this->drupalLogout();
65 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
66 $this->assertResponse(403);
68 // Check that a link to the update page is not accessible to anonymous
70 $this->drupalGet('/update-script-test/database-updates-menu-item');
71 $this->assertNoLink('Run database updates');
73 // Access the update page with the proper permission.
74 $this->drupalLogin($this->updateUser);
75 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
76 $this->assertResponse(200);
78 // Check that a link to the update page is accessible to users with proper
80 $this->drupalGet('/update-script-test/database-updates-menu-item');
81 $this->assertLink('Run database updates');
83 // Access the update page as user 1.
84 $this->drupalLogin($this->rootUser);
85 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
86 $this->assertResponse(200);
88 // Check that a link to the update page is accessible to user 1.
89 $this->drupalGet('/update-script-test/database-updates-menu-item');
90 $this->assertLink('Run database updates');
94 * Tests that requirements warnings and errors are correctly displayed.
96 public function testRequirements() {
97 $update_script_test_config = $this->config('update_script_test.settings');
98 $this->drupalLogin($this->updateUser);
100 // If there are no requirements warnings or errors, we expect to be able to
101 // go through the update process uninterrupted.
102 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
103 $this->clickLink(t('Continue'));
104 $this->assertText(t('No pending updates.'), 'End of update process was reached.');
105 // Confirm that all caches were cleared.
106 $this->assertText(t('hook_cache_flush() invoked for update_script_test.module.'), 'Caches were cleared when there were no requirements warnings or errors.');
108 // If there is a requirements warning, we expect it to be initially
109 // displayed, but clicking the link to proceed should allow us to go
110 // through the rest of the update process uninterrupted.
112 // First, run this test with pending updates to make sure they can be run
114 $update_script_test_config->set('requirement_type', REQUIREMENT_WARNING)->save();
115 drupal_set_installed_schema_version('update_script_test', drupal_get_installed_schema_version('update_script_test') - 1);
116 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
117 $this->assertText('This is a requirements warning provided by the update_script_test module.');
118 $this->clickLink('try again');
119 $this->assertNoText('This is a requirements warning provided by the update_script_test module.');
120 $this->clickLink(t('Continue'));
121 $this->clickLink(t('Apply pending updates'));
122 $this->checkForMetaRefresh();
123 $this->assertText(t('The update_script_test_update_8001() update was executed successfully.'), 'End of update process was reached.');
124 // Confirm that all caches were cleared.
125 $this->assertText(t('hook_cache_flush() invoked for update_script_test.module.'), 'Caches were cleared after resolving a requirements warning and applying updates.');
127 // Now try again without pending updates to make sure that works too.
128 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
129 $this->assertText('This is a requirements warning provided by the update_script_test module.');
130 $this->clickLink('try again');
131 $this->assertNoText('This is a requirements warning provided by the update_script_test module.');
132 $this->clickLink(t('Continue'));
133 $this->assertText(t('No pending updates.'), 'End of update process was reached.');
134 // Confirm that all caches were cleared.
135 $this->assertText(t('hook_cache_flush() invoked for update_script_test.module.'), 'Caches were cleared after applying updates and re-running the script.');
137 // If there is a requirements error, it should be displayed even after
138 // clicking the link to proceed (since the problem that triggered the error
139 // has not been fixed).
140 $update_script_test_config->set('requirement_type', REQUIREMENT_ERROR)->save();
141 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
142 $this->assertText('This is a requirements error provided by the update_script_test module.');
143 $this->clickLink('try again');
144 $this->assertText('This is a requirements error provided by the update_script_test module.');
148 * Tests the effect of using the update script on the theme system.
150 public function testThemeSystem() {
151 // Since visiting update.php triggers a rebuild of the theme system from an
152 // unusual maintenance mode environment, we check that this rebuild did not
153 // put any incorrect information about the themes into the database.
154 $original_theme_data = $this->config('core.extension')->get('theme');
155 $this->drupalLogin($this->updateUser);
156 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
157 $final_theme_data = $this->config('core.extension')->get('theme');
158 $this->assertEqual($original_theme_data, $final_theme_data, 'Visiting update.php does not alter the information about themes stored in the database.');
162 * Tests update.php when there are no updates to apply.
164 public function testNoUpdateFunctionality() {
165 // Click through update.php with 'administer software updates' permission.
166 $this->drupalLogin($this->updateUser);
167 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
168 $this->clickLink(t('Continue'));
169 $this->assertText(t('No pending updates.'));
170 $this->assertNoLink('Administration pages');
171 $this->assertEmpty($this->xpath('//main//a[contains(@href, :href)]', [':href' => 'update.php']));
172 $this->clickLink('Front page');
173 $this->assertResponse(200);
175 // Click through update.php with 'access administration pages' permission.
176 $admin_user = $this->drupalCreateUser(['administer software updates', 'access administration pages']);
177 $this->drupalLogin($admin_user);
178 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
179 $this->clickLink(t('Continue'));
180 $this->assertText(t('No pending updates.'));
181 $this->assertLink('Administration pages');
182 $this->assertEmpty($this->xpath('//main//a[contains(@href, :href)]', [':href' => 'update.php']));
183 $this->clickLink('Administration pages');
184 $this->assertResponse(200);
188 * Tests update.php after performing a successful update.
190 public function testSuccessfulUpdateFunctionality() {
191 $initial_maintenance_mode = $this->container->get('state')->get('system.maintenance_mode');
192 $this->assertFalse($initial_maintenance_mode, 'Site is not in maintenance mode.');
193 $this->runUpdates($initial_maintenance_mode);
194 $final_maintenance_mode = $this->container->get('state')->get('system.maintenance_mode');
195 $this->assertEqual($final_maintenance_mode, $initial_maintenance_mode, 'Maintenance mode should not have changed after database updates.');
197 // Reset the static cache to ensure we have the most current setting.
198 $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
199 $this->assertEqual($schema_version, 8001, 'update_script_test schema version is 8001 after updating.');
201 // Set the installed schema version to one less than the current update.
202 drupal_set_installed_schema_version('update_script_test', $schema_version - 1);
203 $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
204 $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.');
206 // Click through update.php with 'access administration pages' and
207 // 'access site reports' permissions.
208 $admin_user = $this->drupalCreateUser(['administer software updates', 'access administration pages', 'access site reports', 'access site in maintenance mode']);
209 $this->drupalLogin($admin_user);
210 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
211 $this->clickLink(t('Continue'));
212 $this->clickLink(t('Apply pending updates'));
213 $this->checkForMetaRefresh();
214 $this->assertText('Updates were attempted.');
215 $this->assertLink('logged');
216 $this->assertLink('Administration pages');
217 $this->assertEmpty($this->xpath('//main//a[contains(@href, :href)]', [':href' => 'update.php']));
218 $this->clickLink('Administration pages');
219 $this->assertResponse(200);
223 * Tests update.php while in maintenance mode.
225 public function testMaintenanceModeUpdateFunctionality() {
226 $this->container->get('state')
227 ->set('system.maintenance_mode', TRUE);
228 $initial_maintenance_mode = $this->container->get('state')
229 ->get('system.maintenance_mode');
230 $this->assertTrue($initial_maintenance_mode, 'Site is in maintenance mode.');
231 $this->runUpdates($initial_maintenance_mode);
232 $final_maintenance_mode = $this->container->get('state')
233 ->get('system.maintenance_mode');
234 $this->assertEqual($final_maintenance_mode, $initial_maintenance_mode, 'Maintenance mode should not have changed after database updates.');
238 * Tests perfoming updates with update.php in a multilingual environment.
240 public function testSuccessfulMultilingualUpdateFunctionality() {
241 // Add some custom languages.
242 foreach (['aa', 'bb'] as $language_code) {
243 ConfigurableLanguage::create([
244 'id' => $language_code,
245 'label' => $this->randomMachineName(),
249 $config = \Drupal::service('config.factory')->getEditable('language.negotiation');
250 // Ensure path prefix is used to determine the language.
251 $config->set('url.source', 'path_prefix');
252 // Ensure that there's a path prefix set for english as well.
253 $config->set('url.prefixes.en', 'en');
256 // Reset the static cache to ensure we have the most current setting.
257 $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
258 $this->assertEqual($schema_version, 8001, 'update_script_test schema version is 8001 after updating.');
260 // Set the installed schema version to one less than the current update.
261 drupal_set_installed_schema_version('update_script_test', $schema_version - 1);
262 $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
263 $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.');
265 // Create admin user.
266 $admin_user = $this->drupalCreateUser(['administer software updates', 'access administration pages', 'access site reports', 'access site in maintenance mode', 'administer site configuration']);
267 $this->drupalLogin($admin_user);
269 // Visit status report page and ensure, that link to update.php has no path prefix set.
270 $this->drupalGet('en/admin/reports/status', ['external' => TRUE]);
271 $this->assertResponse(200);
272 $this->assertLinkByHref('/update.php');
273 $this->assertNoLinkByHref('en/update.php');
275 // Click through update.php with 'access administration pages' and
276 // 'access site reports' permissions.
277 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
278 $this->clickLink(t('Continue'));
279 $this->clickLink(t('Apply pending updates'));
280 $this->checkForMetaRefresh();
281 $this->assertText('Updates were attempted.');
282 $this->assertLink('logged');
283 $this->assertLink('Administration pages');
284 $this->assertEmpty($this->xpath('//main//a[contains(@href, :href)]', [':href' => 'update.php']));
285 $this->clickLink('Administration pages');
286 $this->assertResponse(200);
290 * Helper function to run updates via the browser.
292 protected function runUpdates($maintenance_mode) {
293 $schema_version = drupal_get_installed_schema_version('update_script_test');
294 $this->assertEqual($schema_version, 8001, 'update_script_test is initially installed with schema version 8001.');
296 // Set the installed schema version to one less than the current update.
297 drupal_set_installed_schema_version('update_script_test', $schema_version - 1);
298 $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
299 $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.');
301 // Click through update.php with 'administer software updates' permission.
302 $this->drupalLogin($this->updateUser);
303 if ($maintenance_mode) {
304 $this->assertText('Operating in maintenance mode.');
307 $this->assertNoText('Operating in maintenance mode.');
309 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
310 $this->clickLink(t('Continue'));
311 $this->clickLink(t('Apply pending updates'));
312 $this->checkForMetaRefresh();
314 // Verify that updates were completed successfully.
315 $this->assertText('Updates were attempted.');
316 $this->assertLink('site');
317 $this->assertText('The update_script_test_update_8001() update was executed successfully.');
319 // Verify that no 7.x updates were run.
320 $this->assertNoText('The update_script_test_update_7200() update was executed successfully.');
321 $this->assertNoText('The update_script_test_update_7201() update was executed successfully.');
323 // Verify that there are no links to different parts of the workflow.
324 $this->assertNoLink('Administration pages');
325 $this->assertEmpty($this->xpath('//main//a[contains(@href, :href)]', [':href' => 'update.php']));
326 $this->assertNoLink('logged');
328 // Verify the front page can be visited following the upgrade.
329 $this->clickLink('Front page');
330 $this->assertResponse(200);
334 * Returns the Drupal 7 system table schema.
336 public function getSystemSchema() {
338 'description' => "A list of all modules, themes, and theme engines that are or have been installed in Drupal's file system.",
341 'description' => 'The path of the primary file for this item, relative to the Drupal root; e.g. modules/node/node.module.',
348 'description' => 'The name of the item; e.g. node.',
355 'description' => 'The type of the item, either module, theme, or theme_engine.',
362 'description' => "A theme's 'parent' . Can be either a theme or an engine.",
369 'description' => 'Boolean indicating whether or not this item is enabled.',
375 'description' => "Boolean indicating whether this module is loaded during Drupal's early bootstrapping phase (e.g. even before the page cache is consulted).",
380 'schema_version' => [
381 'description' => "The module's database schema version number. -1 if the module is not installed (its tables do not exist); \Drupal::CORE_MINIMUM_SCHEMA_VERSION or the largest N of the module's hook_update_N() function that has either been run or existed when the module was first installed.",
388 'description' => "The order in which this module's hooks should be invoked relative to other modules. Equal-weighted modules are ordered by name.",
394 'description' => "A serialized array containing information from the module's .info file; keys can include name, description, package, version, core, dependencies, and php.",
399 'primary key' => ['filename'],
401 'system_list' => ['status', 'bootstrap', 'type', 'weight', 'name'],
402 'type_name' => ['type', 'name'],