3 namespace Drupal\Tests\Core\Breadcrumb;
5 use Drupal\Core\Breadcrumb\Breadcrumb;
6 use Drupal\Core\Breadcrumb\BreadcrumbManager;
7 use Drupal\Core\Cache\Cache;
8 use Drupal\Core\Cache\Context\CacheContextsManager;
9 use Drupal\Core\DependencyInjection\ContainerBuilder;
10 use Drupal\Tests\UnitTestCase;
13 * @coversDefaultClass \Drupal\Core\Breadcrumb\BreadcrumbManager
16 class BreadcrumbManagerTest extends UnitTestCase {
19 * The dependency injection container.
21 * @var \Symfony\Component\DependencyInjection\ContainerBuilder
26 * The breadcrumb object.
28 * @var \Drupal\Core\Breadcrumb\Breadcrumb
30 protected $breadcrumb;
33 * The tested breadcrumb manager.
35 * @var \Drupal\Core\Breadcrumb\BreadcrumbManager
37 protected $breadcrumbManager;
40 * The mocked module handler.
42 * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
44 protected $moduleHandler;
49 protected function setUp() {
50 $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
51 $this->breadcrumbManager = new BreadcrumbManager($this->moduleHandler);
52 $this->breadcrumb = new Breadcrumb();
54 $this->container = new ContainerBuilder();
55 $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
56 $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
57 $cache_contexts_manager->reveal();
58 $this->container->set('cache_contexts_manager', $cache_contexts_manager);
59 \Drupal::setContainer($this->container);
63 * Tests the breadcrumb manager without any set breadcrumb.
65 public function testBuildWithoutBuilder() {
66 $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
67 $this->moduleHandler->expects($this->once())
69 ->with('system_breadcrumb', $this->breadcrumb, $route_match, ['builder' => NULL]);
71 $breadcrumb = $this->breadcrumbManager->build($this->getMock('Drupal\Core\Routing\RouteMatchInterface'));
72 $this->assertEquals([], $breadcrumb->getLinks());
73 $this->assertEquals([], $breadcrumb->getCacheContexts());
74 $this->assertEquals([], $breadcrumb->getCacheTags());
75 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
79 * Tests the build method with a single breadcrumb builder.
81 public function testBuildWithSingleBuilder() {
82 $builder = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
83 $links = ['<a href="/example">Test</a>'];
84 $this->breadcrumb->setLinks($links);
85 $this->breadcrumb->addCacheContexts(['foo'])->addCacheTags(['bar']);
87 $builder->expects($this->once())
89 ->will($this->returnValue(TRUE));
91 $builder->expects($this->once())
93 ->willReturn($this->breadcrumb);
95 $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
96 $this->moduleHandler->expects($this->once())
98 ->with('system_breadcrumb', $this->breadcrumb, $route_match, ['builder' => $builder]);
100 $this->breadcrumbManager->addBuilder($builder, 0);
102 $breadcrumb = $this->breadcrumbManager->build($route_match);
103 $this->assertEquals($links, $breadcrumb->getLinks());
104 $this->assertEquals(['foo'], $breadcrumb->getCacheContexts());
105 $this->assertEquals(['bar'], $breadcrumb->getCacheTags());
106 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
110 * Tests multiple breadcrumb builder with different priority.
112 public function testBuildWithMultipleApplyingBuilders() {
113 $builder1 = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
114 $builder1->expects($this->never())
116 $builder1->expects($this->never())
119 $builder2 = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
120 $links2 = ['<a href="/example2">Test2</a>'];
121 $this->breadcrumb->setLinks($links2);
122 $this->breadcrumb->addCacheContexts(['baz'])->addCacheTags(['qux']);
123 $builder2->expects($this->once())
125 ->will($this->returnValue(TRUE));
126 $builder2->expects($this->once())
128 ->willReturn($this->breadcrumb);
130 $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
132 $this->moduleHandler->expects($this->once())
134 ->with('system_breadcrumb', $this->breadcrumb, $route_match, ['builder' => $builder2]);
136 $this->breadcrumbManager->addBuilder($builder1, 0);
137 $this->breadcrumbManager->addBuilder($builder2, 10);
139 $breadcrumb = $this->breadcrumbManager->build($route_match);
140 $this->assertEquals($links2, $breadcrumb->getLinks());
141 $this->assertEquals(['baz'], $breadcrumb->getCacheContexts());
142 $this->assertEquals(['qux'], $breadcrumb->getCacheTags());
143 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
147 * Tests multiple breadcrumb builders of which one returns NULL.
149 public function testBuildWithOneNotApplyingBuilders() {
150 $builder1 = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
151 $builder1->expects($this->once())
153 ->will($this->returnValue(FALSE));
154 $builder1->expects($this->never())
157 $builder2 = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
158 $links2 = ['<a href="/example2">Test2</a>'];
159 $this->breadcrumb->setLinks($links2);
160 $this->breadcrumb->addCacheContexts(['baz'])->addCacheTags(['qux']);
161 $builder2->expects($this->once())
163 ->will($this->returnValue(TRUE));
164 $builder2->expects($this->once())
166 ->willReturn($this->breadcrumb);
168 $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
170 $this->moduleHandler->expects($this->once())
172 ->with('system_breadcrumb', $this->breadcrumb, $route_match, ['builder' => $builder2]);
174 $this->breadcrumbManager->addBuilder($builder1, 10);
175 $this->breadcrumbManager->addBuilder($builder2, 0);
177 $breadcrumb = $this->breadcrumbManager->build($route_match);
178 $this->assertEquals($links2, $breadcrumb->getLinks());
179 $this->assertEquals(['baz'], $breadcrumb->getCacheContexts());
180 $this->assertEquals(['qux'], $breadcrumb->getCacheTags());
181 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
185 * Tests a breadcrumb builder with a bad return value.
187 public function testBuildWithInvalidBreadcrumbResult() {
188 $builder = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
189 $builder->expects($this->once())
191 ->will($this->returnValue(TRUE));
192 $builder->expects($this->once())
194 ->will($this->returnValue('invalid_result'));
196 $this->breadcrumbManager->addBuilder($builder, 0);
197 $this->setExpectedException(\UnexpectedValueException::class);
198 $this->breadcrumbManager->build($this->getMock('Drupal\Core\Routing\RouteMatchInterface'));