29d4d08e238b5e720f568517555a6793b2176578
[yaffs-website] / web / core / modules / system / tests / src / Functional / Update / UpdateScriptTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Update;
4
5 use Drupal\Core\Url;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests the update script access and functionality.
11  *
12  * @group Update
13  */
14 class UpdateScriptTest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['update_script_test', 'dblog', 'language'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected $dumpHeaders = TRUE;
27
28   /**
29    * URL to the update.php script.
30    *
31    * @var string
32    */
33   private $updateUrl;
34
35   /**
36    * A user with the necessary permissions to administer software updates.
37    *
38    * @var \Drupal\user\UserInterface
39    */
40   private $updateUser;
41
42   protected function setUp() {
43     parent::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();
47   }
48
49   /**
50    * Tests access to the update script.
51    */
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);
58
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');
62
63     // Try accessing update.php as an anonymous user.
64     $this->drupalLogout();
65     $this->drupalGet($this->updateUrl, ['external' => TRUE]);
66     $this->assertResponse(403);
67
68     // Check that a link to the update page is not accessible to anonymous
69     // users.
70     $this->drupalGet('/update-script-test/database-updates-menu-item');
71     $this->assertNoLink('Run database updates');
72
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);
77
78     // Check that a link to the update page is accessible to users with proper
79     // permissions.
80     $this->drupalGet('/update-script-test/database-updates-menu-item');
81     $this->assertLink('Run database updates');
82
83     // Access the update page as user 1.
84     $this->drupalLogin($this->rootUser);
85     $this->drupalGet($this->updateUrl, ['external' => TRUE]);
86     $this->assertResponse(200);
87
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');
91   }
92
93   /**
94    * Tests that requirements warnings and errors are correctly displayed.
95    */
96   public function testRequirements() {
97     $update_script_test_config = $this->config('update_script_test.settings');
98     $this->drupalLogin($this->updateUser);
99
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.');
107
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.
111
112     // First, run this test with pending updates to make sure they can be run
113     // successfully.
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.');
126
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.');
136
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.');
145   }
146
147   /**
148    * Tests the effect of using the update script on the theme system.
149    */
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.');
159   }
160
161   /**
162    * Tests update.php when there are no updates to apply.
163    */
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);
174
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);
185   }
186
187   /**
188    * Tests update.php after performing a successful update.
189    */
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.');
196
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.');
200
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.');
205
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);
220   }
221
222   /**
223    * Tests update.php while in maintenance mode.
224    */
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.');
235   }
236
237   /**
238    * Tests perfoming updates with update.php in a multilingual environment.
239    */
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(),
246         ])->save();
247     }
248
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');
254     $config->save();
255
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.');
259
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.');
264
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);
268
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');
274
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);
287   }
288
289   /**
290    * Helper function to run updates via the browser.
291    */
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.');
295
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.');
300
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.');
305     }
306     else {
307       $this->assertNoText('Operating in maintenance mode.');
308     }
309     $this->drupalGet($this->updateUrl, ['external' => TRUE]);
310     $this->clickLink(t('Continue'));
311     $this->clickLink(t('Apply pending updates'));
312     $this->checkForMetaRefresh();
313
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.');
318
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.');
322
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');
327
328     // Verify the front page can be visited following the upgrade.
329     $this->clickLink('Front page');
330     $this->assertResponse(200);
331   }
332
333   /**
334    * Returns the Drupal 7 system table schema.
335    */
336   public function getSystemSchema() {
337     return [
338       'description' => "A list of all modules, themes, and theme engines that are or have been installed in Drupal's file system.",
339       'fields' => [
340         'filename' => [
341           'description' => 'The path of the primary file for this item, relative to the Drupal root; e.g. modules/node/node.module.',
342           'type' => 'varchar',
343           'length' => 255,
344           'not null' => TRUE,
345           'default' => '',
346         ],
347         'name' => [
348           'description' => 'The name of the item; e.g. node.',
349           'type' => 'varchar',
350           'length' => 255,
351           'not null' => TRUE,
352           'default' => '',
353         ],
354         'type' => [
355           'description' => 'The type of the item, either module, theme, or theme_engine.',
356           'type' => 'varchar',
357           'length' => 12,
358           'not null' => TRUE,
359           'default' => '',
360         ],
361         'owner' => [
362           'description' => "A theme's 'parent' . Can be either a theme or an engine.",
363           'type' => 'varchar',
364           'length' => 255,
365           'not null' => TRUE,
366           'default' => '',
367         ],
368         'status' => [
369           'description' => 'Boolean indicating whether or not this item is enabled.',
370           'type' => 'int',
371           'not null' => TRUE,
372           'default' => 0,
373         ],
374         'bootstrap' => [
375           'description' => "Boolean indicating whether this module is loaded during Drupal's early bootstrapping phase (e.g. even before the page cache is consulted).",
376           'type' => 'int',
377           'not null' => TRUE,
378           'default' => 0,
379         ],
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.",
382           'type' => 'int',
383           'not null' => TRUE,
384           'default' => -1,
385           'size' => 'small',
386         ],
387         'weight' => [
388           'description' => "The order in which this module's hooks should be invoked relative to other modules. Equal-weighted modules are ordered by name.",
389           'type' => 'int',
390           'not null' => TRUE,
391           'default' => 0,
392         ],
393         'info' => [
394           'description' => "A serialized array containing information from the module's .info file; keys can include name, description, package, version, core, dependencies, and php.",
395           'type' => 'blob',
396           'not null' => FALSE,
397         ],
398       ],
399       'primary key' => ['filename'],
400       'indexes' => [
401         'system_list' => ['status', 'bootstrap', 'type', 'weight', 'name'],
402         'type_name' => ['type', 'name'],
403       ],
404     ];
405   }
406
407 }