function ResponsiveImageFormatterTest::testImageFormatterUrlOptions

Same name and namespace in other branches
  1. main core/modules/responsive_image/tests/src/Kernel/ResponsiveImageFormatterTest.php \Drupal\Tests\responsive_image\Kernel\ResponsiveImageFormatterTest::testImageFormatterUrlOptions()

Tests Image Formatter URL options handling.

File

core/modules/responsive_image/tests/src/Kernel/ResponsiveImageFormatterTest.php, line 108

Class

ResponsiveImageFormatterTest
Tests the responsive image field rendering.

Namespace

Drupal\Tests\responsive_image\Kernel

Code

public function testImageFormatterUrlOptions() : void {
  // Create a test entity with the responsive image field set.
  $entity = EntityTest::create([
    'name' => $this->randomMachineName(),
  ]);
  $entity->{$this->fieldName}
    ->generateSampleItems(1);
  $entity->save();
  // Generate the render array to verify URL options are as expected.
  $build = $this->display
    ->build($entity);
  $this->assertInstanceOf(Url::class, $build[$this->fieldName][0]['#url']);
  $build[$this->fieldName][0]['#url']
    ->setOption('attributes', [
    'data-attributes-test' => 'test123',
  ]);
  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $output = $renderer->renderRoot($build[$this->fieldName][0]);
  $this->assertStringContainsString('<a href="' . $entity->toUrl()
    ->toString() . '" data-attributes-test="test123"', (string) $output);
}

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