View source
<?php
namespace Drupal\Tests\Core\Menu;
use Drupal\Core\Menu\StaticMenuLinkOverrides;
use Drupal\Tests\UnitTestCase;
class StaticMenuLinkOverridesTest extends UnitTestCase {
public function testConstruct() {
$config_factory = $this
->getConfigFactoryStub([
'core.menu.static_menu_link_overrides' => [],
]);
$static_override = new StaticMenuLinkOverrides($config_factory);
$this
->assertAttributeEquals($config_factory, 'configFactory', $static_override);
}
public function testReload() {
$config_factory = $this
->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$config_factory
->expects($this
->at(0))
->method('reset')
->with('core.menu.static_menu_link_overrides');
$static_override = new StaticMenuLinkOverrides($config_factory);
$static_override
->reload();
}
public function testLoadOverride($overrides, $id, $expected) {
$config_factory = $this
->getConfigFactoryStub([
'core.menu.static_menu_link_overrides' => [
'definitions' => $overrides,
],
]);
$static_override = new StaticMenuLinkOverrides($config_factory);
$this
->assertEquals($expected, $static_override
->loadOverride($id));
}
public function providerTestLoadOverride() {
$data = [];
$data[] = [
[
'test1' => [
'parent' => 'test0',
],
],
NULL,
[],
];
$data[] = [
[
'test1' => [
'parent' => 'test0',
],
],
'test1',
[
'parent' => 'test0',
],
];
$data[] = [
[
'test1' => [
'parent' => 'test0',
],
],
'test2',
[],
];
$data[] = [
[
'test1__la___ma' => [
'parent' => 'test0',
],
],
'test1.la__ma',
[
'parent' => 'test0',
],
];
return $data;
}
public function testLoadMultipleOverrides() {
$overrides = [];
$overrides['test1'] = [
'parent' => 'test0',
];
$overrides['test2'] = [
'parent' => 'test1',
];
$overrides['test1__la___ma'] = [
'parent' => 'test2',
];
$config_factory = $this
->getConfigFactoryStub([
'core.menu.static_menu_link_overrides' => [
'definitions' => $overrides,
],
]);
$static_override = new StaticMenuLinkOverrides($config_factory);
$this
->assertEquals([
'test1' => [
'parent' => 'test0',
],
'test1.la__ma' => [
'parent' => 'test2',
],
], $static_override
->loadMultipleOverrides([
'test1',
'test1.la__ma',
]));
}
public function testSaveOverride() {
$config = $this
->getMockBuilder('Drupal\\Core\\Config\\Config')
->disableOriginalConstructor()
->getMock();
$config
->expects($this
->at(0))
->method('get')
->with('definitions')
->will($this
->returnValue([]));
$config
->expects($this
->at(1))
->method('get')
->with('definitions')
->will($this
->returnValue([]));
$definition_save_1 = [
'definitions' => [
'test1' => [
'parent' => 'test0',
'menu_name' => '',
'weight' => 0,
'expanded' => FALSE,
'enabled' => FALSE,
],
],
];
$definitions_save_2 = [
'definitions' => [
'test1' => [
'parent' => 'test0',
'menu_name' => '',
'weight' => 0,
'expanded' => FALSE,
'enabled' => FALSE,
],
'test1__la___ma' => [
'parent' => 'test1',
'menu_name' => '',
'weight' => 0,
'expanded' => FALSE,
'enabled' => FALSE,
],
],
];
$config
->expects($this
->at(2))
->method('set')
->with('definitions', $definition_save_1['definitions'])
->will($this
->returnSelf());
$config
->expects($this
->at(3))
->method('save');
$config
->expects($this
->at(4))
->method('get')
->with('definitions')
->will($this
->returnValue($definition_save_1['definitions']));
$config
->expects($this
->at(5))
->method('get')
->with('definitions')
->will($this
->returnValue($definition_save_1['definitions']));
$config
->expects($this
->at(6))
->method('set')
->with('definitions', $definitions_save_2['definitions'])
->will($this
->returnSelf());
$config
->expects($this
->at(7))
->method('save');
$config_factory = $this
->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$config_factory
->expects($this
->once())
->method('getEditable')
->will($this
->returnValue($config));
$static_override = new StaticMenuLinkOverrides($config_factory);
$static_override
->saveOverride('test1', [
'parent' => 'test0',
]);
$static_override
->saveOverride('test1.la__ma', [
'parent' => 'test1',
]);
}
public function testDeleteOverrides($ids, array $old_definitions, array $new_definitions) {
$config = $this
->getMockBuilder('Drupal\\Core\\Config\\Config')
->disableOriginalConstructor()
->getMock();
$config
->expects($this
->at(0))
->method('get')
->with('definitions')
->will($this
->returnValue($old_definitions));
if ($old_definitions != $new_definitions) {
$config
->expects($this
->at(1))
->method('set')
->with('definitions', $new_definitions)
->will($this
->returnSelf());
$config
->expects($this
->at(2))
->method('save');
}
$config_factory = $this
->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$config_factory
->expects($this
->once())
->method('getEditable')
->will($this
->returnValue($config));
$static_override = new StaticMenuLinkOverrides($config_factory);
if (is_array($ids)) {
$static_override
->deleteMultipleOverrides($ids);
}
else {
$static_override
->deleteOverride($ids);
}
}
public function providerTestDeleteOverrides() {
$data = [];
$data[] = [
'test0',
[],
[],
];
$data[] = [
'test1',
[
'test1' => [
'parent' => 'test0',
],
],
[],
];
$data[] = [
'test1.la__ma',
[
'test1__la___ma' => [
'parent' => 'test0',
],
],
[],
];
$data[] = [
[
'test1.la__ma',
'test1',
],
[
'test1' => [
'parent' => 'test0',
],
'test1__la___ma' => [
'parent' => 'test0',
],
],
[],
];
return $data;
}
}