public function ImageThemeFunctionTest::testImageFormatterTheme in Drupal 8
Same name and namespace in other branches
- 9 core/modules/image/tests/src/Kernel/ImageThemeFunctionTest.php \Drupal\Tests\image\Kernel\ImageThemeFunctionTest::testImageFormatterTheme()
Tests usage of the image field formatters.
File
- core/
modules/ image/ tests/ src/ Kernel/ ImageThemeFunctionTest.php, line 84
Class
- ImageThemeFunctionTest
- Tests image theme functions.
Namespace
Drupal\Tests\image\KernelCode
public function testImageFormatterTheme() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container
->get('renderer');
// Create an image.
$files = $this
->drupalGetTestFiles('image');
$file = reset($files);
$original_uri = \Drupal::service('file_system')
->copy($file->uri, 'public://', FileSystemInterface::EXISTS_RENAME);
// Create a style.
$style = ImageStyle::create([
'name' => 'test',
'label' => 'Test',
]);
$style
->save();
$url = file_url_transform_relative($style
->buildUrl($original_uri));
// Create a test entity with the image field set.
$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();
// Create the base element that we'll use in the tests below.
$path = $this
->randomMachineName();
$base_element = [
'#theme' => 'image_formatter',
'#image_style' => 'test',
'#item' => $entity->image_test,
'#url' => Url::fromUri('base:' . $path),
];
// Test using theme_image_formatter() with a NULL value for the alt option.
$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.');
// Test using theme_image_formatter() without an image title, alt text, or
// link options.
$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.');
// Link the image to a fragment on the page, and not a full URL.
$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.');
}