Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / src / Tests / System / SiteMaintenanceTest.php
1 <?php
2
3 namespace Drupal\system\Tests\System;
4
5 use Drupal\Core\Url;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests access to site while in maintenance mode.
10  *
11  * @group system
12  */
13 class SiteMaintenanceTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['node'];
21
22   protected $adminUser;
23
24   protected function setUp() {
25     parent::setUp();
26
27     // Configure 'node' as front page.
28     $this->config('system.site')->set('page.front', '/node')->save();
29     $this->config('system.performance')->set('js.preprocess', 1)->save();
30
31     // Create a user allowed to access site in maintenance mode.
32     $this->user = $this->drupalCreateUser(['access site in maintenance mode']);
33     // Create an administrative user.
34     $this->adminUser = $this->drupalCreateUser(['administer site configuration', 'access site in maintenance mode']);
35     $this->drupalLogin($this->adminUser);
36   }
37
38   /**
39    * Verifies site maintenance mode functionality.
40    */
41   public function testSiteMaintenance() {
42
43     // Verify that permission message is displayed.
44     $permission_handler = $this->container->get('user.permissions');
45     $permissions = $permission_handler->getPermissions();
46     $permission_label = $permissions['access site in maintenance mode']['title'];
47     $permission_message = t('Visitors will only see the maintenance mode message. Only users with the "@permission-label" <a href=":permissions-url">permission</a> will be able to access the site. Authorized users can log in directly via the <a href=":user-login">user login</a> page.', ['@permission-label' => $permission_label, ':permissions-url' => \Drupal::url('user.admin_permissions'), ':user-login' => \Drupal::url('user.login')]);
48     $this->drupalGet(Url::fromRoute('system.site_maintenance_mode'));
49     $this->assertRaw($permission_message, 'Found the permission message.');
50
51     $this->drupalGet(Url::fromRoute('user.page'));
52     // JS should be aggregated, so drupal.js is not in the page source.
53     $links = $this->xpath('//script[contains(@src, :href)]', [':href' => '/core/misc/drupal.js']);
54     $this->assertFalse(isset($links[0]), 'script /core/misc/drupal.js not in page');
55     // Turn on maintenance mode.
56     $edit = [
57       'maintenance_mode' => 1,
58     ];
59     $this->drupalPostForm('admin/config/development/maintenance', $edit, t('Save configuration'));
60
61     $admin_message = t('Operating in maintenance mode. <a href=":url">Go online.</a>', [':url' => \Drupal::url('system.site_maintenance_mode')]);
62     $user_message = t('Operating in maintenance mode.');
63     $offline_message = t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', ['@site' => $this->config('system.site')->get('name')]);
64
65     $this->drupalGet(Url::fromRoute('user.page'));
66     // JS should not be aggregated, so drupal.js is expected in the page source.
67     $links = $this->xpath('//script[contains(@src, :href)]', [':href' => '/core/misc/drupal.js']);
68     $this->assertTrue(isset($links[0]), 'script /core/misc/drupal.js in page');
69     $this->assertRaw($admin_message, 'Found the site maintenance mode message.');
70
71     // Logout and verify that offline message is displayed.
72     $this->drupalLogout();
73     $this->drupalGet('');
74     $this->assertEqual('Site under maintenance', $this->cssSelect('main h1')[0]);
75     $this->assertText($offline_message);
76     $this->drupalGet('node');
77     $this->assertEqual('Site under maintenance', $this->cssSelect('main h1')[0]);
78     $this->assertText($offline_message);
79     $this->drupalGet('user/register');
80     $this->assertEqual('Site under maintenance', $this->cssSelect('main h1')[0]);
81     $this->assertText($offline_message);
82
83     // Verify that user is able to log in.
84     $this->drupalGet('user');
85     $this->assertNoText($offline_message);
86     $this->drupalGet('user/login');
87     $this->assertNoText($offline_message);
88
89     // Log in user and verify that maintenance mode message is displayed
90     // directly after login.
91     $edit = [
92       'name' => $this->user->getUsername(),
93       'pass' => $this->user->pass_raw,
94     ];
95     $this->drupalPostForm(NULL, $edit, t('Log in'));
96     $this->assertText($user_message);
97
98     // Log in administrative user and configure a custom site offline message.
99     $this->drupalLogout();
100     $this->drupalLogin($this->adminUser);
101     $this->drupalGet('admin/config/development/maintenance');
102     $this->assertNoRaw($admin_message, 'Site maintenance mode message not displayed.');
103
104     $offline_message = 'Sorry, not online.';
105     $edit = [
106       'maintenance_mode_message' => $offline_message,
107     ];
108     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
109
110     // Logout and verify that custom site offline message is displayed.
111     $this->drupalLogout();
112     $this->drupalGet('');
113     $this->assertEqual('Site under maintenance', $this->cssSelect('main h1')[0]);
114     $this->assertRaw($offline_message, 'Found the site offline message.');
115
116     // Verify that custom site offline message is not displayed on user/password.
117     $this->drupalGet('user/password');
118     $this->assertText(t('Username or email address'), 'Anonymous users can access user/password');
119
120     // Submit password reset form.
121     $edit = [
122       'name' => $this->user->getUsername(),
123     ];
124     $this->drupalPostForm('user/password', $edit, t('Submit'));
125     $mails = $this->drupalGetMails();
126     $start = strpos($mails[0]['body'], 'user/reset/' . $this->user->id());
127     $path = substr($mails[0]['body'], $start, 66 + strlen($this->user->id()));
128
129     // Log in with temporary login link.
130     $this->drupalPostForm($path, [], t('Log in'));
131     $this->assertText($user_message);
132
133     // Regression test to check if title displays in Bartik on maintenance page.
134     \Drupal::service('theme_handler')->install(['bartik']);
135     $this->config('system.theme')->set('default', 'bartik')->save();
136
137     // Logout and verify that offline message is displayed in Bartik.
138     $this->drupalLogout();
139     $this->drupalGet('');
140     $this->assertEqual('Site under maintenance', $this->cssSelect('main h1')[0]);
141   }
142
143   /**
144    * Tests responses to non-HTML requests when in maintenance mode.
145    */
146   public function testNonHtmlRequest() {
147     $this->drupalLogout();
148     \Drupal::state()->set('system.maintenance_mode', TRUE);
149     $formats = ['json', 'xml', 'non-existing'];
150     foreach ($formats as $format) {
151       $this->pass('Testing format ' . $format);
152       $this->drupalGet('<front>', ['query' => ['_format' => $format]]);
153       $this->assertResponse(503);
154       $this->assertRaw('Drupal is currently under maintenance. We should be back shortly. Thank you for your patience.');
155       $this->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
156     }
157   }
158
159 }