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