View source
<?php
namespace Drupal\Tests\image\Kernel;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\KernelTests\KernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\TestFileCreationTrait;
class ImageThemeFunctionTest extends KernelTestBase {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
compareFiles as drupalCompareFiles;
}
protected static $modules = [
'entity_test',
'field',
'file',
'image',
'system',
'user',
];
protected $image;
protected $imageFactory;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('file');
$this
->installSchema('file', [
'file_usage',
]);
$this
->installEntitySchema('user');
FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'image_test',
'type' => 'image',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])
->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'image_test',
'bundle' => 'entity_test',
])
->save();
\Drupal::service('file_system')
->copy($this->root . '/core/misc/druplicon.png', 'public://example.jpg');
$this->image = File::create([
'uri' => 'public://example.jpg',
]);
$this->image
->save();
$this->imageFactory = $this->container
->get('image.factory');
}
public function testImageFormatterTheme() {
$renderer = $this->container
->get('renderer');
$files = $this
->drupalGetTestFiles('image');
$file = reset($files);
$original_uri = \Drupal::service('file_system')
->copy($file->uri, 'public://', FileSystemInterface::EXISTS_RENAME);
$style = ImageStyle::create([
'name' => 'test',
'label' => 'Test',
]);
$style
->save();
$url = \Drupal::service('file_url_generator')
->transformRelative($style
->buildUrl($original_uri));
$entity = EntityTest::create();
$entity->image_test->target_id = $this->image
->id();
$entity->image_test->alt = NULL;
$entity->image_test->uri = $original_uri;
$image = $this->imageFactory
->get('public://example.jpg');
$entity
->save();
$path = $this
->randomMachineName();
$base_element = [
'#theme' => 'image_formatter',
'#image_style' => 'test',
'#item' => $entity->image_test,
'#url' => Url::fromUri('base:' . $path),
];
$element = $base_element;
$this
->setRawContent($renderer
->renderRoot($element));
$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(),
]);
$this
->assertCount(1, $elements, 'theme_image_formatter() correctly renders with a NULL value for the alt option.');
$element = $base_element;
$element['#item']->alt = '';
$this
->setRawContent($renderer
->renderRoot($element));
$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(),
]);
$this
->assertCount(1, $elements, 'theme_image_formatter() correctly renders without title, alt, or path options.');
$fragment = $this
->randomMachineName();
$element = $base_element;
$element['#url'] = Url::fromRoute('<none>', [], [
'fragment' => $fragment,
]);
$this
->setRawContent($renderer
->renderRoot($element));
$elements = $this
->xpath('//a[@href=:fragment]/img[@src=:url and @width=:width and @height=:height and @alt=""]', [
':fragment' => '#' . $fragment,
':url' => $url,
':width' => $image
->getWidth(),
':height' => $image
->getHeight(),
]);
$this
->assertCount(1, $elements, 'theme_image_formatter() correctly renders a link fragment.');
}
public function testImageStyleTheme() {
$renderer = $this->container
->get('renderer');
$files = $this
->drupalGetTestFiles('image');
$file = reset($files);
$original_uri = \Drupal::service('file_system')
->copy($file->uri, 'public://', FileSystemInterface::EXISTS_RENAME);
$style = ImageStyle::create([
'name' => 'image_test',
'label' => 'Test',
]);
$style
->save();
$url = \Drupal::service('file_url_generator')
->transformRelative($style
->buildUrl($original_uri));
$base_element = [
'#theme' => 'image_style',
'#style_name' => 'image_test',
'#uri' => $original_uri,
];
$element = $base_element;
$this
->setRawContent($renderer
->renderRoot($element));
$elements = $this
->xpath('//img[@src=:url and @alt=""]', [
':url' => $url,
]);
$this
->assertCount(1, $elements, 'theme_image_style() renders an image correctly.');
$element = $base_element;
$element['#alt'] = NULL;
$this
->setRawContent($renderer
->renderRoot($element));
$elements = $this
->xpath('//img[@src=:url]', [
':url' => $url,
]);
$this
->assertCount(1, $elements, 'theme_image_style() renders an image correctly with a NULL value for the alt option.');
}
public function testImageAltFunctionality() {
$renderer = $this->container
->get('renderer');
$image_with_alt_property = [
'#theme' => 'image',
'#uri' => '/core/themes/bartik/logo.svg',
'#alt' => 'Regular alt',
'#title' => 'Test title',
'#width' => '50%',
'#height' => '50%',
'#attributes' => [
'class' => 'image-with-regular-alt',
'id' => 'my-img',
],
];
$this
->setRawContent($renderer
->renderRoot($image_with_alt_property));
$elements = $this
->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [
":class" => "image-with-regular-alt",
":alt" => "Regular alt",
]);
$this
->assertCount(1, $elements, 'Regular alt displays correctly');
$image_with_alt_attribute_alt_attribute = [
'#theme' => 'image',
'#uri' => '/core/themes/bartik/logo.svg',
'#width' => '50%',
'#height' => '50%',
'#attributes' => [
'class' => 'image-with-attribute-alt',
'id' => 'my-img',
'title' => 'New test title',
'alt' => 'Attribute alt',
],
];
$this
->setRawContent($renderer
->renderRoot($image_with_alt_attribute_alt_attribute));
$elements = $this
->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [
":class" => "image-with-attribute-alt",
":alt" => "Attribute alt",
]);
$this
->assertCount(1, $elements, 'Attribute alt displays correctly');
$image_with_alt_attribute_both = [
'#theme' => 'image',
'#uri' => '/core/themes/bartik/logo.svg',
'#width' => '50%',
'#height' => '50%',
'#alt' => 'Kitten sustainable',
'#attributes' => [
'class' => 'image-with-attribute-alt',
'id' => 'my-img',
'title' => 'New test title',
'alt' => 'Attribute alt',
],
];
$this
->setRawContent($renderer
->renderRoot($image_with_alt_attribute_both));
$elements = $this
->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [
":class" => "image-with-attribute-alt",
":alt" => "Attribute alt",
]);
$this
->assertCount(1, $elements, 'Attribute alt overrides alt property if both set.');
}
}