Unverified Commit 3db26fd1 authored by larowlan's avatar larowlan

Issue #3008109 by Lendude, andypost: Convert ResponsiveImageFieldDisplayTest to BrowserTestBase

parent 550e914c
<?php <?php
namespace Drupal\responsive_image\Tests; namespace Drupal\Tests\responsive_image\Functional;
use Drupal\image\Tests\ImageFieldTestBase;
use Drupal\image\Entity\ImageStyle; use Drupal\image\Entity\ImageStyle;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drupal\file\Entity\File; use Drupal\file\Entity\File;
use Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter; use Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter;
use Drupal\responsive_image\Entity\ResponsiveImageStyle; use Drupal\responsive_image\Entity\ResponsiveImageStyle;
use Drupal\Tests\image\Functional\ImageFieldTestBase;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\user\RoleInterface; use Drupal\user\RoleInterface;
/** /**
...@@ -17,6 +18,8 @@ ...@@ -17,6 +18,8 @@
*/ */
class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase { class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
use TestFileCreationTrait;
protected $dumpHeaders = TRUE; protected $dumpHeaders = TRUE;
/** /**
...@@ -172,7 +175,7 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles = ...@@ -172,7 +175,7 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles =
$this->createImageField($field_name, 'article', ['uri_scheme' => $scheme]); $this->createImageField($field_name, 'article', ['uri_scheme' => $scheme]);
// Create a new node with an image attached. Make sure we use a large image // Create a new node with an image attached. Make sure we use a large image
// so the scale effects of the image styles always have an effect. // so the scale effects of the image styles always have an effect.
$test_image = current($this->drupalGetTestFiles('image', 39325)); $test_image = current($this->getTestFiles('image', 39325));
// Create alt text for the image. // Create alt text for the image.
$alt = $this->randomMachineName(); $alt = $this->randomMachineName();
...@@ -240,13 +243,11 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles = ...@@ -240,13 +243,11 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles =
$display->setComponent($field_name, $display_options) $display->setComponent($field_name, $display_options)
->save(); ->save();
$default_output = '<a href="' . file_url_transform_relative(file_create_url($image_uri)) . '"><picture';
$this->drupalGet('node/' . $nid); $this->drupalGet('node/' . $nid);
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); $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->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
$this->removeWhiteSpace(); $this->assertPattern('/<a(.*?)href="' . preg_quote(file_url_transform_relative(file_create_url($image_uri)), '/') . '"(.*?)>\s*<picture/');
$this->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.');
// Verify that the image can be downloaded. // 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.'); $this->assertEqual(file_get_contents($test_image->uri), $this->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.');
if ($scheme == 'private') { if ($scheme == 'private') {
...@@ -369,7 +370,7 @@ public function testResponsiveImageFieldFormattersEmptyMediaQuery() { ...@@ -369,7 +370,7 @@ public function testResponsiveImageFieldFormattersEmptyMediaQuery() {
$field_name = mb_strtolower($this->randomMachineName()); $field_name = mb_strtolower($this->randomMachineName());
$this->createImageField($field_name, 'article', ['uri_scheme' => 'public']); $this->createImageField($field_name, 'article', ['uri_scheme' => 'public']);
// Create a new node with an image attached. // Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image')); $test_image = current($this->getTestFiles('image'));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName()); $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
$node_storage->resetCache([$nid]); $node_storage->resetCache([$nid]);
...@@ -389,7 +390,7 @@ public function testResponsiveImageFieldFormattersEmptyMediaQuery() { ...@@ -389,7 +390,7 @@ public function testResponsiveImageFieldFormattersEmptyMediaQuery() {
$this->drupalGet('node/' . $nid); $this->drupalGet('node/' . $nid);
// Assert an empty media attribute is not output. // Assert an empty media attribute is not output.
$this->assertNoPattern('@srcset="data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== 1x".+?media=".+?/><source@'); $this->assertSession()->responseNotMatches('@srcset="data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== 1x".+?media=".+?/><source@');
// Assert the media attribute is present if it has a value. // Assert the media attribute is present if it has a value.
$thumbnail_style = ImageStyle::load('thumbnail'); $thumbnail_style = ImageStyle::load('thumbnail');
...@@ -417,7 +418,7 @@ public function testResponsiveImageFieldFormattersOneSource() { ...@@ -417,7 +418,7 @@ public function testResponsiveImageFieldFormattersOneSource() {
$field_name = mb_strtolower($this->randomMachineName()); $field_name = mb_strtolower($this->randomMachineName());
$this->createImageField($field_name, 'article', ['uri_scheme' => 'public']); $this->createImageField($field_name, 'article', ['uri_scheme' => 'public']);
// Create a new node with an image attached. // Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image')); $test_image = current($this->getTestFiles('image'));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName()); $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
$node_storage->resetCache([$nid]); $node_storage->resetCache([$nid]);
...@@ -455,7 +456,7 @@ private function assertResponsiveImageFieldFormattersLink($link_type) { ...@@ -455,7 +456,7 @@ private function assertResponsiveImageFieldFormattersLink($link_type) {
$field_settings = ['alt_field_required' => 0]; $field_settings = ['alt_field_required' => 0];
$this->createImageField($field_name, 'article', ['uri_scheme' => 'public'], $field_settings); $this->createImageField($field_name, 'article', ['uri_scheme' => 'public'], $field_settings);
// Create a new node with an image attached. // Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image')); $test_image = current($this->getTestFiles('image'));
// Test the image linked to file formatter. // Test the image linked to file formatter.
$display_options = [ $display_options = [
...@@ -497,16 +498,15 @@ private function assertResponsiveImageFieldFormattersLink($link_type) { ...@@ -497,16 +498,15 @@ private function assertResponsiveImageFieldFormattersLink($link_type) {
// Output should contain all image styles and all breakpoints. // Output should contain all image styles and all breakpoints.
$this->drupalGet('node/' . $nid); $this->drupalGet('node/' . $nid);
$this->removeWhiteSpace();
switch ($link_type) { switch ($link_type) {
case 'file': case 'file':
// Make sure the link to the file is present. // Make sure the link to the file is present.
$this->assertPattern('/<a(.*?)href="' . preg_quote(file_url_transform_relative(file_create_url($image_uri)), '/') . '"(.*?)><picture/'); $this->assertPattern('/<a(.*?)href="' . preg_quote(file_url_transform_relative(file_create_url($image_uri)), '/') . '"(.*?)>\s*<picture/');
break; break;
case 'content': case 'content':
// Make sure the link to the node is present. // Make sure the link to the node is present.
$this->assertPattern('/<a(.*?)href="' . preg_quote($node->url(), '/') . '"(.*?)><picture/'); $this->assertPattern('/<a(.*?)href="' . preg_quote($node->url(), '/') . '"(.*?)>\s*<picture/');
break; break;
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment