3 namespace Drupal\KernelTests\Core\Theme;
5 use Drupal\KernelTests\KernelTestBase;
6 use Symfony\Component\HttpFoundation\Request;
9 * Tests built-in image theme functions.
13 class ImageTest extends KernelTestBase {
20 public static $modules = ['system'];
23 * The images to test with.
27 protected $testImages;
29 protected function setUp() {
32 // The code under test uses file_url_transform_relative(), which relies on
33 // the Request containing the correct hostname. KernelTestBase doesn't set
34 // it, so push another request onto the stack to ensure it's correct.
35 $request = Request::create('/', 'GET', [], [], [], $_SERVER);
36 $this->container = \Drupal::service('kernel')->getContainer();
37 $this->container->get('request_stack')->push($request);
40 'core/misc/druplicon.png',
41 'core/misc/loading.gif',
46 * Tests that an image with the sizes attribute is output correctly.
48 public function testThemeImageWithSizes() {
49 // Test with multipliers.
50 $sizes = '(max-width: ' . rand(10, 30) . 'em) 100vw, (max-width: ' . rand(30, 50) . 'em) 50vw, 30vw';
54 '#uri' => reset($this->testImages),
55 '#width' => rand(0, 1000) . 'px',
56 '#height' => rand(0, 500) . 'px',
57 '#alt' => $this->randomMachineName(),
58 '#title' => $this->randomMachineName(),
60 $this->render($image);
62 // Make sure sizes is set.
63 $this->assertRaw($sizes, 'Sizes is set correctly.');
67 * Tests that an image with the src attribute is output correctly.
69 public function testThemeImageWithSrc() {
73 '#uri' => reset($this->testImages),
74 '#width' => rand(0, 1000) . 'px',
75 '#height' => rand(0, 500) . 'px',
76 '#alt' => $this->randomMachineName(),
77 '#title' => $this->randomMachineName(),
79 $this->render($image);
81 // Make sure the src attribute has the correct value.
82 $this->assertRaw(file_url_transform_relative(file_create_url($image['#uri'])), 'Correct output for an image with the src attribute.');
86 * Tests that an image with the srcset and multipliers is output correctly.
88 public function testThemeImageWithSrcsetMultiplier() {
89 // Test with multipliers.
94 'uri' => $this->testImages[0],
98 'uri' => $this->testImages[1],
102 '#width' => rand(0, 1000) . 'px',
103 '#height' => rand(0, 500) . 'px',
104 '#alt' => $this->randomMachineName(),
105 '#title' => $this->randomMachineName(),
107 $this->render($image);
109 // Make sure the srcset attribute has the correct value.
110 $this->assertRaw(file_url_transform_relative(file_create_url($this->testImages[0])) . ' 1x, ' . file_url_transform_relative(file_create_url($this->testImages[1])) . ' 2x', 'Correct output for image with srcset attribute and multipliers.');
114 * Tests that an image with the srcset and widths is output correctly.
116 public function testThemeImageWithSrcsetWidth() {
117 // Test with multipliers.
120 rand(500, 1000) . 'w',
126 'uri' => $this->testImages[0],
127 'width' => $widths[0],
130 'uri' => $this->testImages[1],
131 'width' => $widths[1],
134 '#width' => rand(0, 1000) . 'px',
135 '#height' => rand(0, 500) . 'px',
136 '#alt' => $this->randomMachineName(),
137 '#title' => $this->randomMachineName(),
139 $this->render($image);
141 // Make sure the srcset attribute has the correct value.
142 $this->assertRaw(file_url_transform_relative(file_create_url($this->testImages[0])) . ' ' . $widths[0] . ', ' . file_url_transform_relative(file_create_url($this->testImages[1])) . ' ' . $widths[1], 'Correct output for image with srcset attribute and width descriptors.');