class ViewPageControllerTest

Same name in other branches
  1. 9 core/modules/views/tests/src/Unit/Routing/ViewPageControllerTest.php \Drupal\Tests\views\Unit\Routing\ViewPageControllerTest
  2. 8.9.x core/modules/views/tests/src/Unit/Routing/ViewPageControllerTest.php \Drupal\Tests\views\Unit\Routing\ViewPageControllerTest
  3. 10 core/modules/views/tests/src/Unit/Routing/ViewPageControllerTest.php \Drupal\Tests\views\Unit\Routing\ViewPageControllerTest

@coversDefaultClass \Drupal\views\Routing\ViewPageController @group views

Hierarchy

Expanded class hierarchy of ViewPageControllerTest

File

core/modules/views/tests/src/Unit/Routing/ViewPageControllerTest.php, line 19

Namespace

Drupal\Tests\views\Unit\Routing
View source
class ViewPageControllerTest extends UnitTestCase {
    
    /**
     * The page controller of views.
     *
     * @var \Drupal\views\Routing\ViewPageController
     */
    public $pageController;
    
    /**
     * A render array expected for every page controller render array result.
     *
     * @var array
     */
    protected $defaultRenderArray = [
        '#cache_properties' => [
            '#view_id',
            '#view_display_show_admin_links',
            '#view_display_plugin_id',
        ],
        '#view_id' => 'test_page_view',
        '#view_display_plugin_id' => NULL,
        '#view_display_show_admin_links' => NULL,
    ];
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->pageController = new ViewPageController();
    }
    
    /**
     * Tests the page controller.
     */
    public function testPageController() : void {
        $build = [
            '#type' => 'view',
            '#name' => 'test_page_view',
            '#display_id' => 'default',
            '#embed' => FALSE,
            '#arguments' => [],
            '#cache' => [
                'keys' => [
                    'view',
                    'test_page_view',
                    'display',
                    'default',
                ],
            ],
        ] + $this->defaultRenderArray;
        $request = new Request();
        $request->attributes
            ->set('view_id', 'test_page_view');
        $request->attributes
            ->set('display_id', 'default');
        $options = [
            '_view_display_plugin_class' => '\\Drupal\\views\\Plugin\\views\\display\\Page',
        ];
        $request->attributes
            ->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/test', [
            'view_id' => 'test_page_view',
            'display_id' => 'default',
        ], [], $options));
        $route_match = RouteMatch::createFromRequest($request);
        $output = $this->pageController
            ->handle($route_match->getParameter('view_id'), $route_match->getParameter('display_id'), $route_match);
        $this->assertIsArray($output);
        $this->assertEquals($build, $output);
    }
    
    /**
     * Tests the page controller with arguments on a non overridden page view.
     */
    public function testHandleWithArgumentsWithoutOverridden() : void {
        $request = new Request();
        $request->attributes
            ->set('view_id', 'test_page_view');
        $request->attributes
            ->set('display_id', 'page_1');
        // Add the argument to the request.
        $request->attributes
            ->set('arg_0', 'test-argument');
        $options = [
            '_view_argument_map' => [
                'arg_0' => 'arg_0',
            ],
            '_view_display_plugin_class' => '\\Drupal\\views\\Plugin\\views\\display\\Page',
        ];
        $request->attributes
            ->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/test/{arg_0}', [
            'view_id' => 'test_page_view',
            'display_id' => 'default',
        ], [], $options));
        $route_match = RouteMatch::createFromRequest($request);
        $result = $this->pageController
            ->handle($route_match->getParameter('view_id'), $route_match->getParameter('display_id'), $route_match);
        $build = [
            '#type' => 'view',
            '#name' => 'test_page_view',
            '#display_id' => 'page_1',
            '#embed' => FALSE,
            '#arguments' => [
                'test-argument',
            ],
            '#cache' => [
                'keys' => [
                    'view',
                    'test_page_view',
                    'display',
                    'page_1',
                    'args',
                    'test-argument',
                ],
            ],
        ] + $this->defaultRenderArray;
        $this->assertEquals($build, $result);
    }
    
    /**
     * Tests the page controller with arguments of an overridden page view.
     *
     * Note: This test does not care about upcasting for now.
     */
    public function testHandleWithArgumentsOnOverriddenRoute() : void {
        $request = new Request();
        $request->attributes
            ->set('view_id', 'test_page_view');
        $request->attributes
            ->set('display_id', 'page_1');
        // Add the argument to the request.
        $request->attributes
            ->set('parameter', 'test-argument');
        $options = [
            '_view_argument_map' => [
                'arg_0' => 'parameter',
            ],
            '_view_display_plugin_class' => '\\Drupal\\views\\Plugin\\views\\display\\Page',
        ];
        $request->attributes
            ->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/test/{parameter}', [
            'view_id' => 'test_page_view',
            'display_id' => 'default',
        ], [], $options));
        $route_match = RouteMatch::createFromRequest($request);
        $result = $this->pageController
            ->handle($route_match->getParameter('view_id'), $route_match->getParameter('display_id'), $route_match);
        $build = [
            '#type' => 'view',
            '#name' => 'test_page_view',
            '#display_id' => 'page_1',
            '#embed' => FALSE,
            '#arguments' => [
                'test-argument',
            ],
            '#cache' => [
                'keys' => [
                    'view',
                    'test_page_view',
                    'display',
                    'page_1',
                    'args',
                    'test-argument',
                ],
            ],
        ] + $this->defaultRenderArray;
        $this->assertEquals($build, $result);
    }
    
    /**
     * Tests the page controller with arguments of an overridden page view.
     *
     * This test care about upcasted values and ensures that the raw variables
     * are pulled in.
     */
    public function testHandleWithArgumentsOnOverriddenRouteWithUpcasting() : void {
        $request = new Request();
        $request->attributes
            ->set('view_id', 'test_page_view');
        $request->attributes
            ->set('display_id', 'page_1');
        // Add the argument to the request.
        $request->attributes
            ->set('test_entity', $this->createMock('Drupal\\Core\\Entity\\EntityInterface'));
        $raw_variables = new InputBag([
            'test_entity' => 'example_id',
        ]);
        $request->attributes
            ->set('_raw_variables', $raw_variables);
        $options = [
            '_view_argument_map' => [
                'arg_0' => 'test_entity',
            ],
            '_view_display_plugin_class' => '\\Drupal\\views\\Plugin\\views\\display\\Page',
        ];
        $request->attributes
            ->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/test/{test_entity}', [
            'view_id' => 'test_page_view',
            'display_id' => 'default',
        ], [], $options));
        $route_match = RouteMatch::createFromRequest($request);
        $result = $this->pageController
            ->handle($route_match->getParameter('view_id'), $route_match->getParameter('display_id'), $route_match);
        $build = [
            '#type' => 'view',
            '#name' => 'test_page_view',
            '#display_id' => 'page_1',
            '#embed' => FALSE,
            '#arguments' => [
                'example_id',
            ],
            '#cache' => [
                'keys' => [
                    'view',
                    'test_page_view',
                    'display',
                    'page_1',
                    'args',
                    'example_id',
                ],
            ],
        ] + $this->defaultRenderArray;
        $this->assertEquals($build, $result);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
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.
UnitTestCase::$root protected property The app root.
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::setUpBeforeClass public static function
ViewPageControllerTest::$defaultRenderArray protected property A render array expected for every page controller render array result.
ViewPageControllerTest::$pageController public property The page controller of views.
ViewPageControllerTest::setUp protected function Overrides UnitTestCase::setUp
ViewPageControllerTest::testHandleWithArgumentsOnOverriddenRoute public function Tests the page controller with arguments of an overridden page view.
ViewPageControllerTest::testHandleWithArgumentsOnOverriddenRouteWithUpcasting public function Tests the page controller with arguments of an overridden page view.
ViewPageControllerTest::testHandleWithArgumentsWithoutOverridden public function Tests the page controller with arguments on a non overridden page view.
ViewPageControllerTest::testPageController public function Tests the page controller.

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