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