From 01563db847bcec46cde346c8213b107ee259ff25 Mon Sep 17 00:00:00 2001 From: GoZ Date: Tue, 14 Apr 2015 10:23:53 +0200 Subject: [PATCH 1/2] Issue #2388023: add cache tag for url file display --- core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php | 2 ++ 1 file changed, 2 insertions(+) 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 -- 1.9.3 (Apple Git-50) From 9dc28718e0193f430b3a9849d836c6499c662fb7 Mon Sep 17 00:00:00 2001 From: GoZ Date: Tue, 14 Apr 2015 10:24:28 +0200 Subject: [PATCH 2/2] Issue #2388023: add tests for cache tags --- .../image/src/Tests/ImageFieldDisplayTest.php | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/core/modules/image/src/Tests/ImageFieldDisplayTest.php b/core/modules/image/src/Tests/ImageFieldDisplayTest.php index 47d7712..ddf06c1 100644 --- a/core/modules/image/src/Tests/ImageFieldDisplayTest.php +++ b/core/modules/image/src/Tests/ImageFieldDisplayTest.php @@ -122,8 +122,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->assertNoCacheTag($file->getCacheTags()[0], 'No image style cache tag found.'); $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 +154,8 @@ 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.'); + $file = file_load($node->{$field_name}->target_id); + $this->assertNoCacheTag($file->getCacheTags()[0], 'No image style cache tag found.'); $elements = $this->xpath( '//a[@href=:path]/img[@src=:url and @alt=:alt and @width=:width and @height=:height]', array( @@ -186,8 +187,9 @@ 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 = entity_load('image_style', 'thumbnail'); + $this->assertCacheTag($image_style->getCacheTags()[0]); $this->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.'); if ($scheme == 'private') { @@ -332,8 +334,8 @@ function testImageFieldDefaultImage() { // Verify that no image is displayed on the page by checking for the class // that would be used on the image field. $this->assertNoPattern('
', 'No image displayed when no image is attached and no default image specified.'); - $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->assertNoCacheTag($file->getCacheTags()[0], 'No image style cache tag found.'); // Add a default image to the public image field. $images = $this->drupalGetTestFiles('image'); @@ -361,8 +363,8 @@ 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.'); + $file = file_load($node->{$field_name}->target_id); + $this->assertNoCacheTag($file->getCacheTags()[0], 'No image style cache tag found.'); $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 @@ -383,9 +385,8 @@ function testImageFieldDefaultImage() { ); $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->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.'); + $file = file_load($node->{$field_name}->target_id); + $this->assertNoCacheTag($file->getCacheTags()[0], 'No image style cache tag found.'); $this->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.'); $this->assertRaw($image_output, 'User supplied image is displayed.'); // Remove default image from the field and make sure it is no longer used. @@ -430,9 +431,8 @@ 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->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.'); + $file = file_load($node->{$field_name}->target_id); + $this->assertNoCacheTag($file->getCacheTags()[0], 'No image style cache tag found.'); $this->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.'); } } -- 1.9.3 (Apple Git-50)