// Get node field original image URI.
$fid = $node->get($field_name)->target_id; $original_uri = File::load($fid)->getFileUri();
// Test that image is displayed using newly created style.
/** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */ $file_url_generator = \Drupal::service('file_url_generator');
// Rename the style and make sure the image field is updated.
$new_style_name = strtolower($this->randomMachineName(10)); $new_style_label = $this->randomString(); $edit = [ 'name' => $new_style_name, 'label' => $new_style_label, ]; $this->drupalGet($style_path . $style_name); $this->submitForm($edit, 'Save'); $this->assertSession()->statusMessageContains('Changes to the style have been saved.', 'status');
// Create a copy of a test image file in root.
$test_uri = 'public://image-test-do.png'; \Drupal::service('file_system')->copy('core/tests/fixtures/files/image-test.png', $test_uri, FileSystemInterface::EXISTS_REPLACE); $this->assertFileExists($test_uri);
// Execute the image style on the test image via a GET request.
$derivative_uri = 'public://styles/image_effect_test/public/image-test-do.png.jpeg'; $this->assertFileDoesNotExist($derivative_uri); $url = \Drupal::service('file_url_generator')->transformRelative($image_style->buildUrl($test_uri)); $this->drupalGet($this->getAbsoluteUrl($url)); $this->assertSession()->statusCodeEquals(200); $this->assertFileExists($derivative_uri); }
if(!$scheme){ $baseUrl = $relative ? base_path() : $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost() . base_path(); return$this->generatePath($baseUrl, $uri); } elseif($scheme == 'http' || $scheme == 'https' || $scheme == 'data'){ // Check for HTTP and data URI-encoded URLs so that we don't have to
// implement getExternalUrl() for the HTTP and data schemes.
return$relative ? $this->transformRelative($uri) : $uri; } elseif($wrapper = $this->streamWrapperManager->getViaUri($uri)){ // Attempt to return an external URL using the appropriate wrapper.
$externalUrl = $wrapper->getExternalUrl(); return$relative ? $this->transformRelative($externalUrl) : $externalUrl; } thrownewInvalidStreamWrapperException(); }
/**
* Generate a URL path.
*
* @param string $base_url
* The base URL.
* @param string $uri
* The URI.
*
* @return string
* The URL path.
*/
$variables = [ '#theme' => 'image_style', '#style_name' => 'test', '#uri' => $original_uri, '#width' => 40, '#height' => 20, ]; // Verify that the original image matches the hard-coded values.
$image_file = $image_factory->get($original_uri); $this->assertEquals($variables['#width'], $image_file->getWidth());
// Test the image URL formatter without an image style.
$display_options = [ 'type' => 'image_url', 'settings' => ['image_style' => ''], ]; $expected_url = $file->createFileUrl(); $this->assertEquals($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']);
// Test the image URL formatter with an image style.
$display_options['settings']['image_style'] = 'thumbnail'; $expected_url = \Drupal::service('file_url_generator')->transformRelative(ImageStyle::load('thumbnail')->buildUrl($image_uri)); $this->assertEquals($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']);
// Enable user pictures on nodes.
$this->config('system.theme.global')->set('features.node_user_picture', TRUE)->save();
$image_style_id = $this->config('core.entity_view_display.user.user.compact')->get('content.user_picture.settings.image_style'); $style = ImageStyle::load($image_style_id); $image_url = \Drupal::service('file_url_generator')->transformRelative($style->buildUrl($file->getFileUri())); $alt_text = 'Profile picture for user ' . $this->webUser->getAccountName();
// Verify that the image is displayed on the node page.
$this->drupalGet('node/' . $node->id()); $elements = $this->cssSelect('article > footer img[alt="' . $alt_text . '"][src="' . $image_url . '"]'); $this->assertCount(1, $elements, 'User picture with alt text found on node page.');
// Enable user pictures on comments, instead of nodes.
$this->config('system.theme.global') ->set('features.node_user_picture', FALSE) ->set('features.comment_user_picture', TRUE)
// Output should contain all image styles and all breakpoints.
$this->drupalGet('node/' . $nid); if(!$empty_styles){ $this->assertSession()->responseContains('/styles/medium/'); // Assert the empty image is present.
$this->assertSession()->responseContains('data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='); $thumbnail_style = ImageStyle::load('thumbnail'); // Assert the output of the 'srcset' attribute (small multipliers first).
$this->assertSession()->responseContains('data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== 1x, ' . $this->fileUrlGenerator->transformRelative($thumbnail_style->buildUrl($image_uri)) . ' 1.5x'); $this->assertSession()->responseContains('/styles/medium/'); // Assert the output of the original image.
$this->assertSession()->responseContains($this->fileUrlGenerator->generateString($image_uri) . ' 3x'); // Assert the output of the breakpoints.
$this->assertSession()->responseContains('media="(min-width: 0px)"'); $this->assertSession()->responseContains('media="(min-width: 560px)"'); // Assert the output of the 'sizes' attribute.
$this->assertSession()->responseContains('sizes="(min-width: 700px) 700px, 100vw"'); $this->assertSession()->responseMatches('/media="\(min-width: 560px\)".+?sizes="\(min-width: 700px\) 700px, 100vw"/'); // Assert the output of the 'srcset' attribute (small images first).
$medium_style = ImageStyle::load('medium');
// Make sure the srcset attribute has the correct value.
$this->assertRaw($this->fileUrlGenerator->transformRelative($this->fileUrlGenerator->generateString($this->testImages[0])) . ' 1x, ' . $this->fileUrlGenerator->transformRelative($this->fileUrlGenerator->generateString($this->testImages[1])) . ' 2x', 'Correct output for image with srcset attribute and multipliers.'); }
/**
* Tests that an image with the srcset and widths is output correctly.
*/ publicfunctiontestThemeImageWithSrcsetWidth(){ // Test with multipliers.
$widths = [ rand(0, 500) . 'w', rand(500, 1000) . 'w', ];