You are here

private function ResponsiveImageFieldDisplayTest::assertResponsiveImageFieldFormattersLink in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/responsive_image/tests/src/Functional/ResponsiveImageFieldDisplayTest.php \Drupal\Tests\responsive_image\Functional\ResponsiveImageFieldDisplayTest::assertResponsiveImageFieldFormattersLink()
  2. 9 core/modules/responsive_image/tests/src/Functional/ResponsiveImageFieldDisplayTest.php \Drupal\Tests\responsive_image\Functional\ResponsiveImageFieldDisplayTest::assertResponsiveImageFieldFormattersLink()

Tests responsive image formatters linked to the file or node.

Parameters

string $link_type: The link type to test. Either 'file' or 'content'.

File

core/modules/responsive_image/tests/src/Functional/ResponsiveImageFieldDisplayTest.php, line 471

Class

ResponsiveImageFieldDisplayTest
Tests responsive image display formatter.

Namespace

Drupal\Tests\responsive_image\Functional

Code

private function assertResponsiveImageFieldFormattersLink(string $link_type) : void {
  $field_name = mb_strtolower($this
    ->randomMachineName());
  $field_settings = [
    'alt_field_required' => 0,
  ];
  $this
    ->createImageField($field_name, 'article', [
    'uri_scheme' => 'public',
  ], $field_settings);

  // Create a new node with an image attached.
  $test_image = current($this
    ->getTestFiles('image'));

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');

  // Test the image linked to file formatter.
  $display_options = [
    'type' => 'responsive_image',
    'settings' => [
      'image_link' => $link_type,
      'responsive_image_style' => 'style_one',
    ],
  ];
  $display_repository
    ->getViewDisplay('node', 'article')
    ->setComponent($field_name, $display_options)
    ->save();

  // Ensure that preview works.
  $this
    ->previewNodeImage($test_image, $field_name, 'article');

  // Look for a picture tag in the preview output
  $this
    ->assertSession()
    ->responseMatches('/picture/');
  $nid = $this
    ->uploadNodeImage($test_image, $field_name, 'article');
  $this->container
    ->get('entity_type.manager')
    ->getStorage('node')
    ->resetCache([
    $nid,
  ]);
  $node = Node::load($nid);

  // Use the responsive image formatter linked to file formatter.
  $display_options = [
    'type' => 'responsive_image',
    'settings' => [
      'image_link' => $link_type,
      'responsive_image_style' => 'style_one',
    ],
  ];
  $display_repository
    ->getViewDisplay('node', 'article')
    ->setComponent($field_name, $display_options)
    ->save();

  // Create a derivative so at least one MIME type will be known.
  $large_style = ImageStyle::load('large');
  $image_uri = File::load($node->{$field_name}->target_id)
    ->getFileUri();
  $large_style
    ->createDerivative($image_uri, $large_style
    ->buildUri($image_uri));

  // Output should contain all image styles and all breakpoints.
  $this
    ->drupalGet('node/' . $nid);
  switch ($link_type) {
    case 'file':

      // Make sure the link to the file is present.
      $this
        ->assertSession()
        ->responseMatches('/<a(.*?)href="' . preg_quote($this->fileUrlGenerator
        ->generateString($image_uri), '/') . '"(.*?)>\\s*<picture/');
      break;
    case 'content':

      // Make sure the link to the node is present.
      $this
        ->assertSession()
        ->responseMatches('/<a(.*?)href="' . preg_quote($node
        ->toUrl()
        ->toString(), '/') . '"(.*?)>\\s*<picture/');
      break;
  }
}