1.20.x layout.test LayoutInterfaceTest::testMenuItemReassignment()

Ensure that reassignment of menu items works when deleting layouts.

File

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

Class

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

Code

function testMenuItemReassignment() {
  // Create a layout that will generate a menu item automatically.
  $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'));

  // Check that the layout and menu item config are generated.
  $result = config_get('layout.layout.' . $layout_name, 'name');
  $this->assertEqual($result, $layout_name, 'Config created for new layout.');
  $result = config_get('layout.menu_item.' . $layout_name, 'name');
  $this->assertEqual($result, $layout_name, 'Config created for new layout menu item.');

  $layout_title = $this->randomName();
  $edit = array(
    'menu[type]' => 'normal',
    'menu[title]' => $layout_title,
    'menu[weight]' => 1,
  );
  $this->backdropPost('admin/structure/layouts/menu/' . $layout_name, $edit, t('Save menu settings'));

  $this->backdropGet('<front>');
  $this->assertLink(check_plain($layout_title), 0, 'Menu link displayed on homepage.');

  // Create another layout at the same path.
  $second_layout_name = strtolower($this->randomName());
  $edit = array(
    'name' => $second_layout_name,
    'title' => $second_layout_name,
    'path' => $layout_path,
    'layout_template' => 'moscone_flipped',
  );
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Create layout'));

  // Check that the layout config is created, but not a menu item, since it
  // will reuse the existing menu item.
  $result = config_get('layout.layout.' . $second_layout_name, 'name');
  $this->assertEqual($result, $second_layout_name, 'Config created for second layout.');
  $result = config_get('layout.menu_item.' . $second_layout_name, 'name');
  $this->assertNull($result, 'No config created for second layout menu item (reuses the existing one).');

  // Delete the first layout and check that the menu item is transferred.
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/delete', array(), t('Delete layout'));

  layout_reset_caches();
  $this->assertFalse(layout_load($layout_name), 'Base layout deleted.');
  $this->assertFalse(layout_menu_item_load($layout_name), 'Base layout menu item deleted.');

  $menu_item = layout_menu_item_load($second_layout_name);
  $this->assertTrue($menu_item, 'Menu item transferred to second layout.');
  $this->assertEqual($menu_item->name, $second_layout_name, 'Menu name updated.');
  $this->assertEqual($menu_item->path, $layout_path, 'Menu path copied correctly.');

  $this->backdropGet('<front>');
  $this->assertLink(check_plain($layout_title), 0, 'Previous menu item link still displayed after deleting first layout.');

  // Delete the second layout
  $this->backdropPost('admin/structure/layouts/manage/' . $second_layout_name . '/delete', array(), t('Delete layout'));
  $this->backdropGet('<front>');
  $this->assertNoLink(check_plain($layout_title), 'Menu item no longer present after deleting last layout.');

  $result = config_get('layout.menu_item.' . $second_layout_name, 'name');
  $this->assertNull($result, 'Config for second menu item has been deleted.');
}