Commit 82584e69 authored by alexpott's avatar alexpott

Issue #2349859 by R. Volk, naxoc: Responsive Image Mappings :: throws a fatal...

Issue #2349859 by R. Volk, naxoc: Responsive Image Mappings :: throws a fatal exception if no image style is set
parent 2b609009
......@@ -318,7 +318,10 @@ function responsive_image_get_image_dimensions($variables) {
);
}
else {
entity_load('image_style', $variables['style_name'])->transformDimensions($dimensions);
$entity = entity_load('image_style', $variables['style_name']);
if ($entity instanceof Drupal\image\Entity\ImageStyle) {
$entity->transformDimensions($dimensions);
}
}
return $dimensions;
......@@ -332,6 +335,10 @@ function _responsive_image_image_style_url($style_name, $path) {
// The smallest data URI for a 1px square transparent GIF image.
return 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
}
return entity_load('image_style', $style_name)->buildUrl($path);
$entity = entity_load('image_style', $style_name);
if ($entity instanceof Drupal\image\Entity\ImageStyle) {
return $entity->buildUrl($path);
}
return file_create_url($path);
}
......@@ -19,6 +19,13 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
protected $dumpHeaders = TRUE;
/**
* Responsive image mapping entity instance we test with.
*
* @var \Drupal\responsive_image\Entity\ResponsiveImageMapping
*/
protected $responsiveImgMapping;
/**
* Modules to enable.
*
......@@ -48,38 +55,74 @@ protected function setUp() {
));
$this->drupalLogin($this->admin_user);
// Add responsive image mapping.
$responsive_image_mapping = entity_create('responsive_image_mapping', array(
$this->responsiveImgMapping = entity_create('responsive_image_mapping', array(
'id' => 'mapping_one',
'label' => 'Mapping One',
'breakpointGroup' => 'responsive_image_test_module',
));
$responsive_image_mapping
->addMapping('responsive_image_test_module.mobile', '1x', 'thumbnail')
->addMapping('responsive_image_test_module.narrow', '1x', 'medium')
->addMapping('responsive_image_test_module.wide', '1x', 'large')
->save();
}
/**
* Test responsive image formatters on node display for public files.
*/
public function testResponsiveImageFieldFormattersPublic() {
$this->_testResponsiveImageFieldFormatters('public');
$this->addTestMappings();
$this->doTestResponsiveImageFieldFormatters('public');
}
/**
* Test responsive image formatters on node display for private files.
*/
public function testResponsiveImageFieldFormattersPrivate() {
$this->addTestMappings();
// Remove access content permission from anonymous users.
user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array('access content' => FALSE));
$this->_testResponsiveImageFieldFormatters('private');
$this->doTestResponsiveImageFieldFormatters('private');
}
/**
* Test responsive image formatters when image style is empty.
*/
public function testResponsiveImageFieldFormattersEmptyStyle() {
$this->addTestMappings(TRUE);
$this->doTestResponsiveImageFieldFormatters('public', TRUE);
}
/**
* Add mappings to the responsive image mapping entity.
*
* @param bool $empty_styles
* If true, the mappings will get empty image styles.
*/
protected function addTestMappings($empty_styles = FALSE) {
if ($empty_styles) {
$this->responsiveImgMapping
->addMapping('responsive_image_test_module.mobile', '1x', '')
->addMapping('responsive_image_test_module.narrow', '1x', '')
->addMapping('responsive_image_test_module.wide', '1x', '')
->save();
}
else {
$this->responsiveImgMapping
->addMapping('responsive_image_test_module.mobile', '1x', 'thumbnail')
->addMapping('responsive_image_test_module.narrow', '1x', 'medium')
->addMapping('responsive_image_test_module.wide', '1x', 'large')
->save();
}
}
/**
* Test responsive image formatters on node display.
*
* If the empty styles param is set, then the function only tests for the
* fallback image style (large).
*
* @param string $scheme
* File scheme to use.
* @param bool $empty_styles
* If true, use an empty string for image style names.
* Defaults to false.
*/
public function _testResponsiveImageFieldFormatters($scheme) {
protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles = FALSE) {
$node_storage = $this->container->get('entity.manager')->getStorage('node');
$field_name = Unicode::strtolower($this->randomMachineName());
$this->createImageField($field_name, 'article', array('uri_scheme' => $scheme));
......@@ -150,16 +193,20 @@ public function _testResponsiveImageFieldFormatters($scheme) {
// Output should contain all image styles and all breakpoints.
$this->drupalGet('node/' . $nid);
$this->assertRaw('/styles/thumbnail/');
$this->assertRaw('/styles/medium/');
if (!$empty_styles) {
$this->assertRaw('/styles/thumbnail/');
$this->assertRaw('/styles/medium/');
}
$this->assertRaw('/styles/large/');
$this->assertRaw('media="(min-width: 0px)"');
$this->assertRaw('media="(min-width: 560px)"');
$this->assertRaw('media="(min-width: 851px)"');
$cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags'));
$this->assertTrue(in_array('responsive_image_mapping:mapping_one', $cache_tags));
$this->assertTrue(in_array('image_style:thumbnail', $cache_tags));
$this->assertTrue(in_array('image_style:medium', $cache_tags));
if (!$empty_styles) {
$this->assertTrue(in_array('image_style:thumbnail', $cache_tags));
$this->assertTrue(in_array('image_style:medium', $cache_tags));
}
$this->assertTrue(in_array('image_style:large', $cache_tags));
// Test the fallback image style.
......
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