1 layout.test LayoutInterfaceTest::testMainContentBlock()

Check special conditions around the main content block.

File

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

Class

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

Code

function testMainContentBlock() {
  // Make a new layout that overrides an existing path.
  $layout_title = $this->randomName();
  $layout_name = strtolower($layout_title);
  $layout_path = 'layout-test-path'; // Provided by layout_test.module.
  $edit = array(
    'title' => $layout_title,
    'name' => $layout_name,
    'layout_template' => 'moscone_flipped',
    'path' => $layout_path,
  );
  // Save directly without checking contexts.
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Create layout'));

  // Add the main content block to the sidebar.
  $this->clickLink(t('Add block'), 3);
  $this->clickLink(t('Main page content'));
  $this->backdropPost(NULL, array(), t('Add block'));

  // Record the UUID for the newly added block.
  $last_block = $this->xpath('(//*[@id="layout-edit-main"]//*[contains(@class,:region)]//*[@data-block-id])[last()]', array(
    ':region' => 'l-sidebar',
  ));
  $new_content_block_uuid = (string) $last_block[0]['data-block-id'];

  // Save the layout.
  $edit = array(
    'title_display' => LAYOUT_TITLE_CUSTOM,
    'title' => 'Generic node title',
  );
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/edit-title/editor/title', $edit, t('Save configuration'));
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name, array(), t('Save layout'));

  // Check that the validation prevented two main content blocks.
  $this->assertText(t('The "@block" block may only be added once to this layout.', array('@block' => t('Main page content'))));

  // Delete the previous content block.
  $old_content_block = $this->xpath('//*[@id="layout-edit-main"]//*[contains(@class,:region)]//*[@data-block-id]', array(
    ':region' => 'l-content',
  ));
  $old_content_block_uuid = (string) $old_content_block[0]['data-block-id'];
  $remove_link = $this->xpath('//*[@id="layout-edit-main"]//*[@data-block-id=:uuid]//a[contains(@class,"remove-block")]', array(
    ':uuid' => $old_content_block_uuid,
  ));
  $remove_url_parts = backdrop_parse_url($remove_link[0]['href']);
  $this->backdropGet($remove_url_parts['path'], $remove_url_parts);

  $this->backdropPost(NULL, array(), t('Save layout'));
  $this->assertText(t('Layout content saved.'), 'Layout saved successfully after removing old content block.');

  // Check the front-end that the content is shown in the sidebar. Note that
  // the main content block does *not* have any wrappers, so we check directly
  // for the content we expect at /node, which is a list of post tags.
  $this->backdropGet($layout_path);
  $front_end_content = $this->xpath('//*[contains(@class,:region)]/div[@id=:page-content]', array(
    ':region' => 'l-sidebar',
    ':page-content' => 'layout-test-page-content',
  ));
  $this->assertEqual(count($front_end_content), 1, 'Main content shown in sidebar after moving main content block.');

  // Now remove the second main content block, which should be allowed even
  // though it is potentially a bad idea.
  $this->backdropGet('admin/structure/layouts/manage/' . $layout_name);
  $remove_link = $this->xpath('//*[@data-block-id=:uuid]//a[contains(@class,"remove-block")]', array(
    ':uuid' => $new_content_block_uuid,
  ));
  $remove_url_parts = backdrop_parse_url($remove_link[0]['href']);
  $this->backdropGet($remove_url_parts['path'], $remove_url_parts);

  $this->backdropPost(NULL, array(), t('Save layout'));
  $this->assertText(t('Layout content saved.'));

  // Check that the main content block is no longer displayed at all.
  $this->backdropGet($layout_path);
  $front_end_content = $this->xpath('//*[@id=:id]', array(
    ':id' => 'layout-test-page-content',
  ));
  $this->assertEqual(count($front_end_content), 0, 'Main content is no longer shown at all.');

  // Edit the default layout and ensure that the main content block cannot be
  // removed.
  $this->backdropGet('admin/structure/layouts/manage/default');
  $default_content_block = $this->xpath('//*[@id="layout-edit-main"]//*[contains(@class,:region)]//*[@data-block-id]', array(
    ':region' => 'l-content',
  ));
  $default_content_block_uuid = (string) $default_content_block[0]['data-block-id'];
  $remove_link = $this->xpath('//*[@data-block-id=:uuid]//a[contains(@class,"remove-block")]', array(
    ':uuid' => $default_content_block_uuid,
  ));
  $remove_url_parts = backdrop_parse_url($remove_link[0]['href']);
  $this->backdropGet($remove_url_parts['path'], $remove_url_parts);

  $this->backdropPost(NULL, array(), t('Save layout'));
  $this->assertText(t('The "@block" block must be added to this layout.', array('@block' => t('Main page content'))));

  // Cancel the changes to the default layout.
  $this->backdropPost(NULL, array(), t('Cancel'));

  // Delete the previous test layout.
  $layout = layout_load($layout_name);
  $layout->delete();

}