DisplayPageTest.php 5.34 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\views\Tests\Plugin\DisplayPageTest.
6 7 8 9
 */

namespace Drupal\views\Tests\Plugin;

10 11 12 13 14 15
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\views\EventSubscriber\RouteSubscriber;
use Drupal\views\Tests\ViewUnitTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\RouteCollection;
16 17 18 19

/**
 * Tests the page display plugin.
 *
20
 * @see \Drupal\views\Plugin\display\Page
21
 */
22
class DisplayPageTest extends ViewUnitTestBase {
23

24 25 26 27 28
  /**
   * Views used by this test.
   *
   * @var array
   */
29 30 31 32 33 34 35
  public static $testViews = array('test_page_display', 'test_page_display_route');

  /**
   * Modules to enable.
   *
   * @var array
   */
36
  public static $modules = array('system', 'user', 'menu_link', 'field');
37 38 39 40 41 42 43

  /**
   * The router dumper to get all routes.
   *
   * @var \Drupal\Core\Routing\MatcherDumper
   */
  protected $routerDumper;
44

45 46 47 48 49 50 51 52
  public static function getInfo() {
    return array(
      'name' => 'Display: Page plugin',
      'description' => 'Tests the page display plugin.',
      'group' => 'Views Plugins',
    );
  }

53 54 55
  /**
   * {@inheritdoc}
   */
56 57 58
  protected function setUp() {
    parent::setUp();

59
    // Setup the needed tables in order to make the drupal router working.
60
    $this->installSchema('system', array('router', 'menu_router', 'url_alias'));
61
    $this->installSchema('menu_link', 'menu_links');
62 63 64 65 66 67
  }

  /**
   * Checks the behavior of the page for access denied/not found behaviours.
   */
  public function testPageResponses() {
68 69 70 71 72 73 74 75 76 77 78 79 80 81
    // @todo Importing a route should fire a container rebuild.
    $this->container->get('router.builder')->rebuild();

    $subrequest = Request::create('/test_page_display_403', 'GET');
    $response = $this->container->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
    $this->assertEqual($response->getStatusCode(), 403);

    $subrequest = Request::create('/test_page_display_404', 'GET');
    $response = $this->container->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
    $this->assertEqual($response->getStatusCode(), 404);

    $subrequest = Request::create('/test_page_display_200', 'GET');
    $response = $this->container->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
    $this->assertEqual($response->getStatusCode(), 200);
82

83 84 85
    $subrequest = Request::create('/test_page_display_200', 'GET');
    \Drupal::getContainer()->set('request', $subrequest);

86 87 88 89
    // Test accessing a disabled page for a view.
    $view = views_get_view('test_page_display');
    // Disable the view, rebuild menu, and request the page again.
    $view->storage->disable()->save();
90

91 92
    $response = $this->container->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
    $this->assertEqual($response->getStatusCode(), 404);
93 94 95 96 97 98
  }

  /**
   * Checks that the router items are properly registered
   */
  public function testPageRouterItems() {
99
    $collection = \Drupal::service('views.route_subscriber')->routes();
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    // Check the controller defaults.
    foreach ($collection as $id => $route) {
      if (strpos($id, 'test_page_display_route') === 0) {
        $this->assertEqual($route->getDefault('_controller'), 'Drupal\views\Routing\ViewPageController::handle');
        $this->assertEqual($route->getDefault('view_id'), 'test_page_display_route');
        $this->assertEqual($route->getDefault('display_id'), str_replace('test_page_display_route.', '', $id));
      }
    }

    // Check the generated patterns and default values.
    $route = $collection->get('view.test_page_display_route.page_1');
    $this->assertEqual($route->getPath(), '/test_route_without_arguments');

    $route = $collection->get('view.test_page_display_route.page_2');
    $this->assertEqual($route->getPath(), '/test_route_with_argument/{arg_id}');
    $this->assertTrue($route->hasDefault('arg_id'), 'A default value is set for the optional argument id.');

    $route = $collection->get('view.test_page_display_route.page_3');
    $this->assertEqual($route->getPath(), '/test_route_with_argument/{arg_id}/suffix');
    $this->assertFalse($route->hasDefault('arg_id'), 'No default value is set for the required argument id.');

    $route = $collection->get('view.test_page_display_route.page_4');
    $this->assertEqual($route->getPath(), '/test_route_with_argument/{arg_id}/suffix/{arg_id_2}');
    $this->assertFalse($route->hasDefault('arg_id'), 'No default value is set for the required argument id.');
    $this->assertTrue($route->hasDefault('arg_id_2'), 'A default value is set for the optional argument id_2.');

    $route = $collection->get('view.test_page_display_route.page_5');
    $this->assertEqual($route->getPath(), '/test_route_with_argument/{arg_id}/{arg_id_2}');
    $this->assertTrue($route->hasDefault('arg_id'), 'A default value is set for the optional argument id.');
    $this->assertTrue($route->hasDefault('arg_id_2'), 'A default value is set for the optional argument id_2.');

    $route = $collection->get('view.test_page_display_route.page_6');
    $this->assertEqual($route->getPath(), '/test_route_with_argument/{arg_id}/{arg_id_2}');
    $this->assertFalse($route->hasDefault('arg_id'), 'No default value is set for the required argument id.');
    $this->assertFalse($route->hasDefault('arg_id_2'), 'No default value is set for the required argument id_2.');
136 137 138
  }

}