class LocalActionManagerTest

Same name in this branch
  1. 9 core/tests/Drupal/KernelTests/Core/Menu/LocalActionManagerTest.php \Drupal\KernelTests\Core\Menu\LocalActionManagerTest
Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Menu/LocalActionManagerTest.php \Drupal\KernelTests\Core\Menu\LocalActionManagerTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Menu/LocalActionManagerTest.php \Drupal\Tests\Core\Menu\LocalActionManagerTest
  3. 10 core/tests/Drupal/KernelTests/Core/Menu/LocalActionManagerTest.php \Drupal\KernelTests\Core\Menu\LocalActionManagerTest
  4. 10 core/tests/Drupal/Tests/Core/Menu/LocalActionManagerTest.php \Drupal\Tests\Core\Menu\LocalActionManagerTest
  5. 11.x core/tests/Drupal/KernelTests/Core/Menu/LocalActionManagerTest.php \Drupal\KernelTests\Core\Menu\LocalActionManagerTest
  6. 11.x core/tests/Drupal/Tests/Core/Menu/LocalActionManagerTest.php \Drupal\Tests\Core\Menu\LocalActionManagerTest

@coversDefaultClass \Drupal\Core\Menu\LocalActionManager @group Menu

Hierarchy

Expanded class hierarchy of LocalActionManagerTest

File

core/tests/Drupal/Tests/Core/Menu/LocalActionManagerTest.php, line 34

Namespace

Drupal\Tests\Core\Menu
View source
class LocalActionManagerTest extends UnitTestCase {
    
    /**
     * The mocked argument resolver.
     *
     * @var \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $argumentResolver;
    
    /**
     * The mocked request.
     *
     * @var \Symfony\Component\HttpFoundation\Request|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $request;
    
    /**
     * The mocked module handler.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $moduleHandler;
    
    /**
     * The mocked router provider.
     *
     * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $routeProvider;
    
    /**
     * The mocked cache backend.
     *
     * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $cacheBackend;
    
    /**
     * The mocked access manager.
     *
     * @var \Drupal\Core\Access\AccessManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $accessManager;
    
    /**
     * The mocked account.
     *
     * @var \Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $account;
    
    /**
     * The mocked factory.
     *
     * @var \Drupal\Component\Plugin\Factory\FactoryInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $factory;
    
    /**
     * The mocked plugin discovery.
     *
     * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $discovery;
    
    /**
     * The tested local action manager.
     *
     * @var \Drupal\Tests\Core\Menu\TestLocalActionManager
     */
    protected $localActionManager;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        $this->argumentResolver = $this->createMock('\\Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolverInterface');
        $this->request = $this->createMock('Symfony\\Component\\HttpFoundation\\Request');
        $this->routeProvider = $this->createMock('Drupal\\Core\\Routing\\RouteProviderInterface');
        $this->moduleHandler = $this->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
        $this->cacheBackend = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
        $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
        $cache_contexts_manager->assertValidTokens(Argument::any())
            ->willReturn(TRUE);
        $container = new Container();
        $container->set('cache_contexts_manager', $cache_contexts_manager->reveal());
        \Drupal::setContainer($container);
        $access_result = (new AccessResultForbidden())->cachePerPermissions();
        $this->accessManager = $this->createMock('Drupal\\Core\\Access\\AccessManagerInterface');
        $this->accessManager
            ->expects($this->any())
            ->method('checkNamedRoute')
            ->willReturn($access_result);
        $this->account = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
        $this->discovery = $this->createMock('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface');
        $this->factory = $this->createMock('Drupal\\Component\\Plugin\\Factory\\FactoryInterface');
        $route_match = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
        $this->localActionManager = new TestLocalActionManager($this->argumentResolver, $this->request, $route_match, $this->routeProvider, $this->moduleHandler, $this->cacheBackend, $this->accessManager, $this->account, $this->discovery, $this->factory);
    }
    
    /**
     * @covers ::getTitle
     */
    public function testGetTitle() {
        $local_action = $this->createMock('Drupal\\Core\\Menu\\LocalActionInterface');
        $local_action->expects($this->once())
            ->method('getTitle')
            ->with('test');
        $this->argumentResolver
            ->expects($this->once())
            ->method('getArguments')
            ->with($this->request, [
            $local_action,
            'getTitle',
        ])
            ->willReturn([
            'test',
        ]);
        $this->localActionManager
            ->getTitle($local_action);
    }
    
    /**
     * @covers ::getActionsForRoute
     *
     * @dataProvider getActionsForRouteProvider
     */
    public function testGetActionsForRoute($route_appears, array $plugin_definitions, array $expected_actions) {
        $this->discovery
            ->expects($this->any())
            ->method('getDefinitions')
            ->willReturn($plugin_definitions);
        $map = [];
        foreach ($plugin_definitions as $plugin_id => $plugin_definition) {
            $plugin = $this->createMock('Drupal\\Core\\Menu\\LocalActionInterface');
            $plugin->expects($this->any())
                ->method('getRouteName')
                ->willReturn($plugin_definition['route_name']);
            $plugin->expects($this->any())
                ->method('getRouteParameters')
                ->willReturn($plugin_definition['route_parameters'] ?? []);
            $plugin->expects($this->any())
                ->method('getTitle')
                ->willReturn($plugin_definition['title']);
            $this->argumentResolver
                ->expects($this->any())
                ->method('getArguments')
                ->with($this->request, [
                $plugin,
                'getTitle',
            ])
                ->willReturn([]);
            $plugin->expects($this->any())
                ->method('getWeight')
                ->willReturn($plugin_definition['weight']);
            $this->argumentResolver
                ->expects($this->any())
                ->method('getArguments')
                ->with($this->request, [
                $plugin,
                'getTitle',
            ])
                ->willReturn([]);
            $map[] = [
                $plugin_id,
                [],
                $plugin,
            ];
        }
        $this->factory
            ->expects($this->any())
            ->method('createInstance')
            ->willReturnMap($map);
        $this->assertEquals($expected_actions, $this->localActionManager
            ->getActionsForRoute($route_appears));
    }
    public function getActionsForRouteProvider() {
        $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
        $cache_contexts_manager->assertValidTokens(Argument::any())
            ->willReturn(TRUE);
        $container = new Container();
        $container->set('cache_contexts_manager', $cache_contexts_manager->reveal());
        \Drupal::setContainer($container);
        // Single available and single expected plugins.
        $data[] = [
            'test_route',
            [
                'plugin_id_1' => [
                    'appears_on' => [
                        'test_route',
                    ],
                    'route_name' => 'test_route_2',
                    'title' => 'Plugin ID 1',
                    'weight' => 0,
                ],
            ],
            [
                '#cache' => [
                    'tags' => [],
                    'contexts' => [
                        'route',
                        'user.permissions',
                    ],
                    'max-age' => 0,
                ],
                'plugin_id_1' => [
                    '#theme' => 'menu_local_action',
                    '#link' => [
                        'title' => 'Plugin ID 1',
                        'url' => Url::fromRoute('test_route_2'),
                        'localized_options' => '',
                    ],
                    '#access' => AccessResult::forbidden()->cachePerPermissions(),
                    '#weight' => 0,
                ],
            ],
        ];
        // Multiple available and single expected plugins.
        $data[] = [
            'test_route',
            [
                'plugin_id_1' => [
                    'appears_on' => [
                        'test_route',
                    ],
                    'route_name' => 'test_route_2',
                    'title' => 'Plugin ID 1',
                    'weight' => 0,
                ],
                'plugin_id_2' => [
                    'appears_on' => [
                        'test_route2',
                    ],
                    'route_name' => 'test_route_3',
                    'title' => 'Plugin ID 2',
                    'weight' => 0,
                ],
            ],
            [
                '#cache' => [
                    'tags' => [],
                    'contexts' => [
                        'route',
                        'user.permissions',
                    ],
                    'max-age' => 0,
                ],
                'plugin_id_1' => [
                    '#theme' => 'menu_local_action',
                    '#link' => [
                        'title' => 'Plugin ID 1',
                        'url' => Url::fromRoute('test_route_2'),
                        'localized_options' => '',
                    ],
                    '#access' => AccessResult::forbidden()->cachePerPermissions(),
                    '#weight' => 0,
                ],
            ],
        ];
        // Multiple available and multiple expected plugins and specified weight.
        $data[] = [
            'test_route',
            [
                'plugin_id_1' => [
                    'appears_on' => [
                        'test_route',
                    ],
                    'route_name' => 'test_route_2',
                    'title' => 'Plugin ID 1',
                    'weight' => 1,
                ],
                'plugin_id_2' => [
                    'appears_on' => [
                        'test_route',
                    ],
                    'route_name' => 'test_route_3',
                    'title' => 'Plugin ID 2',
                    'weight' => 0,
                ],
            ],
            [
                '#cache' => [
                    'contexts' => [
                        'route',
                        'user.permissions',
                    ],
                    'tags' => [],
                    'max-age' => 0,
                ],
                'plugin_id_1' => [
                    '#theme' => 'menu_local_action',
                    '#link' => [
                        'title' => 'Plugin ID 1',
                        'url' => Url::fromRoute('test_route_2'),
                        'localized_options' => '',
                    ],
                    '#access' => AccessResult::forbidden()->cachePerPermissions(),
                    '#weight' => 1,
                ],
                'plugin_id_2' => [
                    '#theme' => 'menu_local_action',
                    '#link' => [
                        'title' => 'Plugin ID 2',
                        'url' => Url::fromRoute('test_route_3'),
                        'localized_options' => '',
                    ],
                    '#access' => AccessResult::forbidden()->cachePerPermissions(),
                    '#weight' => 0,
                ],
            ],
        ];
        // Two plugins with the same route name but different route parameters.
        $data[] = [
            'test_route',
            [
                'plugin_id_1' => [
                    'appears_on' => [
                        'test_route',
                    ],
                    'route_name' => 'test_route_2',
                    'route_parameters' => [
                        'test1',
                    ],
                    'title' => 'Plugin ID 1',
                    'weight' => 1,
                ],
                'plugin_id_2' => [
                    'appears_on' => [
                        'test_route',
                    ],
                    'route_name' => 'test_route_2',
                    'route_parameters' => [
                        'test2',
                    ],
                    'title' => 'Plugin ID 2',
                    'weight' => 0,
                ],
            ],
            [
                '#cache' => [
                    'contexts' => [
                        'route',
                        'user.permissions',
                    ],
                    'tags' => [],
                    'max-age' => 0,
                ],
                'plugin_id_1' => [
                    '#theme' => 'menu_local_action',
                    '#link' => [
                        'title' => 'Plugin ID 1',
                        'url' => Url::fromRoute('test_route_2', [
                            'test1',
                        ]),
                        'localized_options' => '',
                    ],
                    '#access' => AccessResult::forbidden()->cachePerPermissions(),
                    '#weight' => 1,
                ],
                'plugin_id_2' => [
                    '#theme' => 'menu_local_action',
                    '#link' => [
                        'title' => 'Plugin ID 2',
                        'url' => Url::fromRoute('test_route_2', [
                            'test2',
                        ]),
                        'localized_options' => '',
                    ],
                    '#access' => AccessResult::forbidden()->cachePerPermissions(),
                    '#weight' => 0,
                ],
            ],
        ];
        return $data;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
LocalActionManagerTest::$accessManager protected property The mocked access manager.
LocalActionManagerTest::$account protected property The mocked account.
LocalActionManagerTest::$argumentResolver protected property The mocked argument resolver.
LocalActionManagerTest::$cacheBackend protected property The mocked cache backend.
LocalActionManagerTest::$discovery protected property The mocked plugin discovery.
LocalActionManagerTest::$factory protected property The mocked factory.
LocalActionManagerTest::$localActionManager protected property The tested local action manager.
LocalActionManagerTest::$moduleHandler protected property The mocked module handler.
LocalActionManagerTest::$request protected property The mocked request.
LocalActionManagerTest::$routeProvider protected property The mocked router provider.
LocalActionManagerTest::getActionsForRouteProvider public function
LocalActionManagerTest::setUp protected function Overrides UnitTestCase::setUp
LocalActionManagerTest::testGetActionsForRoute public function @covers ::getActionsForRoute
LocalActionManagerTest::testGetTitle public function @covers ::getTitle
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.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function

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