// Check that the 'workspace' cache context is present when the module is
// installed.
$this->drupalGet('<front>'); $this->assertCacheContext('workspace');
// Create a node and check that its render array contains the proper cache
// context.
$this->drupalCreateContentType(['type' => 'page']); $node = $this->createNode();
publicfunctiontestPageNotFoundCustomPageWithAccessDenied(){ // Sets up a 404 page not accessible by the anonymous user.
$this->config('system.site')->set('page.404', '/system-test/custom-4xx')->save();
// 1. anonymous user, without permission.
$this->drupalGet($content_url); $this->assertSession()->pageTextContains('Permission to pet llamas: no!'); $this->assertCacheContext('user.permissions'); $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.role.anonymous'); $this->drupalGet($route_access_url); $this->assertCacheContext('user.permissions'); $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.role.anonymous');
/**
* Tests that URL bubbleable metadata is correctly bubbled.
*/ publicfunctiontestUrlBubbleableMetadataBubbling(){ // Test that regular URLs bubble up bubbleable metadata when converted to
// string.
$url = Url::fromRoute('cache_test.url_bubbling'); $this->drupalGet($url); $this->assertCacheContext('url.site'); $this->assertSession()->responseContains($url->setAbsolute()->toString()); }
}
/**
* Tests if config overrides correctly set cacheability metadata.
*/ publicfunctiontestConfigOverride(){ // Check the default (disabled) state of the cache context. The block label
// should not be overridden.
$this->drupalGet('<front>'); $this->assertSession()->pageTextNotContains('Overridden block label');
// Both the cache context and tag should be present.
$this->assertCacheContext('config_override_integration_test'); $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config_override_integration_test_tag');
// Flip the state of the cache context. The block label should now be
// overridden.
\Drupal::state()->set('config_override_integration_test.enabled', TRUE); $this->drupalGet('<front>'); $this->assertSession()->pageTextContains('Overridden block label');
// Both the cache context and tag should still be present.
$this->assertCacheContext('config_override_integration_test'); $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config_override_integration_test_tag'); }
// Create a content type via the user interface.
$web_user = $this->drupalCreateUser([ 'bypass node access', 'administer content types', ]); $this->drupalLogin($web_user);
// Make sure that the user.permissions cache context and the cache tags
// for the entity are present.
$this->assertCacheContext('user.permissions'); foreach($this->entity->getCacheTags()as$cache_tag){ $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', $cache_tag); }
publicfunctiontestAccessDeniedCustomPageWithAccessDenied(){ // Sets up a 403 page not accessible by the anonymous user.
$this->config('system.site')->set('page.403', '/system-test/custom-4xx')->save();
/**
* Tests that the node grants cache context is auto-added, only when needed.
*
* @see node_query_node_access_alter()
*/ publicfunctiontestNodeAccessCacheabilitySafeguard(){ // The node grants cache context should be added automatically.
$this->drupalGet(newUrl('node_access_test_auto_bubbling')); $this->assertCacheContext('user.node_grants:view');
// The root user has the 'bypass node access' permission, which means the
// node grants cache context is not necessary.
$this->drupalLogin($this->rootUser); $this->drupalGet(newUrl('node_access_test_auto_bubbling')); $this->assertNoCacheContext('user.node_grants:view'); $this->drupalLogout();
// Uninstall the module with the only hook_node_grants() implementation.
$this->container->get('module_installer')->uninstall(['node_access_test']); $this->rebuildContainer();
// Test the access() method on the access plugin.
$this->assertFalse($executable->display_handler->access($this->webUser)); $this->assertTrue($executable->display_handler->access($this->normalUser));
// Reset counter to 0.
$this->drupalGet('pager-test/query-parameters'); // Go back to first page, the count of pager calls need to go to 2.
$this->assertSession()->elementExists('xpath', '//li[contains(@class, "pager__item--last")]/a')->click();