class TitleResolverTest

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php \Drupal\Tests\Core\Controller\TitleResolverTest
  2. 10 core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php \Drupal\Tests\Core\Controller\TitleResolverTest
  3. 11.x core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php \Drupal\Tests\Core\Controller\TitleResolverTest

@coversDefaultClass \Drupal\Core\Controller\TitleResolver @group Controller

Hierarchy

Expanded class hierarchy of TitleResolverTest

File

core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php, line 21

Namespace

Drupal\Tests\Core\Controller
View source
class TitleResolverTest extends UnitTestCase {
    
    /**
     * The mocked controller resolver.
     *
     * @var \Drupal\Core\Controller\ControllerResolverInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $controllerResolver;
    
    /**
     * The mocked translation manager.
     *
     * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $translationManager;
    
    /**
     * The mocked argument resolver.
     *
     * @var \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $argumentResolver;
    
    /**
     * The actual tested title resolver.
     *
     * @var \Drupal\Core\Controller\TitleResolver
     */
    protected $titleResolver;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        $this->controllerResolver = $this->createMock('\\Drupal\\Core\\Controller\\ControllerResolverInterface');
        $this->translationManager = $this->createMock('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
        $this->argumentResolver = $this->createMock('\\Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolverInterface');
        $this->titleResolver = new TitleResolver($this->controllerResolver, $this->translationManager, $this->argumentResolver);
    }
    
    /**
     * Tests a static title without a context.
     *
     * @see \Drupal\Core\Controller\TitleResolver::getTitle()
     */
    public function testStaticTitle() {
        $request = new Request();
        $route = new Route('/test-route', [
            '_title' => 'static title',
        ]);
        $this->assertEquals(new TranslatableMarkup('static title', [], [], $this->translationManager), $this->titleResolver
            ->getTitle($request, $route));
    }
    
    /**
     * Tests a static title of '0'.
     *
     * @see \Drupal\Core\Controller\TitleResolver::getTitle()
     */
    public function testStaticTitleZero() {
        $request = new Request();
        $route = new Route('/test-route', [
            '_title' => '0',
            '_title_context' => '0',
        ]);
        $this->assertEquals(new TranslatableMarkup('0', [], [
            'context' => '0',
        ], $this->translationManager), $this->titleResolver
            ->getTitle($request, $route));
    }
    
    /**
     * Tests a static title with a context.
     *
     * @see \Drupal\Core\Controller\TitleResolver::getTitle()
     */
    public function testStaticTitleWithContext() {
        $request = new Request();
        $route = new Route('/test-route', [
            '_title' => 'static title',
            '_title_context' => 'context',
        ]);
        $this->assertEquals(new TranslatableMarkup('static title', [], [
            'context' => 'context',
        ], $this->translationManager), $this->titleResolver
            ->getTitle($request, $route));
    }
    
    /**
     * Tests a static title with a parameter.
     *
     * @see \Drupal\Core\Controller\TitleResolver::getTitle()
     *
     * @dataProvider providerTestStaticTitleWithParameter
     */
    public function testStaticTitleWithParameter($title, $expected_title) {
        $raw_variables = new ParameterBag([
            'test' => 'value',
            'test2' => 'value2',
        ]);
        $request = new Request();
        $request->attributes
            ->set('_raw_variables', $raw_variables);
        $route = new Route('/test-route', [
            '_title' => $title,
        ]);
        $this->assertEquals($expected_title, $this->titleResolver
            ->getTitle($request, $route));
    }
    public function providerTestStaticTitleWithParameter() {
        $translation_manager = $this->createMock('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
        return [
            [
                'static title @test',
                new TranslatableMarkup('static title @test', [
                    '@test' => 'value',
                    '%test' => 'value',
                    '@test2' => 'value2',
                    '%test2' => 'value2',
                ], [], $translation_manager),
            ],
            [
                'static title %test',
                new TranslatableMarkup('static title %test', [
                    '@test' => 'value',
                    '%test' => 'value',
                    '@test2' => 'value2',
                    '%test2' => 'value2',
                ], [], $translation_manager),
            ],
        ];
    }
    
    /**
     * Tests a static title with a non-scalar value parameter.
     *
     * @see \Drupal\Core\Controller\TitleResolver::getTitle()
     */
    public function testStaticTitleWithNullAndArrayValueParameter() {
        $raw_variables = new ParameterBag([
            'test1' => NULL,
            'test2' => [
                'foo' => 'bar',
            ],
            'test3' => 'value',
        ]);
        $request = new Request();
        $request->attributes
            ->set('_raw_variables', $raw_variables);
        $route = new Route('/test-route', [
            '_title' => 'static title %test1 @test1 %test2 @test2 %test3 @test3',
        ]);
        $translatable_markup = $this->titleResolver
            ->getTitle($request, $route);
        $arguments = $translatable_markup->getArguments();
        $this->assertNotContains('@test1', $arguments);
        $this->assertNotContains('%test1', $arguments);
        $this->assertNotContains('@test2', $arguments);
        $this->assertNotContains('%test2', $arguments);
        $this->assertSame('value', $translatable_markup->getArguments()['@test3']);
        $this->assertSame('value', $translatable_markup->getArguments()['%test3']);
    }
    
    /**
     * Tests a dynamic title.
     *
     * @see \Drupal\Core\Controller\TitleResolver::getTitle()
     */
    public function testDynamicTitle() {
        $request = new Request();
        $route = new Route('/test-route', [
            '_title' => 'static title',
            '_title_callback' => 'Drupal\\Tests\\Core\\Controller\\TitleCallback::example',
        ]);
        $callable = [
            new TitleCallback(),
            'example',
        ];
        $this->controllerResolver
            ->expects($this->once())
            ->method('getControllerFromDefinition')
            ->with('Drupal\\Tests\\Core\\Controller\\TitleCallback::example')
            ->willReturn($callable);
        $this->argumentResolver
            ->expects($this->once())
            ->method('getArguments')
            ->with($request, $callable)
            ->willReturn([
            'example',
        ]);
        $this->assertEquals('test example', $this->titleResolver
            ->getTitle($request, $route));
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
TitleResolverTest::$argumentResolver protected property The mocked argument resolver.
TitleResolverTest::$controllerResolver protected property The mocked controller resolver.
TitleResolverTest::$titleResolver protected property The actual tested title resolver.
TitleResolverTest::$translationManager protected property The mocked translation manager.
TitleResolverTest::providerTestStaticTitleWithParameter public function
TitleResolverTest::setUp protected function Overrides UnitTestCase::setUp
TitleResolverTest::testDynamicTitle public function Tests a dynamic title.
TitleResolverTest::testStaticTitle public function Tests a static title without a context.
TitleResolverTest::testStaticTitleWithContext public function Tests a static title with a context.
TitleResolverTest::testStaticTitleWithNullAndArrayValueParameter public function Tests a static title with a non-scalar value parameter.
TitleResolverTest::testStaticTitleWithParameter public function Tests a static title with a parameter.
TitleResolverTest::testStaticTitleZero public function Tests a static title of '0'.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function

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