function SystemMenuBlockTest::setUp

Same name in other branches
  1. 9 core/modules/system/tests/src/Kernel/Block/SystemMenuBlockTest.php \Drupal\Tests\system\Kernel\Block\SystemMenuBlockTest::setUp()
  2. 8.9.x core/modules/system/tests/src/Kernel/Block/SystemMenuBlockTest.php \Drupal\Tests\system\Kernel\Block\SystemMenuBlockTest::setUp()
  3. 11.x core/modules/system/tests/src/Kernel/Block/SystemMenuBlockTest.php \Drupal\Tests\system\Kernel\Block\SystemMenuBlockTest::setUp()

Overrides KernelTestBase::setUp

File

core/modules/system/tests/src/Kernel/Block/SystemMenuBlockTest.php, line 84

Class

SystemMenuBlockTest
Tests \Drupal\system\Plugin\Block\SystemMenuBlock.

Namespace

Drupal\Tests\system\Kernel\Block

Code

protected function setUp() : void {
    parent::setUp();
    $this->installEntitySchema('user');
    $this->installEntitySchema('menu_link_content');
    $account = User::create([
        'name' => $this->randomMachineName(),
        'status' => 1,
    ]);
    $account->save();
    $this->container
        ->get('current_user')
        ->setAccount($account);
    $this->menuLinkManager = $this->container
        ->get('plugin.manager.menu.link');
    $this->linkTree = $this->container
        ->get('menu.link_tree');
    $this->blockManager = $this->container
        ->get('plugin.manager.block');
    $routes = new RouteCollection();
    $requirements = [
        '_access' => 'TRUE',
    ];
    $options = [
        '_access_checks' => [
            'access_check.default',
        ],
    ];
    $routes->add('example1', new Route('/example1', [], $requirements, $options));
    $routes->add('example2', new Route('/example2', [], $requirements, $options));
    $routes->add('example3', new Route('/example3', [], $requirements, $options));
    $routes->add('example4', new Route('/example4', [], $requirements, $options));
    $routes->add('example5', new Route('/example5', [], $requirements, $options));
    $routes->add('example6', new Route('/example6', [], $requirements, $options));
    $routes->add('example7', new Route('/example7', [], $requirements, $options));
    $routes->add('example8', new Route('/example8', [], $requirements, $options));
    $mock_route_provider = new MockRouteProvider($routes);
    $this->container
        ->set('router.route_provider', $mock_route_provider);
    // Add a new custom menu.
    $menu_name = 'mock';
    $label = $this->randomMachineName(16);
    $this->menu = Menu::create([
        'id' => $menu_name,
        'label' => $label,
        'description' => 'Description text',
    ]);
    $this->menu
        ->save();
    // This creates a tree with the following structure:
    // - 1
    // - 2
    //   - 3
    //     - 4
    // - 5
    //   - 7
    // - 6
    // - 8
    // With link 6 being the only external link.
    $links = [
        1 => MenuLinkMock::create([
            'id' => 'test.example1',
            'route_name' => 'example1',
            'title' => 'foo',
            'parent' => '',
            'weight' => 0,
        ]),
        2 => MenuLinkMock::create([
            'id' => 'test.example2',
            'route_name' => 'example2',
            'title' => 'bar',
            'parent' => '',
            'route_parameters' => [
                'foo' => 'bar',
            ],
            'weight' => 1,
        ]),
        3 => MenuLinkMock::create([
            'id' => 'test.example3',
            'route_name' => 'example3',
            'title' => 'baz',
            'parent' => 'test.example2',
            'weight' => 2,
        ]),
        4 => MenuLinkMock::create([
            'id' => 'test.example4',
            'route_name' => 'example4',
            'title' => 'qux',
            'parent' => 'test.example3',
            'weight' => 3,
        ]),
        5 => MenuLinkMock::create([
            'id' => 'test.example5',
            'route_name' => 'example5',
            'title' => 'title5',
            'parent' => '',
            'expanded' => TRUE,
            'weight' => 4,
        ]),
        6 => MenuLinkMock::create([
            'id' => 'test.example6',
            'route_name' => '',
            'url' => 'https://www.drupal.org/',
            'title' => 'barbar',
            'parent' => '',
            'weight' => 5,
        ]),
        7 => MenuLinkMock::create([
            'id' => 'test.example7',
            'route_name' => 'example7',
            'title' => 'title7',
            'parent' => 'test.example5',
            'weight' => 6,
        ]),
        8 => MenuLinkMock::create([
            'id' => 'test.example8',
            'route_name' => 'example8',
            'title' => 'title8',
            'parent' => '',
            'weight' => 7,
        ]),
    ];
    foreach ($links as $instance) {
        $this->menuLinkManager
            ->addDefinition($instance->getPluginId(), $instance->getPluginDefinition());
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.