1 image.test ImageFieldDisplayTestCase::testImageFieldDefaultImage()

Test use of a default image with an image field.

File

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

Class

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

Code

function testImageFieldDefaultImage() {
  // Create a new image field.
  $field_name = strtolower($this->randomName());
  $this->createImageField($field_name, 'post');

  // Create a new node, with no images and verify that no images are
  // displayed.
  $node = $this->backdropCreateNode(array('type' => 'post'));
  $this->backdropGet('node/' . $node->nid);
  // Verify that no image is displayed on the page by checking for the class
  // that would be used on the image field.
  $this->assertNoPattern('<div class="(.*?)field-name-' . strtr($field_name, '_', '-') . '(.*?)">', 'No image displayed when no image is attached and no default image specified.');

  // Add a default image to the public imagefield instance.
  $images = $this->backdropGetTestFiles('image');
  $edit = array(
    'files[field_settings_default_image_file]' => backdrop_realpath($images[0]->uri),
  );
  $this->backdropPost('admin/structure/types/manage/post/fields/' . $field_name, $edit, t('Save settings'));
  // Clear field info cache so the new default image is detected.
  field_info_cache_clear();
  $field = field_info_field($field_name);
  $image_uri = $field['settings']['default_image'];
  $default_output = theme('image', array('uri' => $image_uri));
  $this->backdropGet('node/' . $node->nid);
  $this->assertRaw($default_output, 'Default image displayed when no user supplied image is present.');

  // Create a node with an image attached and ensure that the default image
  // is not displayed.
  $nid = $this->uploadNodeImage($images[1], $field_name, 'post');
  $node = node_load($nid, NULL, TRUE);
  $image_info = array(
    'uri' => $node->{$field_name}[LANGUAGE_NONE][0]['uri'],
    'width' => 40,
    'height' => 20,
  );
  $image_output = theme('image', $image_info);
  $this->backdropGet('node/' . $nid);
  $this->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.');
  $this->assertRaw($image_output, 'User supplied image is displayed.');

  // Remove default image from the field and make sure it is no longer used.
  $edit = array(
    'field[settings][default_image][remove]' => 1,
  );
  $this->backdropPost('admin/structure/types/manage/post/fields/' . $field_name, $edit, t('Save settings'));
  // Clear field info cache so the new default image is detected.
  field_info_cache_clear();
  $field = field_info_field($field_name);
  $this->assertFalse($field['settings']['default_image'], 'Default image removed from field.');
  // Create an image field that uses the private:// scheme and test that the
  // default image works as expected.
  $private_field_name = strtolower($this->randomName());
  $this->createImageField($private_field_name, 'post', array('uri_scheme' => 'private'));
  // Add a default image to the new field.
  $edit = array(
    'files[field_settings_default_image_file]' => backdrop_realpath($images[1]->uri),
  );
  $this->backdropPost('admin/structure/types/manage/post/fields/' . $private_field_name, $edit, t('Save settings'));
  $private_field = field_info_field($private_field_name);
  $uri = $private_field['settings']['default_image'];
  $this->assertEqual('private', file_uri_scheme($uri), 'Default image uses private:// scheme.');
  // Create a new node with no image attached and ensure that default private
  // image is displayed.
  $node = $this->backdropCreateNode(array('type' => 'post'));
  $default_output = theme('image', array('uri' => $uri));
  $this->backdropGet('node/' . $node->nid);
  $this->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.');
}