1 layout.test LayoutSelectionTest::testRoleSelection()

Test role-based selection of layouts.

File

core/modules/layout/tests/layout.test, line 1857
Tests for the Layout module.

Class

LayoutSelectionTest
Tests that the correct layout is used in various situations.

Code

function testRoleSelection() {
  // Create and login admin user.
  $admin_user = $this->backdropCreateUser(array(
    'access administration pages',
    'administer site configuration',
    'administer modules',
    'administer layouts',
    'administer nodes',
  ));
  $this->backdropLogin($admin_user);

  // Create a web user.
  $web_user = $this->backdropCreateUser(array('access user profiles', 'access content'));

  // Create a layout at a custom path.
  $layout_name = strtolower($this->randomName());
  $layout_path = $layout_name;
  $edit = array(
    'name' => $layout_name,
    'title' => $layout_name,
    'path' => $layout_path,
    'layout_template' => 'moscone_flipped',
  );

  // Note that this adds a condition, not saving the layout yet.
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Add visibility condition'));

  // Select the condition to add.
  $edit = array(
    'condition' => 'user_role',
  );
  $this->backdropPost(NULL, $edit, t('Load condition'));

  // Allow authenticated users to see the layout only.
  $edit = array(
    'roles[authenticated]' => TRUE,
  );
  $this->backdropPost(NULL, $edit, t('Add visibility condition'));

  // Save the layout.
  $this->backdropPost(NULL, array(), t('Create layout'));

  // View as an administrator.
  $this->backdropGet($layout_path);
  $this->assertResponse(200);
  $this->backdropLogout();

  // View as a basic user.
  $this->backdropLogin($web_user);
  $this->backdropGet($layout_path);
  $this->assertResponse(200);
  $this->backdropLogout();

  // View as anonymous, where they should get a 404.
  $this->backdropGet($layout_path);
  $this->assertResponse(404);
}