function ImageThemeFunctionTest::testImageFormatterTheme

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

Tests usage of the image field formatters.

File

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

Class

ImageThemeFunctionTest
Tests image theme functions.

Namespace

Drupal\Tests\image\Kernel

Code

public function testImageFormatterTheme() : void {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    // Create an image.
    $files = $this->drupalGetTestFiles('image');
    $file = reset($files);
    $original_uri = \Drupal::service('file_system')->copy($file->uri, 'public://', FileExists::Rename);
    // Create a style.
    $style = ImageStyle::create([
        'name' => 'test',
        'label' => 'Test',
    ]);
    $style->save();
    $url = \Drupal::service('file_url_generator')->transformRelative($style->buildUrl($original_uri));
    // Create a test entity with the image field set.
    $entity = EntityTest::create();
    $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 = [
        '#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[@src=:url and @width=:width and @height=:height]', [
        ':path' => base_path() . $path,
        ':url' => $url,
        ':width' => $image->getWidth(),
        ':height' => $image->getHeight(),
    ]);
    $this->assertCount(1, $elements, '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[@src=:url and @width=:width and @height=:height and @alt=""]', [
        ':path' => base_path() . $path,
        ':url' => $url,
        ':width' => $image->getWidth(),
        ':height' => $image->getHeight(),
    ]);
    $this->assertCount(1, $elements, '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[@src=:url and @width=:width and @height=:height and @alt=""]', [
        ':fragment' => '#' . $fragment,
        ':url' => $url,
        ':width' => $image->getWidth(),
        ':height' => $image->getHeight(),
    ]);
    $this->assertCount(1, $elements, 'theme_image_formatter() correctly renders a link fragment.');
}

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