View source
<?php
namespace Drupal\Tests\menu_trail_by_path\Functional;
use Drupal\Core\Url;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\menu_trail_by_path\MenuTrailByPathActiveTrail;
use Drupal\system\Entity\Menu;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Menu\AssertMenuActiveTrailTrait;
use Drupal\Tests\Traits\Core\PathAliasTestTrait;
class MenuTrailByPathActiveTrailHtmlClassTest extends BrowserTestBase {
use AssertMenuActiveTrailTrait;
use PathAliasTestTrait;
public static $modules = [
'node',
'block',
'menu_link_content',
'menu_trail_by_path',
];
protected $defaultTheme = 'classy';
protected $menuUrls = [];
protected $authenticatedUser;
protected function setUp() {
parent::setUp();
$this->authenticatedUser = $this
->drupalCreateUser();
$this
->drupalCreateContentType(array(
'type' => 'page',
'name' => 'Basic page',
));
$node1 = $this
->drupalCreateNode();
$this
->createPathAlias('/node/' . $node1
->id(), '/news');
$node2 = $this
->drupalCreateNode();
$this
->createPathAlias('/node/' . $node2
->id(), '/news/category-a');
$this->menuUrls = [
'Home' => Url::fromUri('route:<front>'),
'User password' => Url::fromUri('route:user.pass'),
'User login' => Url::fromUri('route:user.login'),
'User' => Url::fromUri('route:user.page'),
'News' => Url::fromUri('route:entity.node.canonical;node=' . $node1
->id()),
'News » News overview' => Url::fromUri('route:entity.node.canonical;node=' . $node1
->id()),
'News » Category A' => Url::fromUri('route:entity.node.canonical;node=' . $node2
->id()),
];
$this
->buildMenu();
$this
->drupalPlaceBlock('system_menu_block:main', [
'id' => 'system-menu-block-main',
'label' => 'Main menu',
'region' => 'header',
]);
}
public function testUrlHome() {
$this
->drupalGet(clone $this->menuUrls['Home']);
$this
->assertMenuActiveTrail([
$this
->menuUrlBasePath('Home') => 'Home',
], TRUE);
}
public function testUrlUserPassword() {
$this
->drupalGet(clone $this->menuUrls['User password']);
$this
->assertMenuActiveTrail([
$this
->menuUrlBasePath('User password') => 'User password',
], TRUE);
$this
->config('menu_trail_by_path.settings')
->set('trail_source', MenuTrailByPathActiveTrail::MENU_TRAIL_CORE)
->save();
$this
->rebuildAll();
$this
->drupalGet(clone $this->menuUrls['User password']);
$this
->assertMenuActiveTrail([
$this
->menuUrlBasePath('User password') => 'User password',
], TRUE);
$this
->config('menu_trail_by_path.settings')
->set('trail_source', MenuTrailByPathActiveTrail::MENU_TRAIL_DISABLED)
->save();
$this
->rebuildAll();
$this
->drupalGet(clone $this->menuUrls['User password']);
$this
->assertNoRaw('menu-item--active-trail');
$menu = Menu::load('main');
$menu
->setThirdPartySetting('menu_trail_by_path', 'trail_source', MenuTrailByPathActiveTrail::MENU_TRAIL_PATH);
$menu
->save();
$this
->drupalGet(clone $this->menuUrls['User password']);
$this
->assertMenuActiveTrail([
$this
->menuUrlBasePath('User password') => 'User password',
], TRUE);
}
public function testUrlUserLogin() {
$this
->drupalGet(clone $this->menuUrls['User login']);
$this
->assertMenuActiveTrail([
$this
->menuUrlBasePath('User login') => 'User login',
], TRUE);
}
public function testUrlUser() {
$this
->drupalLogin($this->authenticatedUser);
$this
->drupalGet(clone $this->menuUrls['User']);
$this
->assertMenuActiveTrail([
$this
->menuUrlBasePath('User') => 'User',
], FALSE);
}
public function testUrlNews() {
$this
->drupalGet(clone $this->menuUrls['News']);
$this
->assertMenuActiveTrail([
$this
->menuUrlBasePath('News') => 'News',
], TRUE);
}
public function testUrlNewsNewsOverview() {
$this
->drupalGet(clone $this->menuUrls['News » News overview']);
$this
->assertMenuActiveTrail([
$this
->menuUrlBasePath('News » News overview') => 'News overview',
], TRUE);
$this
->assertMenuActiveTrail([
$this
->menuUrlBasePath('News') => 'News',
], TRUE);
}
public function testUrlNewsCategorya() {
$this
->drupalGet(clone $this->menuUrls['News » Category A']);
$this
->assertMenuActiveTrail([
$this
->menuUrlBasePath('News') => 'News',
$this
->menuUrlBasePath('News » Category A') => 'Category A',
], TRUE);
}
public function testUrlNewsCategoryaItema() {
$node3 = $this
->drupalCreateNode();
\Drupal::service('path.alias_storage')
->save('/node/' . $node3
->id(), '/news/category-a/item-a');
$this
->drupalGet($node3
->toUrl());
$this
->assertMenuActiveTrail([
$this
->menuUrlBasePath('News') => 'News',
$this
->menuUrlBasePath('News » Category A') => 'Category A',
], FALSE);
$this
->config('menu_trail_by_path.settings')
->set('trail_source', MenuTrailByPathActiveTrail::MENU_TRAIL_CORE)
->save();
$this
->rebuildAll();
$this
->drupalGet($node3
->toUrl());
$this
->assertNoRaw('menu-item--active-trail');
}
protected function buildMenu($menu_name = 'main') {
$menu_handler = \Drupal::service('plugin.manager.menu.link');
$menu_handler
->deleteLinksInMenu($menu_name);
$menu_links = [];
$menu_link_weight = -30;
foreach ($this->menuUrls as $title => $url) {
$titles = explode(' » ', $title);
$title_short = array_pop($titles);
$parent = $titles ? $menu_links[implode(' » ', $titles)]
->getPluginId() : NULL;
$menu_links[$title] = MenuLinkContent::create([
'menu_name' => $menu_name,
'title' => $title_short,
'link' => [
'uri' => $url
->toUriString(),
],
'parent' => $parent,
'expanded' => TRUE,
'weight' => $menu_link_weight,
]);
$menu_links[$title]
->save();
$menu_link_weight++;
}
}
protected function menuUrlBasePath($name) {
$url = $this->menuUrls[$name];
return '/' . preg_replace('/^' . preg_quote(base_path(), '/') . '/', '', $url
->toString());
}
}