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