b287f12eb3aa3052ff10ee9e0f2df77f9461dc28
[yaffs-website] / StaticMenuLinkOverridesTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Menu;
4
5 use Drupal\Core\Menu\StaticMenuLinkOverrides;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Menu\StaticMenuLinkOverrides
10  * @group Menu
11  */
12 class StaticMenuLinkOverridesTest extends UnitTestCase {
13
14   /**
15    * Tests the constructor.
16    *
17    * @covers ::__construct
18    */
19   public function testConstruct() {
20     $config_factory = $this->getConfigFactoryStub(['core.menu.static_menu_link_overrides' => []]);
21     $static_override = new StaticMenuLinkOverrides($config_factory);
22
23     $this->assertAttributeEquals($config_factory, 'configFactory', $static_override);
24   }
25
26   /**
27    * Tests the reload method.
28    *
29    * @covers ::reload
30    */
31   public function testReload() {
32     $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
33     $config_factory->expects($this->at(0))
34       ->method('reset')
35       ->with('core.menu.static_menu_link_overrides');
36
37     $static_override = new StaticMenuLinkOverrides($config_factory);
38
39     $static_override->reload();
40   }
41
42   /**
43    * Tests the loadOverride method.
44    *
45    * @dataProvider providerTestLoadOverride
46    *
47    * @covers ::loadOverride
48    * @covers ::getConfig
49    */
50   public function testLoadOverride($overrides, $id, $expected) {
51     $config_factory = $this->getConfigFactoryStub(['core.menu.static_menu_link_overrides' => ['definitions' => $overrides]]);
52     $static_override = new StaticMenuLinkOverrides($config_factory);
53
54     $this->assertEquals($expected, $static_override->loadOverride($id));
55   }
56
57   /**
58    * Provides test data for testLoadOverride.
59    */
60   public function providerTestLoadOverride() {
61     $data = [];
62     // No specified ID.
63     $data[] = [['test1' => ['parent' => 'test0']], NULL, []];
64     // Valid ID.
65     $data[] = [['test1' => ['parent' => 'test0']], 'test1', ['parent' => 'test0']];
66     // Non existing ID.
67     $data[] = [['test1' => ['parent' => 'test0']], 'test2', []];
68     // Ensure that the ID is encoded properly
69     $data[] = [['test1__la___ma' => ['parent' => 'test0']], 'test1.la__ma', ['parent' => 'test0']];
70
71     return $data;
72   }
73
74   /**
75    * Tests the loadMultipleOverrides method.
76    *
77    * @covers ::loadMultipleOverrides
78    * @covers ::getConfig
79    */
80   public function testLoadMultipleOverrides() {
81     $overrides = [];
82     $overrides['test1'] = ['parent' => 'test0'];
83     $overrides['test2'] = ['parent' => 'test1'];
84     $overrides['test1__la___ma'] = ['parent' => 'test2'];
85
86     $config_factory = $this->getConfigFactoryStub(['core.menu.static_menu_link_overrides' => ['definitions' => $overrides]]);
87     $static_override = new StaticMenuLinkOverrides($config_factory);
88
89     $this->assertEquals(['test1' => ['parent' => 'test0'], 'test1.la__ma' => ['parent' => 'test2']], $static_override->loadMultipleOverrides(['test1', 'test1.la__ma']));
90   }
91
92   /**
93    * Tests the saveOverride method.
94    *
95    * @covers ::saveOverride
96    * @covers ::loadOverride
97    * @covers ::getConfig
98    */
99   public function testSaveOverride() {
100     $config = $this->getMockBuilder('Drupal\Core\Config\Config')
101       ->disableOriginalConstructor()
102       ->getMock();
103     $config->expects($this->at(0))
104       ->method('get')
105       ->with('definitions')
106       ->will($this->returnValue([]));
107     $config->expects($this->at(1))
108       ->method('get')
109       ->with('definitions')
110       ->will($this->returnValue([]));
111
112     $definition_save_1 = [
113       'definitions' => [
114         'test1' => ['parent' => 'test0', 'menu_name' => '', 'weight' => 0, 'expanded' => FALSE, 'enabled' => FALSE],
115       ],
116     ];
117     $definitions_save_2 = [
118       'definitions' => [
119         'test1' => ['parent' => 'test0', 'menu_name' => '', 'weight' => 0, 'expanded' => FALSE, 'enabled' => FALSE],
120         'test1__la___ma' => ['parent' => 'test1', 'menu_name' => '', 'weight' => 0, 'expanded' => FALSE, 'enabled' => FALSE],
121       ],
122     ];
123     $config->expects($this->at(2))
124       ->method('set')
125       ->with('definitions', $definition_save_1['definitions'])
126       ->will($this->returnSelf());
127     $config->expects($this->at(3))
128       ->method('save');
129     $config->expects($this->at(4))
130       ->method('get')
131       ->with('definitions')
132       ->will($this->returnValue($definition_save_1['definitions']));
133     $config->expects($this->at(5))
134       ->method('get')
135       ->with('definitions')
136       ->will($this->returnValue($definition_save_1['definitions']));
137     $config->expects($this->at(6))
138       ->method('set')
139       ->with('definitions', $definitions_save_2['definitions'])
140       ->will($this->returnSelf());
141     $config->expects($this->at(7))
142       ->method('save');
143
144     $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
145     $config_factory->expects($this->once())
146       ->method('getEditable')
147       ->will($this->returnValue($config));
148
149     $static_override = new StaticMenuLinkOverrides($config_factory);
150
151     $static_override->saveOverride('test1', ['parent' => 'test0']);
152     $static_override->saveOverride('test1.la__ma', ['parent' => 'test1']);
153   }
154
155   /**
156    * Tests the deleteOverride and deleteOverrides method.
157    *
158    * @param array|string $ids
159    *   Either a single ID or multiple ones as array.
160    * @param array $old_definitions
161    *   The definitions before the deleting
162    * @param array $new_definitions
163    *   The definitions after the deleting.
164    *
165    * @dataProvider providerTestDeleteOverrides
166    */
167   public function testDeleteOverrides($ids, array $old_definitions, array $new_definitions) {
168     $config = $this->getMockBuilder('Drupal\Core\Config\Config')
169       ->disableOriginalConstructor()
170       ->getMock();
171     $config->expects($this->at(0))
172       ->method('get')
173       ->with('definitions')
174       ->will($this->returnValue($old_definitions));
175
176     // Only save if the definitions changes.
177     if ($old_definitions != $new_definitions) {
178       $config->expects($this->at(1))
179         ->method('set')
180         ->with('definitions', $new_definitions)
181         ->will($this->returnSelf());
182       $config->expects($this->at(2))
183         ->method('save');
184     }
185
186     $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
187     $config_factory->expects($this->once())
188       ->method('getEditable')
189       ->will($this->returnValue($config));
190
191     $static_override = new StaticMenuLinkOverrides($config_factory);
192
193     if (is_array($ids)) {
194       $static_override->deleteMultipleOverrides($ids);
195     }
196     else {
197       $static_override->deleteOverride($ids);
198     }
199   }
200
201   /**
202    * Provides test data for testDeleteOverrides.
203    */
204   public function providerTestDeleteOverrides() {
205     $data = [];
206     // Delete a non existing ID.
207     $data[] = ['test0', [], []];
208     // Delete an existing ID.
209     $data[] = ['test1', ['test1' => ['parent' => 'test0']], []];
210     // Delete an existing ID with a special ID.
211     $data[] = ['test1.la__ma', ['test1__la___ma' => ['parent' => 'test0']], []];
212     // Delete multiple IDs.
213     $data[] = [['test1.la__ma', 'test1'], ['test1' => ['parent' => 'test0'], 'test1__la___ma' => ['parent' => 'test0']], []];
214
215     return $data;
216   }
217
218 }