View source
<?php
namespace Drupal\Tests\system\Unit\Breadcrumbs;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Link;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\system\PathBasedBreadcrumbBuilder;
use Drupal\Tests\UnitTestCase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
class PathBasedBreadcrumbBuilderTest extends UnitTestCase {
protected $builder;
protected $titleResolver;
protected $accessManager;
protected $requestMatcher;
protected $context;
protected $currentUser;
protected $pathProcessor;
protected $currentPath;
protected function setUp() {
parent::setUp();
$this->requestMatcher = $this
->getMock('\\Symfony\\Component\\Routing\\Matcher\\RequestMatcherInterface');
$config_factory = $this
->getConfigFactoryStub(array(
'system.site' => array(
'front' => 'test_frontpage',
),
));
$this->pathProcessor = $this
->getMock('\\Drupal\\Core\\PathProcessor\\InboundPathProcessorInterface');
$this->context = $this
->getMock('\\Drupal\\Core\\Routing\\RequestContext');
$this->accessManager = $this
->getMock('\\Drupal\\Core\\Access\\AccessManagerInterface');
$this->titleResolver = $this
->getMock('\\Drupal\\Core\\Controller\\TitleResolverInterface');
$this->currentUser = $this
->getMock('Drupal\\Core\\Session\\AccountInterface');
$this->currentPath = $this
->getMockBuilder('Drupal\\Core\\Path\\CurrentPathStack')
->disableOriginalConstructor()
->getMock();
$this->builder = new TestPathBasedBreadcrumbBuilder($this->context, $this->accessManager, $this->requestMatcher, $this->pathProcessor, $config_factory, $this->titleResolver, $this->currentUser, $this->currentPath);
$this->builder
->setStringTranslation($this
->getStringTranslationStub());
$cache_contexts_manager = $this
->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$cache_contexts_manager
->method('assertValidTokens')
->willReturn(TRUE);
$cache_contexts_manager
->expects($this
->any())
->method('validate_tokens');
$container = new Container();
$container
->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}
public function testBuildOnFrontpage() {
$this->context
->expects($this
->once())
->method('getPathInfo')
->will($this
->returnValue('/'));
$breadcrumb = $this->builder
->build($this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
$this
->assertEquals([], $breadcrumb
->getLinks());
$this
->assertEquals([
'url.path',
], $breadcrumb
->getCacheContexts());
$this
->assertEquals([], $breadcrumb
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $breadcrumb
->getCacheMaxAge());
}
public function testBuildWithOnePathElement() {
$this->context
->expects($this
->once())
->method('getPathInfo')
->will($this
->returnValue('/example'));
$breadcrumb = $this->builder
->build($this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
$this
->assertEquals([
0 => new Link('Home', new Url('<front>')),
], $breadcrumb
->getLinks());
$this
->assertEquals([
'url.path',
], $breadcrumb
->getCacheContexts());
$this
->assertEquals([], $breadcrumb
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $breadcrumb
->getCacheMaxAge());
}
public function testBuildWithTwoPathElements() {
$this->context
->expects($this
->once())
->method('getPathInfo')
->will($this
->returnValue('/example/baz'));
$this
->setupStubPathProcessor();
$route_1 = new Route('/example');
$this->requestMatcher
->expects($this
->exactly(1))
->method('matchRequest')
->will($this
->returnCallback(function (Request $request) use ($route_1) {
if ($request
->getPathInfo() == '/example') {
return array(
RouteObjectInterface::ROUTE_NAME => 'example',
RouteObjectInterface::ROUTE_OBJECT => $route_1,
'_raw_variables' => new ParameterBag(array()),
);
}
}));
$this
->setupAccessManagerToAllow();
$breadcrumb = $this->builder
->build($this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
$this
->assertEquals([
0 => new Link('Home', new Url('<front>')),
1 => new Link('Example', new Url('example')),
], $breadcrumb
->getLinks());
$this
->assertEquals([
'url.path',
'user.permissions',
], $breadcrumb
->getCacheContexts());
$this
->assertEquals([], $breadcrumb
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $breadcrumb
->getCacheMaxAge());
}
public function testBuildWithThreePathElements() {
$this->context
->expects($this
->once())
->method('getPathInfo')
->will($this
->returnValue('/example/bar/baz'));
$this
->setupStubPathProcessor();
$route_1 = new Route('/example/bar');
$route_2 = new Route('/example');
$this->requestMatcher
->expects($this
->exactly(2))
->method('matchRequest')
->will($this
->returnCallback(function (Request $request) use ($route_1, $route_2) {
if ($request
->getPathInfo() == '/example/bar') {
return array(
RouteObjectInterface::ROUTE_NAME => 'example_bar',
RouteObjectInterface::ROUTE_OBJECT => $route_1,
'_raw_variables' => new ParameterBag(array()),
);
}
elseif ($request
->getPathInfo() == '/example') {
return array(
RouteObjectInterface::ROUTE_NAME => 'example',
RouteObjectInterface::ROUTE_OBJECT => $route_2,
'_raw_variables' => new ParameterBag(array()),
);
}
}));
$this->accessManager
->expects($this
->any())
->method('check')
->willReturnOnConsecutiveCalls(AccessResult::allowed()
->cachePerPermissions(), AccessResult::allowed()
->addCacheContexts([
'bar',
])
->addCacheTags([
'example',
]));
$breadcrumb = $this->builder
->build($this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
$this
->assertEquals([
new Link('Home', new Url('<front>')),
new Link('Example', new Url('example')),
new Link('Bar', new Url('example_bar')),
], $breadcrumb
->getLinks());
$this
->assertEquals([
'bar',
'url.path',
'user.permissions',
], $breadcrumb
->getCacheContexts());
$this
->assertEquals([
'example',
], $breadcrumb
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $breadcrumb
->getCacheMaxAge());
}
public function testBuildWithException($exception_class, $exception_argument) {
$this->context
->expects($this
->once())
->method('getPathInfo')
->will($this
->returnValue('/example/bar'));
$this
->setupStubPathProcessor();
$this->requestMatcher
->expects($this
->any())
->method('matchRequest')
->will($this
->throwException(new $exception_class($exception_argument)));
$breadcrumb = $this->builder
->build($this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
$this
->assertEquals([
0 => new Link('Home', new Url('<front>')),
], $breadcrumb
->getLinks());
$this
->assertEquals([
'url.path',
], $breadcrumb
->getCacheContexts());
$this
->assertEquals([], $breadcrumb
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $breadcrumb
->getCacheMaxAge());
}
public function providerTestBuildWithException() {
return array(
array(
'Drupal\\Core\\ParamConverter\\ParamNotConvertedException',
'',
),
array(
'Symfony\\Component\\Routing\\Exception\\MethodNotAllowedException',
array(),
),
array(
'Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException',
'',
),
);
}
public function testBuildWithNonProcessedPath() {
$this->context
->expects($this
->once())
->method('getPathInfo')
->will($this
->returnValue('/example/bar'));
$this->pathProcessor
->expects($this
->once())
->method('processInbound')
->will($this
->returnValue(FALSE));
$this->requestMatcher
->expects($this
->any())
->method('matchRequest')
->will($this
->returnValue(array()));
$breadcrumb = $this->builder
->build($this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
$this
->assertEquals([
0 => new Link('Home', new Url('<front>')),
], $breadcrumb
->getLinks());
$this
->assertEquals([
'url.path',
], $breadcrumb
->getCacheContexts());
$this
->assertEquals([], $breadcrumb
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $breadcrumb
->getCacheMaxAge());
}
public function testApplies() {
$this
->assertTrue($this->builder
->applies($this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface')));
}
public function testBuildWithUserPath() {
$this->context
->expects($this
->once())
->method('getPathInfo')
->will($this
->returnValue('/user/1/edit'));
$this
->setupStubPathProcessor();
$route_1 = new Route('/user/1');
$this->requestMatcher
->expects($this
->exactly(1))
->method('matchRequest')
->will($this
->returnCallback(function (Request $request) use ($route_1) {
if ($request
->getPathInfo() == '/user/1') {
return array(
RouteObjectInterface::ROUTE_NAME => 'user_page',
RouteObjectInterface::ROUTE_OBJECT => $route_1,
'_raw_variables' => new ParameterBag(array()),
);
}
}));
$this
->setupAccessManagerToAllow();
$this->titleResolver
->expects($this
->once())
->method('getTitle')
->with($this
->anything(), $route_1)
->will($this
->returnValue('Admin'));
$breadcrumb = $this->builder
->build($this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
$this
->assertEquals([
0 => new Link('Home', new Url('<front>')),
1 => new Link('Admin', new Url('user_page')),
], $breadcrumb
->getLinks());
$this
->assertEquals([
'url.path',
'user.permissions',
], $breadcrumb
->getCacheContexts());
$this
->assertEquals([], $breadcrumb
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $breadcrumb
->getCacheMaxAge());
}
public function setupAccessManagerToAllow() {
$this->accessManager
->expects($this
->any())
->method('check')
->willReturn((new AccessResultAllowed())
->cachePerPermissions());
}
protected function setupStubPathProcessor() {
$this->pathProcessor
->expects($this
->any())
->method('processInbound')
->will($this
->returnArgument(0));
}
}
class TestPathBasedBreadcrumbBuilder extends PathBasedBreadcrumbBuilder {
public function setStringTranslation(TranslationInterface $string_translation) {
$this->stringTranslation = $string_translation;
}
public function setLinkGenerator(LinkGeneratorInterface $link_generator) {
$this->linkGenerator = $link_generator;
}
}