You are here

public function ImageFieldDisplayTest::_testImageFieldFormatters in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php \Drupal\Tests\image\Functional\ImageFieldDisplayTest::_testImageFieldFormatters()

Test image formatters on node display.

2 calls to ImageFieldDisplayTest::_testImageFieldFormatters()
ImageFieldDisplayTest::testImageFieldFormattersPrivate in core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php
Test image formatters on node display for private files.
ImageFieldDisplayTest::testImageFieldFormattersPublic in core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php
Test image formatters on node display for public files.

File

core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php, line 61

Class

ImageFieldDisplayTest
Tests the display of image fields.

Namespace

Drupal\Tests\image\Functional

Code

public function _testImageFieldFormatters($scheme) {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $field_name = strtolower($this
    ->randomMachineName());
  $field_settings = [
    'alt_field_required' => 0,
  ];
  $instance = $this
    ->createImageField($field_name, 'article', [
    'uri_scheme' => $scheme,
  ], $field_settings);

  // Go to manage display page.
  $this
    ->drupalGet("admin/structure/types/manage/article/display");

  // Test for existence of link to image styles configuration.
  $this
    ->drupalPostForm(NULL, [], "{$field_name}_settings_edit");
  $this
    ->assertLinkByHref(Url::fromRoute('entity.image_style.collection')
    ->toString(), 0, 'Link to image styles configuration is found');

  // Remove 'administer image styles' permission from testing admin user.
  $admin_user_roles = $this->adminUser
    ->getRoles(TRUE);
  user_role_change_permissions(reset($admin_user_roles), [
    'administer image styles' => FALSE,
  ]);

  // Go to manage display page again.
  $this
    ->drupalGet("admin/structure/types/manage/article/display");

  // Test for absence of link to image styles configuration.
  $this
    ->drupalPostForm(NULL, [], "{$field_name}_settings_edit");
  $this
    ->assertNoLinkByHref(Url::fromRoute('entity.image_style.collection')
    ->toString(), 'Link to image styles configuration is absent when permissions are insufficient');

  // Restore 'administer image styles' permission to testing admin user
  user_role_change_permissions(reset($admin_user_roles), [
    'administer image styles' => TRUE,
  ]);

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

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

  // After previewing, make the alt field required. It cannot be required
  // during preview because the form validation will fail.
  $instance
    ->setSetting('alt_field_required', 1);
  $instance
    ->save();

  // Create alt text for the image.
  $alt = $this
    ->randomMachineName();

  // Save node.
  $nid = $this
    ->uploadNodeImage($test_image, $field_name, 'article', $alt);
  $node_storage
    ->resetCache([
    $nid,
  ]);
  $node = $node_storage
    ->load($nid);

  // Test that the default formatter is being used.
  $file = $node->{$field_name}->entity;
  $image_uri = $file
    ->getFileUri();
  $image = [
    '#theme' => 'image',
    '#uri' => $image_uri,
    '#width' => 40,
    '#height' => 20,
    '#alt' => $alt,
  ];
  $default_output = str_replace("\n", NULL, $renderer
    ->renderRoot($image));
  $this
    ->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');

  // Test the image linked to file formatter.
  $display_options = [
    'type' => 'image',
    'settings' => [
      'image_link' => 'file',
    ],
  ];
  $display = \Drupal::service('entity_display.repository')
    ->getViewDisplay('node', $node
    ->getType());
  $display
    ->setComponent($field_name, $display_options)
    ->save();
  $image = [
    '#theme' => 'image',
    '#uri' => $image_uri,
    '#width' => 40,
    '#height' => 20,
    '#alt' => $alt,
  ];
  $default_output = '<a href="' . file_create_url($image_uri) . '">' . $renderer
    ->renderRoot($image) . '</a>';
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertCacheTag($file
    ->getCacheTags()[0]);

  // @todo Remove in https://www.drupal.org/node/2646744.
  $this
    ->assertCacheContext('url.site');
  $cache_tags_header = $this
    ->drupalGetHeader('X-Drupal-Cache-Tags');
  $this
    ->assertTrue(!preg_match('/ image_style\\:/', $cache_tags_header), 'No image style cache tag found.');
  $this
    ->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.');

  // Verify that the image can be downloaded.
  $this
    ->assertEqual(file_get_contents($test_image->uri), $this
    ->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.');
  if ($scheme == 'private') {

    // Only verify HTTP headers when using private scheme and the headers are
    // sent by Drupal.
    $this
      ->assertEqual($this
      ->drupalGetHeader('Content-Type'), 'image/png', 'Content-Type header was sent.');
    $this
      ->assertTrue(strstr($this
      ->drupalGetHeader('Cache-Control'), 'private') !== FALSE, 'Cache-Control header was sent.');

    // Log out and ensure the file cannot be accessed.
    $this
      ->drupalLogout();
    $this
      ->drupalGet(file_create_url($image_uri));
    $this
      ->assertSession()
      ->statusCodeEquals(403);

    // Log in again.
    $this
      ->drupalLogin($this->adminUser);
  }

  // Test the image linked to content formatter.
  $display_options['settings']['image_link'] = 'content';
  $display
    ->setComponent($field_name, $display_options)
    ->save();
  $image = [
    '#theme' => 'image',
    '#uri' => $image_uri,
    '#width' => 40,
    '#height' => 20,
  ];
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertCacheTag($file
    ->getCacheTags()[0]);
  $cache_tags_header = $this
    ->drupalGetHeader('X-Drupal-Cache-Tags');
  $this
    ->assertTrue(!preg_match('/ image_style\\:/', $cache_tags_header), 'No image style cache tag found.');
  $elements = $this
    ->xpath('//a[@href=:path]/img[@src=:url and @alt=:alt and @width=:width and @height=:height]', [
    ':path' => $node
      ->toUrl()
      ->toString(),
    ':url' => file_url_transform_relative(file_create_url($image['#uri'])),
    ':width' => $image['#width'],
    ':height' => $image['#height'],
    ':alt' => $alt,
  ]);
  $this
    ->assertCount(1, $elements, 'Image linked to content formatter displaying correctly on full node view.');

  // Test the image style 'thumbnail' formatter.
  $display_options['settings']['image_link'] = '';
  $display_options['settings']['image_style'] = 'thumbnail';
  $display
    ->setComponent($field_name, $display_options)
    ->save();

  // Ensure the derivative image is generated so we do not have to deal with
  // image style callback paths.
  $this
    ->drupalGet(ImageStyle::load('thumbnail')
    ->buildUrl($image_uri));
  $image_style = [
    '#theme' => 'image_style',
    '#uri' => $image_uri,
    '#width' => 40,
    '#height' => 20,
    '#style_name' => 'thumbnail',
    '#alt' => $alt,
  ];
  $default_output = $renderer
    ->renderRoot($image_style);
  $this
    ->drupalGet('node/' . $nid);
  $image_style = ImageStyle::load('thumbnail');
  $this
    ->assertCacheTag($image_style
    ->getCacheTags()[0]);
  $this
    ->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.');
  if ($scheme == 'private') {

    // Log out and ensure the file cannot be accessed.
    $this
      ->drupalLogout();
    $this
      ->drupalGet(ImageStyle::load('thumbnail')
      ->buildUrl($image_uri));
    $this
      ->assertSession()
      ->statusCodeEquals(403);
  }

  // Test the image URL formatter without an image style.
  $display_options = [
    'type' => 'image_url',
    'settings' => [
      'image_style' => '',
    ],
  ];
  $expected_url = file_url_transform_relative(file_create_url($image_uri));
  $this
    ->assertEqual($expected_url, $node->{$field_name}
    ->view($display_options)[0]['#markup']);

  // Test the image URL formatter with an image style.
  $display_options['settings']['image_style'] = 'thumbnail';
  $expected_url = file_url_transform_relative(ImageStyle::load('thumbnail')
    ->buildUrl($image_uri));
  $this
    ->assertEqual($expected_url, $node->{$field_name}
    ->view($display_options)[0]['#markup']);
}