Mercurial > hg > isophonics-drupal-site
comparison core/modules/image/tests/src/Kernel/ImageThemeFunctionTest.php @ 16:c2387f117808
Routine composer update
author | Chris Cannam |
---|---|
date | Tue, 10 Jul 2018 15:07:59 +0100 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
15:e200cb7efeb3 | 16:c2387f117808 |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\image\Kernel; | |
4 | |
5 use Drupal\Core\Field\FieldStorageDefinitionInterface; | |
6 use Drupal\Core\Url; | |
7 use Drupal\entity_test\Entity\EntityTest; | |
8 use Drupal\field\Entity\FieldConfig; | |
9 use Drupal\file\Entity\File; | |
10 use Drupal\image\Entity\ImageStyle; | |
11 use Drupal\KernelTests\KernelTestBase; | |
12 use Drupal\field\Entity\FieldStorageConfig; | |
13 use Drupal\Tests\TestFileCreationTrait; | |
14 | |
15 /** | |
16 * Tests image theme functions. | |
17 * | |
18 * @group image | |
19 */ | |
20 class ImageThemeFunctionTest extends KernelTestBase { | |
21 | |
22 use TestFileCreationTrait { | |
23 getTestFiles as drupalGetTestFiles; | |
24 compareFiles as drupalCompareFiles; | |
25 } | |
26 | |
27 /** | |
28 * Modules to enable. | |
29 * | |
30 * @var array | |
31 */ | |
32 public static $modules = ['entity_test', 'field', 'file', 'image', 'system', 'simpletest', 'user']; | |
33 | |
34 /** | |
35 * Created file entity. | |
36 * | |
37 * @var \Drupal\file\Entity\File | |
38 */ | |
39 protected $image; | |
40 | |
41 /** | |
42 * @var \Drupal\Core\Image\ImageFactory | |
43 */ | |
44 protected $imageFactory; | |
45 | |
46 protected function setUp() { | |
47 parent::setUp(); | |
48 | |
49 $this->installEntitySchema('entity_test'); | |
50 $this->installEntitySchema('file'); | |
51 $this->installSchema('file', ['file_usage']); | |
52 $this->installEntitySchema('user'); | |
53 | |
54 FieldStorageConfig::create([ | |
55 'entity_type' => 'entity_test', | |
56 'field_name' => 'image_test', | |
57 'type' => 'image', | |
58 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, | |
59 ])->save(); | |
60 FieldConfig::create([ | |
61 'entity_type' => 'entity_test', | |
62 'field_name' => 'image_test', | |
63 'bundle' => 'entity_test', | |
64 ])->save(); | |
65 file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg'); | |
66 $this->image = File::create([ | |
67 'uri' => 'public://example.jpg', | |
68 ]); | |
69 $this->image->save(); | |
70 $this->imageFactory = $this->container->get('image.factory'); | |
71 } | |
72 | |
73 /** | |
74 * Tests usage of the image field formatters. | |
75 */ | |
76 public function testImageFormatterTheme() { | |
77 /** @var \Drupal\Core\Render\RendererInterface $renderer */ | |
78 $renderer = $this->container->get('renderer'); | |
79 | |
80 // Create an image. | |
81 $files = $this->drupalGetTestFiles('image'); | |
82 $file = reset($files); | |
83 $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); | |
84 | |
85 // Create a style. | |
86 $style = ImageStyle::create(['name' => 'test', 'label' => 'Test']); | |
87 $style->save(); | |
88 $url = file_url_transform_relative($style->buildUrl($original_uri)); | |
89 | |
90 // Create a test entity with the image field set. | |
91 $entity = EntityTest::create(); | |
92 $entity->image_test->target_id = $this->image->id(); | |
93 $entity->image_test->alt = NULL; | |
94 $entity->image_test->uri = $original_uri; | |
95 $image = $this->imageFactory->get('public://example.jpg'); | |
96 $entity->save(); | |
97 | |
98 // Create the base element that we'll use in the tests below. | |
99 $path = $this->randomMachineName(); | |
100 $base_element = [ | |
101 '#theme' => 'image_formatter', | |
102 '#image_style' => 'test', | |
103 '#item' => $entity->image_test, | |
104 '#url' => Url::fromUri('base:' . $path), | |
105 ]; | |
106 | |
107 // Test using theme_image_formatter() with a NULL value for the alt option. | |
108 $element = $base_element; | |
109 $this->setRawContent($renderer->renderRoot($element)); | |
110 $elements = $this->xpath('//a[@href=:path]/img[@src=:url and @width=:width and @height=:height]', [':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()]); | |
111 $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders with a NULL value for the alt option.'); | |
112 | |
113 // Test using theme_image_formatter() without an image title, alt text, or | |
114 // link options. | |
115 $element = $base_element; | |
116 $element['#item']->alt = ''; | |
117 $this->setRawContent($renderer->renderRoot($element)); | |
118 $elements = $this->xpath('//a[@href=:path]/img[@src=:url and @width=:width and @height=:height and @alt=""]', [':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()]); | |
119 $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders without title, alt, or path options.'); | |
120 | |
121 // Link the image to a fragment on the page, and not a full URL. | |
122 $fragment = $this->randomMachineName(); | |
123 $element = $base_element; | |
124 $element['#url'] = Url::fromRoute('<none>', [], ['fragment' => $fragment]); | |
125 $this->setRawContent($renderer->renderRoot($element)); | |
126 $elements = $this->xpath('//a[@href=:fragment]/img[@src=:url and @width=:width and @height=:height and @alt=""]', [ | |
127 ':fragment' => '#' . $fragment, | |
128 ':url' => $url, | |
129 ':width' => $image->getWidth(), | |
130 ':height' => $image->getHeight() | |
131 ]); | |
132 $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders a link fragment.'); | |
133 } | |
134 | |
135 /** | |
136 * Tests usage of the image style theme function. | |
137 */ | |
138 public function testImageStyleTheme() { | |
139 /** @var \Drupal\Core\Render\RendererInterface $renderer */ | |
140 $renderer = $this->container->get('renderer'); | |
141 | |
142 // Create an image. | |
143 $files = $this->drupalGetTestFiles('image'); | |
144 $file = reset($files); | |
145 $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); | |
146 | |
147 // Create a style. | |
148 $style = ImageStyle::create(['name' => 'image_test', 'label' => 'Test']); | |
149 $style->save(); | |
150 $url = file_url_transform_relative($style->buildUrl($original_uri)); | |
151 | |
152 // Create the base element that we'll use in the tests below. | |
153 $base_element = [ | |
154 '#theme' => 'image_style', | |
155 '#style_name' => 'image_test', | |
156 '#uri' => $original_uri, | |
157 ]; | |
158 | |
159 $element = $base_element; | |
160 $this->setRawContent($renderer->renderRoot($element)); | |
161 $elements = $this->xpath('//img[@src=:url and @alt=""]', [':url' => $url]); | |
162 $this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly.'); | |
163 | |
164 // Test using theme_image_style() with a NULL value for the alt option. | |
165 $element = $base_element; | |
166 $element['#alt'] = NULL; | |
167 $this->setRawContent($renderer->renderRoot($element)); | |
168 $elements = $this->xpath('//img[@src=:url]', [':url' => $url]); | |
169 $this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly with a NULL value for the alt option.'); | |
170 } | |
171 | |
172 /** | |
173 * Tests image alt attribute functionality. | |
174 */ | |
175 public function testImageAltFunctionality() { | |
176 /** @var \Drupal\Core\Render\RendererInterface $renderer */ | |
177 $renderer = $this->container->get('renderer'); | |
178 | |
179 // Test using alt directly with alt attribute. | |
180 $image_with_alt_property = [ | |
181 '#theme' => 'image', | |
182 '#uri' => '/core/themes/bartik/logo.svg', | |
183 '#alt' => 'Regular alt', | |
184 '#title' => 'Test title', | |
185 '#width' => '50%', | |
186 '#height' => '50%', | |
187 '#attributes' => ['class' => 'image-with-regular-alt', 'id' => 'my-img'], | |
188 ]; | |
189 | |
190 $this->setRawContent($renderer->renderRoot($image_with_alt_property)); | |
191 $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-regular-alt", ":alt" => "Regular alt"]); | |
192 $this->assertEqual(count($elements), 1, 'Regular alt displays correctly'); | |
193 | |
194 // Test using alt attribute inside attributes. | |
195 $image_with_alt_attribute_alt_attribute = [ | |
196 '#theme' => 'image', | |
197 '#uri' => '/core/themes/bartik/logo.svg', | |
198 '#width' => '50%', | |
199 '#height' => '50%', | |
200 '#attributes' => [ | |
201 'class' => 'image-with-attribute-alt', | |
202 'id' => 'my-img', | |
203 'title' => 'New test title', | |
204 'alt' => 'Attribute alt', | |
205 ], | |
206 ]; | |
207 | |
208 $this->setRawContent($renderer->renderRoot($image_with_alt_attribute_alt_attribute)); | |
209 $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-attribute-alt", ":alt" => "Attribute alt"]); | |
210 $this->assertEqual(count($elements), 1, 'Attribute alt displays correctly'); | |
211 | |
212 // Test using alt attribute as property and inside attributes. | |
213 $image_with_alt_attribute_both = [ | |
214 '#theme' => 'image', | |
215 '#uri' => '/core/themes/bartik/logo.svg', | |
216 '#width' => '50%', | |
217 '#height' => '50%', | |
218 '#alt' => 'Kitten sustainable', | |
219 '#attributes' => [ | |
220 'class' => 'image-with-attribute-alt', | |
221 'id' => 'my-img', | |
222 'title' => 'New test title', | |
223 'alt' => 'Attribute alt', | |
224 ], | |
225 ]; | |
226 | |
227 $this->setRawContent($renderer->renderRoot($image_with_alt_attribute_both)); | |
228 $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-attribute-alt", ":alt" => "Attribute alt"]); | |
229 $this->assertEqual(count($elements), 1, 'Attribute alt overrides alt property if both set.'); | |
230 } | |
231 | |
232 } |