MenuTest.php

Same filename in this branch
  1. 11.x core/modules/jsonapi/tests/src/Functional/MenuTest.php
  2. 11.x core/modules/system/tests/src/Kernel/Plugin/migrate/source/MenuTest.php
  3. 11.x core/modules/migrate_drupal_ui/tests/modules/migrate_drupal_message_test/src/Plugin/migrate/source/MenuTest.php
Same filename in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/MenuTest.php
  2. 9 core/modules/views/tests/src/Functional/Wizard/MenuTest.php
  3. 9 core/modules/system/tests/src/Kernel/Plugin/migrate/source/MenuTest.php
  4. 8.9.x core/modules/jsonapi/tests/src/Functional/MenuTest.php
  5. 8.9.x core/modules/views/tests/src/Functional/Wizard/MenuTest.php
  6. 8.9.x core/modules/system/tests/src/Kernel/Plugin/migrate/source/MenuTest.php
  7. 10 core/modules/jsonapi/tests/src/Functional/MenuTest.php
  8. 10 core/modules/views/tests/src/Functional/Wizard/MenuTest.php
  9. 10 core/modules/system/tests/src/Kernel/Plugin/migrate/source/MenuTest.php
  10. 10 core/modules/migrate_drupal_ui/tests/modules/migrate_drupal_message_test/src/Plugin/migrate/source/MenuTest.php

Namespace

Drupal\Tests\views\Functional\Wizard

File

core/modules/views/tests/src/Functional/Wizard/MenuTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\views\Functional\Wizard;

use Drupal\Core\Url;

/**
 * Tests the ability of the views wizard to put views in a menu.
 *
 * @group views
 */
class MenuTest extends WizardTestBase {
    
    /**
     * {@inheritdoc}
     */
    protected $defaultTheme = 'stark';
    
    /**
     * Tests the menu functionality.
     */
    public function testMenus() : void {
        $this->drupalPlaceBlock('system_menu_block:main');
        // Create a view with a page display and a menu link in the Main Menu.
        $view = [];
        $view['label'] = $this->randomMachineName(16);
        $view['id'] = $this->randomMachineName(16);
        $view['description'] = $this->randomMachineName(16);
        $view['page[create]'] = 1;
        $view['page[title]'] = $this->randomMachineName(16);
        $view['page[path]'] = $this->randomMachineName(16);
        $view['page[link]'] = 1;
        $view['page[link_properties][parent]'] = 'main:';
        $view['page[link_properties][title]'] = $this->randomMachineName(16);
        $this->drupalGet('admin/structure/views/add');
        $this->submitForm($view, 'Save and edit');
        // Make sure there is a link to the view from the front page (where we
        // expect the main menu to display).
        $this->drupalGet('');
        $this->assertSession()
            ->statusCodeEquals(200);
        $this->assertSession()
            ->linkExists($view['page[link_properties][title]']);
        $this->assertSession()
            ->linkByHrefExists(Url::fromUri('base:' . $view['page[path]'])->toString());
        // Make sure the link is associated with the main menu.
        
        /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
        $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
        
        /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
        $link = $menu_link_manager->createInstance('views_view:views.' . $view['id'] . '.page_1');
        $url = $link->getUrlObject();
        $this->assertEquals('view.' . $view['id'] . '.page_1', $url->getRouteName(), "Found a link to {$view['page[path]']} in the main menu");
        $metadata = $link->getMetaData();
        $this->assertEquals([
            'view_id' => $view['id'],
            'display_id' => 'page_1',
        ], $metadata);
    }

}

Classes

Title Deprecated Summary
MenuTest Tests the ability of the views wizard to put views in a menu.

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