You are here

function ImageFieldDisplayTest::_testImageFieldFormatters in Zircon Profile 8

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

Test image formatters on node display.

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

File

core/modules/image/src/Tests/ImageFieldDisplayTest.php, line 50
Contains \Drupal\image\Tests\ImageFieldDisplayTest.

Class

ImageFieldDisplayTest
Tests the display of image fields.

Namespace

Drupal\image\Tests

Code

function _testImageFieldFormatters($scheme) {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $node_storage = $this->container
    ->get('entity.manager')
    ->getStorage('node');
  $field_name = strtolower($this
    ->randomMachineName());
  $field_settings = array(
    'alt_field_required' => 0,
  );
  $instance = $this
    ->createImageField($field_name, 'article', array(
    '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
    ->drupalPostAjaxForm(NULL, array(), "{$field_name}_settings_edit");
  $this
    ->assertLinkByHref(\Drupal::url('entity.image_style.collection'), 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), array(
    '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
    ->drupalPostAjaxForm(NULL, array(), "{$field_name}_settings_edit");
  $this
    ->assertNoLinkByHref(\Drupal::url('entity.image_style.collection'), '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), array(
    '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(array(
    $nid,
  ));
  $node = $node_storage
    ->load($nid);

  // Test that the default formatter is being used.
  $file = $node->{$field_name}->entity;
  $image_uri = $file
    ->getFileUri();
  $image = array(
    '#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 = array(
    'type' => 'image',
    'settings' => array(
      'image_link' => 'file',
    ),
  );
  $display = entity_get_display('node', $node
    ->getType(), 'default');
  $display
    ->setComponent($field_name, $display_options)
    ->save();
  $image = array(
    '#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]);
  $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 try to access the file.
    $this
      ->drupalLogout();
    $this
      ->drupalGet(file_create_url($image_uri));
    $this
      ->assertResponse('403', 'Access denied to original image as anonymous user.');

    // 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 = array(
    '#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]', array(
    ':path' => $node
      ->url(),
    ':url' => file_create_url($image['#uri']),
    ':width' => $image['#width'],
    ':height' => $image['#height'],
    ':alt' => $alt,
  ));
  $this
    ->assertEqual(count($elements), 1, '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 = array(
    '#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 try to access the file.
    $this
      ->drupalLogout();
    $this
      ->drupalGet(ImageStyle::load('thumbnail')
      ->buildUrl($image_uri));
    $this
      ->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.');
  }
}