View source
<?php
namespace Drupal\system\Tests\System;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
class SiteMaintenanceTest extends WebTestBase {
public static $modules = array(
'node',
);
protected $adminUser;
protected function setUp() {
parent::setUp();
$this
->config('system.site')
->set('page.front', '/node')
->save();
$this
->config('system.performance')
->set('js.preprocess', 1)
->save();
$this->user = $this
->drupalCreateUser(array(
'access site in maintenance mode',
));
$this->adminUser = $this
->drupalCreateUser(array(
'administer site configuration',
'access site in maintenance mode',
));
$this
->drupalLogin($this->adminUser);
}
protected function testSiteMaintenance() {
$this
->drupalGet(Url::fromRoute('user.page'));
$links = $this
->xpath('//script[contains(@src, :href)]', array(
':href' => '/core/misc/drupal.js',
));
$this
->assertFalse(isset($links[0]), 'script /core/misc/drupal.js not in page');
$edit = array(
'maintenance_mode' => 1,
);
$this
->drupalPostForm('admin/config/development/maintenance', $edit, t('Save configuration'));
$admin_message = t('Operating in maintenance mode. <a href=":url">Go online.</a>', array(
':url' => \Drupal::url('system.site_maintenance_mode'),
));
$user_message = t('Operating in maintenance mode.');
$offline_message = t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array(
'@site' => $this
->config('system.site')
->get('name'),
));
$this
->drupalGet(Url::fromRoute('user.page'));
$links = $this
->xpath('//script[contains(@src, :href)]', array(
':href' => '/core/misc/drupal.js',
));
$this
->assertTrue(isset($links[0]), 'script /core/misc/drupal.js in page');
$this
->assertRaw($admin_message, 'Found the site maintenance mode message.');
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertEqual('Site under maintenance', $this
->cssSelect('main h1')[0]);
$this
->assertText($offline_message);
$this
->drupalGet('node');
$this
->assertEqual('Site under maintenance', $this
->cssSelect('main h1')[0]);
$this
->assertText($offline_message);
$this
->drupalGet('user/register');
$this
->assertEqual('Site under maintenance', $this
->cssSelect('main h1')[0]);
$this
->assertText($offline_message);
$this
->drupalGet('user');
$this
->assertNoText($offline_message);
$this
->drupalGet('user/login');
$this
->assertNoText($offline_message);
$edit = array(
'name' => $this->user
->getUsername(),
'pass' => $this->user->pass_raw,
);
$this
->drupalPostForm(NULL, $edit, t('Log in'));
$this
->assertText($user_message);
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/development/maintenance');
$this
->assertNoRaw($admin_message, 'Site maintenance mode message not displayed.');
$offline_message = 'Sorry, not online.';
$edit = array(
'maintenance_mode_message' => $offline_message,
);
$this
->drupalPostForm(NULL, $edit, t('Save configuration'));
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertEqual('Site under maintenance', $this
->cssSelect('main h1')[0]);
$this
->assertRaw($offline_message, 'Found the site offline message.');
$this
->drupalGet('user/password');
$this
->assertText(t('Username or email address'), 'Anonymous users can access user/password');
$edit = array(
'name' => $this->user
->getUsername(),
);
$this
->drupalPostForm('user/password', $edit, t('Submit'));
$mails = $this
->drupalGetMails();
$start = strpos($mails[0]['body'], 'user/reset/' . $this->user
->id());
$path = substr($mails[0]['body'], $start, 66 + strlen($this->user
->id()));
$this
->drupalPostForm($path, array(), t('Log in'));
$this
->assertText($user_message);
\Drupal::service('theme_handler')
->install(array(
'bartik',
));
\Drupal::service('theme_handler')
->setDefault('bartik');
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertEqual('Site under maintenance', $this
->cssSelect('main h1')[0]);
}
}