Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Breadcrumb / BreadcrumbManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Breadcrumb;
4
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;
11
12 /**
13  * @coversDefaultClass \Drupal\Core\Breadcrumb\BreadcrumbManager
14  * @group Breadcrumb
15  */
16 class BreadcrumbManagerTest extends UnitTestCase {
17
18   /**
19    * The dependency injection container.
20    *
21    * @var \Symfony\Component\DependencyInjection\ContainerBuilder
22    */
23   protected $container;
24
25   /**
26    * The breadcrumb object.
27    *
28    * @var \Drupal\Core\Breadcrumb\Breadcrumb
29    */
30   protected $breadcrumb;
31
32   /**
33    * The tested breadcrumb manager.
34    *
35    * @var \Drupal\Core\Breadcrumb\BreadcrumbManager
36    */
37   protected $breadcrumbManager;
38
39   /**
40    * The mocked module handler.
41    *
42    * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
43    */
44   protected $moduleHandler;
45
46   /**
47    * {@inheritdoc}
48    */
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();
53
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);
60   }
61
62   /**
63    * Tests the breadcrumb manager without any set breadcrumb.
64    */
65   public function testBuildWithoutBuilder() {
66     $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
67     $this->moduleHandler->expects($this->once())
68       ->method('alter')
69       ->with('system_breadcrumb', $this->breadcrumb, $route_match, ['builder' => NULL]);
70
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());
76   }
77
78   /**
79    * Tests the build method with a single breadcrumb builder.
80    */
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']);
86
87     $builder->expects($this->once())
88       ->method('applies')
89       ->will($this->returnValue(TRUE));
90
91     $builder->expects($this->once())
92       ->method('build')
93       ->willReturn($this->breadcrumb);
94
95     $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
96     $this->moduleHandler->expects($this->once())
97       ->method('alter')
98       ->with('system_breadcrumb', $this->breadcrumb, $route_match, ['builder' => $builder]);
99
100     $this->breadcrumbManager->addBuilder($builder, 0);
101
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());
107   }
108
109   /**
110    * Tests multiple breadcrumb builder with different priority.
111    */
112   public function testBuildWithMultipleApplyingBuilders() {
113     $builder1 = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
114     $builder1->expects($this->never())
115       ->method('applies');
116     $builder1->expects($this->never())
117       ->method('build');
118
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())
124       ->method('applies')
125       ->will($this->returnValue(TRUE));
126     $builder2->expects($this->once())
127       ->method('build')
128       ->willReturn($this->breadcrumb);
129
130     $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
131
132     $this->moduleHandler->expects($this->once())
133       ->method('alter')
134       ->with('system_breadcrumb', $this->breadcrumb, $route_match, ['builder' => $builder2]);
135
136     $this->breadcrumbManager->addBuilder($builder1, 0);
137     $this->breadcrumbManager->addBuilder($builder2, 10);
138
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());
144   }
145
146   /**
147    * Tests multiple breadcrumb builders of which one returns NULL.
148    */
149   public function testBuildWithOneNotApplyingBuilders() {
150     $builder1 = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
151     $builder1->expects($this->once())
152       ->method('applies')
153       ->will($this->returnValue(FALSE));
154     $builder1->expects($this->never())
155       ->method('build');
156
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())
162       ->method('applies')
163       ->will($this->returnValue(TRUE));
164     $builder2->expects($this->once())
165       ->method('build')
166       ->willReturn($this->breadcrumb);
167
168     $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
169
170     $this->moduleHandler->expects($this->once())
171       ->method('alter')
172       ->with('system_breadcrumb', $this->breadcrumb, $route_match, ['builder' => $builder2]);
173
174     $this->breadcrumbManager->addBuilder($builder1, 10);
175     $this->breadcrumbManager->addBuilder($builder2, 0);
176
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());
182   }
183
184   /**
185    * Tests a breadcrumb builder with a bad return value.
186    */
187   public function testBuildWithInvalidBreadcrumbResult() {
188     $builder = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
189     $builder->expects($this->once())
190       ->method('applies')
191       ->will($this->returnValue(TRUE));
192     $builder->expects($this->once())
193       ->method('build')
194       ->will($this->returnValue('invalid_result'));
195
196     $this->breadcrumbManager->addBuilder($builder, 0);
197     $this->setExpectedException(\UnexpectedValueException::class);
198     $this->breadcrumbManager->build($this->getMock('Drupal\Core\Routing\RouteMatchInterface'));
199   }
200
201 }