class StaticMenuLinkOverridesTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php \Drupal\Tests\Core\Menu\StaticMenuLinkOverridesTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php \Drupal\Tests\Core\Menu\StaticMenuLinkOverridesTest
  3. 11.x core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php \Drupal\Tests\Core\Menu\StaticMenuLinkOverridesTest

@coversDefaultClass \Drupal\Core\Menu\StaticMenuLinkOverrides @group Menu

Hierarchy

Expanded class hierarchy of StaticMenuLinkOverridesTest

File

core/tests/Drupal/Tests/Core/Menu/StaticMenuLinkOverridesTest.php, line 14

Namespace

Drupal\Tests\Core\Menu
View source
class StaticMenuLinkOverridesTest extends UnitTestCase {
    
    /**
     * Tests the reload method.
     *
     * @covers ::reload
     */
    public function testReload() : void {
        $config_factory = $this->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
        $config_factory->expects($this->once())
            ->method('reset')
            ->with('core.menu.static_menu_link_overrides');
        $static_override = new StaticMenuLinkOverrides($config_factory);
        $static_override->reload();
    }
    
    /**
     * Tests the loadOverride method.
     *
     * @dataProvider providerTestLoadOverride
     *
     * @covers ::loadOverride
     * @covers ::getConfig
     */
    public function testLoadOverride($overrides, $id, $expected) : void {
        $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));
    }
    
    /**
     * Provides test data for testLoadOverride.
     */
    public static function providerTestLoadOverride() {
        $data = [];
        // Valid ID.
        $data[] = [
            [
                'test1' => [
                    'parent' => 'test0',
                ],
            ],
            'test1',
            [
                'parent' => 'test0',
            ],
        ];
        // Non existing ID.
        $data[] = [
            [
                'test1' => [
                    'parent' => 'test0',
                ],
            ],
            'test2',
            [],
        ];
        // Ensure that the ID is encoded properly
        $data[] = [
            [
                'test1__la___ma' => [
                    'parent' => 'test0',
                ],
            ],
            'test1.la__ma',
            [
                'parent' => 'test0',
            ],
        ];
        return $data;
    }
    
    /**
     * Tests the loadMultipleOverrides method.
     *
     * @covers ::loadMultipleOverrides
     * @covers ::getConfig
     */
    public function testLoadMultipleOverrides() : void {
        $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',
        ]));
    }
    
    /**
     * Tests the saveOverride method.
     *
     * @covers ::saveOverride
     * @covers ::loadOverride
     * @covers ::getConfig
     */
    public function testSaveOverride() : void {
        $config = $this->getMockBuilder('Drupal\\Core\\Config\\Config')
            ->disableOriginalConstructor()
            ->getMock();
        $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->exactly(4))
            ->method('get')
            ->with('definitions')
            ->willReturnOnConsecutiveCalls([], [], $definition_save_1['definitions'], $definition_save_1['definitions']);
        $definitions = [
            $definition_save_1['definitions'],
            $definitions_save_2['definitions'],
        ];
        $config->expects($this->exactly(count($definitions)))
            ->method('set')
            ->with('definitions', $this->callback(function (array $value) use (&$definitions) : bool {
            return array_shift($definitions) === $value;
        }))
            ->willReturnSelf();
        $config->expects($this->exactly(2))
            ->method('save');
        $config_factory = $this->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
        $config_factory->expects($this->once())
            ->method('getEditable')
            ->willReturn($config);
        $static_override = new StaticMenuLinkOverrides($config_factory);
        $static_override->saveOverride('test1', [
            'parent' => 'test0',
        ]);
        $static_override->saveOverride('test1.la__ma', [
            'parent' => 'test1',
        ]);
    }
    
    /**
     * Tests the deleteOverride and deleteOverrides method.
     *
     * @param array|string $ids
     *   Either a single ID or multiple ones as array.
     * @param array $old_definitions
     *   The definitions before the deleting
     * @param array $new_definitions
     *   The definitions after the deleting.
     *
     * @dataProvider providerTestDeleteOverrides
     */
    public function testDeleteOverrides($ids, array $old_definitions, array $new_definitions) : void {
        $config = $this->getMockBuilder('Drupal\\Core\\Config\\Config')
            ->disableOriginalConstructor()
            ->getMock();
        $config->expects($this->once())
            ->method('get')
            ->with('definitions')
            ->willReturn($old_definitions);
        // Only save if the definitions changes.
        $config->expects($old_definitions != $new_definitions ? $this->once() : $this->never())
            ->method('set')
            ->with('definitions', $new_definitions)
            ->willReturnSelf();
        $config->expects($old_definitions != $new_definitions ? $this->once() : $this->never())
            ->method('save');
        $config_factory = $this->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
        $config_factory->expects($this->once())
            ->method('getEditable')
            ->willReturn($config);
        $static_override = new StaticMenuLinkOverrides($config_factory);
        if (is_array($ids)) {
            $static_override->deleteMultipleOverrides($ids);
        }
        else {
            $static_override->deleteOverride($ids);
        }
    }
    
    /**
     * Provides test data for testDeleteOverrides.
     */
    public static function providerTestDeleteOverrides() {
        $data = [];
        // Delete a non existing ID.
        $data[] = [
            'test0',
            [],
            [],
        ];
        // Delete an existing ID.
        $data[] = [
            'test1',
            [
                'test1' => [
                    'parent' => 'test0',
                ],
            ],
            [],
        ];
        // Delete an existing ID with a special ID.
        $data[] = [
            'test1.la__ma',
            [
                'test1__la___ma' => [
                    'parent' => 'test0',
                ],
            ],
            [],
        ];
        // Delete multiple IDs.
        $data[] = [
            [
                'test1.la__ma',
                'test1',
            ],
            [
                'test1' => [
                    'parent' => 'test0',
                ],
                'test1__la___ma' => [
                    'parent' => 'test0',
                ],
            ],
            [],
        ];
        return $data;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
StaticMenuLinkOverridesTest::providerTestDeleteOverrides public static function Provides test data for testDeleteOverrides.
StaticMenuLinkOverridesTest::providerTestLoadOverride public static function Provides test data for testLoadOverride.
StaticMenuLinkOverridesTest::testDeleteOverrides public function Tests the deleteOverride and deleteOverrides method.
StaticMenuLinkOverridesTest::testLoadMultipleOverrides public function Tests the loadMultipleOverrides method.
StaticMenuLinkOverridesTest::testLoadOverride public function Tests the loadOverride method.
StaticMenuLinkOverridesTest::testReload public function Tests the reload method.
StaticMenuLinkOverridesTest::testSaveOverride public function Tests the saveOverride method.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 358
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.