3 namespace Drupal\image\Tests;
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\simpletest\WebTestBase;
12 use Drupal\field\Entity\FieldStorageConfig;
15 * Tests image theme functions.
19 class ImageThemeFunctionTest extends WebTestBase {
26 public static $modules = ['image', 'entity_test'];
29 * Created file entity.
31 * @var \Drupal\file\Entity\File
36 * @var \Drupal\Core\Image\ImageFactory
38 protected $imageFactory;
40 protected function setUp() {
43 FieldStorageConfig::create([
44 'entity_type' => 'entity_test',
45 'field_name' => 'image_test',
47 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
50 'entity_type' => 'entity_test',
51 'field_name' => 'image_test',
52 'bundle' => 'entity_test',
54 file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
55 $this->image = File::create([
56 'uri' => 'public://example.jpg',
59 $this->imageFactory = $this->container->get('image.factory');
63 * Tests usage of the image field formatters.
65 public function testImageFormatterTheme() {
66 /** @var \Drupal\Core\Render\RendererInterface $renderer */
67 $renderer = $this->container->get('renderer');
70 $files = $this->drupalGetTestFiles('image');
71 $file = reset($files);
72 $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
75 $style = ImageStyle::create(['name' => 'test', 'label' => 'Test']);
77 $url = file_url_transform_relative($style->buildUrl($original_uri));
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');
87 // Create the base element that we'll use in the tests below.
88 $path = $this->randomMachineName();
90 '#theme' => 'image_formatter',
91 '#image_style' => 'test',
92 '#item' => $entity->image_test,
93 '#url' => Url::fromUri('base:' . $path),
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.');
102 // Test using theme_image_formatter() without an image title, alt text, or
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.');
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,
118 ':width' => $image->getWidth(),
119 ':height' => $image->getHeight()
121 $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders a link fragment.');
125 * Tests usage of the image style theme function.
127 public function testImageStyleTheme() {
128 /** @var \Drupal\Core\Render\RendererInterface $renderer */
129 $renderer = $this->container->get('renderer');
132 $files = $this->drupalGetTestFiles('image');
133 $file = reset($files);
134 $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
137 $style = ImageStyle::create(['name' => 'image_test', 'label' => 'Test']);
139 $url = file_url_transform_relative($style->buildUrl($original_uri));
141 // Create the base element that we'll use in the tests below.
143 '#theme' => 'image_style',
144 '#style_name' => 'image_test',
145 '#uri' => $original_uri,
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.');
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.');
162 * Tests image alt attribute functionality.
164 public function testImageAltFunctionality() {
165 /** @var \Drupal\Core\Render\RendererInterface $renderer */
166 $renderer = $this->container->get('renderer');
168 // Test using alt directly with alt attribute.
169 $image_with_alt_property = [
171 '#uri' => '/core/themes/bartik/logo.svg',
172 '#alt' => 'Regular alt',
173 '#title' => 'Test title',
176 '#attributes' => ['class' => 'image-with-regular-alt', 'id' => 'my-img'],
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');
183 // Test using alt attribute inside attributes.
184 $image_with_alt_attribute_alt_attribute = [
186 '#uri' => '/core/themes/bartik/logo.svg',
190 'class' => 'image-with-attribute-alt',
192 'title' => 'New test title',
193 'alt' => 'Attribute alt',
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');
201 // Test using alt attribute as property and inside attributes.
202 $image_with_alt_attribute_both = [
204 '#uri' => '/core/themes/bartik/logo.svg',
207 '#alt' => 'Kitten sustainable',
209 'class' => 'image-with-attribute-alt',
211 'title' => 'New test title',
212 'alt' => 'Attribute alt',
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.');