assertCacheTags example

'local_task',
      'config:block.block.page_actions_block',
      'config:block.block.page_tabs_block',
      'config:block_list',
    ];
    $expected_tags = Cache::mergeTags($expected_tags$block_tags);
    $additional_tags = [
      'node_list',
      'rendered',
    ];
    $expected_tags = Cache::mergeTags($expected_tags$additional_tags);
    $this->assertCacheTags($expected_tags);

    // Delete a node and ensure it no longer appears on the tracker.     $published->delete();
    $this->drupalGet('activity');
    $this->assertSession()->pageTextNotContains($published->label());

    // Test proper display of time on activity page when comments are disabled.     // Disable comments.     FieldStorageConfig::loadByName('node', 'comment')->delete();
    $node = $this->drupalCreateNode([
      // This title is required to trigger the custom changed time set in the
// Ensure that the display is using the medium image style.     $component = $this->display->getComponent($this->fieldName);
    $component['settings']['image_style'] = 'medium';
    $this->display->setComponent($this->fieldName, $component)->save();

    $build = $this->display->build($entity);

    // The first image is a PNG, so it is supported by the GD image toolkit.     // The image style should be applied with its cache tags, image derivative     // computed with its URI and dimensions.     $this->assertCacheTags($build[$this->fieldName][0], ImageStyle::load('medium')->getCacheTags());
    $renderer->renderRoot($build[$this->fieldName][0]);
    $this->assertEquals('medium', $build[$this->fieldName][0]['#image_style']);
    // We check that the image URL contains the expected style directory     // structure.     $this->assertStringContainsString('styles/medium/public/test-image.png', $build[$this->fieldName][0]['#markup']);
    $this->assertStringContainsString('width="220"', $build[$this->fieldName][0]['#markup']);
    $this->assertStringContainsString('height="220"', $build[$this->fieldName][0]['#markup']);

    // The second image is an SVG, which is not supported by the GD toolkit.     // The image style should still be applied with its cache tags, but image     // derivative will not be available so <img> tag will point to the original
/** * Checks the behavior of the Serializer callback paths and row plugins. */
  public function testSerializerResponses() {
    // Test the serialize callback.     $view = Views::getView('test_serializer_display_field');
    $view->initDisplay();
    $this->executeView($view);

    $actual_json = $this->drupalGet('test/serialize/field', ['query' => ['_format' => 'json']]);
    $this->assertSession()->statusCodeEquals(200);
    $this->assertCacheTags($view->getCacheTags());
    $this->assertCacheContexts(['languages:language_interface', 'theme', 'request_format']);
    // @todo Due to https://www.drupal.org/node/2352009 we can't yet test the     // propagation of cache max-age.
    // Test the http Content-type.     $headers = $this->getSession()->getResponseHeaders();
    $this->assertSame(['application/json']$headers['Content-Type']);

    $expected = [];
    foreach ($view->result as $row) {
      $expected_row = [];
      
// Get some page and check if the block is displayed.     $this->drupalGet('user');
    $this->assertSession()->pageTextContains('Popular content');
    $this->assertSession()->pageTextContains("Today's");
    $this->assertSession()->pageTextContains('All time');
    $this->assertSession()->pageTextContains('Last viewed');

    $tags = Cache::mergeTags($node->getCacheTags()$block->getCacheTags());
    $tags = Cache::mergeTags($tags$this->blockingUser->getCacheTags());
    $tags = Cache::mergeTags($tags['block_view', 'config:block_list', 'node_list', 'rendered', 'user_view']);
    $this->assertCacheTags($tags);
    $contexts = Cache::mergeContexts($node->getCacheContexts()$block->getCacheContexts());
    $contexts = Cache::mergeContexts($contexts['url.query_args:_wrapper_format', 'url.site']);
    $this->assertCacheContexts($contexts);

    // Check if the node link is displayed.     $this->assertSession()->responseContains(Link::fromTextAndUrl($node->label()$node->toUrl('canonical'))->toString());
  }

}
$this->drupalGet('');
    $this->assertSession()->elementTextEquals('xpath', $block_title_xpath, 'test_view_block');

    // Hide the title.     $block->getPlugin()->setConfigurationValue('label_display', FALSE);
    $block->save();

    $this->drupalGet('');
    $this->assertSession()->elementNotExists('xpath', $block_title_xpath);

    $this->assertCacheTags(array_merge($block->getCacheTags()['block_view', 'config:block_list', 'config:system.site', 'config:views.view.test_view_block', 'http_response', 'rendered']));
  }

  /** * Tests the various testcases of empty block rendering. */
  public function testBlockEmptyRendering() {
    $url = new Url('test_page_test.test_page');
    // Remove all views_test_data entries.     \Drupal::database()->truncate('views_test_data')->execute();
    /** @var \Drupal\views\ViewEntityInterface $view */
    $view = View::load('test_view_block');
    
$expected_cache_tags = [
      'block_view',
      'config:block.block.title',
      'config:block_list',
      'config:shortcut.set.default',
      'config:system.menu.admin',
      'config:system.theme',
      'config:user.role.authenticated',
      'rendered',
      'user:' . $this->rootUser->id(),
    ];
    $this->assertCacheTags($expected_cache_tags);

    \Drupal::configFactory()
      ->getEditable('stark.settings')
      ->set('third_party_settings.shortcut.module_link', TRUE)
      ->save(TRUE);

    // Add cron to the default shortcut set, now the shortcut list cache tag     // is expected.     $this->drupalGet('admin/config/system/cron');
    $this->clickLink('Add to Default shortcuts');
    $expected_cache_tags[] = 'config:shortcut_set_list';
    


  /** * Tests that tags are invalidated when an entity with that bundle changes. */
  public function testBundleListingCache() {
    // Access to lists of test entities with each bundle.     $bundle_a_url = Url::fromRoute('cache_test_list.bundle_tags', ['entity_type_id' => 'entity_test_with_bundle', 'bundle' => 'bundle_a']);
    $bundle_b_url = Url::fromRoute('cache_test_list.bundle_tags', ['entity_type_id' => 'entity_test_with_bundle', 'bundle' => 'bundle_b']);
    $this->drupalGet($bundle_a_url);
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertCacheTags(['rendered', 'entity_test_with_bundle_list:bundle_a']);

    $this->drupalGet($bundle_a_url);
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    $this->assertCacheTags(['rendered', 'entity_test_with_bundle_list:bundle_a']);
    $this->drupalGet($bundle_b_url);
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertCacheTags(['rendered', 'entity_test_with_bundle_list:bundle_b']);
    $this->drupalGet($bundle_b_url);
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    $entity1 = EntityTestWithBundle::create(['type' => 'bundle_a', 'name' => 'entity1']);
    $entity1->save();
    
$this->submitForm($edit, 'Search');
    $this->assertSession()->pageTextContains('bike shed shop');
    $this->assertSession()->pageTextContains('Llama shop');
    $expected_cache_tags = Cache::mergeTags($default_search_tags[
      'node:1',
      'user:2',
      'node:2',
      'user:3',
      'node_view',
      'config:filter.format.plain_text',
    ]);
    $this->assertCacheTags($expected_cache_tags);

    // Only get the new node in the search results, should result in node:1,     // node:2 and user:3 as cache tags even though only node:1 is shown. This is     // because node:2 is reference in node:1 as an entity reference.     $edit = [];
    $edit['keys'] = 'Llama';
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()->pageTextContains('Llama shop');
    $expected_cache_tags = Cache::mergeTags($default_search_tags[
      'node:1',
      

  protected function assertPageCacheContextsAndTags(Url $url, array $expected_contexts, array $expected_tags) {
    $absolute_url = $url->setAbsolute()->toString();
    sort($expected_contexts);
    sort($expected_tags);

    // Assert cache miss + expected cache contexts + tags.     $this->drupalGet($absolute_url);
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertCacheTags($expected_tags);
    $this->assertCacheContexts($expected_contexts);

    // Assert cache hit + expected cache contexts + tags.     $this->drupalGet($absolute_url);
    $this->assertCacheTags($expected_tags);
    $this->assertCacheContexts($expected_contexts);

    // Assert page cache item + expected cache tags.     $cid_parts = [$url->setAbsolute()->toString(), ''];
    $cid = implode(':', $cid_parts);
    $cache_entry = \Drupal::cache('page')->get($cid);
    
$cache_contexts = [
      'languages:language_interface',
      'theme',
      'url.site',
      'user.node_grants:view',
      'user.permissions',
      'timezone',
    ];
    $this->assertCacheContexts($cache_contexts);

    $cache_context_tags = \Drupal::service('cache_contexts_manager')->convertTokensToKeys($cache_contexts)->getCacheTags();
    $this->assertCacheTags(Cache::mergeTags($cache_context_tags[
      'config:views.view.frontpage',
      'node:1', 'node_list',
      'node_view',
      'user:3',
    ]));

    $raw = '<comments>' . $this->node->toUrl('canonical', ['fragment' => 'comments', 'absolute' => TRUE])->toString() . '</comments>';
    $this->assertSession()->responseContains($raw);

    // Hide comments from RSS feed and check presence.     $this->node->set('comment', CommentItemInterface::HIDDEN);
    
/** * Tests a token replacement on an actual website. */
  public function testTokens() {
    $node = $this->drupalCreateNode();
    $account = $this->drupalCreateUser();
    $this->drupalLogin($account);

    $this->drupalGet('token-test/' . $node->id());
    $this->assertSession()->pageTextContains("Tokens: {$node->id()} {$account->id()}");
    $this->assertCacheTags(['node:1', 'rendered', 'user:2']);
    $this->assertCacheContexts(['languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user']);

    $this->drupalGet('token-test-without-bubbleable-metadata/' . $node->id());
    $this->assertSession()->pageTextContains("Tokens: {$node->id()} {$account->id()}");
    $this->assertCacheTags(['node:1', 'rendered', 'user:2']);
    $this->assertCacheContexts(['languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user']);
  }

}
$this->assertFalse($render_cache->get($cache_element));

    $this->drupalGet('test-display');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertNotEmpty($render_cache->get($cache_element));
    $cache_tags = [
      'config:user.role.anonymous',
      'config:views.view.test_display',
      'node_list',
      'rendered',
    ];
    $this->assertCacheTags($cache_tags);

    $this->drupalGet('test-display');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertNotEmpty($render_cache->get($cache_element));
    $this->assertCacheTags($cache_tags);
  }

  /** * Tests that a display without caching still contains the cache metadata. */
  public function testDisplayWithoutCacheStillBubblesMetadata() {
    
Home | Imprint | This part of the site doesn't use cookies.