3 namespace Drupal\Tests\Core\Theme;
5 use Drupal\Core\DependencyInjection\ClassResolver;
6 use Drupal\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\Core\Routing\RouteMatch;
8 use Drupal\Core\Theme\ThemeNegotiator;
9 use Drupal\Tests\UnitTestCase;
10 use Symfony\Component\Routing\Route;
13 * @coversDefaultClass \Drupal\Core\Theme\ThemeNegotiator
16 class ThemeNegotiatorTest extends UnitTestCase {
19 * The mocked theme access checker.
21 * @var \Drupal\Core\Theme\ThemeAccessCheck|\PHPUnit_Framework_MockObject_MockObject
23 protected $themeAccessCheck;
26 * The container builder.
28 * @var \Drupal\Core\DependencyInjection\ContainerBuilder
35 * @var \Symfony\Component\HttpFoundation\RequestStack
37 protected $requestStack;
40 * The actual tested theme negotiator.
42 * @var \Drupal\Core\Theme\ThemeNegotiator
44 protected $themeNegotiator;
49 protected function setUp() {
50 $this->themeAccessCheck = $this->getMockBuilder('\Drupal\Core\Theme\ThemeAccessCheck')
51 ->disableOriginalConstructor()
53 $this->container = new ContainerBuilder();
57 * Tests determining the theme.
59 * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
61 public function testDetermineActiveTheme() {
62 $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
63 $negotiator->expects($this->once())
64 ->method('determineActiveTheme')
65 ->will($this->returnValue('example_test'));
66 $negotiator->expects($this->once())
68 ->will($this->returnValue(TRUE));
70 $this->container->set('test_negotiator', $negotiator);
72 $negotiators = ['test_negotiator'];
74 $this->themeAccessCheck->expects($this->any())
75 ->method('checkAccess')
76 ->will($this->returnValue(TRUE));
78 $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
79 $theme = $this->createThemeNegotiator($negotiators)->determineActiveTheme($route_match);
81 $this->assertEquals('example_test', $theme);
85 * Tests determining with two negotiators checking the priority.
87 * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
89 public function testDetermineActiveThemeWithPriority() {
92 $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
93 $negotiator->expects($this->once())
94 ->method('determineActiveTheme')
95 ->will($this->returnValue('example_test'));
96 $negotiator->expects($this->once())
98 ->will($this->returnValue(TRUE));
100 $negotiators['test_negotiator_1'] = $negotiator;
102 $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
103 $negotiator->expects($this->never())
104 ->method('determineActiveTheme');
105 $negotiator->expects($this->never())
108 $negotiators['test_negotiator_2'] = $negotiator;
110 foreach ($negotiators as $id => $negotiator) {
111 $this->container->set($id, $negotiator);
114 $this->themeAccessCheck->expects($this->any())
115 ->method('checkAccess')
116 ->will($this->returnValue(TRUE));
118 $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
119 $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match);
121 $this->assertEquals('example_test', $theme);
125 * Tests determining with two negotiators of which just one returns access.
127 * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
129 public function testDetermineActiveThemeWithAccessCheck() {
132 $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
133 $negotiator->expects($this->once())
134 ->method('determineActiveTheme')
135 ->will($this->returnValue('example_test'));
136 $negotiator->expects($this->once())
138 ->will($this->returnValue(TRUE));
140 $negotiators['test_negotiator_1'] = $negotiator;
142 $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
143 $negotiator->expects($this->once())
144 ->method('determineActiveTheme')
145 ->will($this->returnValue('example_test2'));
146 $negotiator->expects($this->once())
148 ->will($this->returnValue(TRUE));
150 $negotiators['test_negotiator_2'] = $negotiator;
152 foreach ($negotiators as $id => $negotiator) {
153 $this->container->set($id, $negotiator);
156 $this->themeAccessCheck->expects($this->at(0))
157 ->method('checkAccess')
158 ->with('example_test')
159 ->will($this->returnValue(FALSE));
161 $this->themeAccessCheck->expects($this->at(1))
162 ->method('checkAccess')
163 ->with('example_test2')
164 ->will($this->returnValue(TRUE));
166 $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
167 $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match);
169 $this->assertEquals('example_test2', $theme);
173 * Tests determining with two negotiators of which one does not apply.
175 * @see \Drupal\Core\Theme\ThemeNegotiatorInterface
177 public function testDetermineActiveThemeWithNotApplyingNegotiator() {
180 $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
181 $negotiator->expects($this->never())
182 ->method('determineActiveTheme');
183 $negotiator->expects($this->once())
185 ->will($this->returnValue(FALSE));
187 $negotiators['test_negotiator_1'] = $negotiator;
189 $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
190 $negotiator->expects($this->once())
191 ->method('determineActiveTheme')
192 ->will($this->returnValue('example_test2'));
193 $negotiator->expects($this->once())
195 ->will($this->returnValue(TRUE));
197 $negotiators['test_negotiator_2'] = $negotiator;
199 foreach ($negotiators as $id => $negotiator) {
200 $this->container->set($id, $negotiator);
203 $this->themeAccessCheck->expects($this->any())
204 ->method('checkAccess')
205 ->will($this->returnValue(TRUE));
207 $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
208 $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match);
210 $this->assertEquals('example_test2', $theme);
214 * Creates a new theme negotiator instance.
216 * @param array $negotiators
217 * An array of negotiator IDs.
219 * @return \Drupal\Core\Theme\ThemeNegotiator
221 protected function createThemeNegotiator(array $negotiators) {
222 $resolver = new ClassResolver();
223 $resolver->setContainer($this->container);
224 $theme_negotiator = new ThemeNegotiator($this->themeAccessCheck, $resolver, $negotiators);
225 return $theme_negotiator;