You are here

private function ResponsiveImageFieldDisplayTest::assertResponsiveImageFieldFormattersLink in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/responsive_image/tests/src/Functional/ResponsiveImageFieldDisplayTest.php \Drupal\Tests\responsive_image\Functional\ResponsiveImageFieldDisplayTest::assertResponsiveImageFieldFormattersLink()
  2. 10 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'.

2 calls to ResponsiveImageFieldDisplayTest::assertResponsiveImageFieldFormattersLink()
ResponsiveImageFieldDisplayTest::testResponsiveImageFieldFormattersLinkToFile in core/modules/responsive_image/tests/src/Functional/ResponsiveImageFieldDisplayTest.php
Tests responsive image formatters on node display linked to the file.
ResponsiveImageFieldDisplayTest::testResponsiveImageFieldFormattersLinkToNode in core/modules/responsive_image/tests/src/Functional/ResponsiveImageFieldDisplayTest.php
Tests responsive image formatters on node display linked to the node.

File

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

Class

ResponsiveImageFieldDisplayTest
Tests responsive image display formatter.

Namespace

Drupal\Tests\responsive_image\Functional

Code

private function assertResponsiveImageFieldFormattersLink($link_type) {
  $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
    ->assertPattern('/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
        ->assertPattern('/<a(.*?)href="' . preg_quote(file_url_transform_relative(file_create_url($image_uri)), '/') . '"(.*?)>\\s*<picture/');
      break;
    case 'content':

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