3 namespace Drupal\Tests\Core\Menu;
5 use Drupal\Core\Menu\LocalActionDefault;
6 use Drupal\Core\StringTranslation\TranslatableMarkup;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
11 * @coversDefaultClass \Drupal\Core\Menu\LocalActionDefault
14 class LocalActionDefaultTest extends UnitTestCase {
17 * The tested local action default plugin.
19 * @var \Drupal\Core\Menu\LocalActionDefault
21 protected $localActionDefault;
24 * The used plugin configuration.
28 protected $config = [];
35 protected $pluginId = 'local_action_default';
38 * The used plugin definition.
42 protected $pluginDefinition = [
43 'id' => 'local_action_default',
47 * The mocked translator.
49 * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
51 protected $stringTranslation;
54 * The mocked route provider.
56 * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
58 protected $routeProvider;
60 protected function setUp() {
63 $this->stringTranslation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
64 $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
68 * Setups the local action default.
70 protected function setupLocalActionDefault() {
71 $this->localActionDefault = new LocalActionDefault($this->config, $this->pluginId, $this->pluginDefinition, $this->routeProvider);
75 * Tests the getTitle method without a translation context.
77 * @see \Drupal\Core\Menu\LocalTaskDefault::getTitle()
79 public function testGetTitle() {
80 $this->pluginDefinition['title'] = (new TranslatableMarkup('Example', [], [], $this->stringTranslation));
81 $this->stringTranslation->expects($this->once())
82 ->method('translateString')
83 ->with($this->pluginDefinition['title'])
84 ->will($this->returnValue('Example translated'));
86 $this->setupLocalActionDefault();
87 $this->assertEquals('Example translated', $this->localActionDefault->getTitle());
91 * Tests the getTitle method with a translation context.
93 * @see \Drupal\Core\Menu\LocalTaskDefault::getTitle()
95 public function testGetTitleWithContext() {
96 $this->pluginDefinition['title'] = (new TranslatableMarkup('Example', [], ['context' => 'context'], $this->stringTranslation));
97 $this->stringTranslation->expects($this->once())
98 ->method('translateString')
99 ->with($this->pluginDefinition['title'])
100 ->will($this->returnValue('Example translated with context'));
102 $this->setupLocalActionDefault();
103 $this->assertEquals('Example translated with context', $this->localActionDefault->getTitle());
107 * Tests the getTitle method with title arguments.
109 public function testGetTitleWithTitleArguments() {
110 $this->pluginDefinition['title'] = (new TranslatableMarkup('Example @test', ['@test' => 'value'], [], $this->stringTranslation));
111 $this->stringTranslation->expects($this->once())
112 ->method('translateString')
113 ->with($this->pluginDefinition['title'])
114 ->will($this->returnValue('Example value'));
116 $this->setupLocalActionDefault();
117 $request = new Request();
118 $this->assertEquals('Example value', $this->localActionDefault->getTitle($request));