3 namespace Drupal\Tests\Core\Theme;
5 use Drupal\Core\Routing\RouteMatch;
6 use Drupal\Core\Theme\ThemeNegotiator;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\Routing\Route;
11 * @coversDefaultClass \Drupal\Core\Theme\ThemeNegotiator
14 class ThemeNegotiatorTest extends UnitTestCase {
17 * The mocked theme access checker.
19 * @var \Drupal\Core\Theme\ThemeAccessCheck|\PHPUnit_Framework_MockObject_MockObject
21 protected $themeAccessCheck;
26 * @var \Symfony\Component\HttpFoundation\RequestStack
28 protected $requestStack;
31 * The actual tested theme negotiator.
33 * @var \Drupal\Core\Theme\ThemeNegotiator
35 protected $themeNegotiator;
37 protected function setUp() {
38 $this->themeAccessCheck = $this->getMockBuilder('\Drupal\Core\Theme\ThemeAccessCheck')
39 ->disableOriginalConstructor()
41 $this->themeNegotiator = new ThemeNegotiator($this->themeAccessCheck);
45 * Tests determining the theme.
47 * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
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())
56 ->will($this->returnValue(TRUE));
58 $this->themeNegotiator->addNegotiator($negotiator, 0);
60 $this->themeAccessCheck->expects($this->any())
61 ->method('checkAccess')
62 ->will($this->returnValue(TRUE));
64 $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
65 $theme = $this->themeNegotiator->determineActiveTheme($route_match);
67 $this->assertEquals('example_test', $theme);
71 * Tests determining with two negotiators checking the priority.
73 * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
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())
82 ->will($this->returnValue(TRUE));
84 $this->themeNegotiator->addNegotiator($negotiator, 10);
86 $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
87 $negotiator->expects($this->never())
88 ->method('determineActiveTheme');
89 $negotiator->expects($this->never())
92 $this->themeNegotiator->addNegotiator($negotiator, 0);
94 $this->themeAccessCheck->expects($this->any())
95 ->method('checkAccess')
96 ->will($this->returnValue(TRUE));
98 $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
99 $theme = $this->themeNegotiator->determineActiveTheme($route_match);
101 $this->assertEquals('example_test', $theme);
105 * Tests determining with two negotiators of which just one returns access.
107 * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
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())
116 ->will($this->returnValue(TRUE));
118 $this->themeNegotiator->addNegotiator($negotiator, 10);
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())
126 ->will($this->returnValue(TRUE));
128 $this->themeNegotiator->addNegotiator($negotiator, 0);
130 $this->themeAccessCheck->expects($this->at(0))
131 ->method('checkAccess')
132 ->with('example_test')
133 ->will($this->returnValue(FALSE));
135 $this->themeAccessCheck->expects($this->at(1))
136 ->method('checkAccess')
137 ->with('example_test2')
138 ->will($this->returnValue(TRUE));
140 $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
141 $theme = $this->themeNegotiator->determineActiveTheme($route_match);
143 $this->assertEquals('example_test2', $theme);
147 * Tests determining with two negotiators of which one does not apply.
149 * @see \Drupal\Core\Theme\ThemeNegotiatorInterface
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())
157 ->will($this->returnValue(FALSE));
159 $this->themeNegotiator->addNegotiator($negotiator, 10);
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())
167 ->will($this->returnValue(TRUE));
169 $this->themeNegotiator->addNegotiator($negotiator, 0);
171 $this->themeAccessCheck->expects($this->any())
172 ->method('checkAccess')
173 ->will($this->returnValue(TRUE));
175 $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
176 $theme = $this->themeNegotiator->determineActiveTheme($route_match);
178 $this->assertEquals('example_test2', $theme);