View source
<?php
namespace Drupal\Tests\token\Functional;
use Behat\Mink\Element\NodeElement;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\system\Entity\Menu;
class TokenMenuTest extends TokenTestBase {
public static $modules = [
'menu_ui',
'node',
'block',
'language',
'block_content',
'content_translation',
];
function testMenuTokens() {
$this
->drupalCreateContentType([
'type' => 'page',
]);
$menu = Menu::create([
'id' => 'main-menu',
'label' => 'Main menu',
'description' => 'The <em>Main</em> menu is used on many sites to show the major sections of the site, often in a top navigation bar.',
]);
$menu
->save();
$this
->drupalPlaceBlock('system_menu_block:main-menu');
$root_link = MenuLinkContent::create([
'link' => [
'uri' => 'internal:/admin',
],
'title' => 'Administration',
'menu_name' => 'main-menu',
]);
$root_link
->save();
$parent_link = MenuLinkContent::create([
'link' => [
'uri' => 'internal:/admin/config',
],
'title' => 'Configuration',
'menu_name' => 'main-menu',
'parent' => $root_link
->getPluginId(),
]);
$parent_link
->save();
$tokens = [
'id' => $parent_link
->getPluginId(),
'title' => 'Configuration',
'menu' => 'Main menu',
'menu:name' => 'Main menu',
'menu:machine-name' => $menu
->id(),
'menu:description' => 'The <em>Main</em> menu is used on many sites to show the major sections of the site, often in a top navigation bar.',
'menu:menu-link-count' => '2',
'menu:edit-url' => Url::fromRoute('entity.menu.edit_form', [
'menu' => 'main-menu',
], [
'absolute' => TRUE,
])
->toString(),
'url' => Url::fromRoute('system.admin_config', [], [
'absolute' => TRUE,
])
->toString(),
'url:absolute' => Url::fromRoute('system.admin_config', [], [
'absolute' => TRUE,
])
->toString(),
'url:relative' => Url::fromRoute('system.admin_config', [], [
'absolute' => FALSE,
])
->toString(),
'url:path' => '/admin/config',
'url:alias' => '/admin/config',
'edit-url' => Url::fromRoute('entity.menu_link_content.canonical', [
'menu_link_content' => $parent_link
->id(),
], [
'absolute' => TRUE,
])
->toString(),
'parent' => 'Administration',
'parent:id' => $root_link
->getPluginId(),
'parent:title' => 'Administration',
'parent:menu' => 'Main menu',
'parent:parent' => NULL,
'parents' => 'Administration',
'parents:count' => 1,
'parents:keys' => $root_link
->getPluginId(),
'root' => 'Administration',
'root:id' => $root_link
->getPluginId(),
'root:parent' => NULL,
'root:root' => NULL,
];
$this
->assertTokens('menu-link', [
'menu-link' => $parent_link,
], $tokens);
$node = $this
->drupalCreateNode();
$node_link = MenuLinkContent::create([
'link' => [
'uri' => 'entity:node/' . $node
->id(),
],
'title' => 'Node link',
'parent' => $parent_link
->getPluginId(),
'menu_name' => 'main-menu',
]);
$node_link
->save();
$tokens = [
'menu-link' => 'Node link',
'menu-link:id' => $node_link
->getPluginId(),
'menu-link:title' => 'Node link',
'menu-link:menu' => 'Main menu',
'menu-link:url' => $node
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString(),
'menu-link:url:path' => '/node/' . $node
->id(),
'menu-link:edit-url' => $node_link
->toUrl('edit-form', [
'absolute' => TRUE,
])
->toString(),
'menu-link:parent' => 'Configuration',
'menu-link:parent:id' => $parent_link
->getPluginId(),
'menu-link:parents' => 'Administration, Configuration',
'menu-link:parents:count' => 2,
'menu-link:parents:keys' => $root_link
->getPluginId() . ', ' . $parent_link
->getPluginId(),
'menu-link:root' => 'Administration',
'menu-link:root:id' => $root_link
->getPluginId(),
];
$this
->assertTokens('node', [
'node' => $node,
], $tokens);
$loaded_node = Node::load($node
->id());
$this
->assertTokens('node', [
'node' => $loaded_node,
], $tokens);
$this
->assertTrue(!isset($loaded_node->menu), 'The $node->menu property was not modified during token replacement.', 'Regression');
$this
->drupalLogin($this
->drupalCreateUser([
'create page content',
'edit any page content',
'administer menu',
'administer nodes',
'administer content types',
'access administration pages',
]));
$edit = [
'menu_options[main-menu]' => 1,
'menu_options[main]' => 1,
'menu_parent' => 'main-menu:',
];
$this
->drupalPostForm('admin/structure/types/manage/page', $edit, 'Save content type');
$this
->drupalGet('node/add/page');
$this
->drupalPostForm(NULL, [
'title[0][value]' => 'Node menu title test',
'body[0][value]' => 'This is a [node:menu-link:title] token to the menu link title',
'menu[enabled]' => 1,
'menu[title]' => 'Test preview',
], 'Save');
$node = $this
->drupalGetNodeByTitle('Node menu title test');
$this
->assertEquals('This is a Test preview token to the menu link title', $node->body->value);
$link = menu_ui_get_menu_link_defaults($node);
$this
->drupalPostForm('admin/structure/menu/manage/main-menu', [
'links[menu_plugin_id:' . $link['id'] . '][enabled]' => FALSE,
], 'Save');
$this
->assertText('Menu Main menu has been updated.');
$this
->drupalPostForm('node/' . $node
->id() . '/edit', [], 'Save');
$this
->assertSession()
->linkNotExists('Test preview');
$this
->drupalGet('node/add/page');
$selects = $this
->cssSelect('select[name="menu[menu_parent]"]');
$select = reset($selects);
$options = $select
->findAll('css', 'option');
$options = array_filter($options, function (NodeElement $element) {
return strpos($element
->getText(), 'Test preview') !== FALSE;
});
$this
->assertCount(1, $options);
$this
->drupalPostForm(NULL, [
'title[0][value]' => 'Node menu title parent path test',
'body[0][value]' => 'This is a [node:menu-link:parent:url:path] token to the menu link parent',
'menu[enabled]' => 1,
'menu[title]' => 'Child link',
'menu[menu_parent]' => 'main-menu:' . $parent_link
->getPluginId(),
], 'Save');
$node = $this
->drupalGetNodeByTitle('Node menu title parent path test');
$this
->assertEquals('This is a /admin/config token to the menu link parent', $node->body->value);
$this
->drupalPostForm('node/' . $node
->id() . '/edit', [
'menu[menu_parent]' => 'main-menu:' . $node_link
->getPluginId(),
'title[0][value]' => 'Node menu title edit parent path test',
'body[0][value]' => 'This is a [node:menu-link:parent:url:path] token to the menu link parent',
], 'Save');
$node = $this
->drupalGetNodeByTitle('Node menu title edit parent path test', TRUE);
$this
->assertEquals(sprintf('This is a /node/%d token to the menu link parent', $loaded_node
->id()), $node->body->value);
$this
->drupalGet('node/add/page');
$selects = $this
->cssSelect('select[name="menu[menu_parent]"]');
$select = reset($selects);
$options = $select
->findAll('css', 'option');
$options = array_filter($options, function (NodeElement $item) {
return strpos($item
->getText(), 'Child link') !== FALSE;
});
$this
->assertCount(1, $options);
$this
->drupalGet('node/add/page');
$this
->drupalPostForm(NULL, [
'title[0][value]' => 'Node menu adding menu later test',
'body[0][value]' => 'Going to add a menu link on edit',
'menu[enabled]' => 0,
], 'Save');
$node = $this
->drupalGetNodeByTitle('Node menu adding menu later test');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->drupalPostForm(NULL, [
'title[0][value]' => 'Node menu adding menu later test',
'body[0][value]' => 'This is a [node:menu-link:parent:url:path] token to the menu link parent',
'menu[enabled]' => 1,
'menu[title]' => 'Child link',
'menu[menu_parent]' => 'main-menu:' . $parent_link
->getPluginId(),
], 'Save');
$node = $this
->drupalGetNodeByTitle('Node menu adding menu later test', TRUE);
$this
->assertEquals('This is a /admin/config token to the menu link parent', $node->body->value);
$link = menu_ui_get_menu_link_defaults($node);
$this
->assertNotEmpty($link['entity_id']);
$query = \Drupal::entityQuery('menu_link_content')
->condition('link.uri', 'entity:node/' . $node
->id())
->sort('id', 'ASC')
->range(0, 1);
$result = $query
->execute();
$this
->assertNotEmpty($result);
$node_title = $this
->randomMachineName();
$edit = [
'title[0][value]' => $node_title,
'menu[enabled]' => 1,
'menu[title]' => 'menu link provided by node',
];
$this
->drupalPostForm('node/add/page', $edit, 'Save');
$this
->assertText('page ' . $node_title . ' has been created');
$node = $this
->drupalGetNodeByTitle($node_title);
$menu_ui_link1 = MenuLinkContent::create([
'link' => [
'uri' => 'entity:node/' . $node
->id(),
],
'title' => 'menu link 1 provided by menu ui',
'menu_name' => 'main-menu',
]);
$menu_ui_link1
->save();
$menu_ui_link2 = MenuLinkContent::create([
'link' => [
'uri' => 'entity:node/' . $node
->id(),
],
'title' => 'menu link 2 provided by menu ui',
'menu_name' => 'main-menu',
]);
$menu_ui_link2
->save();
$tokens = [
'menu-link' => 'menu link provided by node',
'menu-link:title' => 'menu link provided by node',
];
$this
->assertTokens('node', [
'node' => $node,
], $tokens);
}
function testMultilingualMenu() {
$this
->drupalPlaceBlock('system_menu_block:main');
$language = ConfigurableLanguage::create([
'id' => 'de',
'label' => 'German',
]);
$language
->save();
$node_type = NodeType::create([
'type' => 'article',
]);
$node_type
->save();
$permissions = [
'access administration pages',
'administer content translation',
'administer content types',
'administer languages',
'create content translations',
'create article content',
'edit any article content',
'translate any entity',
'administer menu',
];
$this
->drupalLogin($this
->drupalCreateUser($permissions));
$this
->drupalGet('admin/config/regional/content-language');
$edit = [
'entity_types[node]' => TRUE,
'entity_types[menu_link_content]' => TRUE,
'settings[node][article][translatable]' => TRUE,
'settings[node][article][fields][title]' => TRUE,
'settings[menu_link_content][menu_link_content][translatable]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, 'Save configuration');
$this
->assertText('Settings successfully updated.');
$this
->drupalGet('/node/add/article');
$edit = [
'title[0][value]' => 'English test node with menu',
'menu[enabled]' => TRUE,
'menu[title]' => 'English menu title',
];
$this
->drupalPostForm('/node/add/article', $edit, 'Save');
$this
->assertText('English test node with menu has been created.');
$this
->drupalGet('node/1/translations');
$this
->clickLink('Add');
$edit = [
'title[0][value]' => 'German test node with menu',
'menu[enabled]' => TRUE,
'menu[title]' => 'German menu title',
];
$this
->drupalPostForm(NULL, $edit, 'Save (this translation)');
$this
->assertText('German test node with menu has been updated.');
$this
->drupalGet('node/1');
$this
->assertSession()
->linkExists('English menu title');
$this
->drupalGet('de/node/1');
$this
->assertSession()
->linkExists('German menu title');
$node = Node::load(1);
$this
->assertTokens('node', [
'node' => $node,
], [
'menu-link' => 'English menu title',
]);
$this
->assertTokens('node', [
'node' => $node,
], [
'menu-link' => 'German menu title',
'menu-link:title' => 'German menu title',
], [
'langcode' => 'de',
]);
$url = $node
->toUrl();
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$links = $menu_link_manager
->loadLinksByRoute($url
->getRouteName(), $url
->getRouteParameters());
$link = reset($links);
$base_options = [
'provider' => 'menu_test',
'menu_name' => 'menu_test',
];
$child_1 = $base_options + [
'title' => 'child_1 title EN',
'link' => [
'uri' => 'internal:/menu-test/hierarchy/parent/child_1',
],
'parent' => $link
->getPluginId(),
'langcode' => 'en',
];
$child_1 = MenuLinkContent::create($child_1);
$child_1
->save();
$child_1
->addTranslation('de', [
'title' => 'child_1 title DE',
] + $child_1
->toArray());
$child_1
->save();
$this
->assertTokens('menu-link', [
'menu-link' => $child_1,
], [
'title' => 'child_1 title EN',
'parents' => 'English menu title',
'root' => 'English menu title',
]);
$this
->assertTokens('menu-link', [
'menu-link' => $child_1,
], [
'title' => 'child_1 title DE',
'parents' => 'German menu title',
'root' => 'German menu title',
], [
'langcode' => 'de',
]);
}
public function testMenuLinkParentsToken() {
Menu::create([
'id' => 'menu_test',
'label' => 'Test menu',
])
->save();
$base_options = [
'provider' => 'menu_test',
'menu_name' => 'menu_test',
];
$parent = $base_options + [
'title' => 'parent title',
'link' => [
'uri' => 'internal:/menu-test/hierarchy/parent',
],
];
$parent = MenuLinkContent::create($parent);
$parent
->save();
$child_1 = $base_options + [
'title' => 'child_1 title',
'link' => [
'uri' => 'internal:/menu-test/hierarchy/parent/child_1',
],
'parent' => $parent
->getPluginId(),
];
$child_1 = MenuLinkContent::create($child_1);
$child_1
->save();
$child_1_1 = $base_options + [
'title' => 'child_1_1 title',
'link' => [
'uri' => 'internal:/menu-test/hierarchy/parent/child_1/child_1_1',
],
'parent' => $child_1
->getPluginId(),
];
$child_1_1 = MenuLinkContent::create($child_1_1);
$child_1_1
->save();
$this
->assertTokens('menu-link', [
'menu-link' => $child_1_1,
], [
'parents' => 'parent title, child_1 title',
]);
$child_1_1->parent->value = $parent
->getPluginId();
$child_1_1
->save();
$this
->assertTokens('menu-link', [
'menu-link' => $child_1_1,
], [
'parents' => 'parent title',
]);
$child_1_1->parent->value = '';
$child_1_1
->save();
$this
->assertNoTokens('menu-link', [
'menu-link' => $child_1_1,
], [
'parents',
]);
}
public function testPreviewMenuLink() {
$this
->drupalCreateContentType([
'type' => 'article',
]);
$permissions = [
'access administration pages',
'administer content types',
'create article content',
'edit any article content',
'administer menu',
];
$this
->drupalLogin($this
->drupalCreateUser($permissions));
$this
->drupalGet('/node/add/article');
$edit = [
'title[0][value]' => 'English test node with menu',
'menu[enabled]' => TRUE,
'menu[title]' => 'English menu title',
];
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Preview');
$menu_links = \Drupal::entityTypeManager()
->getStorage('menu_link_content')
->loadByProperties([
'menu_name' => 'main',
]);
$this
->assertEmpty($menu_links);
}
}