function ImageAdminStylesTest::testPreviewImageShowInPrivateScheme

Same name in other branches
  1. 11.x core/modules/image/tests/src/Functional/ImageAdminStylesTest.php \Drupal\Tests\image\Functional\ImageAdminStylesTest::testPreviewImageShowInPrivateScheme()

Tests the display of preview images using a private scheme.

File

core/modules/image/tests/src/Functional/ImageAdminStylesTest.php, line 526

Class

ImageAdminStylesTest
Tests creation, deletion, and editing of image styles and effects.

Namespace

Drupal\Tests\image\Functional

Code

public function testPreviewImageShowInPrivateScheme() : void {
    $this->config('system.file')
        ->set('default_scheme', 'private')
        ->save();
    
    /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    $file_url_generator = \Drupal::service('file_url_generator');
    // Get the original preview image file in core config.
    $original_path = $this->config('image.settings')
        ->get('preview_image');
    $style = ImageStyle::create([
        'name' => 'test_foo',
        'label' => 'test foo',
    ]);
    $style->save();
    // Build the derivative preview image file with the Image Style.
    // @see template_preprocess_image_style_preview()
    $preview_file = $style->buildUri($original_path);
    $style->createDerivative($original_path, $preview_file);
    // Check if the derivative image exists.
    $this->assertFileExists($preview_file);
    // Generate itok token for the preview image.
    $itok = $style->getPathToken('private://' . $original_path);
    $url = $file_url_generator->generateAbsoluteString($preview_file);
    $url .= '?itok=' . $itok;
    // Check if the preview image with style is shown.
    $this->drupalGet($url);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->responseHeaderContains('Content-Type', 'image/png');
}

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