ThemeNegotiatorTest.php in Drupal 8
File
core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php
View source
<?php
namespace Drupal\Tests\Core\Theme;
use Drupal\Core\DependencyInjection\ClassResolver;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Theme\ThemeNegotiator;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route;
class ThemeNegotiatorTest extends UnitTestCase {
protected $themeAccessCheck;
protected $container;
protected $requestStack;
protected $themeNegotiator;
protected function setUp() {
$this->themeAccessCheck = $this
->getMockBuilder('\\Drupal\\Core\\Theme\\ThemeAccessCheck')
->disableOriginalConstructor()
->getMock();
$this->container = new ContainerBuilder();
}
public function testDetermineActiveTheme() {
$negotiator = $this
->createMock('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);
}
public function testDetermineActiveThemeWithPriority() {
$negotiators = [];
$negotiator = $this
->createMock('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
->createMock('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);
}
public function testDetermineActiveThemeWithAccessCheck() {
$negotiators = [];
$negotiator = $this
->createMock('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
->createMock('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);
}
public function testDetermineActiveThemeWithNotApplyingNegotiator() {
$negotiators = [];
$negotiator = $this
->createMock('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
->createMock('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);
}
protected function createThemeNegotiator(array $negotiators) {
$resolver = new ClassResolver();
$resolver
->setContainer($this->container);
$theme_negotiator = new ThemeNegotiator($this->themeAccessCheck, $resolver, $negotiators);
return $theme_negotiator;
}
}