function ResponsiveImageFieldDisplayTest::testResponsiveImageFieldFormattersEmptyMediaQuery

Same name in other branches
  1. 9 core/modules/responsive_image/tests/src/Functional/ResponsiveImageFieldDisplayTest.php \Drupal\Tests\responsive_image\Functional\ResponsiveImageFieldDisplayTest::testResponsiveImageFieldFormattersEmptyMediaQuery()
  2. 8.9.x core/modules/responsive_image/tests/src/Functional/ResponsiveImageFieldDisplayTest.php \Drupal\Tests\responsive_image\Functional\ResponsiveImageFieldDisplayTest::testResponsiveImageFieldFormattersEmptyMediaQuery()
  3. 11.x core/modules/responsive_image/tests/src/Functional/ResponsiveImageFieldDisplayTest.php \Drupal\Tests\responsive_image\Functional\ResponsiveImageFieldDisplayTest::testResponsiveImageFieldFormattersEmptyMediaQuery()

Tests responsive image formatter on node display with an empty media query.

File

core/modules/responsive_image/tests/src/Functional/ResponsiveImageFieldDisplayTest.php, line 375

Class

ResponsiveImageFieldDisplayTest
Tests responsive image display formatter.

Namespace

Drupal\Tests\responsive_image\Functional

Code

public function testResponsiveImageFieldFormattersEmptyMediaQuery() : void {
    $this->responsiveImgStyle
        ->addImageStyleMapping('responsive_image_test_module.empty', '1x', [
        'image_mapping_type' => 'image_style',
        'image_mapping' => ResponsiveImageStyleInterface::EMPTY_IMAGE,
    ])
        ->addImageStyleMapping('responsive_image_test_module.mobile', '1x', [
        'image_mapping_type' => 'image_style',
        'image_mapping' => 'thumbnail',
    ])
        ->save();
    $node_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('node');
    $field_name = $this->randomMachineName();
    $this->createImageField($field_name, 'node', 'article', [
        'uri_scheme' => 'public',
    ]);
    // Create a new node with an image attached.
    $test_image = current($this->getTestFiles('image'));
    $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
    $node_storage->resetCache([
        $nid,
    ]);
    // Use the responsive image formatter linked to file formatter.
    $display_options = [
        'type' => 'responsive_image',
        'settings' => [
            'image_link' => '',
            'responsive_image_style' => 'style_one',
        ],
    ];
    $display = \Drupal::service('entity_display.repository')->getViewDisplay('node', 'article');
    $display->setComponent($field_name, $display_options)
        ->save();
    // View the node.
    $this->drupalGet('node/' . $nid);
    // Assert an empty media attribute is not output.
    $this->assertSession()
        ->responseNotMatches('@srcset="data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== 1x".+?media=".+?/><source@');
    // Assert the media attribute is present if it has a value.
    $thumbnail_style = ImageStyle::load('thumbnail');
    $node = $node_storage->load($nid);
    $image_uri = File::load($node->{$field_name}->target_id)
        ->getFileUri();
    $this->assertSession()
        ->responseMatches('/srcset="' . preg_quote($this->fileUrlGenerator
        ->transformRelative($thumbnail_style->buildUrl($image_uri)), '/') . ' 1x".+?media="\\(min-width: 0px\\)"/');
}

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