Update to Drupal 8.1.1. For more information, see https://www.drupal.org/node/2718713

This commit is contained in:
Pantheon Automation 2016-05-04 14:35:41 -07:00 committed by Greg Anderson
parent c0a0d5a94c
commit 9eae24d844
669 changed files with 3873 additions and 1553 deletions

View file

@ -0,0 +1,143 @@
<?php
namespace Drupal\Tests\menu_link_content\Kernel;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
/**
* Ensures that rendered menu links bubble the necessary bubbleable metadata
* for outbound path/route processing.
*
* @group menu_link_content
*/
class MenuLinkContentCacheabilityBubblingTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['menu_link_content', 'system', 'link', 'outbound_processing_test', 'url_alter_test', 'user'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('menu_link_content');
$this->installEntitySchema('user');
// Ensure that the weight of module_link_content is higher than system.
// @see menu_link_content_install()
module_set_weight('menu_link_content', 1);
}
/**
* Tests bubbleable metadata of menu links' outbound route/path processing.
*/
public function testOutboundPathAndRouteProcessing() {
\Drupal::service('router.builder')->rebuild();
$request_stack = \Drupal::requestStack();
/** @var \Symfony\Component\Routing\RequestContext $request_context */
$request_context = \Drupal::service('router.request_context');
$request = Request::create('/');
$request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<front>');
$request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/'));
$request_stack->push($request);
$request_context->fromRequest($request);
$menu_tree = \Drupal::menuTree();
$renderer = \Drupal::service('renderer');
$default_menu_cacheability = (new BubbleableMetadata())
->setCacheMaxAge(Cache::PERMANENT)
->setCacheTags(['config:system.menu.tools'])
->setCacheContexts(['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions']);
User::create(['uid' => 1, 'name' => $this->randomString()])->save();
User::create(['uid' => 2, 'name' => $this->randomString()])->save();
// Five test cases, four asserting one outbound path/route processor, and
// together covering one of each:
// - no cacheability metadata,
// - a cache context,
// - a cache tag,
// - a cache max-age.
// Plus an additional test case to verify that multiple links adding
// cacheability metadata of the same type is working (two links with cache
// tags).
$test_cases = [
// \Drupal\Core\RouteProcessor\RouteProcessorCurrent: 'route' cache context.
[
'uri' => 'route:<current>',
'cacheability' => (new BubbleableMetadata())->setCacheContexts(['route']),
],
// \Drupal\Core\Access\RouteProcessorCsrf: placeholder.
[
'uri' => 'route:outbound_processing_test.route.csrf',
'cacheability' => (new BubbleableMetadata())->setCacheContexts(['session'])->setAttachments(['placeholders' => []]),
],
// \Drupal\Core\PathProcessor\PathProcessorFront: permanently cacheable.
[
'uri' => 'internal:/',
'cacheability' => (new BubbleableMetadata()),
],
// \Drupal\url_alter_test\PathProcessorTest: user entity's cache tags.
[
'uri' => 'internal:/user/1',
'cacheability' => (new BubbleableMetadata())->setCacheTags(User::load(1)->getCacheTags()),
],
[
'uri' => 'internal:/user/2',
'cacheability' => (new BubbleableMetadata())->setCacheTags(User::load(2)->getCacheTags()),
],
];
// Test each expectation individually.
foreach ($test_cases as $expectation) {
$menu_link_content = MenuLinkContent::create([
'link' => ['uri' => $expectation['uri']],
'menu_name' => 'tools',
]);
$menu_link_content->save();
$tree = $menu_tree->load('tools', new MenuTreeParameters());
$build = $menu_tree->build($tree);
$renderer->renderRoot($build);
$expected_cacheability = $default_menu_cacheability->merge($expectation['cacheability']);
$this->assertEqual($expected_cacheability, BubbleableMetadata::createFromRenderArray($build));
$menu_link_content->delete();
}
// Now test them all together in one menu: the rendered menu's cacheability
// metadata should be the combination of the cacheability of all links, and
// thus of all tested outbound path & route processors.
$expected_cacheability = new BubbleableMetadata();
foreach ($test_cases as $expectation) {
$menu_link_content = MenuLinkContent::create([
'link' => ['uri' => $expectation['uri']],
'menu_name' => 'tools',
]);
$menu_link_content->save();
$expected_cacheability = $expected_cacheability->merge($expectation['cacheability']);
}
$tree = $menu_tree->load('tools', new MenuTreeParameters());
$build = $menu_tree->build($tree);
$renderer->renderRoot($build);
$expected_cacheability = $expected_cacheability->merge($default_menu_cacheability);
$this->assertEqual($expected_cacheability, BubbleableMetadata::createFromRenderArray($build));
}
}

View file

@ -0,0 +1,104 @@
<?php
namespace Drupal\Tests\menu_link_content\Kernel;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\Routing\Route;
/**
* Tests the menu link content deriver.
*
* @group menu_link_content
*/
class MenuLinkContentDeriverTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['menu_link_content', 'link', 'system', 'menu_link_content_dynamic_route'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('menu_link_content');
}
/**
* Tests the rediscovering.
*/
public function testRediscover() {
\Drupal::state()->set('menu_link_content_dynamic_route.routes', [
'route_name_1' => new Route('/example-path'),
]);
\Drupal::service('router.builder')->rebuild();
// Set up a custom menu link pointing to a specific path.
$parent = MenuLinkContent::create([
'title' => '<script>alert("Welcome to the discovered jungle!")</script>',
'link' => [['uri' => 'internal:/example-path']],
'menu_name' => 'tools',
]);
$parent->save();
$menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual(1, count($menu_tree));
/** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
$tree_element = reset($menu_tree);
$this->assertEqual('route_name_1', $tree_element->link->getRouteName());
// Change the underlying route and trigger the rediscovering.
\Drupal::state()->set('menu_link_content_dynamic_route.routes', [
'route_name_2' => new Route('/example-path'),
]);
\Drupal::service('router.builder')->rebuild();
// Ensure that the new route name / parameters are captured by the tree.
$menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual(1, count($menu_tree));
/** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
$tree_element = reset($menu_tree);
$this->assertEqual('route_name_2', $tree_element->link->getRouteName());
$title = $tree_element->link->getTitle();
$this->assertFalse($title instanceof TranslatableMarkup);
$this->assertIdentical('<script>alert("Welcome to the discovered jungle!")</script>', $title);
// Create a hierarchy.
\Drupal::state()->set('menu_link_content_dynamic_route.routes', [
'route_name_1' => new Route('/example-path'),
'route_name_2' => new Route('/example-path/child'),
]);
$child = MenuLinkContent::create([
'title' => 'Child',
'link' => [['uri' => 'entity:/example-path/child']],
'menu_name' => 'tools',
'parent' => 'menu_link_content:' . $parent->uuid(),
]);
$child->save();
$parent->set('link', [['uri' => 'entity:/example-path']]);
$parent->save();
$menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual(1, count($menu_tree));
/** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
$tree_element = reset($menu_tree);
$this->assertTrue($tree_element->hasChildren);
$this->assertEqual(1, count($tree_element->subtree));
// Edit child element link to use 'internal' instead of 'entity'.
$child->set('link', [['uri' => 'internal:/example-path/child']]);
$child->save();
\Drupal::service('plugin.manager.menu.link')->rebuild();
$menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual(1, count($menu_tree));
/** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
$tree_element = reset($menu_tree);
$this->assertTrue($tree_element->hasChildren);
$this->assertEqual(1, count($tree_element->subtree));
}
}

View file

@ -0,0 +1,84 @@
<?php
namespace Drupal\Tests\menu_link_content\Kernel;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\KernelTests\KernelTestBase;
/**
* Ensures that the menu tree adapts to path alias changes.
*
* @group menu_link_content
*/
class PathAliasMenuLinkContentTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['menu_link_content', 'system', 'link', 'test_page_test'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('menu_link_content');
// Ensure that the weight of module_link_content is higher than system.
// @see menu_link_content_install()
module_set_weight('menu_link_content', 1);
}
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
parent::register($container);
$definition = $container->getDefinition('path_processor_alias');
$definition
->addTag('path_processor_inbound', ['priority' => 100]);
}
/**
* Tests the path aliasing changing.
*/
public function testPathAliasChange() {
\Drupal::service('router.builder')->rebuild();
/** @var \Drupal\Core\Path\AliasStorageInterface $path_alias_storage */
$path_alias_storage = \Drupal::service('path.alias_storage');
$alias = $path_alias_storage->save('/test-page', '/my-blog');
$pid = $alias['pid'];
$menu_link_content = MenuLinkContent::create([
'title' => 'Menu title',
'link' => ['uri' => 'internal:/my-blog'],
'menu_name' => 'tools',
]);
$menu_link_content->save();
$tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual('test_page_test.test_page', $tree[$menu_link_content->getPluginId()]->link->getPluginDefinition()['route_name']);
// Saving an alias should clear the alias manager cache.
$path_alias_storage->save('/test-render-title', '/my-blog', LanguageInterface::LANGCODE_NOT_SPECIFIED, $pid);
$tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual('test_page_test.render_title', $tree[$menu_link_content->getPluginId()]->link->getPluginDefinition()['route_name']);
// Delete the alias.
$path_alias_storage->delete(['pid' => $pid]);
$tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertTrue(isset($tree[$menu_link_content->getPluginId()]));
$this->assertEqual('', $tree[$menu_link_content->getPluginId()]->link->getRouteName());
// Verify the plugin now references a path that does not match any route.
$this->assertEqual('base:my-blog', $tree[$menu_link_content->getPluginId()]->link->getUrlObject()->getUri());
}
}