function ArgumentDefaultTest::testArgumentDefaultCacheability

Tests the cacheability of the date argument default.

File

core/modules/views/tests/src/Functional/Plugin/ArgumentDefaultTest.php, line 267

Class

ArgumentDefaultTest
Tests pluggable argument_default for views.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testArgumentDefaultCacheability() : void {
  // Create page for testing.
  $view = Views::getView('test_argument_default_date');
  $view->setDisplay();
  $view->newDisplay('page', 'Page', 'page_1');
  $view->displayHandlers
    ->get('page_1')
    ->overrideOption('path', 'path-page-1');
  $view->displayHandlers
    ->get('page_1')
    ->overrideOption('cache', [
    'type' => 'time',
    'options' => [
      // To eliminate UNCACHEABLE from the page as is.
'results_lifespan' => '10000',
    ],
  ]);
  $view->save();
  $this->container
    ->get('module_installer')
    ->uninstall([
    'page_cache',
  ]);
  // Check that the page is not cached with date argument default.
  $this->drupalGet('path-page-1');
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertEquals('UNCACHEABLE (poor cacheability)', $this->getSession()
    ->getResponseHeader(DynamicPageCacheSubscriber::HEADER));
  // Double check.
  $this->drupalGet('path-page-1');
  $this->assertEquals('UNCACHEABLE (poor cacheability)', $this->getSession()
    ->getResponseHeader(DynamicPageCacheSubscriber::HEADER));
  // Change the argument to some cached option.
  $view = Views::getView('test_argument_default_date');
  $view->setDisplay();
  $view->displayHandlers
    ->get('page_1')
    ->overrideOption('arguments', [
    'null' => [
      'id' => 'null',
      'table' => 'views',
      'field' => 'null',
    ],
  ]);
  $view->save();
  // Check that the page is cached without date argument default.
  $this->drupalGet('path-page-1');
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertEquals('MISS', $this->getSession()
    ->getResponseHeader(DynamicPageCacheSubscriber::HEADER));
  $this->drupalGet('path-page-1');
  $this->assertEquals('HIT', $this->getSession()
    ->getResponseHeader(DynamicPageCacheSubscriber::HEADER));
}

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