3 namespace Drupal\Tests\system\Functional\System;
5 use Drupal\Core\Test\AssertMailTrait;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests access to site while in maintenance mode.
14 class SiteMaintenanceTest extends BrowserTestBase {
17 getMails as drupalGetMails;
25 public static $modules = ['node'];
29 protected function setUp() {
32 // Configure 'node' as front page.
33 $this->config('system.site')->set('page.front', '/node')->save();
34 $this->config('system.performance')->set('js.preprocess', 1)->save();
36 // Create a user allowed to access site in maintenance mode.
37 $this->user = $this->drupalCreateUser(['access site in maintenance mode']);
38 // Create an administrative user.
39 $this->adminUser = $this->drupalCreateUser(['administer site configuration', 'access site in maintenance mode']);
40 $this->drupalLogin($this->adminUser);
44 * Verifies site maintenance mode functionality.
46 public function testSiteMaintenance() {
48 // Verify that permission message is displayed.
49 $permission_handler = $this->container->get('user.permissions');
50 $permissions = $permission_handler->getPermissions();
51 $permission_label = $permissions['access site in maintenance mode']['title'];
52 $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')]);
53 $this->drupalGet(Url::fromRoute('system.site_maintenance_mode'));
54 $this->assertRaw($permission_message, 'Found the permission message.');
56 $this->drupalGet(Url::fromRoute('user.page'));
57 // JS should be aggregated, so drupal.js is not in the page source.
58 $links = $this->xpath('//script[contains(@src, :href)]', [':href' => '/core/misc/drupal.js']);
59 $this->assertFalse(isset($links[0]), 'script /core/misc/drupal.js not in page');
60 // Turn on maintenance mode.
62 'maintenance_mode' => 1,
64 $this->drupalPostForm('admin/config/development/maintenance', $edit, t('Save configuration'));
66 $admin_message = t('Operating in maintenance mode. <a href=":url">Go online.</a>', [':url' => \Drupal::url('system.site_maintenance_mode')]);
67 $user_message = t('Operating in maintenance mode.');
68 $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')]);
70 $this->drupalGet(Url::fromRoute('user.page'));
71 // JS should not be aggregated, so drupal.js is expected in the page source.
72 $links = $this->xpath('//script[contains(@src, :href)]', [':href' => '/core/misc/drupal.js']);
73 $this->assertTrue(isset($links[0]), 'script /core/misc/drupal.js in page');
74 $this->assertRaw($admin_message, 'Found the site maintenance mode message.');
76 // Logout and verify that offline message is displayed.
77 $this->drupalLogout();
79 $this->assertEqual('Site under maintenance', $this->cssSelect('main h1')[0]->getText());
80 $this->assertText($offline_message);
81 $this->drupalGet('node');
82 $this->assertEqual('Site under maintenance', $this->cssSelect('main h1')[0]->getText());
83 $this->assertText($offline_message);
84 $this->drupalGet('user/register');
85 $this->assertEqual('Site under maintenance', $this->cssSelect('main h1')[0]->getText());
86 $this->assertText($offline_message);
88 // Verify that user is able to log in.
89 $this->drupalGet('user');
90 $this->assertNoText($offline_message);
91 $this->drupalGet('user/login');
92 $this->assertNoText($offline_message);
94 // Log in user and verify that maintenance mode message is displayed
95 // directly after login.
97 'name' => $this->user->getUsername(),
98 'pass' => $this->user->pass_raw,
100 $this->drupalPostForm(NULL, $edit, t('Log in'));
101 $this->assertText($user_message);
103 // Log in administrative user and configure a custom site offline message.
104 $this->drupalLogout();
105 $this->drupalLogin($this->adminUser);
106 $this->drupalGet('admin/config/development/maintenance');
107 $this->assertNoRaw($admin_message, 'Site maintenance mode message not displayed.');
109 $offline_message = 'Sorry, not online.';
111 'maintenance_mode_message' => $offline_message,
113 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
115 // Logout and verify that custom site offline message is displayed.
116 $this->drupalLogout();
117 $this->drupalGet('');
118 $this->assertEqual('Site under maintenance', $this->cssSelect('main h1')[0]->getText());
119 $this->assertRaw($offline_message, 'Found the site offline message.');
121 // Verify that custom site offline message is not displayed on user/password.
122 $this->drupalGet('user/password');
123 $this->assertText(t('Username or email address'), 'Anonymous users can access user/password');
125 // Submit password reset form.
127 'name' => $this->user->getUsername(),
129 $this->drupalPostForm('user/password', $edit, t('Submit'));
130 $mails = $this->drupalGetMails();
131 $start = strpos($mails[0]['body'], 'user/reset/' . $this->user->id());
132 $path = substr($mails[0]['body'], $start, 66 + strlen($this->user->id()));
134 // Log in with temporary login link.
135 $this->drupalPostForm($path, [], t('Log in'));
136 $this->assertText($user_message);
138 // Regression test to check if title displays in Bartik on maintenance page.
139 \Drupal::service('theme_handler')->install(['bartik']);
140 $this->config('system.theme')->set('default', 'bartik')->save();
142 // Logout and verify that offline message is displayed in Bartik.
143 $this->drupalLogout();
144 $this->drupalGet('');
145 $this->assertEqual('Site under maintenance', $this->cssSelect('main h1')[0]->getText());
149 * Tests responses to non-HTML requests when in maintenance mode.
151 public function testNonHtmlRequest() {
152 $this->drupalLogout();
153 \Drupal::state()->set('system.maintenance_mode', TRUE);
154 $formats = ['json', 'xml', 'non-existing'];
155 foreach ($formats as $format) {
156 $this->pass('Testing format ' . $format);
157 $this->drupalGet('<front>', ['query' => ['_format' => $format]]);
158 $this->assertResponse(503);
159 $this->assertRaw('Drupal is currently under maintenance. We should be back shortly. Thank you for your patience.');
160 $this->assertHeader('Content-Type', 'text/plain; charset=UTF-8');