function ImageFieldDisplayTest::_testImageFieldFormatters

Test image formatters on node display.

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

File

drupal/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php, line 51
Definition of Drupal\image\Tests\ImageFieldDisplayTest.

Class

ImageFieldDisplayTest
Test class to check that formatters and display settings are working.

Namespace

Drupal\image\Tests

Code

function _testImageFieldFormatters($scheme) {
  $field_name = strtolower($this
    ->randomName());
  $this
    ->createImageField($field_name, 'article', array(
    'uri_scheme' => $scheme,
  ));

  // Create a new node with an image attached.
  $test_image = current($this
    ->drupalGetTestFiles('image'));
  $nid = $this
    ->uploadNodeImage($test_image, $field_name, 'article');
  $node = node_load($nid, TRUE);

  // Test that the default formatter is being used.
  $image_uri = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid'])->uri;
  $image_info = array(
    'uri' => $image_uri,
    'width' => 40,
    'height' => 20,
  );
  $default_output = theme('image', $image_info);
  $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->type, 'default');
  $display
    ->setComponent($field_name, $display_options)
    ->save();
  $default_output = l(theme('image', $image_info), file_create_url($image_uri), array(
    'html' => TRUE,
  ));
  $this
    ->drupalGet('node/' . $nid);
  $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->admin_user);
  }

  // Test the image linked to content formatter.
  $display_options['settings']['image_link'] = 'content';
  $display
    ->setComponent($field_name, $display_options)
    ->save();
  $default_output = l(theme('image', $image_info), 'node/' . $nid, array(
    'html' => TRUE,
    'attributes' => array(
      'class' => 'active',
    ),
  ));
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertRaw($default_output, '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(image_style_url('thumbnail', $image_uri));
  $image_info['uri'] = $image_uri;
  $image_info['width'] = 100;
  $image_info['height'] = 50;
  $image_info['style_name'] = 'thumbnail';
  $default_output = theme('image_style', $image_info);
  $this
    ->drupalGet('node/' . $nid);
  $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(image_style_url('thumbnail', $image_uri));
    $this
      ->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.');
  }
}