You are here

public function DisplayPageTest::testPageResponses in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Plugin/DisplayPageTest.php \Drupal\Tests\views\Kernel\Plugin\DisplayPageTest::testPageResponses()

Checks the behavior of the page for access denied/not found behaviors.

File

core/modules/views/tests/src/Kernel/Plugin/DisplayPageTest.php, line 46

Class

DisplayPageTest
Tests the page display plugin.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testPageResponses() {
  \Drupal::currentUser()
    ->setAccount(new AnonymousUserSession());
  $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);
  $subrequest = Request::create('/test_page_display_200', 'GET');
  \Drupal::getContainer()
    ->get('request_stack')
    ->push($subrequest);

  // Test accessing a disabled page for a view.
  $view = Views::getView('test_page_display');

  // Disable the view, rebuild menu, and request the page again.
  $view->storage
    ->disable()
    ->save();

  // Router rebuild would occur in a kernel terminate event so we need to
  // simulate that here.
  \Drupal::service('router.builder')
    ->rebuild();
  $response = $this->container
    ->get('http_kernel')
    ->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
  $this
    ->assertEqual($response
    ->getStatusCode(), 404);
}