Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Theme / ImageTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Theme;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Symfony\Component\HttpFoundation\Request;
7
8 /**
9  * Tests built-in image theme functions.
10  *
11  * @group Theme
12  */
13 class ImageTest extends KernelTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['system'];
21
22   /*
23    * The images to test with.
24    *
25    * @var array
26    */
27   protected $testImages;
28
29   protected function setUp() {
30     parent::setUp();
31
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);
38
39     $this->testImages = [
40       'core/misc/druplicon.png',
41       'core/misc/loading.gif',
42     ];
43   }
44
45   /**
46    * Tests that an image with the sizes attribute is output correctly.
47    */
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';
51     $image = [
52       '#theme' => 'image',
53       '#sizes' => $sizes,
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(),
59     ];
60     $this->render($image);
61
62     // Make sure sizes is set.
63     $this->assertRaw($sizes, 'Sizes is set correctly.');
64   }
65
66   /**
67    * Tests that an image with the src attribute is output correctly.
68    */
69   public function testThemeImageWithSrc() {
70
71     $image = [
72       '#theme' => 'image',
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(),
78     ];
79     $this->render($image);
80
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.');
83   }
84
85   /**
86    * Tests that an image with the srcset and multipliers is output correctly.
87    */
88   public function testThemeImageWithSrcsetMultiplier() {
89     // Test with multipliers.
90     $image = [
91       '#theme' => 'image',
92       '#srcset' => [
93         [
94           'uri' => $this->testImages[0],
95           'multiplier' => '1x',
96         ],
97         [
98           'uri' => $this->testImages[1],
99           'multiplier' => '2x',
100         ],
101       ],
102       '#width' => rand(0, 1000) . 'px',
103       '#height' => rand(0, 500) . 'px',
104       '#alt' => $this->randomMachineName(),
105       '#title' => $this->randomMachineName(),
106     ];
107     $this->render($image);
108
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.');
111   }
112
113   /**
114    * Tests that an image with the srcset and widths is output correctly.
115    */
116   public function testThemeImageWithSrcsetWidth() {
117     // Test with multipliers.
118     $widths = [
119       rand(0, 500) . 'w',
120       rand(500, 1000) . 'w',
121     ];
122     $image = [
123       '#theme' => 'image',
124       '#srcset' => [
125         [
126           'uri' => $this->testImages[0],
127           'width' => $widths[0],
128         ],
129         [
130           'uri' => $this->testImages[1],
131           'width' => $widths[1],
132         ],
133       ],
134       '#width' => rand(0, 1000) . 'px',
135       '#height' => rand(0, 500) . 'px',
136       '#alt' => $this->randomMachineName(),
137       '#title' => $this->randomMachineName(),
138     ];
139     $this->render($image);
140
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.');
143   }
144
145 }