Version 1
[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\Routing\RouteMatch;
6 use Drupal\Core\Theme\ThemeNegotiator;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\Routing\Route;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\Theme\ThemeNegotiator
12  * @group Theme
13  */
14 class ThemeNegotiatorTest extends UnitTestCase {
15
16   /**
17    * The mocked theme access checker.
18    *
19    * @var \Drupal\Core\Theme\ThemeAccessCheck|\PHPUnit_Framework_MockObject_MockObject
20    */
21   protected $themeAccessCheck;
22
23   /**
24    * The request stack.
25    *
26    * @var \Symfony\Component\HttpFoundation\RequestStack
27    */
28   protected $requestStack;
29
30   /**
31    * The actual tested theme negotiator.
32    *
33    * @var \Drupal\Core\Theme\ThemeNegotiator
34    */
35   protected $themeNegotiator;
36
37   protected function setUp() {
38     $this->themeAccessCheck = $this->getMockBuilder('\Drupal\Core\Theme\ThemeAccessCheck')
39       ->disableOriginalConstructor()
40       ->getMock();
41     $this->themeNegotiator = new ThemeNegotiator($this->themeAccessCheck);
42   }
43
44   /**
45    * Tests determining the theme.
46    *
47    * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
48    */
49   public function testDetermineActiveTheme() {
50     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
51     $negotiator->expects($this->once())
52       ->method('determineActiveTheme')
53       ->will($this->returnValue('example_test'));
54     $negotiator->expects($this->once())
55       ->method('applies')
56       ->will($this->returnValue(TRUE));
57
58     $this->themeNegotiator->addNegotiator($negotiator, 0);
59
60     $this->themeAccessCheck->expects($this->any())
61       ->method('checkAccess')
62       ->will($this->returnValue(TRUE));
63
64     $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
65     $theme = $this->themeNegotiator->determineActiveTheme($route_match);
66
67     $this->assertEquals('example_test', $theme);
68   }
69
70   /**
71    * Tests determining with two negotiators checking the priority.
72    *
73    * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
74    */
75   public function testDetermineActiveThemeWithPriority() {
76     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
77     $negotiator->expects($this->once())
78       ->method('determineActiveTheme')
79       ->will($this->returnValue('example_test'));
80     $negotiator->expects($this->once())
81       ->method('applies')
82       ->will($this->returnValue(TRUE));
83
84     $this->themeNegotiator->addNegotiator($negotiator, 10);
85
86     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
87     $negotiator->expects($this->never())
88       ->method('determineActiveTheme');
89     $negotiator->expects($this->never())
90       ->method('applies');
91
92     $this->themeNegotiator->addNegotiator($negotiator, 0);
93
94     $this->themeAccessCheck->expects($this->any())
95       ->method('checkAccess')
96       ->will($this->returnValue(TRUE));
97
98     $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
99     $theme = $this->themeNegotiator->determineActiveTheme($route_match);
100
101     $this->assertEquals('example_test', $theme);
102   }
103
104   /**
105    * Tests determining with two negotiators of which just one returns access.
106    *
107    * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
108    */
109   public function testDetermineActiveThemeWithAccessCheck() {
110     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
111     $negotiator->expects($this->once())
112       ->method('determineActiveTheme')
113       ->will($this->returnValue('example_test'));
114     $negotiator->expects($this->once())
115       ->method('applies')
116       ->will($this->returnValue(TRUE));
117
118     $this->themeNegotiator->addNegotiator($negotiator, 10);
119
120     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
121     $negotiator->expects($this->once())
122       ->method('determineActiveTheme')
123       ->will($this->returnValue('example_test2'));
124     $negotiator->expects($this->once())
125       ->method('applies')
126       ->will($this->returnValue(TRUE));
127
128     $this->themeNegotiator->addNegotiator($negotiator, 0);
129
130     $this->themeAccessCheck->expects($this->at(0))
131       ->method('checkAccess')
132       ->with('example_test')
133       ->will($this->returnValue(FALSE));
134
135     $this->themeAccessCheck->expects($this->at(1))
136       ->method('checkAccess')
137       ->with('example_test2')
138       ->will($this->returnValue(TRUE));
139
140     $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
141     $theme = $this->themeNegotiator->determineActiveTheme($route_match);
142
143     $this->assertEquals('example_test2', $theme);
144   }
145
146   /**
147    * Tests determining with two negotiators of which one does not apply.
148    *
149    * @see \Drupal\Core\Theme\ThemeNegotiatorInterface
150    */
151   public function testDetermineActiveThemeWithNotApplyingNegotiator() {
152     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
153     $negotiator->expects($this->never())
154       ->method('determineActiveTheme');
155     $negotiator->expects($this->once())
156       ->method('applies')
157       ->will($this->returnValue(FALSE));
158
159     $this->themeNegotiator->addNegotiator($negotiator, 10);
160
161     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
162     $negotiator->expects($this->once())
163       ->method('determineActiveTheme')
164       ->will($this->returnValue('example_test2'));
165     $negotiator->expects($this->once())
166       ->method('applies')
167       ->will($this->returnValue(TRUE));
168
169     $this->themeNegotiator->addNegotiator($negotiator, 0);
170
171     $this->themeAccessCheck->expects($this->any())
172       ->method('checkAccess')
173       ->will($this->returnValue(TRUE));
174
175     $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
176     $theme = $this->themeNegotiator->determineActiveTheme($route_match);
177
178     $this->assertEquals('example_test2', $theme);
179   }
180
181 }