View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\Core\Controller;
use Drupal\Core\Controller\TitleResolver;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Drupal\Core\StringTranslation\TranslatableMarkup;
class TitleResolverTest extends UnitTestCase {
protected $controllerResolver;
protected $translationManager;
protected $argumentResolver;
protected $titleResolver;
protected function setUp() : void {
parent::setUp();
$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);
}
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));
}
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));
}
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));
}
public function testStaticTitleWithParameter() {
$raw_variables = new InputBag([
'test' => 'value',
'test2' => 'value2',
]);
$request = new Request();
$request->attributes
->set('_raw_variables', $raw_variables);
$route = new Route('/test-route', [
'_title' => 'static title @test',
]);
$this
->assertEquals(new TranslatableMarkup('static title @test', [
'@test' => 'value',
'%test' => 'value',
'@test2' => 'value2',
'%test2' => 'value2',
], [], $this->translationManager), $this->titleResolver
->getTitle($request, $route));
$route = new Route('/test-route', [
'_title' => 'static title %test',
]);
$this
->assertEquals(new TranslatableMarkup('static title %test', [
'@test' => 'value',
'%test' => 'value',
'@test2' => 'value2',
'%test2' => 'value2',
], [], $this->translationManager), $this->titleResolver
->getTitle($request, $route));
}
public function testStaticTitleWithNullAndArrayValueParameter() {
$raw_variables = new InputBag([
'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']);
}
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));
}
}
class TitleCallback {
public function example($value) {
return 'test ' . $value;
}
}