Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Functional / System / SiteMaintenanceTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\System;
4
5 use Drupal\Core\Test\AssertMailTrait;
6 use Drupal\Core\Url;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests access to site while in maintenance mode.
11  *
12  * @group system
13  */
14 class SiteMaintenanceTest extends BrowserTestBase {
15
16   use AssertMailTrait {
17     getMails as drupalGetMails;
18   }
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['node'];
26
27   protected $adminUser;
28
29   protected function setUp() {
30     parent::setUp();
31
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();
35
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);
41   }
42
43   /**
44    * Verifies site maintenance mode functionality.
45    */
46   public function testSiteMaintenance() {
47
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.');
55
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.
61     $edit = [
62       'maintenance_mode' => 1,
63     ];
64     $this->drupalPostForm('admin/config/development/maintenance', $edit, t('Save configuration'));
65
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')]);
69
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.');
75
76     // Logout and verify that offline message is displayed.
77     $this->drupalLogout();
78     $this->drupalGet('');
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);
87
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);
93
94     // Log in user and verify that maintenance mode message is displayed
95     // directly after login.
96     $edit = [
97       'name' => $this->user->getUsername(),
98       'pass' => $this->user->pass_raw,
99     ];
100     $this->drupalPostForm(NULL, $edit, t('Log in'));
101     $this->assertText($user_message);
102
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.');
108
109     $offline_message = 'Sorry, not online.';
110     $edit = [
111       'maintenance_mode_message' => $offline_message,
112     ];
113     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
114
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.');
120
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');
124
125     // Submit password reset form.
126     $edit = [
127       'name' => $this->user->getUsername(),
128     ];
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()));
133
134     // Log in with temporary login link.
135     $this->drupalPostForm($path, [], t('Log in'));
136     $this->assertText($user_message);
137
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();
141
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());
146   }
147
148   /**
149    * Tests responses to non-HTML requests when in maintenance mode.
150    */
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');
161     }
162   }
163
164 }