You are here

class ViewPageControllerTest in Drupal 9

Same name and namespace in other branches
  1. 8 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 17

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,
  ];
  protected function setUp() : void {
    $this->pageController = new ViewPageController();
  }

  /**
   * Tests the page controller.
   */
  public function testPageController() {
    $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() {
    $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() {
    $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() {
    $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 ParameterBag([
      '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

Namesort descending Modifiers Type Description 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.
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
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.