diff -u b/core/lib/Drupal/Core/Menu/ContextualLinkDefault.php b/core/lib/Drupal/Core/Menu/ContextualLinkDefault.php --- b/core/lib/Drupal/Core/Menu/ContextualLinkDefault.php +++ b/core/lib/Drupal/Core/Menu/ContextualLinkDefault.php @@ -18,7 +18,11 @@ * {@inheritdoc} */ public function getTitle() { - 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); } /** diff -u b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php --- b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php @@ -13,6 +13,9 @@ /** * Tests the contextual links manager. * + * @group Drupal + * @group Menu + * * @see \Drupal\Core\Menu\ContextualLinkManager */ class ContextualLinkManagerTest extends UnitTestCase { only in patch2: unchanged: --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php @@ -0,0 +1,171 @@ + 'contextual_link_default', + ); + + /** + * The mocked translator. + * + * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $stringTranslation; + + /** + * The mocked route provider. + * + * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $routeProvider; + + public static function getInfo() { + return array( + 'name' => 'Contextual links default.', + 'description' => 'Tests the contextual link default class.', + 'group' => 'Menu', + ); + } + + protected function setUp() { + parent::setUp(); + + $this->stringTranslation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface'); + $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface'); + + $container = new ContainerBuilder(); + $container->set('string_translation', $this->stringTranslation); + $container->set('router.route_provider', $this->routeProvider); + \Drupal::setContainer($container); + } + + protected function setupContextualLinkDefault() { + $this->contextualLinkDefault = new ContextualLinkDefault($this->config, $this->pluginId, $this->pluginDefinition); + } + + /** + * 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') + ->with($this->pluginDefinition['title'], array(), array()) + ->will($this->returnValue('Example translated')); + + $this->setupContextualLinkDefault(); + $this->assertEquals('Example translated', $this->contextualLinkDefault->getTitle()); + } + + /** + * 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->setupContextualLinkDefault(); + $this->assertEquals('Example translated with context', $this->contextualLinkDefault->getTitle()); + } + + /** + * Tests the getRouteName() method. + * + * @covers \Drupal\Core\Menu\ContextualLinkDefault::getRouteName() + */ + public function testGetRouteName($route_name = 'test_route_name') { + $this->pluginDefinition['route_name'] = $route_name; + $this->setupContextualLinkDefault(); + + $this->assertEquals($route_name, $this->contextualLinkDefault->getRouteName()); + } + + /** + * Tests the getGroup() method. + * + * @covers \Drupal\Core\Menu\ContextualLinkDefault::getGroup() + */ + public function testGetGroup($group_name = 'test_group') { + $this->pluginDefinition['group'] = $group_name; + $this->setupContextualLinkDefault(); + + $this->assertEquals($group_name, $this->contextualLinkDefault->getGroup()); + } + + /** + * Tests the getOptions() method. + * + * @covers \Drupal\Core\Menu\ContextualLinkDefault::getOptions() + */ + public function testGetOptions($options = array('key' => 'value')) { + $this->pluginDefinition['options'] = $options; + $this->setupContextualLinkDefault(); + + $this->assertEquals($options, $this->contextualLinkDefault->getOptions()); + } + + /** + * Tests the getWeight() method. + * + * @covers \Drupal\Core\Menu\ContextualLinkDefault::getWeight() + */ + public function testGetWeight($weight = 5) { + $this->pluginDefinition['weight'] = $weight; + $this->setupContextualLinkDefault(); + + $this->assertEquals($weight, $this->contextualLinkDefault->getWeight()); + } + +}