class ThemeNegotiatorTest in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php \Drupal\Tests\Core\Theme\ThemeNegotiatorTest
@coversDefaultClass \Drupal\Core\Theme\ThemeNegotiator @group Theme
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\Core\Theme\ThemeNegotiatorTest
Expanded class hierarchy of ThemeNegotiatorTest
File
- core/
tests/ Drupal/ Tests/ Core/ Theme/ ThemeNegotiatorTest.php, line 16
Namespace
Drupal\Tests\Core\ThemeView source
class ThemeNegotiatorTest extends UnitTestCase {
/**
* The mocked theme access checker.
*
* @var \Drupal\Core\Theme\ThemeAccessCheck|\PHPUnit\Framework\MockObject\MockObject
*/
protected $themeAccessCheck;
/**
* The container builder.
*
* @var \Drupal\Core\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The actual tested theme negotiator.
*
* @var \Drupal\Core\Theme\ThemeNegotiator
*/
protected $themeNegotiator;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
$this->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
->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);
}
/**
* Tests determining with two negotiators checking the priority.
*
* @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
*/
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);
}
/**
* Tests determining with two negotiators of which just one returns access.
*
* @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
*/
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
->exactly(2))
->method('checkAccess')
->willReturnMap([
[
'example_test',
FALSE,
],
[
'example_test2',
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
->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);
}
/**
* 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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
ThemeNegotiatorTest:: |
protected | property | The container builder. | |
ThemeNegotiatorTest:: |
protected | property | The request stack. | |
ThemeNegotiatorTest:: |
protected | property | The mocked theme access checker. | |
ThemeNegotiatorTest:: |
protected | property | The actual tested theme negotiator. | |
ThemeNegotiatorTest:: |
protected | function | Creates a new theme negotiator instance. | |
ThemeNegotiatorTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ThemeNegotiatorTest:: |
public | function | Tests determining the theme. | |
ThemeNegotiatorTest:: |
public | function | Tests determining with two negotiators of which just one returns access. | |
ThemeNegotiatorTest:: |
public | function | Tests determining with two negotiators of which one does not apply. | |
ThemeNegotiatorTest:: |
public | function | Tests determining with two negotiators checking the priority. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
public static | function |