You are here

function ImageFieldDisplayTest::testImageFieldDefaultImage in Zircon Profile 8.0

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

Test use of a default image with an image field.

File

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

Class

ImageFieldDisplayTest
Tests the display of image fields.

Namespace

Drupal\image\Tests

Code

function testImageFieldDefaultImage() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $node_storage = $this->container
    ->get('entity.manager')
    ->getStorage('node');

  // Create a new image field.
  $field_name = strtolower($this
    ->randomMachineName());
  $this
    ->createImageField($field_name, 'article');

  // Create a new node, with no images and verify that no images are
  // displayed.
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $this
    ->drupalGet('node/' . $node
    ->id());

  // 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.');
  $cache_tags_header = $this
    ->drupalGetHeader('X-Drupal-Cache-Tags');
  $this
    ->assertTrue(!preg_match('/ image_style\\:/', $cache_tags_header), 'No image style cache tag found.');

  // Add a default image to the public image field.
  $images = $this
    ->drupalGetTestFiles('image');
  $alt = $this
    ->randomString(512);
  $title = $this
    ->randomString(1024);
  $edit = array(
    'files[settings_default_image_uuid]' => drupal_realpath($images[0]->uri),
    'settings[default_image][alt]' => $alt,
    'settings[default_image][title]' => $title,
  );
  $this
    ->drupalPostForm("admin/structure/types/manage/article/fields/node.article.{$field_name}/storage", $edit, t('Save field settings'));

  // Clear field definition cache so the new default image is detected.
  \Drupal::entityManager()
    ->clearCachedFieldDefinitions();
  $field_storage = FieldStorageConfig::loadByName('node', $field_name);
  $default_image = $field_storage
    ->getSetting('default_image');
  $file = \Drupal::entityManager()
    ->loadEntityByUuid('file', $default_image['uuid']);
  $this
    ->assertTrue($file
    ->isPermanent(), 'The default image status is permanent.');
  $image = array(
    '#theme' => 'image',
    '#uri' => $file
      ->getFileUri(),
    '#alt' => $alt,
    '#title' => $title,
    '#width' => 40,
    '#height' => 20,
  );
  $default_output = str_replace("\n", NULL, $renderer
    ->renderRoot($image));
  $this
    ->drupalGet('node/' . $node
    ->id());
  $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, '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.
  // Create alt text for the image.
  $alt = $this
    ->randomMachineName();
  $nid = $this
    ->uploadNodeImage($images[1], $field_name, 'article', $alt);
  $node_storage
    ->resetCache(array(
    $nid,
  ));
  $node = $node_storage
    ->load($nid);
  $file = $node->{$field_name}->entity;
  $image = array(
    '#theme' => 'image',
    '#uri' => $file
      ->getFileUri(),
    '#width' => 40,
    '#height' => 20,
    '#alt' => $alt,
  );
  $image_output = str_replace("\n", NULL, $renderer
    ->renderRoot($image));
  $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
    ->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(
    'settings[default_image][uuid][fids]' => 0,
  );
  $this
    ->drupalPostForm("admin/structure/types/manage/article/fields/node.article.{$field_name}/storage", $edit, t('Save field settings'));

  // Clear field definition cache so the new default image is detected.
  \Drupal::entityManager()
    ->clearCachedFieldDefinitions();
  $field_storage = FieldStorageConfig::loadByName('node', $field_name);
  $default_image = $field_storage
    ->getSetting('default_image');
  $this
    ->assertFalse($default_image['uuid'], '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
    ->randomMachineName());
  $this
    ->createImageField($private_field_name, 'article', array(
    'uri_scheme' => 'private',
  ));

  // Add a default image to the new field.
  $edit = array(
    'files[settings_default_image_uuid]' => drupal_realpath($images[1]->uri),
    'settings[default_image][alt]' => $alt,
    'settings[default_image][title]' => $title,
  );
  $this
    ->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $private_field_name . '/storage', $edit, t('Save field settings'));

  // Clear field definition cache so the new default image is detected.
  \Drupal::entityManager()
    ->clearCachedFieldDefinitions();
  $private_field_storage = FieldStorageConfig::loadByName('node', $private_field_name);
  $default_image = $private_field_storage
    ->getSetting('default_image');
  $file = \Drupal::entityManager()
    ->loadEntityByUuid('file', $default_image['uuid']);
  $this
    ->assertEqual('private', file_uri_scheme($file
    ->getFileUri()), 'Default image uses private:// scheme.');
  $this
    ->assertTrue($file
    ->isPermanent(), 'The default image status is permanent.');

  // Create a new node with no image attached and ensure that default private
  // image is displayed.
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $image = array(
    '#theme' => 'image',
    '#uri' => $file
      ->getFileUri(),
    '#alt' => $alt,
    '#title' => $title,
    '#width' => 40,
    '#height' => 20,
  );
  $default_output = str_replace("\n", NULL, $renderer
    ->renderRoot($image));
  $this
    ->drupalGet('node/' . $node
    ->id());
  $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, 'Default private image displayed when no user supplied image is present.');
}