function ImageTest::testThemeImageWithSrcsetWidth

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Theme/ImageTest.php \Drupal\KernelTests\Core\Theme\ImageTest::testThemeImageWithSrcsetWidth()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Theme/ImageTest.php \Drupal\KernelTests\Core\Theme\ImageTest::testThemeImageWithSrcsetWidth()
  3. 11.x core/tests/Drupal/KernelTests/Core/Theme/ImageTest.php \Drupal\KernelTests\Core\Theme\ImageTest::testThemeImageWithSrcsetWidth()

Tests that an image with the srcset and widths is output correctly.

File

core/tests/Drupal/KernelTests/Core/Theme/ImageTest.php, line 131

Class

ImageTest
Tests built-in image theme functions.

Namespace

Drupal\KernelTests\Core\Theme

Code

public function testThemeImageWithSrcsetWidth() : void {
    // Test with multipliers.
    $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);
    // Make sure the srcset attribute has the correct value.
    $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.