3 namespace Drupal\Tests\Core\Menu;
5 use Drupal\Core\Menu\ContextualLinkDefault;
6 use Drupal\Core\StringTranslation\TranslatableMarkup;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
12 * @coversDefaultClass \Drupal\Core\Menu\ContextualLinkDefault
14 class ContextualLinkDefaultTest extends UnitTestCase {
17 * The tested contextual link default plugin.
19 * @var \Drupal\Core\Menu\ContextualLinkDefault
21 protected $contextualLinkDefault;
24 * The used plugin configuration.
28 protected $config = [];
35 protected $pluginId = 'contextual_link_default';
38 * The used plugin definition.
42 protected $pluginDefinition = [
43 'id' => 'contextual_link_default',
47 * The mocked translator.
49 * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
51 protected $stringTranslation;
53 protected function setUp() {
56 $this->stringTranslation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
59 protected function setupContextualLinkDefault() {
60 $this->contextualLinkDefault = new ContextualLinkDefault($this->config, $this->pluginId, $this->pluginDefinition);
66 public function testGetTitle() {
68 $this->pluginDefinition['title'] = (new TranslatableMarkup($title, [], [], $this->stringTranslation));
69 $this->stringTranslation->expects($this->once())
70 ->method('translateString')
71 ->with($this->pluginDefinition['title'])
72 ->will($this->returnValue('Example translated'));
74 $this->setupContextualLinkDefault();
75 $this->assertEquals('Example translated', $this->contextualLinkDefault->getTitle());
81 public function testGetTitleWithContext() {
83 $this->pluginDefinition['title'] = (new TranslatableMarkup($title, [], ['context' => 'context'], $this->stringTranslation));
84 $this->stringTranslation->expects($this->once())
85 ->method('translateString')
86 ->with($this->pluginDefinition['title'])
87 ->will($this->returnValue('Example translated with context'));
89 $this->setupContextualLinkDefault();
90 $this->assertEquals('Example translated with context', $this->contextualLinkDefault->getTitle());
96 public function testGetTitleWithTitleArguments() {
97 $title = 'Example @test';
98 $this->pluginDefinition['title'] = (new TranslatableMarkup($title, ['@test' => 'value'], [], $this->stringTranslation));
99 $this->stringTranslation->expects($this->once())
100 ->method('translateString')
101 ->with($this->pluginDefinition['title'])
102 ->will($this->returnValue('Example value'));
104 $this->setupContextualLinkDefault();
105 $request = new Request();
106 $this->assertEquals('Example value', $this->contextualLinkDefault->getTitle($request));
110 * @covers ::getRouteName
112 public function testGetRouteName($route_name = 'test_route_name') {
113 $this->pluginDefinition['route_name'] = $route_name;
114 $this->setupContextualLinkDefault();
116 $this->assertEquals($route_name, $this->contextualLinkDefault->getRouteName());
122 public function testGetGroup($group_name = 'test_group') {
123 $this->pluginDefinition['group'] = $group_name;
124 $this->setupContextualLinkDefault();
126 $this->assertEquals($group_name, $this->contextualLinkDefault->getGroup());
130 * @covers ::getOptions
132 public function testGetOptions($options = ['key' => 'value']) {
133 $this->pluginDefinition['options'] = $options;
134 $this->setupContextualLinkDefault();
136 $this->assertEquals($options, $this->contextualLinkDefault->getOptions());
140 * @covers ::getWeight
142 public function testGetWeight($weight = 5) {
143 $this->pluginDefinition['weight'] = $weight;
144 $this->setupContextualLinkDefault();
146 $this->assertEquals($weight, $this->contextualLinkDefault->getWeight());