More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Theme / ThemeNegotiatorTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Theme;
4
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;
11
12 /**
13  * @coversDefaultClass \Drupal\Core\Theme\ThemeNegotiator
14  * @group Theme
15  */
16 class ThemeNegotiatorTest extends UnitTestCase {
17
18   /**
19    * The mocked theme access checker.
20    *
21    * @var \Drupal\Core\Theme\ThemeAccessCheck|\PHPUnit_Framework_MockObject_MockObject
22    */
23   protected $themeAccessCheck;
24
25   /**
26    * The container builder.
27    *
28    * @var \Drupal\Core\DependencyInjection\ContainerBuilder
29    */
30   protected $container;
31
32   /**
33    * The request stack.
34    *
35    * @var \Symfony\Component\HttpFoundation\RequestStack
36    */
37   protected $requestStack;
38
39   /**
40    * The actual tested theme negotiator.
41    *
42    * @var \Drupal\Core\Theme\ThemeNegotiator
43    */
44   protected $themeNegotiator;
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     $this->themeAccessCheck = $this->getMockBuilder('\Drupal\Core\Theme\ThemeAccessCheck')
51       ->disableOriginalConstructor()
52       ->getMock();
53     $this->container = new ContainerBuilder();
54   }
55
56   /**
57    * Tests determining the theme.
58    *
59    * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
60    */
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())
67       ->method('applies')
68       ->will($this->returnValue(TRUE));
69
70     $this->container->set('test_negotiator', $negotiator);
71
72     $negotiators = ['test_negotiator'];
73
74     $this->themeAccessCheck->expects($this->any())
75       ->method('checkAccess')
76       ->will($this->returnValue(TRUE));
77
78     $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
79     $theme = $this->createThemeNegotiator($negotiators)->determineActiveTheme($route_match);
80
81     $this->assertEquals('example_test', $theme);
82   }
83
84   /**
85    * Tests determining with two negotiators checking the priority.
86    *
87    * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
88    */
89   public function testDetermineActiveThemeWithPriority() {
90     $negotiators = [];
91
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())
97       ->method('applies')
98       ->will($this->returnValue(TRUE));
99
100     $negotiators['test_negotiator_1'] = $negotiator;
101
102     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
103     $negotiator->expects($this->never())
104       ->method('determineActiveTheme');
105     $negotiator->expects($this->never())
106       ->method('applies');
107
108     $negotiators['test_negotiator_2'] = $negotiator;
109
110     foreach ($negotiators as $id => $negotiator) {
111       $this->container->set($id, $negotiator);
112     }
113
114     $this->themeAccessCheck->expects($this->any())
115       ->method('checkAccess')
116       ->will($this->returnValue(TRUE));
117
118     $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
119     $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match);
120
121     $this->assertEquals('example_test', $theme);
122   }
123
124   /**
125    * Tests determining with two negotiators of which just one returns access.
126    *
127    * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
128    */
129   public function testDetermineActiveThemeWithAccessCheck() {
130     $negotiators = [];
131
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())
137       ->method('applies')
138       ->will($this->returnValue(TRUE));
139
140     $negotiators['test_negotiator_1'] = $negotiator;
141
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())
147       ->method('applies')
148       ->will($this->returnValue(TRUE));
149
150     $negotiators['test_negotiator_2'] = $negotiator;
151
152     foreach ($negotiators as $id => $negotiator) {
153       $this->container->set($id, $negotiator);
154     }
155
156     $this->themeAccessCheck->expects($this->at(0))
157       ->method('checkAccess')
158       ->with('example_test')
159       ->will($this->returnValue(FALSE));
160
161     $this->themeAccessCheck->expects($this->at(1))
162       ->method('checkAccess')
163       ->with('example_test2')
164       ->will($this->returnValue(TRUE));
165
166     $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
167     $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match);
168
169     $this->assertEquals('example_test2', $theme);
170   }
171
172   /**
173    * Tests determining with two negotiators of which one does not apply.
174    *
175    * @see \Drupal\Core\Theme\ThemeNegotiatorInterface
176    */
177   public function testDetermineActiveThemeWithNotApplyingNegotiator() {
178     $negotiators = [];
179
180     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
181     $negotiator->expects($this->never())
182       ->method('determineActiveTheme');
183     $negotiator->expects($this->once())
184       ->method('applies')
185       ->will($this->returnValue(FALSE));
186
187     $negotiators['test_negotiator_1'] = $negotiator;
188
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())
194       ->method('applies')
195       ->will($this->returnValue(TRUE));
196
197     $negotiators['test_negotiator_2'] = $negotiator;
198
199     foreach ($negotiators as $id => $negotiator) {
200       $this->container->set($id, $negotiator);
201     }
202
203     $this->themeAccessCheck->expects($this->any())
204       ->method('checkAccess')
205       ->will($this->returnValue(TRUE));
206
207     $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
208     $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match);
209
210     $this->assertEquals('example_test2', $theme);
211   }
212
213   /**
214    * Creates a new theme negotiator instance.
215    *
216    * @param array $negotiators
217    *   An array of negotiator IDs.
218    *
219    * @return \Drupal\Core\Theme\ThemeNegotiator
220    */
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;
226   }
227
228 }