function ImageThemeFunctionTest::testImageAltFunctionality

Same name and namespace in other branches
  1. 8.9.x core/modules/image/tests/src/Kernel/ImageThemeFunctionTest.php \Drupal\Tests\image\Kernel\ImageThemeFunctionTest::testImageAltFunctionality()
  2. 10 core/modules/image/tests/src/Kernel/ImageThemeFunctionTest.php \Drupal\Tests\image\Kernel\ImageThemeFunctionTest::testImageAltFunctionality()
  3. 11.x core/modules/image/tests/src/Kernel/ImageThemeFunctionTest.php \Drupal\Tests\image\Kernel\ImageThemeFunctionTest::testImageAltFunctionality()

Tests image alt attribute functionality.

File

core/modules/image/tests/src/Kernel/ImageThemeFunctionTest.php, line 186

Class

ImageThemeFunctionTest
Tests image theme functions.

Namespace

Drupal\Tests\image\Kernel

Code

public function testImageAltFunctionality() {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    // Test using alt directly with alt attribute.
    $image_with_alt_property = [
        '#theme' => 'image',
        '#uri' => '/core/themes/olivero/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');
    // Test using alt attribute inside attributes.
    $image_with_alt_attribute_alt_attribute = [
        '#theme' => 'image',
        '#uri' => '/core/themes/olivero/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');
    // Test using alt attribute as property and inside attributes.
    $image_with_alt_attribute_both = [
        '#theme' => 'image',
        '#uri' => '/core/themes/olivero/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.');
}

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