5 * Contains \Drupal\Tests\system\Unit\Breadcrumbs\PathBasedBreadcrumbBuilderTest.
8 namespace Drupal\Tests\system\Unit\Breadcrumbs;
10 use Drupal\Core\Access\AccessResult;
11 use Drupal\Core\Cache\Cache;
13 use Drupal\Core\Access\AccessResultAllowed;
14 use Drupal\Core\Path\PathMatcherInterface;
15 use Drupal\Core\StringTranslation\TranslationInterface;
17 use Drupal\Core\Utility\LinkGeneratorInterface;
18 use Drupal\system\PathBasedBreadcrumbBuilder;
19 use Drupal\Tests\UnitTestCase;
20 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
21 use Symfony\Component\DependencyInjection\Container;
22 use Symfony\Component\HttpFoundation\ParameterBag;
23 use Symfony\Component\HttpFoundation\Request;
24 use Symfony\Component\Routing\Route;
27 * @coversDefaultClass \Drupal\system\PathBasedBreadcrumbBuilder
30 class PathBasedBreadcrumbBuilderTest extends UnitTestCase {
33 * The path based breadcrumb builder object to test.
35 * @var \Drupal\system\PathBasedBreadcrumbBuilder
40 * The mocked title resolver.
42 * @var \Drupal\Core\Controller\TitleResolverInterface|\PHPUnit_Framework_MockObject_MockObject
44 protected $titleResolver;
47 * The mocked access manager.
49 * @var \Drupal\Core\Access\AccessManagerInterface|\PHPUnit_Framework_MockObject_MockObject
51 protected $accessManager;
54 * The request matching mock object.
56 * @var \Symfony\Component\Routing\Matcher\RequestMatcherInterface|\PHPUnit_Framework_MockObject_MockObject
58 protected $requestMatcher;
61 * The mocked route request context.
63 * @var \Drupal\Core\Routing\RequestContext|\PHPUnit_Framework_MockObject_MockObject
68 * The mocked current user.
70 * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
72 protected $currentUser;
75 * The mocked path processor.
77 * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
79 protected $pathProcessor;
82 * The mocked current path.
84 * @var \Drupal\Core\Path\CurrentPathStack|\PHPUnit_Framework_MockObject_MockObject
86 protected $currentPath;
89 * The mocked path matcher service.
91 * @var \Drupal\Core\Path\PathMatcherInterface|\PHPUnit_Framework_MockObject_MockObject
93 protected $pathMatcher;
98 * @covers ::__construct
100 protected function setUp() {
103 $this->requestMatcher = $this->getMock('\Symfony\Component\Routing\Matcher\RequestMatcherInterface');
105 $config_factory = $this->getConfigFactoryStub(['system.site' => ['front' => 'test_frontpage']]);
107 $this->pathProcessor = $this->getMock('\Drupal\Core\PathProcessor\InboundPathProcessorInterface');
108 $this->context = $this->getMock('\Drupal\Core\Routing\RequestContext');
110 $this->accessManager = $this->getMock('\Drupal\Core\Access\AccessManagerInterface');
111 $this->titleResolver = $this->getMock('\Drupal\Core\Controller\TitleResolverInterface');
112 $this->currentUser = $this->getMock('Drupal\Core\Session\AccountInterface');
113 $this->currentPath = $this->getMockBuilder('Drupal\Core\Path\CurrentPathStack')
114 ->disableOriginalConstructor()
117 $this->pathMatcher = $this->getMock(PathMatcherInterface::class);
119 $this->builder = new TestPathBasedBreadcrumbBuilder(
121 $this->accessManager,
122 $this->requestMatcher,
123 $this->pathProcessor,
125 $this->titleResolver,
131 $this->builder->setStringTranslation($this->getStringTranslationStub());
133 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
134 ->disableOriginalConstructor()
136 $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
137 $container = new Container();
138 $container->set('cache_contexts_manager', $cache_contexts_manager);
139 \Drupal::setContainer($container);
143 * Tests the build method on the frontpage.
147 public function testBuildOnFrontpage() {
148 $this->pathMatcher->expects($this->once())
149 ->method('isFrontPage')
152 $breadcrumb = $this->builder->build($this->getMock('Drupal\Core\Routing\RouteMatchInterface'));
153 $this->assertEquals([], $breadcrumb->getLinks());
154 $this->assertEquals(['url.path.parent'], $breadcrumb->getCacheContexts());
155 $this->assertEquals([], $breadcrumb->getCacheTags());
156 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
160 * Tests the build method with one path element.
164 public function testBuildWithOnePathElement() {
165 $this->context->expects($this->once())
166 ->method('getPathInfo')
167 ->will($this->returnValue('/example'));
169 $breadcrumb = $this->builder->build($this->getMock('Drupal\Core\Routing\RouteMatchInterface'));
170 $this->assertEquals([0 => new Link('Home', new Url('<front>'))], $breadcrumb->getLinks());
171 $this->assertEquals(['url.path.parent'], $breadcrumb->getCacheContexts());
172 $this->assertEquals([], $breadcrumb->getCacheTags());
173 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
177 * Tests the build method with two path elements.
180 * @covers ::getRequestForPath
182 public function testBuildWithTwoPathElements() {
183 $this->context->expects($this->once())
184 ->method('getPathInfo')
185 ->will($this->returnValue('/example/baz'));
186 $this->setupStubPathProcessor();
188 $route_1 = new Route('/example');
190 $this->requestMatcher->expects($this->exactly(1))
191 ->method('matchRequest')
192 ->will($this->returnCallback(function (Request $request) use ($route_1) {
193 if ($request->getPathInfo() == '/example') {
195 RouteObjectInterface::ROUTE_NAME => 'example',
196 RouteObjectInterface::ROUTE_OBJECT => $route_1,
197 '_raw_variables' => new ParameterBag([]),
202 $this->setupAccessManagerToAllow();
204 $breadcrumb = $this->builder->build($this->getMock('Drupal\Core\Routing\RouteMatchInterface'));
205 $this->assertEquals([0 => new Link('Home', new Url('<front>')), 1 => new Link('Example', new Url('example'))], $breadcrumb->getLinks());
206 $this->assertEquals(['url.path.parent', 'user.permissions'], $breadcrumb->getCacheContexts());
207 $this->assertEquals([], $breadcrumb->getCacheTags());
208 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
212 * Tests the build method with three path elements.
215 * @covers ::getRequestForPath
217 public function testBuildWithThreePathElements() {
218 $this->context->expects($this->once())
219 ->method('getPathInfo')
220 ->will($this->returnValue('/example/bar/baz'));
221 $this->setupStubPathProcessor();
223 $route_1 = new Route('/example/bar');
224 $route_2 = new Route('/example');
226 $this->requestMatcher->expects($this->exactly(2))
227 ->method('matchRequest')
228 ->will($this->returnCallback(function (Request $request) use ($route_1, $route_2) {
229 if ($request->getPathInfo() == '/example/bar') {
231 RouteObjectInterface::ROUTE_NAME => 'example_bar',
232 RouteObjectInterface::ROUTE_OBJECT => $route_1,
233 '_raw_variables' => new ParameterBag([]),
236 elseif ($request->getPathInfo() == '/example') {
238 RouteObjectInterface::ROUTE_NAME => 'example',
239 RouteObjectInterface::ROUTE_OBJECT => $route_2,
240 '_raw_variables' => new ParameterBag([]),
245 $this->accessManager->expects($this->any())
247 ->willReturnOnConsecutiveCalls(
248 AccessResult::allowed()->cachePerPermissions(),
249 AccessResult::allowed()->addCacheContexts(['bar'])->addCacheTags(['example'])
251 $breadcrumb = $this->builder->build($this->getMock('Drupal\Core\Routing\RouteMatchInterface'));
252 $this->assertEquals([
253 new Link('Home', new Url('<front>')),
254 new Link('Example', new Url('example')),
255 new Link('Bar', new Url('example_bar')),
256 ], $breadcrumb->getLinks());
257 $this->assertEquals(['bar', 'url.path.parent', 'user.permissions'], $breadcrumb->getCacheContexts());
258 $this->assertEquals(['example'], $breadcrumb->getCacheTags());
259 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
263 * Tests that exceptions during request matching are caught.
266 * @covers ::getRequestForPath
268 * @dataProvider providerTestBuildWithException
270 public function testBuildWithException($exception_class, $exception_argument) {
271 $this->context->expects($this->once())
272 ->method('getPathInfo')
273 ->will($this->returnValue('/example/bar'));
274 $this->setupStubPathProcessor();
276 $this->requestMatcher->expects($this->any())
277 ->method('matchRequest')
278 ->will($this->throwException(new $exception_class($exception_argument)));
280 $breadcrumb = $this->builder->build($this->getMock('Drupal\Core\Routing\RouteMatchInterface'));
282 // No path matched, though at least the frontpage is displayed.
283 $this->assertEquals([0 => new Link('Home', new Url('<front>'))], $breadcrumb->getLinks());
284 $this->assertEquals(['url.path.parent'], $breadcrumb->getCacheContexts());
285 $this->assertEquals([], $breadcrumb->getCacheTags());
286 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
290 * Provides exception types for testBuildWithException.
293 * The list of exception test cases.
295 * @see \Drupal\Tests\system\Unit\Breadcrumbs\PathBasedBreadcrumbBuilderTest::testBuildWithException()
297 public function providerTestBuildWithException() {
299 ['Drupal\Core\ParamConverter\ParamNotConvertedException', ''],
300 ['Symfony\Component\Routing\Exception\MethodNotAllowedException', []],
301 ['Symfony\Component\Routing\Exception\ResourceNotFoundException', ''],
306 * Tests the build method with a non processed path.
309 * @covers ::getRequestForPath
311 public function testBuildWithNonProcessedPath() {
312 $this->context->expects($this->once())
313 ->method('getPathInfo')
314 ->will($this->returnValue('/example/bar'));
316 $this->pathProcessor->expects($this->once())
317 ->method('processInbound')
318 ->will($this->returnValue(FALSE));
320 $this->requestMatcher->expects($this->any())
321 ->method('matchRequest')
322 ->will($this->returnValue([]));
324 $breadcrumb = $this->builder->build($this->getMock('Drupal\Core\Routing\RouteMatchInterface'));
326 // No path matched, though at least the frontpage is displayed.
327 $this->assertEquals([0 => new Link('Home', new Url('<front>'))], $breadcrumb->getLinks());
328 $this->assertEquals(['url.path.parent'], $breadcrumb->getCacheContexts());
329 $this->assertEquals([], $breadcrumb->getCacheTags());
330 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
334 * Tests the applied method.
338 public function testApplies() {
339 $this->assertTrue($this->builder->applies($this->getMock('Drupal\Core\Routing\RouteMatchInterface')));
343 * Tests the breadcrumb for a user path.
346 * @covers ::getRequestForPath
348 public function testBuildWithUserPath() {
349 $this->context->expects($this->once())
350 ->method('getPathInfo')
351 ->will($this->returnValue('/user/1/edit'));
352 $this->setupStubPathProcessor();
354 $route_1 = new Route('/user/1');
356 $this->requestMatcher->expects($this->exactly(1))
357 ->method('matchRequest')
358 ->will($this->returnCallback(function (Request $request) use ($route_1) {
359 if ($request->getPathInfo() == '/user/1') {
361 RouteObjectInterface::ROUTE_NAME => 'user_page',
362 RouteObjectInterface::ROUTE_OBJECT => $route_1,
363 '_raw_variables' => new ParameterBag([]),
368 $this->setupAccessManagerToAllow();
369 $this->titleResolver->expects($this->once())
371 ->with($this->anything(), $route_1)
372 ->will($this->returnValue('Admin'));
374 $breadcrumb = $this->builder->build($this->getMock('Drupal\Core\Routing\RouteMatchInterface'));
375 $this->assertEquals([0 => new Link('Home', new Url('<front>')), 1 => new Link('Admin', new Url('user_page'))], $breadcrumb->getLinks());
376 $this->assertEquals(['url.path.parent', 'user.permissions'], $breadcrumb->getCacheContexts());
377 $this->assertEquals([], $breadcrumb->getCacheTags());
378 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
382 * Setup the access manager to always allow access to routes.
384 public function setupAccessManagerToAllow() {
385 $this->accessManager->expects($this->any())
387 ->willReturn((new AccessResultAllowed())->cachePerPermissions());
390 protected function setupStubPathProcessor() {
391 $this->pathProcessor->expects($this->any())
392 ->method('processInbound')
393 ->will($this->returnArgument(0));
399 * Helper class for testing purposes only.
401 class TestPathBasedBreadcrumbBuilder extends PathBasedBreadcrumbBuilder {
403 public function setStringTranslation(TranslationInterface $string_translation) {
404 $this->stringTranslation = $string_translation;
407 public function setLinkGenerator(LinkGeneratorInterface $link_generator) {
408 $this->linkGenerator = $link_generator;