function NavigationMenuMarkupTest::setUp

Same name in other branches
  1. 11.x core/modules/navigation/tests/src/Kernel/NavigationMenuMarkupTest.php \Drupal\Tests\navigation\Kernel\NavigationMenuMarkupTest::setUp()

Overrides KernelTestBase::setUp

File

core/modules/navigation/tests/src/Kernel/NavigationMenuMarkupTest.php, line 72

Class

NavigationMenuMarkupTest
Tests \Drupal\navigation\Plugin\Block\NavigationMenuBlock.

Namespace

Drupal\Tests\navigation\Kernel

Code

protected function setUp() : void {
    parent::setUp();
    $this->installEntitySchema('menu_link_content');
    $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));
    // Define our RouteProvider mock.
    $mock_route_provider = new MockRouteProvider($routes);
    $this->container
        ->set('router.route_provider', $mock_route_provider);
    // Define our UrlGenerator service that use the new RouteProvider.
    $url_generator_non_bubbling = new UrlGenerator($mock_route_provider, $this->container
        ->get('path_processor_manager'), $this->container
        ->get('route_processor_manager'), $this->container
        ->get('request_stack'), $this->container
        ->getParameter('filter_protocols'));
    $url_generator = new MetadataBubblingUrlGenerator($url_generator_non_bubbling, $this->container
        ->get('renderer'));
    $this->container
        ->set('url_generator', $url_generator);
    // 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
    // phpcs:disable
    $links = [
        1 => MenuLinkMock::create([
            'id' => 'test.example1',
            'route_name' => 'example1',
            'title' => 'title 1',
            'parent' => '',
            'weight' => 0,
        ]),
        2 => MenuLinkMock::create([
            'id' => 'test.example2',
            'route_name' => 'example2',
            'title' => 'Another title',
            'parent' => '',
            'route_parameters' => [
                'foo' => 'bar',
            ],
            'weight' => 1,
        ]),
        3 => MenuLinkMock::create([
            'id' => 'test.example3',
            'route_name' => 'example3',
            'title' => 'Nested menu link',
            'parent' => 'test.example2',
            'weight' => 2,
        ]),
    ];
    // phpcs:enable
    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.