themeAccessCheck = $this->getMockBuilder('\Drupal\Core\Theme\ThemeAccessCheck') ->disableOriginalConstructor() ->getMock(); $this->container = new ContainerBuilder(); } /** * Tests determining the theme. * * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme() */ public function testDetermineActiveTheme() { $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->once()) ->method('determineActiveTheme') ->will($this->returnValue('example_test')); $negotiator->expects($this->once()) ->method('applies') ->will($this->returnValue(TRUE)); $this->container->set('test_negotiator', $negotiator); $negotiators = ['test_negotiator']; $this->themeAccessCheck->expects($this->any()) ->method('checkAccess') ->will($this->returnValue(TRUE)); $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []); $theme = $this->createThemeNegotiator($negotiators)->determineActiveTheme($route_match); $this->assertEquals('example_test', $theme); } /** * Tests determining with two negotiators checking the priority. * * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme() */ public function testDetermineActiveThemeWithPriority() { $negotiators = []; $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->once()) ->method('determineActiveTheme') ->will($this->returnValue('example_test')); $negotiator->expects($this->once()) ->method('applies') ->will($this->returnValue(TRUE)); $negotiators['test_negotiator_1'] = $negotiator; $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->never()) ->method('determineActiveTheme'); $negotiator->expects($this->never()) ->method('applies'); $negotiators['test_negotiator_2'] = $negotiator; foreach ($negotiators as $id => $negotiator) { $this->container->set($id, $negotiator); } $this->themeAccessCheck->expects($this->any()) ->method('checkAccess') ->will($this->returnValue(TRUE)); $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []); $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match); $this->assertEquals('example_test', $theme); } /** * Tests determining with two negotiators of which just one returns access. * * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme() */ public function testDetermineActiveThemeWithAccessCheck() { $negotiators = []; $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->once()) ->method('determineActiveTheme') ->will($this->returnValue('example_test')); $negotiator->expects($this->once()) ->method('applies') ->will($this->returnValue(TRUE)); $negotiators['test_negotiator_1'] = $negotiator; $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->once()) ->method('determineActiveTheme') ->will($this->returnValue('example_test2')); $negotiator->expects($this->once()) ->method('applies') ->will($this->returnValue(TRUE)); $negotiators['test_negotiator_2'] = $negotiator; foreach ($negotiators as $id => $negotiator) { $this->container->set($id, $negotiator); } $this->themeAccessCheck->expects($this->at(0)) ->method('checkAccess') ->with('example_test') ->will($this->returnValue(FALSE)); $this->themeAccessCheck->expects($this->at(1)) ->method('checkAccess') ->with('example_test2') ->will($this->returnValue(TRUE)); $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []); $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match); $this->assertEquals('example_test2', $theme); } /** * Tests determining with two negotiators of which one does not apply. * * @see \Drupal\Core\Theme\ThemeNegotiatorInterface */ public function testDetermineActiveThemeWithNotApplyingNegotiator() { $negotiators = []; $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->never()) ->method('determineActiveTheme'); $negotiator->expects($this->once()) ->method('applies') ->will($this->returnValue(FALSE)); $negotiators['test_negotiator_1'] = $negotiator; $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->once()) ->method('determineActiveTheme') ->will($this->returnValue('example_test2')); $negotiator->expects($this->once()) ->method('applies') ->will($this->returnValue(TRUE)); $negotiators['test_negotiator_2'] = $negotiator; foreach ($negotiators as $id => $negotiator) { $this->container->set($id, $negotiator); } $this->themeAccessCheck->expects($this->any()) ->method('checkAccess') ->will($this->returnValue(TRUE)); $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []); $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match); $this->assertEquals('example_test2', $theme); } /** * Creates a new theme negotiator instance. * * @param array $negotiators * An array of negotiator IDs. * * @return \Drupal\Core\Theme\ThemeNegotiator */ protected function createThemeNegotiator(array $negotiators) { $resolver = new ClassResolver(); $resolver->setContainer($this->container); $theme_negotiator = new ThemeNegotiator($this->themeAccessCheck, $resolver, $negotiators); return $theme_negotiator; } }