1 image.test public ImageFieldDisplayTestCase::_testImageFieldFormatters($scheme)

Test image formatters on node display.

File

core/modules/image/tests/image.test, line 777
Tests for image.module.

Class

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

Code

public function _testImageFieldFormatters($scheme) {
  $field_name = strtolower($this->randomName());
  $this->createImageField($field_name, 'post', array('uri_scheme' => $scheme));
  // Create a new node with an image attached.
  $test_image = current($this->backdropGetTestFiles('image'));
  $nid = $this->uploadNodeImage($test_image, $field_name, 'post');
  $node = node_load($nid, NULL, TRUE);

  // Test that the default formatter is being used.
  $image_uri = $node->{$field_name}[LANGUAGE_NONE][0]['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.
  $instance = field_info_instance('node', $field_name, 'post');
  $instance['display']['default']['type'] = 'image';
  $instance['display']['default']['settings']['image_link'] = 'file';
  field_update_instance($instance);
  $default_output = l(theme('image', $image_info), file_create_url($image_uri), array('html' => TRUE));
  $this->backdropGet('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->backdropGet(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 Backdrop.
    $this->assertEqual($this->backdropGetHeader('Content-Type'), 'image/png', 'Content-Type header was sent.');
    $this->assertEqual($this->backdropGetHeader('Cache-Control'), 'private', 'Cache-Control header was sent.');

    // Log out and try to access the file.
    $this->backdropLogout();
    $this->backdropGet(file_create_url($image_uri));
    $this->assertResponse('403', 'Access denied to original image as anonymous user.');

    // Log in again.
    $this->backdropLogin($this->admin_user);
  }

  // Test the image linked to content formatter.
  $instance['display']['default']['settings']['image_link'] = 'content';
  field_update_instance($instance);
  $default_output = l(theme('image', $image_info), 'node/' . $nid, array('html' => TRUE, 'attributes' => array(
    'class' => 'active',
    'aria-current' => 'page',
  )));
  $this->backdropGet('node/' . $nid);
  $this->assertRaw($default_output, t('Image linked to content formatter displaying correctly on full node view.'));

  // Test the lazy loading attribute.
  $instance['display']['default']['settings']['image_link'] = '';
  $instance['display']['default']['settings']['image_load'] = 'lazy';
  field_update_instance($instance);
  $image_info['attributes']['loading'] = 'lazy';
  $default_output = theme('image', $image_info);
  $this->backdropGet('node/' . $nid);
  $this->assertRaw($default_output, t('Image loaded lazily displaying correctly on full node view.'));

  // Test the lazy loading attribute.
  $instance['display']['default']['settings']['image_load'] = 'eager';
  field_update_instance($instance);
  $image_info['attributes']['loading'] = 'eager';
  $default_output = theme('image', $image_info);
  $this->backdropGet('node/' . $nid);
  $this->assertRaw($default_output, t('Image loaded eagerly displaying correctly on full node view.'));

  // Test the image style 'thumbnail' formatter.
  $instance['display']['default']['settings']['image_load'] = 'auto';
  $instance['display']['default']['settings']['image_style'] = 'thumbnail';
  field_update_instance($instance);
  // Ensure the derivative image is generated so we do not have to deal with
  // image style callback paths.
  $this->backdropGet(image_style_url('thumbnail', $image_uri));
  $image_info['uri'] = image_style_path('thumbnail', $image_uri);
  $image_info['width'] = 100;
  $image_info['height'] = 50;
  unset($image_info['attributes']['loading']);
  $default_output = theme('image', $image_info);
  $this->backdropGet('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->backdropLogout();
    $this->backdropGet(image_style_url('thumbnail', $image_uri));
    $this->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.');
  }
}