You are here

function ImageThemeFunctionTest::testImageAltFunctionality in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/image/src/Tests/ImageThemeFunctionTest.php \Drupal\image\Tests\ImageThemeFunctionTest::testImageAltFunctionality()

Tests image alt attribute functionality.

File

core/modules/image/src/Tests/ImageThemeFunctionTest.php, line 164
Contains \Drupal\image\Tests\ImageThemeFunctionTest.

Class

ImageThemeFunctionTest
Tests image theme functions.

Namespace

Drupal\image\Tests

Code

function testImageAltFunctionality() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');

  // Test using alt directly with alt attribute.
  $image_with_alt_property = array(
    '#theme' => 'image',
    '#uri' => '/core/themes/bartik/logo.svg',
    '#alt' => 'Regular alt',
    '#title' => 'Test title',
    '#width' => '50%',
    '#height' => '50%',
    '#attributes' => array(
      '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)]', array(
    ":class" => "image-with-regular-alt",
    ":alt" => "Regular alt",
  ));
  $this
    ->assertEqual(count($elements), 1, 'Regular alt displays correctly');

  // Test using alt attribute inside attributes.
  $image_with_alt_attribute_alt_attribute = array(
    '#theme' => 'image',
    '#uri' => '/core/themes/bartik/logo.svg',
    '#width' => '50%',
    '#height' => '50%',
    '#attributes' => array(
      '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)]', array(
    ":class" => "image-with-attribute-alt",
    ":alt" => "Attribute alt",
  ));
  $this
    ->assertEqual(count($elements), 1, 'Attribute alt displays correctly');

  // Test using alt attribute as property and inside attributes.
  $image_with_alt_attribute_both = array(
    '#theme' => 'image',
    '#uri' => '/core/themes/bartik/logo.svg',
    '#width' => '50%',
    '#height' => '50%',
    '#alt' => 'Kitten sustainable',
    '#attributes' => array(
      '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)]', array(
    ":class" => "image-with-attribute-alt",
    ":alt" => "Attribute alt",
  ));
  $this
    ->assertEqual(count($elements), 1, 'Attribute alt overrides alt property if both set.');
}