1 layout.test LayoutInterfaceTest::testLayoutChange()

Tests switching between different layouts, ensuring block copying.

File

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

Class

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

Code

function testLayoutChange() {
  // Add blocks to the default layout for checking.
  $default_layout = layout_load('default');
  $default_layout->addBlock('layout_test', 'foo', 'content');
  $default_layout->addBlock('layout_test', 'foo', 'sidebar');
  $default_layout->save();

  // Create a layout should inherit all the blocks. Since the default layout
  // also two columns, all blocks should be copied over.
  $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->backdropPost('admin/structure/layouts/add', $edit, t('Create layout'));

  layout_reset_caches();
  $layout = layout_load($layout_name);
  $this->assertBlocksMatch($layout, $default_layout);

  // Change the layout to the test layout, which has identical regions.
  $edit = array(
    'layout_template' => 'layout_test_layout',
  );
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/configure', $edit, t('Save layout'));

  layout_reset_caches();
  $layout = layout_load($layout_name);
  $this->assertBlocksMatch($layout, $default_layout);

  // Change the layout to the boxton layout, eliminating the sidebar.
  $edit = array(
    'layout_template' => 'boxton',
  );
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/configure', $edit, t('Save layout'));

  $orphaned_uuids = $default_layout->positions['sidebar'];
  $this->orphaned_blocks = !empty($orphaned_uuids);
  layout_reset_caches();
  $layout = layout_load($layout_name);
  $this->assertBlocksMatch($layout, $default_layout);
  $this->assertFalse(isset($layout->positions['sidebar']), 'Sidebar region blocks moved out when changing layout to single column.');

  $intersection = array_intersect(array_keys($layout->content), $orphaned_uuids);
  $this->assertEqual($intersection, array(), 'The sidebar blocks have been removed from the layout content array.');
  // Check that the sidebar and content region blocks in the old layout are
  // the same number of blocks in the content region of the new layout.
  $default_layout_sidebar_content_regions_count = count($default_layout->positions['sidebar']) + count($default_layout->positions['content']);
  $new_content_region_count = count($layout->positions['content']);
  // Count the system main block separately since it doesn't exist in the
  // new layout.
  if ($default_layout->positions['content'][0] == 'default') {
    $new_content_region_count++;
  }
  $this->assertTrue($default_layout_sidebar_content_regions_count == $new_content_region_count, 'The blocks from the Sidebar and Default regions in the old layout all exist in the Default region of the new layout.');
  // Make sure the moved block is part of the module we expect.
  $moved_block_uuid = $layout->positions['content'][1];
  $moved_block = $layout->content[$moved_block_uuid];
  $this->assertTrue($moved_block->module == 'layout_test', 'The moved block is from the layout_test module.');
}