only in patch2: unchanged: --- a/core/lib/Drupal/Core/Menu/LocalActionDefault.php +++ b/core/lib/Drupal/Core/Menu/LocalActionDefault.php @@ -67,7 +67,11 @@ public function getRouteName() { */ public function getTitle() { // Subclasses may pull in the request or specific attributes as parameters. - return $this->t($this->pluginDefinition['title']); + $options = array(); + if (!empty($this->pluginDefinition['title_context'])) { + $options['context'] = $this->pluginDefinition['title_context']; + } + return $this->t($this->pluginDefinition['title'], array(), $options); } /** only in patch2: unchanged: --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php @@ -0,0 +1,135 @@ + 'Title resolver', + 'description' => 'Tests the title resolver.', + 'group' => 'Routing', + ); + } + + 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 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)); + } + +} + +/** + * Provides an example title callback for the testDynamicTitle method above. + */ +class TitleCallback { + + /** + * Gets the example string. + * + * @param string $value + * The dynamic value. + * + * @return string + * Returns the example string. + */ + public function example($value) { + return String::format('test @value', array('@value' => $value)); + } + +} only in patch2: unchanged: --- a/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php @@ -253,14 +253,15 @@ public function testActive() { } /** - * Tests the getTitle method. + * Tests the getTitle method without a translation context. * * @see \Drupal\Core\Menu\LocalTaskDefault::getTitle() */ public function testGetTitle() { $this->pluginDefinition['title'] = 'Example'; $this->stringTranslation->expects($this->once()) - ->method('translate', $this->pluginDefinition['title']) + ->method('translate') + ->with($this->pluginDefinition['title'], array(), array()) ->will($this->returnValue('Example translated')); $this->setupLocalTaskDefault(); @@ -268,6 +269,23 @@ public function testGetTitle() { } /** + * Tests the getTitle method with a translation context. + * + * @see \Drupal\Core\Menu\LocalTaskDefault::getTitle() + */ + public function testGetTitleWithContext() { + $this->pluginDefinition['title'] = 'Example'; + $this->pluginDefinition['title_context'] = 'context'; + $this->stringTranslation->expects($this->once()) + ->method('translate') + ->with($this->pluginDefinition['title'], array(), array('context' => 'context')) + ->will($this->returnValue('Example translated with context')); + + $this->setupLocalTaskDefault(); + $this->assertEquals('Example translated with context', $this->localTaskBase->getTitle()); + } + + /** * Tests the getOption method. * * @see \Drupal\Core\Menu\LocalTaskDefault::getOption()