3 namespace Drupal\system\Tests\Update;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\simpletest\WebTestBase;
10 * Tests the update script access and functionality.
14 class UpdateScriptTest extends WebTestBase {
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->assertText(t('The update_script_test_update_8001() update was executed successfully.'), 'End of update process was reached.');
123 // Confirm that all caches were cleared.
124 $this->assertText(t('hook_cache_flush() invoked for update_script_test.module.'), 'Caches were cleared after resolving a requirements warning and applying updates.');
126 // Now try again without pending updates to make sure that works too.
127 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
128 $this->assertText('This is a requirements warning provided by the update_script_test module.');
129 $this->clickLink('try again');
130 $this->assertNoText('This is a requirements warning provided by the update_script_test module.');
131 $this->clickLink(t('Continue'));
132 $this->assertText(t('No pending updates.'), 'End of update process was reached.');
133 // Confirm that all caches were cleared.
134 $this->assertText(t('hook_cache_flush() invoked for update_script_test.module.'), 'Caches were cleared after applying updates and re-running the script.');
136 // If there is a requirements error, it should be displayed even after
137 // clicking the link to proceed (since the problem that triggered the error
138 // has not been fixed).
139 $update_script_test_config->set('requirement_type', REQUIREMENT_ERROR)->save();
140 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
141 $this->assertText('This is a requirements error provided by the update_script_test module.');
142 $this->clickLink('try again');
143 $this->assertText('This is a requirements error provided by the update_script_test module.');
147 * Tests the effect of using the update script on the theme system.
149 public function testThemeSystem() {
150 // Since visiting update.php triggers a rebuild of the theme system from an
151 // unusual maintenance mode environment, we check that this rebuild did not
152 // put any incorrect information about the themes into the database.
153 $original_theme_data = $this->config('core.extension')->get('theme');
154 $this->drupalLogin($this->updateUser);
155 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
156 $final_theme_data = $this->config('core.extension')->get('theme');
157 $this->assertEqual($original_theme_data, $final_theme_data, 'Visiting update.php does not alter the information about themes stored in the database.');
161 * Tests update.php when there are no updates to apply.
163 public function testNoUpdateFunctionality() {
164 // Click through update.php with 'administer software updates' permission.
165 $this->drupalLogin($this->updateUser);
166 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
167 $this->clickLink(t('Continue'));
168 $this->assertText(t('No pending updates.'));
169 $this->assertNoLink('Administration pages');
170 $this->assertNoLinkByHrefInMainRegion('update.php', 0);
171 $this->clickLink('Front page');
172 $this->assertResponse(200);
174 // Click through update.php with 'access administration pages' permission.
175 $admin_user = $this->drupalCreateUser(['administer software updates', 'access administration pages']);
176 $this->drupalLogin($admin_user);
177 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
178 $this->clickLink(t('Continue'));
179 $this->assertText(t('No pending updates.'));
180 $this->assertLink('Administration pages');
181 $this->assertNoLinkByHrefInMainRegion('update.php', 1);
182 $this->clickLink('Administration pages');
183 $this->assertResponse(200);
187 * Tests update.php after performing a successful update.
189 public function testSuccessfulUpdateFunctionality() {
190 $initial_maintenance_mode = $this->container->get('state')->get('system.maintenance_mode');
191 $this->assertFalse($initial_maintenance_mode, 'Site is not in maintenance mode.');
192 $this->updateScriptTest($initial_maintenance_mode);
193 $final_maintenance_mode = $this->container->get('state')->get('system.maintenance_mode');
194 $this->assertEqual($final_maintenance_mode, $initial_maintenance_mode, 'Maintenance mode should not have changed after database updates.');
196 // Reset the static cache to ensure we have the most current setting.
197 $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
198 $this->assertEqual($schema_version, 8001, 'update_script_test schema version is 8001 after updating.');
200 // Set the installed schema version to one less than the current update.
201 drupal_set_installed_schema_version('update_script_test', $schema_version - 1);
202 $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
203 $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.');
205 // Click through update.php with 'access administration pages' and
206 // 'access site reports' permissions.
207 $admin_user = $this->drupalCreateUser(['administer software updates', 'access administration pages', 'access site reports', 'access site in maintenance mode']);
208 $this->drupalLogin($admin_user);
209 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
210 $this->clickLink(t('Continue'));
211 $this->clickLink(t('Apply pending updates'));
212 $this->assertText('Updates were attempted.');
213 $this->assertLink('logged');
214 $this->assertLink('Administration pages');
215 $this->assertNoLinkByHrefInMainRegion('update.php', 1);
216 $this->clickLink('Administration pages');
217 $this->assertResponse(200);
221 * Tests update.php while in maintenance mode.
223 public function testMaintenanceModeUpdateFunctionality() {
224 $this->container->get('state')
225 ->set('system.maintenance_mode', TRUE);
226 $initial_maintenance_mode = $this->container->get('state')
227 ->get('system.maintenance_mode');
228 $this->assertTrue($initial_maintenance_mode, 'Site is in maintenance mode.');
229 $this->updateScriptTest($initial_maintenance_mode);
230 $final_maintenance_mode = $this->container->get('state')
231 ->get('system.maintenance_mode');
232 $this->assertEqual($final_maintenance_mode, $initial_maintenance_mode, 'Maintenance mode should not have changed after database updates.');
236 * Tests perfoming updates with update.php in a multilingual environment.
238 public function testSuccessfulMultilingualUpdateFunctionality() {
239 // Add some custom languages.
240 foreach (['aa', 'bb'] as $language_code) {
241 ConfigurableLanguage::create([
242 'id' => $language_code,
243 'label' => $this->randomMachineName(),
247 $config = \Drupal::service('config.factory')->getEditable('language.negotiation');
248 // Ensure path prefix is used to determine the language.
249 $config->set('url.source', 'path_prefix');
250 // Ensure that there's a path prefix set for english as well.
251 $config->set('url.prefixes.en', 'en');
254 // Reset the static cache to ensure we have the most current setting.
255 $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
256 $this->assertEqual($schema_version, 8001, 'update_script_test schema version is 8001 after updating.');
258 // Set the installed schema version to one less than the current update.
259 drupal_set_installed_schema_version('update_script_test', $schema_version - 1);
260 $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
261 $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.');
263 // Create admin user.
264 $admin_user = $this->drupalCreateUser(['administer software updates', 'access administration pages', 'access site reports', 'access site in maintenance mode', 'administer site configuration']);
265 $this->drupalLogin($admin_user);
267 // Visit status report page and ensure, that link to update.php has no path prefix set.
268 $this->drupalGet('en/admin/reports/status', ['external' => TRUE]);
269 $this->assertResponse(200);
270 $this->assertLinkByHref('/update.php');
271 $this->assertNoLinkByHref('en/update.php');
273 // Click through update.php with 'access administration pages' and
274 // 'access site reports' permissions.
275 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
276 $this->clickLink(t('Continue'));
277 $this->clickLink(t('Apply pending updates'));
278 $this->assertText('Updates were attempted.');
279 $this->assertLink('logged');
280 $this->assertLink('Administration pages');
281 $this->assertNoLinkByHrefInMainRegion('update.php', 1);
282 $this->clickLink('Administration pages');
283 $this->assertResponse(200);
287 * Helper function to run updates via the browser.
289 protected function updateScriptTest($maintenance_mode) {
290 $schema_version = drupal_get_installed_schema_version('update_script_test');
291 $this->assertEqual($schema_version, 8001, 'update_script_test is initially installed with schema version 8001.');
293 // Set the installed schema version to one less than the current update.
294 drupal_set_installed_schema_version('update_script_test', $schema_version - 1);
295 $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
296 $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.');
298 // Click through update.php with 'administer software updates' permission.
299 $this->drupalLogin($this->updateUser);
300 if ($maintenance_mode) {
301 $this->assertText('Operating in maintenance mode.');
304 $this->assertNoText('Operating in maintenance mode.');
306 $this->drupalGet($this->updateUrl, ['external' => TRUE]);
307 $this->clickLink(t('Continue'));
308 $this->clickLink(t('Apply pending updates'));
310 // Verify that updates were completed successfully.
311 $this->assertText('Updates were attempted.');
312 $this->assertLink('site');
313 $this->assertText('The update_script_test_update_8001() update was executed successfully.');
315 // Verify that no 7.x updates were run.
316 $this->assertNoText('The update_script_test_update_7200() update was executed successfully.');
317 $this->assertNoText('The update_script_test_update_7201() update was executed successfully.');
319 // Verify that there are no links to different parts of the workflow.
320 $this->assertNoLink('Administration pages');
321 $this->assertNoLinkByHrefInMainRegion('update.php', 0);
322 $this->assertNoLink('logged');
324 // Verify the front page can be visited following the upgrade.
325 $this->clickLink('Front page');
326 $this->assertResponse(200);
330 * Returns the Drupal 7 system table schema.
332 public function getSystemSchema() {
334 'description' => "A list of all modules, themes, and theme engines that are or have been installed in Drupal's file system.",
337 'description' => 'The path of the primary file for this item, relative to the Drupal root; e.g. modules/node/node.module.',
344 'description' => 'The name of the item; e.g. node.',
351 'description' => 'The type of the item, either module, theme, or theme_engine.',
358 'description' => "A theme's 'parent' . Can be either a theme or an engine.",
365 'description' => 'Boolean indicating whether or not this item is enabled.',
371 'description' => "Boolean indicating whether this module is loaded during Drupal's early bootstrapping phase (e.g. even before the page cache is consulted).",
376 'schema_version' => [
377 '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.",
384 'description' => "The order in which this module's hooks should be invoked relative to other modules. Equal-weighted modules are ordered by name.",
390 'description' => "A serialized array containing information from the module's .info file; keys can include name, description, package, version, core, dependencies, and php.",
395 'primary key' => ['filename'],
397 'system_list' => ['status', 'bootstrap', 'type', 'weight', 'name'],
398 'type_name' => ['type', 'name'],