From 19b2196c190c2dd49ff94864fa265bcd579d2362 Mon Sep 17 00:00:00 2001 From: GoZ Date: Tue, 14 Apr 2015 10:23:53 +0200 Subject: [PATCH] Issue #2388023: add cache tag for url file display and add tests --- .../Plugin/Field/FieldFormatter/ImageFormatter.php | 2 ++ .../image/src/Tests/ImageFieldDisplayTest.php | 38 ++++++++++++---------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php index 38a163c..d3fed52 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php @@ -16,6 +16,7 @@ use Drupal\Core\Utility\LinkGeneratorInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Cache\Cache; /** * Plugin implementation of the 'image' formatter. @@ -200,6 +201,7 @@ public function viewElements(FieldItemListInterface $items) { if (isset($link_file)) { $image_uri = $file->getFileUri(); $url = Url::fromUri(file_create_url($image_uri)); + $cache_tags = Cache::mergeTags($cache_tags, $file->getCacheTags()); } // Extract field item attributes for the theme function, and unset them diff --git a/core/modules/image/src/Tests/ImageFieldDisplayTest.php b/core/modules/image/src/Tests/ImageFieldDisplayTest.php index 47d7712..2cf6026 100644 --- a/core/modules/image/src/Tests/ImageFieldDisplayTest.php +++ b/core/modules/image/src/Tests/ImageFieldDisplayTest.php @@ -10,6 +10,8 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\field\Entity\FieldStorageConfig; use Drupal\user\RoleInterface; +use Drupal\file\Entity\File; +use Drupal\image\Entity\ImageStyle; /** * Tests the display of image fields. @@ -93,7 +95,7 @@ function _testImageFieldFormatters($scheme) { $node = $node_storage->load($nid); // Test that the default formatter is being used. - $image_uri = file_load($node->{$field_name}->target_id)->getFileUri(); + $image_uri = File::load($node->{$field_name}->target_id)->getFileUri(); $image = array( '#theme' => 'image', '#uri' => $image_uri, @@ -122,8 +124,9 @@ function _testImageFieldFormatters($scheme) { ); $default_output = '' . drupal_render($image) . ''; $this->drupalGet('node/' . $nid); - $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); - $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); + + $file = File::load($node->{$field_name}->target_id); + $this->assertCacheTag($file->getCacheTags()[0]); $this->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.'); // Verify that the image can be downloaded. $this->assertEqual(file_get_contents($test_image->uri), $this->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.'); @@ -153,8 +156,7 @@ function _testImageFieldFormatters($scheme) { '#height' => 20, ); $this->drupalGet('node/' . $nid); - $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); - $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); + $this->assertNoCacheTag($file->getCacheTags()[0]); $elements = $this->xpath( '//a[@href=:path]/img[@src=:url and @alt=:alt and @width=:width and @height=:height]', array( @@ -175,7 +177,7 @@ function _testImageFieldFormatters($scheme) { // Ensure the derivative image is generated so we do not have to deal with // image style callback paths. - $this->drupalGet(entity_load('image_style', 'thumbnail')->buildUrl($image_uri)); + $this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri)); $image_style = array( '#theme' => 'image_style', '#uri' => $image_uri, @@ -186,14 +188,15 @@ function _testImageFieldFormatters($scheme) { ); $default_output = drupal_render($image_style); $this->drupalGet('node/' . $nid); - $cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags')); - $this->assertTrue(in_array('config:image.style.thumbnail', $cache_tags)); + + $image_style = ImageStyle::load('thumbnail'); + $this->assertCacheTag($image_style->getCacheTags()[0]); $this->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.'); if ($scheme == 'private') { // Log out and try to access the file. $this->drupalLogout(); - $this->drupalGet(entity_load('image_style', 'thumbnail')->buildUrl($image_uri)); + $this->drupalGet($image_style->buildUrl($image_uri)); $this->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.'); } } @@ -246,9 +249,10 @@ function testImageFieldSettings() { // style. $node_storage->resetCache(array($nid)); $node = $node_storage->load($nid); + $file = File::load($node->{$field_name}->target_id); $image_style = array( '#theme' => 'image_style', - '#uri' => file_load($node->{$field_name}->target_id)->getFileUri(), + '#uri' => $file->getFileUri(), '#width' => 40, '#height' => 20, '#style_name' => 'medium', @@ -259,7 +263,7 @@ function testImageFieldSettings() { // Add alt/title fields to the image and verify that they are displayed. $image = array( '#theme' => 'image', - '#uri' => file_load($node->{$field_name}->target_id)->getFileUri(), + '#uri' => $file->getFileUri(), '#alt' => $alt, '#title' => $this->randomMachineName(), '#width' => 40, @@ -361,8 +365,7 @@ function testImageFieldDefaultImage() { ); $default_output = str_replace("\n", NULL, drupal_render($image)); $this->drupalGet('node/' . $node->id()); - $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); - $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); + $this->assertNoCacheTag($file->getCacheTags()[0]); $this->assertRaw($default_output, 'Default image displayed when no user supplied image is present.'); // Create a node with an image attached and ensure that the default image @@ -374,17 +377,17 @@ function testImageFieldDefaultImage() { $nid = $this->uploadNodeImage($images[1], $field_name, 'article', $alt); $node_storage->resetCache(array($nid)); $node = $node_storage->load($nid); + $file = File::load($node->{$field_name}->target_id); $image = array( '#theme' => 'image', - '#uri' => file_load($node->{$field_name}->target_id)->getFileUri(), + '#uri' => $file->getFileUri(), '#width' => 40, '#height' => 20, '#alt' => $alt, ); $image_output = str_replace("\n", NULL, drupal_render($image)); $this->drupalGet('node/' . $nid); - $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); - $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); + $this->assertNoCacheTag($file->getCacheTags()[0]); $this->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.'); $this->assertRaw($image_output, 'User supplied image is displayed.'); @@ -430,8 +433,7 @@ function testImageFieldDefaultImage() { ); $default_output = str_replace("\n", NULL, drupal_render($image)); $this->drupalGet('node/' . $node->id()); - $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); - $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); + $this->assertNoCacheTag($file->getCacheTags()[0]); $this->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.'); } -- 1.9.3 (Apple Git-50)