1 layout.test LayoutInterfaceTest::testBlockHttpResponseCodeConditions()

Tests 403 and 404 visibility conditions.

File

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

Class

LayoutInterfaceTest
Tests the interface for adding, removing, and moving blocks.

Code

function testBlockHttpResponseCodeConditions() {
  // Create a new layout at a custom path.
  $layout_title = 'Response Codes Test';
  $layout_name = 'response_codes_test';
  $layout_url = 'response-codes-test';
  $edit = array(
    'title' => $layout_title,
    'name' => $layout_name,
    'layout_template' => 'moscone_flipped',
    'path' => $layout_url,
  );
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Create layout'));

  // Add a test block that shows on 404 pages only.
  $this->clickLink(t('Add block'), 3);
  $this->clickLink(t('Layout foo block'));
  $block_title_404 = $this->randomString();
  $edit = array(
    'title_display' => LAYOUT_TITLE_CUSTOM,
    'title' => $block_title_404,
  );
  $this->backdropPost(NULL, $edit, t('Add condition'));
  $edit = array(
    'condition' => 'http_response_code',
  );
  $this->backdropPost(NULL, $edit, t('Load condition'));
  $edit = array(
    'http_response_codes[404]' => TRUE,
  );
  $this->backdropPost(NULL, $edit, t('Add condition'));
  $this->backdropPost(NULL, array(), t('Update block'));

  // Add another test block that shows on 403 pages only.
  $this->clickLink(t('Add block'), 3);
  $this->clickLink(t('Layout foo block'));
  $block_title_403 = $this->randomString();
  $edit = array(
    'title_display' => LAYOUT_TITLE_CUSTOM,
    'title' => $block_title_403,
  );
  $this->backdropPost(NULL, $edit, t('Add condition'));
  $edit = array(
    'condition' => 'http_response_code',
  );
  $this->backdropPost(NULL, $edit, t('Load condition'));
  $edit = array(
    'http_response_codes[403]' => TRUE,
  );
  $this->backdropPost(NULL, $edit, t('Add condition'));
  $this->backdropPost(NULL, array(), t('Update block'));

  // Add another test block that shows non-200 pages only.
  $this->clickLink(t('Add block'), 3);
  $this->clickLink(t('Layout foo block'));
  $block_title_non_200 = $this->randomString();
  $edit = array(
    'title_display' => LAYOUT_TITLE_CUSTOM,
    'title' => $block_title_non_200,
  );
  $this->backdropPost(NULL, $edit, t('Add condition'));
  $edit = array(
    'condition' => 'http_response_code',
  );
  $this->backdropPost(NULL, $edit, t('Load condition'));
  $edit = array(
    'http_response_codes[200]' => TRUE,
    'negate' => TRUE,
  );
  $this->backdropPost(NULL, $edit, t('Add condition'));
  $this->backdropPost(NULL, array(), t('Update block'));

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

  // Set this test layout as the 404 page.
  $edit = array(
    'site_404' => 'response-codes-test',
    'site_403' => 'response-codes-test',
  );
  $this->backdropPost('admin/config/system/site-information', $edit, t('Save configuration'));

  // When visiting the layout directly, none of the blocks should show.
  $this->backdropGet('response-codes-test');
  $this->assertNoText(check_plain($block_title_403), 'Block not shown when not on a 403 page.');
  $this->assertNoText(check_plain($block_title_404), 'Block not shown when not on a 404 page.');
  $this->assertNoText(check_plain($block_title_non_200), 'Block not shown when not on a non-200 page.');

  // Now visit a non-existent page, which should show the 404 block.
  $this->backdropGet('non-existent-page-test-url');
  $this->assertText(check_plain($block_title_404), 'Block shown when on a 404 page.');
  $this->assertText(check_plain($block_title_non_200), 'Block shown when on a non-200 page.');

  // Log out and visit an access denied page.
  $this->backdropLogout();
  $this->backdropGet('admin');
  $this->assertText(check_plain($block_title_403), 'Block shown when on a 403 page.');
  $this->assertText(check_plain($block_title_non_200), 'Block shown when on a non-200 page.');
}