You are here

class TitleResolverTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 20
Contains \Drupal\Tests\Core\Controller\TitleResolverTest.

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 actual tested title resolver.
   *
   * @var \Drupal\Core\Controller\TitleResolver
   */
  protected $titleResolver;
  protected function setUp() {
    $this->controllerResolver = $this
      ->getMock('\\Drupal\\Core\\Controller\\ControllerResolverInterface');
    $this->translationManager = $this
      ->getMock('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
    $this->titleResolver = new TitleResolver($this->controllerResolver, $this->translationManager);
  }

  /**
   * Tests a static title without a context.
   *
   * @see \Drupal\Core\Controller\TitleResolver::getTitle()
   */
  public function testStaticTitle() {
    $request = new Request();
    $route = new Route('/test-route', array(
      '_title' => 'static title',
    ));
    $this->translationManager
      ->expects($this
      ->once())
      ->method('translate')
      ->with('static title', array(), array())
      ->will($this
      ->returnValue('translated title'));
    $this
      ->assertEquals('translated title', $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', array(
      '_title' => 'static title',
      '_title_context' => 'context',
    ));
    $this->translationManager
      ->expects($this
      ->once())
      ->method('translate')
      ->with('static title', array(), array(
      'context' => 'context',
    ))
      ->will($this
      ->returnValue('translated title with context'));
    $this
      ->assertEquals('translated title with context', $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(array(
      'test' => 'value',
      'test2' => 'value2',
    ));
    $request = new Request();
    $request->attributes
      ->set('_raw_variables', $raw_variables);
    $route = new Route('/test-route', array(
      '_title' => $title,
    ));
    $this->translationManager
      ->expects($this
      ->once())
      ->method('translate')
      ->with($title, $this
      ->logicalOr($this
      ->arrayHasKey('@test'), $this
      ->arrayHasKey('%test'), $this
      ->arrayHasKey('!test')), array())
      ->will($this
      ->returnValue('static title value'));
    $this
      ->assertEquals($expected_title, $this->titleResolver
      ->getTitle($request, $route));
  }
  public function providerTestStaticTitleWithParameter() {
    return array(
      array(
        'static title @test',
        'static title value',
      ),
      array(
        'static title !test',
        'static title value',
      ),
      array(
        'static title %test',
        'static title value',
      ),
    );
  }

  /**
   * Tests a dynamic title.
   *
   * @see \Drupal\Core\Controller\TitleResolver::getTitle()
   */
  public function testDynamicTitle() {
    $request = new Request();
    $route = new Route('/test-route', array(
      '_title' => 'static title',
      '_title_callback' => 'Drupal\\Tests\\Core\\Controller\\TitleCallback::example',
    ));
    $callable = array(
      new TitleCallback(),
      'example',
    );
    $this->controllerResolver
      ->expects($this
      ->once())
      ->method('getControllerFromDefinition')
      ->with('Drupal\\Tests\\Core\\Controller\\TitleCallback::example')
      ->will($this
      ->returnValue($callable));
    $this->controllerResolver
      ->expects($this
      ->once())
      ->method('getArguments')
      ->with($request, $callable)
      ->will($this
      ->returnValue(array(
      'example',
    )));
    $this
      ->assertEquals('test example', $this->titleResolver
      ->getTitle($request, $route));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::testStaticTitleWithParameter public function Tests a static title with a parameter.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.