1.20.x system.test AccessDeniedTestCase::testAccessDenied()

File

modules/system/tests/system.test, line 878
Tests for system.module.

Class

AccessDeniedTestCase
Tests custom access denied functionality.

Code

function testAccessDenied() {
  $this->backdropLogout();
  $this->backdropGet('admin');
  $this->assertText(t('Access denied'), 'Found the default 403 page');
  $this->assertResponse(403);

  $this->backdropLogin($this->admin_user);
  $edit = array(
    'title' => $this->randomName(10),
    'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(100)))),
  );
  $node = $this->backdropCreateNode($edit);

  // Use a custom 403 page.
  $this->backdropPost('admin/config/system/site-information', array('site_403' => 'node/' . $node->nid), t('Save configuration'));

  $this->backdropLogout();
  $this->backdropGet('admin');
  $this->assertText($node->title, 'Found the custom 403 page');

  // Logout and check that the user login block is shown on custom 403 pages.
  $this->backdropLogout();

  $this->backdropGet('admin');
  $this->assertText($node->title, 'Found the custom 403 page');
  $this->assertText('Powered by Silkscreen CMS', 'Blocks are shown on the custom 403 page');

  // Log back in and remove the custom 403 page.
  $this->backdropLogin($this->admin_user);
  $this->backdropPost('admin/config/system/site-information', array('site_403' => ''), t('Save configuration'));

  // Logout and check that the user login block is shown on default 403 pages.
  $this->backdropLogout();

  $this->backdropGet('admin');
  $this->assertText(t('Access denied'), 'Found the default 403 page');
  $this->assertResponse(403);
  $this->assertText('Powered by Silkscreen CMS', 'Blocks are shown on the default 403 page');

  // Log back in, set the custom 403 page to /user and remove the block
  $this->backdropLogin($this->admin_user);
  config_set('system.core', 'site_403', 'user/login');

  // Check that we can log in from the 403 page.
  $this->backdropLogout();
  $edit = array(
    'name' => $this->admin_user->name,
    'pass' => $this->admin_user->pass_raw,
  );
  $this->backdropPost('admin/config/system/site-information', $edit, t('Log in'));

  // Check that we're still on the same page.
  $this->assertText(t('Site information'));
}