You are here

public function MenuFirstchildTest::addCustomMenu in Menu Firstchild 2.x

Creates a custom menu.

Borrowed from Drupal\Tests\menu_ui\Traits\MenuUiTrait.

Return value

\Drupal\system\Entity\Menu The custom menu that has been created.

1 call to MenuFirstchildTest::addCustomMenu()
MenuFirstchildTest::testBasicFunc in tests/src/Functional/MenuFirstchildTest.php
Test Basic Functionality.

File

tests/src/Functional/MenuFirstchildTest.php, line 252

Class

MenuFirstchildTest
Test the Member Discounts api.

Namespace

Drupal\Tests\menu_firstchild\Functional

Code

public function addCustomMenu() {

  // Try adding a menu using a menu_name that is too long.
  $this
    ->drupalGet('admin/structure/menu/add');
  $menu_name = strtolower($this
    ->randomMachineName(MenuStorage::MAX_ID_LENGTH + 1));
  $label = $this
    ->randomMachineName(16);
  $edit = [
    'id' => $menu_name,
    'description' => '',
    'label' => $label,
  ];
  $this
    ->drupalPostForm('admin/structure/menu/add', $edit, $this
    ->t('Save'));

  // Verify that using a menu_name that is too long results in a validation
  // message.
  $this
    ->assertRaw($this
    ->t('@name cannot be longer than %max characters but is currently %length characters long.', [
    '@name' => $this
      ->t('Menu name'),
    '%max' => MenuStorage::MAX_ID_LENGTH,
    '%length' => mb_strlen($menu_name),
  ]));

  // Change the menu_name so it no longer exceeds the maximum length.
  $menu_name = strtolower($this
    ->randomMachineName(MenuStorage::MAX_ID_LENGTH));
  $edit['id'] = $menu_name;
  $this
    ->drupalPostForm('admin/structure/menu/add', $edit, $this
    ->t('Save'));

  // Verify that no validation error is given for menu_name length.
  $this
    ->assertNoRaw($this
    ->t('@name cannot be longer than %max characters but is currently %length characters long.', [
    '@name' => $this
      ->t('Menu name'),
    '%max' => MenuStorage::MAX_ID_LENGTH,
    '%length' => mb_strlen($menu_name),
  ]));

  // Verify that the confirmation message is displayed.
  $this
    ->assertRaw($this
    ->t('Menu %label has been added.', [
    '%label' => $label,
  ]));
  $this
    ->drupalGet('admin/structure/menu');
  $this
    ->assertText($label, 'Menu created');

  // Confirm that the custom menu block is available.
  $this
    ->drupalGet('admin/structure/block/list/' . $this
    ->config('system.theme')
    ->get('default'));
  $this
    ->clickLink('Place block');
  $this
    ->assertText($label);

  // Enable the block.
  $block = $this
    ->drupalPlaceBlock('system_menu_block:' . $menu_name, [
    'label' => 'Primary admin actions',
    'region' => 'content',
    'theme' => 'seven',
  ]);
  $this->blockPlacements[$menu_name] = $block
    ->id();
  return Menu::load($menu_name);
}