class ThemeNegotiatorTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php \Drupal\Tests\Core\Theme\ThemeNegotiatorTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php \Drupal\Tests\Core\Theme\ThemeNegotiatorTest
  3. 10 core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php \Drupal\Tests\Core\Theme\ThemeNegotiatorTest

@coversDefaultClass \Drupal\Core\Theme\ThemeNegotiator @group Theme

Hierarchy

Expanded class hierarchy of ThemeNegotiatorTest

File

core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php, line 18

Namespace

Drupal\Tests\Core\Theme
View 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 {
        parent::setUp();
        $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() : void {
        $negotiator = $this->createMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
        $negotiator->expects($this->once())
            ->method('determineActiveTheme')
            ->willReturn('example_test');
        $negotiator->expects($this->once())
            ->method('applies')
            ->willReturn(TRUE);
        $this->container
            ->set('test_negotiator', $negotiator);
        $negotiators = [
            'test_negotiator',
        ];
        $this->themeAccessCheck
            ->expects($this->any())
            ->method('checkAccess')
            ->willReturn(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() : void {
        $negotiators = [];
        $negotiator = $this->createMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
        $negotiator->expects($this->once())
            ->method('determineActiveTheme')
            ->willReturn('example_test');
        $negotiator->expects($this->once())
            ->method('applies')
            ->willReturn(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')
            ->willReturn(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() : void {
        $negotiators = [];
        $negotiator = $this->createMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
        $negotiator->expects($this->once())
            ->method('determineActiveTheme')
            ->willReturn('example_test');
        $negotiator->expects($this->once())
            ->method('applies')
            ->willReturn(TRUE);
        $negotiators['test_negotiator_1'] = $negotiator;
        $negotiator = $this->createMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
        $negotiator->expects($this->once())
            ->method('determineActiveTheme')
            ->willReturn('example_test2');
        $negotiator->expects($this->once())
            ->method('applies')
            ->willReturn(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() : void {
        $negotiators = [];
        $negotiator = $this->createMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
        $negotiator->expects($this->never())
            ->method('determineActiveTheme');
        $negotiator->expects($this->once())
            ->method('applies')
            ->willReturn(FALSE);
        $negotiators['test_negotiator_1'] = $negotiator;
        $negotiator = $this->createMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
        $negotiator->expects($this->once())
            ->method('determineActiveTheme')
            ->willReturn('example_test2');
        $negotiator->expects($this->once())
            ->method('applies')
            ->willReturn(TRUE);
        $negotiators['test_negotiator_2'] = $negotiator;
        foreach ($negotiators as $id => $negotiator) {
            $this->container
                ->set($id, $negotiator);
        }
        $this->themeAccessCheck
            ->expects($this->any())
            ->method('checkAccess')
            ->willReturn(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($this->container);
        $theme_negotiator = new ThemeNegotiator($this->themeAccessCheck, $resolver, $negotiators);
        return $theme_negotiator;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
ThemeNegotiatorTest::$container protected property The container builder.
ThemeNegotiatorTest::$requestStack protected property The request stack.
ThemeNegotiatorTest::$themeAccessCheck protected property The mocked theme access checker.
ThemeNegotiatorTest::$themeNegotiator protected property The actual tested theme negotiator.
ThemeNegotiatorTest::createThemeNegotiator protected function Creates a new theme negotiator instance.
ThemeNegotiatorTest::setUp protected function Overrides UnitTestCase::setUp
ThemeNegotiatorTest::testDetermineActiveTheme public function Tests determining the theme.
ThemeNegotiatorTest::testDetermineActiveThemeWithAccessCheck public function Tests determining with two negotiators of which just one returns access.
ThemeNegotiatorTest::testDetermineActiveThemeWithNotApplyingNegotiator public function Tests determining with two negotiators of which one does not apply.
ThemeNegotiatorTest::testDetermineActiveThemeWithPriority public function Tests determining with two negotiators checking the priority.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.