1 system.test SiteMaintenanceTestCase::testSiteMaintenance()

Verify site maintenance mode functionality.

File

core/modules/system/tests/system.test, line 1237
Tests for system.module.

Class

SiteMaintenanceTestCase
Tests site maintenance functionality.

Code

function testSiteMaintenance() {
  // Start with the page cache disabled.
  config_set('system.core', 'page_cache_maximum_age', 0);

  // Turn on maintenance mode.
  $edit = array(
    'maintenance_mode' => TRUE,
  );
  $this->backdropPost('admin/config/development/maintenance', $edit, 'Save configuration');
  $this->assertRaw('The site is now in maintenance mode. Only users with the "Access site in maintenance mode" permission will be able to access the site.', 'Maintenance confirmation message shown.');

  $path = config_get('system.core', 'site_frontpage');
  $admin_message = format_string('The site is currently in <a href="@url">maintenance mode</a>', array('@url' => url('admin/config/development/maintenance')));
  $user_message = 'The site is currently in maintenance mode.';
  $offline_message = format_string('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => config_get('system.core', 'site_name')));

  $this->backdropGet($path);
  $this->assertRaw($admin_message, 'Found the site maintenance mode message.');

  // Logout and verify that offline message is displayed.
  $this->backdropLogout();

  $this->backdropGet('token/tree');
  $this->assertResponse(503);
  $this->assertText($offline_message);

  // With caching disabled, the maintenance page is also disabled.
  $this->assertIdentical(strpos($this->backdropGetHeader('Cache-Control'), 'no-cache, must-revalidate'), 0);

  // Enable caching and check that the default maintenance max age is set.
  config_set('system.core', 'page_cache_maximum_age', 300);
  $this->backdropGet('token/tree');
  $this->assertIdentical($this->backdropGetHeader('Cache-Control'), 'public; max-age: 10');

  // Increase the cache lifetime and try a different path.
  config_set('system.core', 'maintenance_page_maximum_age', 60);
  $this->backdropGet('user/register');
  $this->assertResponse(503);
  $this->assertText($offline_message);
  $this->assertIdentical($this->backdropGetHeader('Cache-Control'), 'public; max-age: 60');

  // Verify that user is able to log in.
  $this->backdropGet('user');
  $this->assertResponse(200);
  $this->assertNoText($offline_message);
  // The user should also be able to reset their password.
  $this->backdropGet('user/password');
  $this->assertResponse(200);
  $this->assertNoText($offline_message);
  $this->backdropGet('user/login');
  $this->assertResponse(200);
  $this->assertNoText($offline_message);

  // Log in user and verify that maintenance mode message is displayed
  // directly after login.
  $edit = array(
    'name' => $this->user->name,
    'pass' => $this->user->pass_raw,
  );
  $this->backdropPost(NULL, $edit, 'Log in');
  $this->assertText($user_message);

  // Log in administrative user and configure a custom site offline message.
  $this->backdropLogout();
  $this->backdropLogin($this->admin_user);
  $this->backdropGet('admin/config/development/maintenance');
  $this->assertNoRaw($admin_message, 'Site maintenance mode message not displayed.');

  $offline_message = 'Backdrop is currently under maintenance. We should be back shortly. Thank you for your patience.';
  $edit = array(
    'maintenance_mode_message' => $offline_message,
  );
  $this->backdropPost(NULL, $edit, 'Save configuration');

  // Logout and verify that custom site offline message is displayed.
  $this->backdropLogout();
  $this->backdropGet('');
  $this->assertRaw($offline_message, 'Found the site offline message.');

  // Verify that custom site offline message is not displayed on user/password.
  $this->backdropGet('user/password');
  $this->assertText('Username or email address', 'Anonymous users can access user/password');

  // Submit password reset form.
  $edit = array(
    'name' => $this->user->name,
  );
  $this->backdropPost('user/password', $edit, 'Reset password');
  $mails = $this->backdropGetMails();
  $start = strpos($mails[0]['body'], 'user/reset/' . $this->user->uid);
  $path = substr($mails[0]['body'], $start, 66 + strlen($this->user->uid));

  // Log in with temporary login link.
  $pass = user_password();
  $pass_edit = array(
    'pass[pass1]' => $pass,
    'pass[pass2]' => $pass,
  );
  $this->backdropPost($path, $pass_edit, 'Save password & log in');
  $this->assertText($user_message);
  $this->backdropLogout();

  // Disable maintenance mode.
  $this->backdropLogin($this->admin_user);
  $edit = array(
    'maintenance_mode' => FALSE,
  );
  $this->backdropPost('admin/config/development/maintenance', $edit, 'Save configuration');
  $this->assertRaw('The site is no longer in maintenance mode.');
}