1 layout.test LayoutSelectionTest::testRoleSelection()

Test role-based selection of layouts.

File

core/modules/layout/tests/layout.test, line 2564
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'));

  // Test ordinary role-based selection.
  // 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',
  );

  // This adds a condition, not saving the layout yet.
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Add 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 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);

  // Go back and configure the visibility condition to test a negated role.
  $this->backdropLogin($admin_user);

  $edit = array(
    'roles[authenticated]' => FALSE,
    'roles[anonymous]' => TRUE,
    'negate' => TRUE,
  );
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/condition/edit/layout/0', $edit, t('Save visibility condition'));

  // 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);
}