You are here

function ImageThemeFunctionTest::testImageFormatterTheme 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::testImageFormatterTheme()

Tests usage of the image field formatters.

File

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

Class

ImageThemeFunctionTest
Tests image theme functions.

Namespace

Drupal\image\Tests

Code

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 = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);

  // Create a style.
  $style = entity_create('image_style', array(
    'name' => 'test',
    'label' => 'Test',
  ));
  $style
    ->save();
  $url = $style
    ->buildUrl($original_uri);

  // Create a test entity with the image field set.
  $entity = entity_create('entity_test');
  $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 = array(
    '#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[@class="image-style-test" and @src=:url and @width=:width and @height=:height]', array(
    ':path' => base_path() . $path,
    ':url' => $url,
    ':width' => $image
      ->getWidth(),
    ':height' => $image
      ->getHeight(),
  ));
  $this
    ->assertEqual(count($elements), 1, '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[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', array(
    ':path' => base_path() . $path,
    ':url' => $url,
    ':width' => $image
      ->getWidth(),
    ':height' => $image
      ->getHeight(),
  ));
  $this
    ->assertEqual(count($elements), 1, '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[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', array(
    ':fragment' => '#' . $fragment,
    ':url' => $url,
    ':width' => $image
      ->getWidth(),
    ':height' => $image
      ->getHeight(),
  ));
  $this
    ->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders a link fragment.');
}