ed076b8156799e5eb7baa68b5d185db11444288b
[yaffs-website] / web / core / modules / system / src / Tests / Update / UpdateScriptTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Update;
4
5 use Drupal\Core\Url;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\simpletest\WebTestBase;
8
9 /**
10  * Tests the update script access and functionality.
11  *
12  * @group Update
13  */
14 class UpdateScriptTest extends WebTestBase {
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->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.');
125
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.');
135
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.');
144   }
145
146   /**
147    * Tests the effect of using the update script on the theme system.
148    */
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.');
158   }
159
160   /**
161    * Tests update.php when there are no updates to apply.
162    */
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);
173
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);
184   }
185
186   /**
187    * Tests update.php after performing a successful update.
188    */
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.');
195
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.');
199
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.');
204
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);
218   }
219
220   /**
221    * Tests update.php while in maintenance mode.
222    */
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.');
233   }
234
235   /**
236    * Tests perfoming updates with update.php in a multilingual environment.
237    */
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(),
244         ])->save();
245     }
246
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');
252     $config->save();
253
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.');
257
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.');
262
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);
266
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');
272
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);
284   }
285
286   /**
287    * Helper function to run updates via the browser.
288    */
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.');
292
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.');
297
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.');
302     }
303     else {
304       $this->assertNoText('Operating in maintenance mode.');
305     }
306     $this->drupalGet($this->updateUrl, ['external' => TRUE]);
307     $this->clickLink(t('Continue'));
308     $this->clickLink(t('Apply pending updates'));
309
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.');
314
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.');
318
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');
323
324     // Verify the front page can be visited following the upgrade.
325     $this->clickLink('Front page');
326     $this->assertResponse(200);
327   }
328
329   /**
330    * Returns the Drupal 7 system table schema.
331    */
332   public function getSystemSchema() {
333     return [
334       'description' => "A list of all modules, themes, and theme engines that are or have been installed in Drupal's file system.",
335       'fields' => [
336         'filename' => [
337           'description' => 'The path of the primary file for this item, relative to the Drupal root; e.g. modules/node/node.module.',
338           'type' => 'varchar',
339           'length' => 255,
340           'not null' => TRUE,
341           'default' => '',
342         ],
343         'name' => [
344           'description' => 'The name of the item; e.g. node.',
345           'type' => 'varchar',
346           'length' => 255,
347           'not null' => TRUE,
348           'default' => '',
349         ],
350         'type' => [
351           'description' => 'The type of the item, either module, theme, or theme_engine.',
352           'type' => 'varchar',
353           'length' => 12,
354           'not null' => TRUE,
355           'default' => '',
356         ],
357         'owner' => [
358           'description' => "A theme's 'parent' . Can be either a theme or an engine.",
359           'type' => 'varchar',
360           'length' => 255,
361           'not null' => TRUE,
362           'default' => '',
363         ],
364         'status' => [
365           'description' => 'Boolean indicating whether or not this item is enabled.',
366           'type' => 'int',
367           'not null' => TRUE,
368           'default' => 0,
369         ],
370         'bootstrap' => [
371           'description' => "Boolean indicating whether this module is loaded during Drupal's early bootstrapping phase (e.g. even before the page cache is consulted).",
372           'type' => 'int',
373           'not null' => TRUE,
374           'default' => 0,
375         ],
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.",
378           'type' => 'int',
379           'not null' => TRUE,
380           'default' => -1,
381           'size' => 'small',
382         ],
383         'weight' => [
384           'description' => "The order in which this module's hooks should be invoked relative to other modules. Equal-weighted modules are ordered by name.",
385           'type' => 'int',
386           'not null' => TRUE,
387           'default' => 0,
388         ],
389         'info' => [
390           'description' => "A serialized array containing information from the module's .info file; keys can include name, description, package, version, core, dependencies, and php.",
391           'type' => 'blob',
392           'not null' => FALSE,
393         ],
394       ],
395       'primary key' => ['filename'],
396       'indexes' => [
397         'system_list' => ['status', 'bootstrap', 'type', 'weight', 'name'],
398         'type_name' => ['type', 'name'],
399       ],
400     ];
401   }
402
403 }