function ImageFieldDisplayTest::testImageLoadingAttribute

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

Tests for image loading attribute settings.

File

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

Class

ImageFieldDisplayTest
Tests the display of image fields.

Namespace

Drupal\Tests\image\Functional

Code

public function testImageLoadingAttribute() : void {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    $node_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('node');
    $field_name = $this->randomMachineName();
    $field_settings = [
        'alt_field_required' => 0,
    ];
    $instance = $this->createImageField($field_name, 'node', 'article', [], $field_settings);
    // Go to manage display page.
    $this->drupalGet("admin/structure/types/manage/article/display");
    // Test for existence of link to image styles configuration.
    $this->submitForm([], "{$field_name}_settings_edit");
    $this->assertSession()
        ->linkByHrefExists(Url::fromRoute('entity.image_style.collection')->toString(), 0, 'Link to image styles configuration is found');
    // Remove 'administer image styles' permission from testing admin user.
    $admin_user_roles = $this->adminUser
        ->getRoles(TRUE);
    user_role_change_permissions(reset($admin_user_roles), [
        'administer image styles' => FALSE,
    ]);
    // Go to manage display page again.
    $this->drupalGet("admin/structure/types/manage/article/display");
    // Test for absence of link to image styles configuration.
    $this->submitForm([], "{$field_name}_settings_edit");
    $this->assertSession()
        ->linkByHrefNotExists(Url::fromRoute('entity.image_style.collection')->toString(), 'Link to image styles configuration is absent when permissions are insufficient');
    // Restore 'administer image styles' permission to testing admin user
    user_role_change_permissions(reset($admin_user_roles), [
        'administer image styles' => TRUE,
    ]);
    // Create a new node with an image attached.
    $test_image = current($this->drupalGetTestFiles('image'));
    // Ensure that preview works.
    $this->previewNodeImage($test_image, $field_name, 'article');
    // After previewing, make the alt field required. It cannot be required
    // during preview because the form validation will fail.
    $instance->setSetting('alt_field_required', 1);
    $instance->save();
    // Create alt text for the image.
    $alt = $this->randomMachineName();
    // Save node.
    $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt);
    $node_storage->resetCache([
        $nid,
    ]);
    $node = $node_storage->load($nid);
    // Test that the default image loading attribute is being used.
    
    /** @var \Drupal\file\FileInterface $file */
    $file = $node->{$field_name}->entity;
    $image_uri = $file->getFileUri();
    $image = [
        '#theme' => 'image',
        '#uri' => $image_uri,
        '#width' => 40,
        '#height' => 20,
        '#alt' => $alt,
        '#attributes' => [
            'loading' => 'lazy',
        ],
    ];
    $default_output = str_replace("\n", '', (string) $renderer->renderRoot($image));
    $this->assertSession()
        ->responseContains($default_output);
    // Test overrides of image loading attribute.
    $display_options = [
        'type' => 'image',
        'settings' => [
            'image_link' => '',
            'image_style' => '',
            'image_loading' => [
                'attribute' => 'eager',
            ],
        ],
    ];
    $display = \Drupal::service('entity_display.repository')->getViewDisplay('node', $node->getType());
    $display->setComponent($field_name, $display_options)
        ->save();
    $image = [
        '#theme' => 'image',
        '#uri' => $image_uri,
        '#width' => 40,
        '#height' => 20,
        '#alt' => $alt,
        '#attributes' => [
            'loading' => 'eager',
        ],
    ];
    $default_output = (string) $renderer->renderRoot($image);
    $this->drupalGet('node/' . $nid);
    $this->assertSession()
        ->responseContains($default_output);
    // Test the image loading "priority" formatter works together with "image_style".
    $display_options['settings']['image_style'] = 'thumbnail';
    $display->setComponent($field_name, $display_options)
        ->save();
    // Ensure the derivative image is generated so we do not have to deal with
    // image style callback paths.
    $this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri));
    $image_style = [
        '#theme' => 'image_style',
        '#uri' => $image_uri,
        '#width' => 40,
        '#height' => 20,
        '#style_name' => 'thumbnail',
        '#alt' => $alt,
        '#attributes' => [
            'loading' => 'eager',
        ],
    ];
    $default_output = (string) $renderer->renderRoot($image_style);
    $this->drupalGet('node/' . $nid);
    $this->assertSession()
        ->responseContains($default_output);
}

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