3 namespace Drupal\Tests\image\Kernel;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
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;
16 * Tests image theme functions.
20 class ImageThemeFunctionTest extends KernelTestBase {
22 use TestFileCreationTrait {
23 getTestFiles as drupalGetTestFiles;
24 compareFiles as drupalCompareFiles;
32 public static $modules = ['entity_test', 'field', 'file', 'image', 'system', 'simpletest', 'user'];
35 * Created file entity.
37 * @var \Drupal\file\Entity\File
42 * @var \Drupal\Core\Image\ImageFactory
44 protected $imageFactory;
46 protected function setUp() {
49 $this->installEntitySchema('entity_test');
50 $this->installEntitySchema('file');
51 $this->installSchema('file', ['file_usage']);
52 $this->installEntitySchema('user');
54 FieldStorageConfig::create([
55 'entity_type' => 'entity_test',
56 'field_name' => 'image_test',
58 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
61 'entity_type' => 'entity_test',
62 'field_name' => 'image_test',
63 'bundle' => 'entity_test',
65 file_unmanaged_copy($this->root . '/core/misc/druplicon.png', 'public://example.jpg');
66 $this->image = File::create([
67 'uri' => 'public://example.jpg',
70 $this->imageFactory = $this->container->get('image.factory');
74 * Tests usage of the image field formatters.
76 public function testImageFormatterTheme() {
77 /** @var \Drupal\Core\Render\RendererInterface $renderer */
78 $renderer = $this->container->get('renderer');
81 $files = $this->drupalGetTestFiles('image');
82 $file = reset($files);
83 $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
86 $style = ImageStyle::create(['name' => 'test', 'label' => 'Test']);
88 $url = file_url_transform_relative($style->buildUrl($original_uri));
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');
98 // Create the base element that we'll use in the tests below.
99 $path = $this->randomMachineName();
101 '#theme' => 'image_formatter',
102 '#image_style' => 'test',
103 '#item' => $entity->image_test,
104 '#url' => Url::fromUri('base:' . $path),
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.');
113 // Test using theme_image_formatter() without an image title, alt text, or
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.');
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,
129 ':width' => $image->getWidth(),
130 ':height' => $image->getHeight(),
132 $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders a link fragment.');
136 * Tests usage of the image style theme function.
138 public function testImageStyleTheme() {
139 /** @var \Drupal\Core\Render\RendererInterface $renderer */
140 $renderer = $this->container->get('renderer');
143 $files = $this->drupalGetTestFiles('image');
144 $file = reset($files);
145 $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
148 $style = ImageStyle::create(['name' => 'image_test', 'label' => 'Test']);
150 $url = file_url_transform_relative($style->buildUrl($original_uri));
152 // Create the base element that we'll use in the tests below.
154 '#theme' => 'image_style',
155 '#style_name' => 'image_test',
156 '#uri' => $original_uri,
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.');
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.');
173 * Tests image alt attribute functionality.
175 public function testImageAltFunctionality() {
176 /** @var \Drupal\Core\Render\RendererInterface $renderer */
177 $renderer = $this->container->get('renderer');
179 // Test using alt directly with alt attribute.
180 $image_with_alt_property = [
182 '#uri' => '/core/themes/bartik/logo.svg',
183 '#alt' => 'Regular alt',
184 '#title' => 'Test title',
187 '#attributes' => ['class' => 'image-with-regular-alt', 'id' => 'my-img'],
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');
194 // Test using alt attribute inside attributes.
195 $image_with_alt_attribute_alt_attribute = [
197 '#uri' => '/core/themes/bartik/logo.svg',
201 'class' => 'image-with-attribute-alt',
203 'title' => 'New test title',
204 'alt' => 'Attribute alt',
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');
212 // Test using alt attribute as property and inside attributes.
213 $image_with_alt_attribute_both = [
215 '#uri' => '/core/themes/bartik/logo.svg',
218 '#alt' => 'Kitten sustainable',
220 'class' => 'image-with-attribute-alt',
222 'title' => 'New test title',
223 'alt' => 'Attribute alt',
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.');