diff --git a/core/lib/Drupal/Core/Menu/StaticMenuLinkOverrides.php b/core/lib/Drupal/Core/Menu/StaticMenuLinkOverrides.php index ebde906..fbde872 100644 --- a/core/lib/Drupal/Core/Menu/StaticMenuLinkOverrides.php +++ b/core/lib/Drupal/Core/Menu/StaticMenuLinkOverrides.php @@ -134,10 +134,9 @@ public function saveOverride($id, array $definition) { // Filter the overrides to only those that are expected. $definition = array_intersect_key($definition, $expected); if ($definition) { - $id = static::encodeId($id); $all_overrides = $this->getConfig()->get('definitions'); // Combine with any existing data. - $all_overrides[$id] = $definition + $this->loadOverride($id); + $all_overrides[static::encodeId($id)] = $definition + $this->loadOverride($id); $this->getConfig()->set('definitions', $all_overrides)->save(); } return array_keys($definition); diff --git a/core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php b/core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php index c436d6f..e68682f 100644 --- a/core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php @@ -105,52 +105,113 @@ public function testSaveOverride() { $config = $this->getMockBuilder('Drupal\Core\Config\Config') ->disableOriginalConstructor() ->getMock(); + $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface'); + $config_factory->expects($this->once()) + ->method('get') + ->will($this->returnValue($config)); + $static_override = new StaticMenuLinkOverrides($config_factory); + + $existing_defintions = []; + $new_definitions = ['test1' => ['parent' => 'test0']]; $config->expects($this->at(0)) ->method('get') ->with('definitions') - ->will($this->returnValue(array())); + ->willReturn($existing_defintions); $config->expects($this->at(1)) ->method('get') ->with('definitions') - ->will($this->returnValue(array())); - - $definition_save_1 = array('definitions' => array('test1' => array('parent' => 'test0'))); - $definitions_save_2 = array( - 'definitions' => array( - 'test1' => array('parent' => 'test0'), - 'test1__la___ma' => array('parent' => 'test1') - ) - ); + ->willReturn($existing_defintions); $config->expects($this->at(2)) ->method('set') - ->with('definitions', $definition_save_1['definitions']) - ->will($this->returnSelf()); + ->with('definitions', $new_definitions) + ->willReturnSelf(); $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'); + $static_override->saveOverride('test1', array('parent' => 'test0')); + } + + /** + * Tests the saveOverride method when there are exisitng definitions. + * + * @covers ::saveOverride + * @covers ::loadOverride + * @covers ::getConfig + */ + public function testSaveOverrideExistingDefinitions() { + $config = $this->getMockBuilder('Drupal\Core\Config\Config') + ->disableOriginalConstructor() + ->getMock(); + $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface'); + $config_factory->expects($this->once()) + ->method('get') + ->will($this->returnValue($config)); + $static_override = new StaticMenuLinkOverrides($config_factory); + + $existing_definitions = ['test1' => ['parent' => 'test0']]; + $new_definitions = [ + 'test1' => ['parent' => 'test0'], + 'test1__la___ma' => ['parent' => 'test1'] + ]; + $config->expects($this->at(0)) + ->method('get') + ->with('definitions') + ->willReturn($existing_definitions); + $config->expects($this->at(1)) + ->method('get') + ->with('definitions') + ->willReturn($existing_definitions); + $config->expects($this->at(2)) + ->method('set') + ->with('definitions', $new_definitions) + ->willReturnSelf(); + $config->expects($this->at(3)) + ->method('save'); + + $static_override->saveOverride('test1.la__ma', array('parent' => 'test1')); + } + + /** + * Tests the saveOverride method when it has to merge configuration. + * + * @covers ::saveOverride + * @covers ::loadOverride + * @covers ::getConfig + */ + public function testSaveOverrideMergeWithExistingDefinitions() { + $config = $this->getMockBuilder('Drupal\Core\Config\Config') + ->disableOriginalConstructor() + ->getMock(); $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface'); $config_factory->expects($this->once()) ->method('get') ->will($this->returnValue($config)); - $static_override = new StaticMenuLinkOverrides($config_factory); - $static_override->saveOverride('test1', array('parent' => 'test0')); - $static_override->saveOverride('test1.la__ma', array('parent' => 'test1')); + $existing_definitions = [ + 'test1' => ['parent' => 'test0'], + 'test1__la___ma' => ['parent' => 'test1'] + ]; + $new_definitions = [ + 'test1' => ['parent' => 'test0'], + 'test1__la___ma' => ['parent' => 'test1', 'expanded'=> TRUE] + ]; + $config->expects($this->at(0)) + ->method('get') + ->with('definitions') + ->willReturn($existing_definitions); + $config->expects($this->at(1)) + ->method('get') + ->with('definitions') + ->willReturn($existing_definitions); + $config->expects($this->at(2)) + ->method('set') + ->with('definitions', $new_definitions) + ->willReturnSelf(); + $config->expects($this->at(3)) + ->method('save'); + + $static_override->saveOverride('test1.la__ma', array('expanded' => TRUE)); } /**