function ImageFieldDisplayTest::testImageFieldDefaultImage

Same name in other branches
  1. 9 core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php \Drupal\Tests\image\Functional\ImageFieldDisplayTest::testImageFieldDefaultImage()
  2. 10 core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php \Drupal\Tests\image\Functional\ImageFieldDisplayTest::testImageFieldDefaultImage()
  3. 11.x core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php \Drupal\Tests\image\Functional\ImageFieldDisplayTest::testImageFieldDefaultImage()

Test use of a default image with an image field.

File

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

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, '_', '-') . '(.*?)">', '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 = [
        // 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->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::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,
    ];
    $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();
    // 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,
    ];
    $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.
    // 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(t('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->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::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->assertEqual('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,
    ];
    $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.