View source
<?php
namespace Drupal\KernelTests\Core\Theme;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
class ImageTest extends KernelTestBase {
protected static $modules = [
'system',
];
protected $fileUrlGenerator;
protected $testImages;
protected function setUp() : void {
parent::setUp();
$request = Request::create('/', 'GET', [], [], [], $_SERVER);
$this->container = \Drupal::service('kernel')
->getContainer();
$this->container
->get('request_stack')
->push($request);
$this->fileUrlGenerator = $this->container
->get('file_url_generator');
$this->testImages = [
'core/misc/druplicon.png',
'core/misc/loading.gif',
];
}
public function testThemeImageWithSizes() {
$sizes = '(max-width: ' . rand(10, 30) . 'em) 100vw, (max-width: ' . rand(30, 50) . 'em) 50vw, 30vw';
$image = [
'#theme' => 'image',
'#sizes' => $sizes,
'#uri' => reset($this->testImages),
'#width' => rand(0, 1000) . 'px',
'#height' => rand(0, 500) . 'px',
'#alt' => $this
->randomMachineName(),
'#title' => $this
->randomMachineName(),
];
$this
->render($image);
$this
->assertRaw($sizes, 'Sizes is set correctly.');
}
public function testThemeImageWithSrc() {
$image = [
'#theme' => 'image',
'#uri' => reset($this->testImages),
'#width' => rand(0, 1000) . 'px',
'#height' => rand(0, 500) . 'px',
'#alt' => $this
->randomMachineName(),
'#title' => $this
->randomMachineName(),
];
$this
->render($image);
$this
->assertRaw($this->fileUrlGenerator
->generateString($image['#uri']), 'Correct output for an image with the src attribute.');
}
public function testThemeImageWithSrcsetMultiplier() {
$image = [
'#theme' => 'image',
'#srcset' => [
[
'uri' => $this->testImages[0],
'multiplier' => '1x',
],
[
'uri' => $this->testImages[1],
'multiplier' => '2x',
],
],
'#width' => rand(0, 1000) . 'px',
'#height' => rand(0, 500) . 'px',
'#alt' => $this
->randomMachineName(),
'#title' => $this
->randomMachineName(),
];
$this
->render($image);
$this
->assertRaw($this->fileUrlGenerator
->transformRelative($this->fileUrlGenerator
->generateString($this->testImages[0])) . ' 1x, ' . $this->fileUrlGenerator
->transformRelative($this->fileUrlGenerator
->generateString($this->testImages[1])) . ' 2x', 'Correct output for image with srcset attribute and multipliers.');
}
public function testThemeImageWithSrcsetWidth() {
$widths = [
rand(0, 500) . 'w',
rand(500, 1000) . 'w',
];
$image = [
'#theme' => 'image',
'#srcset' => [
[
'uri' => $this->testImages[0],
'width' => $widths[0],
],
[
'uri' => $this->testImages[1],
'width' => $widths[1],
],
],
'#width' => rand(0, 1000) . 'px',
'#height' => rand(0, 500) . 'px',
'#alt' => $this
->randomMachineName(),
'#title' => $this
->randomMachineName(),
];
$this
->render($image);
$this
->assertRaw($this->fileUrlGenerator
->generateString($this->testImages[0]) . ' ' . $widths[0] . ', ' . $this->fileUrlGenerator
->transformRelative($this->fileUrlGenerator
->generateString($this->testImages[1])) . ' ' . $widths[1], 'Correct output for image with srcset attribute and width descriptors.');
}
}