3 namespace Drupal\Tests\Core\Menu;
5 use Drupal\Core\Menu\StaticMenuLinkOverrides;
6 use Drupal\Tests\UnitTestCase;
9 * @coversDefaultClass \Drupal\Core\Menu\StaticMenuLinkOverrides
12 class StaticMenuLinkOverridesTest extends UnitTestCase {
15 * Tests the constructor.
17 * @covers ::__construct
19 public function testConstruct() {
20 $config_factory = $this->getConfigFactoryStub(['core.menu.static_menu_link_overrides' => []]);
21 $static_override = new StaticMenuLinkOverrides($config_factory);
23 $this->assertAttributeEquals($config_factory, 'configFactory', $static_override);
27 * Tests the reload method.
31 public function testReload() {
32 $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
33 $config_factory->expects($this->at(0))
35 ->with('core.menu.static_menu_link_overrides');
37 $static_override = new StaticMenuLinkOverrides($config_factory);
39 $static_override->reload();
43 * Tests the loadOverride method.
45 * @dataProvider providerTestLoadOverride
47 * @covers ::loadOverride
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);
54 $this->assertEquals($expected, $static_override->loadOverride($id));
58 * Provides test data for testLoadOverride.
60 public function providerTestLoadOverride() {
63 $data[] = [['test1' => ['parent' => 'test0']], NULL, []];
65 $data[] = [['test1' => ['parent' => 'test0']], 'test1', ['parent' => 'test0']];
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']];
75 * Tests the loadMultipleOverrides method.
77 * @covers ::loadMultipleOverrides
80 public function testLoadMultipleOverrides() {
82 $overrides['test1'] = ['parent' => 'test0'];
83 $overrides['test2'] = ['parent' => 'test1'];
84 $overrides['test1__la___ma'] = ['parent' => 'test2'];
86 $config_factory = $this->getConfigFactoryStub(['core.menu.static_menu_link_overrides' => ['definitions' => $overrides]]);
87 $static_override = new StaticMenuLinkOverrides($config_factory);
89 $this->assertEquals(['test1' => ['parent' => 'test0'], 'test1.la__ma' => ['parent' => 'test2']], $static_override->loadMultipleOverrides(['test1', 'test1.la__ma']));
93 * Tests the saveOverride method.
95 * @covers ::saveOverride
96 * @covers ::loadOverride
99 public function testSaveOverride() {
100 $config = $this->getMockBuilder('Drupal\Core\Config\Config')
101 ->disableOriginalConstructor()
103 $config->expects($this->at(0))
105 ->with('definitions')
106 ->will($this->returnValue([]));
107 $config->expects($this->at(1))
109 ->with('definitions')
110 ->will($this->returnValue([]));
112 $definition_save_1 = [
114 'test1' => ['parent' => 'test0', 'menu_name' => '', 'weight' => 0, 'expanded' => FALSE, 'enabled' => FALSE]
117 $definitions_save_2 = [
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]
123 $config->expects($this->at(2))
125 ->with('definitions', $definition_save_1['definitions'])
126 ->will($this->returnSelf());
127 $config->expects($this->at(3))
129 $config->expects($this->at(4))
131 ->with('definitions')
132 ->will($this->returnValue($definition_save_1['definitions']));
133 $config->expects($this->at(5))
135 ->with('definitions')
136 ->will($this->returnValue($definition_save_1['definitions']));
137 $config->expects($this->at(6))
139 ->with('definitions', $definitions_save_2['definitions'])
140 ->will($this->returnSelf());
141 $config->expects($this->at(7))
144 $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
145 $config_factory->expects($this->once())
146 ->method('getEditable')
147 ->will($this->returnValue($config));
149 $static_override = new StaticMenuLinkOverrides($config_factory);
151 $static_override->saveOverride('test1', ['parent' => 'test0']);
152 $static_override->saveOverride('test1.la__ma', ['parent' => 'test1']);
156 * Tests the deleteOverride and deleteOverrides method.
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.
165 * @dataProvider providerTestDeleteOverrides
167 public function testDeleteOverrides($ids, array $old_definitions, array $new_definitions) {
168 $config = $this->getMockBuilder('Drupal\Core\Config\Config')
169 ->disableOriginalConstructor()
171 $config->expects($this->at(0))
173 ->with('definitions')
174 ->will($this->returnValue($old_definitions));
176 // Only save if the definitions changes.
177 if ($old_definitions != $new_definitions) {
178 $config->expects($this->at(1))
180 ->with('definitions', $new_definitions)
181 ->will($this->returnSelf());
182 $config->expects($this->at(2))
186 $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
187 $config_factory->expects($this->once())
188 ->method('getEditable')
189 ->will($this->returnValue($config));
191 $static_override = new StaticMenuLinkOverrides($config_factory);
193 if (is_array($ids)) {
194 $static_override->deleteMultipleOverrides($ids);
197 $static_override->deleteOverride($ids);
202 * Provides test data for testDeleteOverrides.
204 public function providerTestDeleteOverrides() {
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']], []];