You are here

private function ResponsiveImageFieldDisplayTest::assertResponsiveImageFieldFormattersLink in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php \Drupal\responsive_image\Tests\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/src/Tests/ResponsiveImageFieldDisplayTest.php
Tests responsive image formatters on node display linked to the file.
ResponsiveImageFieldDisplayTest::testResponsiveImageFieldFormattersLinkToNode in core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php
Tests responsive image formatters on node display linked to the node.

File

core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php, line 461
Contains \Drupal\responsive_image\Tests\ResponsiveImageFieldDisplayTest.

Class

ResponsiveImageFieldDisplayTest
Tests responsive image display formatter.

Namespace

Drupal\responsive_image\Tests

Code

private function assertResponsiveImageFieldFormattersLink($link_type) {
  $field_name = Unicode::strtolower($this
    ->randomMachineName());
  $field_settings = array(
    'alt_field_required' => 0,
  );
  $this
    ->createImageField($field_name, 'article', array(
    'uri_scheme' => 'public',
  ), $field_settings);

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

  // Test the image linked to file formatter.
  $display_options = array(
    'type' => 'responsive_image',
    'settings' => array(
      'image_link' => $link_type,
      'responsive_image_style' => 'style_one',
    ),
  );
  entity_get_display('node', 'article', 'default')
    ->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.manager')
    ->getStorage('node')
    ->resetCache(array(
    $nid,
  ));
  $node = Node::load($nid);

  // Use the responsive image formatter linked to file formatter.
  $display_options = array(
    'type' => 'responsive_image',
    'settings' => array(
      'image_link' => $link_type,
      'responsive_image_style' => 'style_one',
    ),
  );
  entity_get_display('node', 'article', 'default')
    ->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);
  $this
    ->removeWhiteSpace();
  switch ($link_type) {
    case 'file':

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

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