You are here

public function ImageFieldDisplayTest::testImageFieldDefaultImage in Drupal 10

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

Tests use of a default image with an image field.

File

core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php, line 472

Class

ImageFieldDisplayTest
Tests the display of image fields.

Namespace

Drupal\Tests\image\Functional

Code

public function testImageFieldDefaultImage() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $node_storage = $this->container
    ->get('entity_type.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([
    '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
    ->assertSession()
    ->responseNotMatches('<div class="(.*?)field--name-' . strtr($field_name, '_', '-') . '(.*?)">');

  // Verify that no image style cache tags are found.
  $this
    ->assertSession()
    ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'image_style:');

  // Add a default image to the public image field.
  $images = $this
    ->drupalGetTestFiles('image');
  $alt = $this
    ->randomString(512);
  $title = $this
    ->randomString(1024);
  $edit = [
    // Get the path of the 'image-test.png' file.
    'files[settings_default_image_uuid]' => \Drupal::service('file_system')
      ->realpath($images[0]->uri),
    'settings[default_image][alt]' => $alt,
    'settings[default_image][title]' => $title,
  ];
  $this
    ->drupalGet("admin/structure/types/manage/article/fields/node.article.{$field_name}/storage");
  $this
    ->submitForm($edit, 'Save field settings');

  // Clear field definition cache so the new default image is detected.
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  $field_storage = FieldStorageConfig::loadByName('node', $field_name);
  $default_image = $field_storage
    ->getSetting('default_image');
  $file = \Drupal::service('entity.repository')
    ->loadEntityByUuid('file', $default_image['uuid']);
  $this
    ->assertTrue($file
    ->isPermanent(), 'The default image status is permanent.');
  $image = [
    '#theme' => 'image',
    '#uri' => $file
      ->getFileUri(),
    '#alt' => $alt,
    '#title' => $title,
    '#width' => 40,
    '#height' => 20,
    '#attributes' => [
      'loading' => 'lazy',
    ],
  ];
  $default_output = str_replace("\n", '', $renderer
    ->renderRoot($image));
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', $file
    ->getCacheTags()[0]);

  // Verify that no image style cache tags are found.
  $this
    ->assertSession()
    ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'image_style:');
  $this
    ->assertSession()
    ->responseContains($default_output);

  // 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();

  // Upload the 'image-test.gif' file.
  $nid = $this
    ->uploadNodeImage($images[2], $field_name, 'article', $alt);
  $node_storage
    ->resetCache([
    $nid,
  ]);
  $node = $node_storage
    ->load($nid);
  $file = $node->{$field_name}->entity;
  $image = [
    '#theme' => 'image',
    '#uri' => $file
      ->getFileUri(),
    '#width' => 40,
    '#height' => 20,
    '#alt' => $alt,
    '#attributes' => [
      'loading' => 'lazy',
    ],
  ];
  $image_output = str_replace("\n", '', $renderer
    ->renderRoot($image));
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', $file
    ->getCacheTags()[0]);

  // Verify that no image style cache tags are found.
  $this
    ->assertSession()
    ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'image_style:');

  // Default image should not be displayed.
  $this
    ->assertSession()
    ->responseNotContains($default_output);

  // User supplied image should be displayed.
  $this
    ->assertSession()
    ->responseContains($image_output);

  // Remove default image from the field and make sure it is no longer used.
  // Can't use fillField cause Mink can't fill hidden fields.
  $this
    ->drupalGet("admin/structure/types/manage/article/fields/node.article.{$field_name}/storage");
  $this
    ->getSession()
    ->getPage()
    ->find('css', 'input[name="settings[default_image][uuid][fids]"]')
    ->setValue(0);
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Save field settings');

  // Clear field definition cache so the new default image is detected.
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  $field_storage = FieldStorageConfig::loadByName('node', $field_name);
  $default_image = $field_storage
    ->getSetting('default_image');
  $this
    ->assertEmpty($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', [
    'uri_scheme' => 'private',
  ]);

  // Add a default image to the new field.
  $edit = [
    // Get the path of the 'image-test.gif' file.
    'files[settings_default_image_uuid]' => \Drupal::service('file_system')
      ->realpath($images[2]->uri),
    'settings[default_image][alt]' => $alt,
    'settings[default_image][title]' => $title,
  ];
  $this
    ->drupalGet('admin/structure/types/manage/article/fields/node.article.' . $private_field_name . '/storage');
  $this
    ->submitForm($edit, 'Save field settings');

  // Clear field definition cache so the new default image is detected.
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  $private_field_storage = FieldStorageConfig::loadByName('node', $private_field_name);
  $default_image = $private_field_storage
    ->getSetting('default_image');
  $file = \Drupal::service('entity.repository')
    ->loadEntityByUuid('file', $default_image['uuid']);
  $this
    ->assertEquals('private', StreamWrapperManager::getScheme($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([
    'type' => 'article',
  ]);
  $image = [
    '#theme' => 'image',
    '#uri' => $file
      ->getFileUri(),
    '#alt' => $alt,
    '#title' => $title,
    '#width' => 40,
    '#height' => 20,
    '#attributes' => [
      'loading' => 'lazy',
    ],
  ];
  $default_output = str_replace("\n", '', $renderer
    ->renderRoot($image));
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', $file
    ->getCacheTags()[0]);

  // Verify that no image style cache tags are found.
  $this
    ->assertSession()
    ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'image_style:');

  // Default private image should be displayed when no user supplied image
  // is present.
  $this
    ->assertSession()
    ->responseContains($default_output);
}