a956d83467b248ac6359d4de6a56464e5cb97923
[yaffs-website] / ContextualLinkDefaultTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Menu;
4
5 use Drupal\Core\Menu\ContextualLinkDefault;
6 use Drupal\Core\StringTranslation\TranslatableMarkup;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
9
10 /**
11  * @group Menu
12  * @coversDefaultClass \Drupal\Core\Menu\ContextualLinkDefault
13  */
14 class ContextualLinkDefaultTest extends UnitTestCase {
15
16   /**
17    * The tested contextual link default plugin.
18    *
19    * @var \Drupal\Core\Menu\ContextualLinkDefault
20    */
21   protected $contextualLinkDefault;
22
23   /**
24    * The used plugin configuration.
25    *
26    * @var array
27    */
28   protected $config = [];
29
30   /**
31    * The used plugin ID.
32    *
33    * @var string
34    */
35   protected $pluginId = 'contextual_link_default';
36
37   /**
38    * The used plugin definition.
39    *
40    * @var array
41    */
42   protected $pluginDefinition = [
43     'id' => 'contextual_link_default',
44   ];
45
46   /**
47    * The mocked translator.
48    *
49    * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
50    */
51   protected $stringTranslation;
52
53   protected function setUp() {
54     parent::setUp();
55
56     $this->stringTranslation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
57   }
58
59   protected function setupContextualLinkDefault() {
60     $this->contextualLinkDefault = new ContextualLinkDefault($this->config, $this->pluginId, $this->pluginDefinition);
61   }
62
63   /**
64    * @covers ::getTitle
65    */
66   public function testGetTitle() {
67     $title = 'Example';
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'));
73
74     $this->setupContextualLinkDefault();
75     $this->assertEquals('Example translated', $this->contextualLinkDefault->getTitle());
76   }
77
78   /**
79    * @covers ::getTitle
80    */
81   public function testGetTitleWithContext() {
82     $title = 'Example';
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'));
88
89     $this->setupContextualLinkDefault();
90     $this->assertEquals('Example translated with context', $this->contextualLinkDefault->getTitle());
91   }
92
93   /**
94    * @covers ::getTitle
95    */
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'));
103
104     $this->setupContextualLinkDefault();
105     $request = new Request();
106     $this->assertEquals('Example value', $this->contextualLinkDefault->getTitle($request));
107   }
108
109   /**
110    * @covers ::getRouteName
111    */
112   public function testGetRouteName($route_name = 'test_route_name') {
113     $this->pluginDefinition['route_name'] = $route_name;
114     $this->setupContextualLinkDefault();
115
116     $this->assertEquals($route_name, $this->contextualLinkDefault->getRouteName());
117   }
118
119   /**
120    * @covers ::getGroup
121    */
122   public function testGetGroup($group_name = 'test_group') {
123     $this->pluginDefinition['group'] = $group_name;
124     $this->setupContextualLinkDefault();
125
126     $this->assertEquals($group_name, $this->contextualLinkDefault->getGroup());
127   }
128
129   /**
130    * @covers ::getOptions
131    */
132   public function testGetOptions($options = ['key' => 'value']) {
133     $this->pluginDefinition['options'] = $options;
134     $this->setupContextualLinkDefault();
135
136     $this->assertEquals($options, $this->contextualLinkDefault->getOptions());
137   }
138
139   /**
140    * @covers ::getWeight
141    */
142   public function testGetWeight($weight = 5) {
143     $this->pluginDefinition['weight'] = $weight;
144     $this->setupContextualLinkDefault();
145
146     $this->assertEquals($weight, $this->contextualLinkDefault->getWeight());
147   }
148
149 }