You are here

public function EditorUploadImageScaleTest::testEditorUploadImageScale in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/editor/tests/src/Functional/EditorUploadImageScaleTest.php \Drupal\Tests\editor\Functional\EditorUploadImageScaleTest::testEditorUploadImageScale()

Tests scaling of inline images.

File

core/modules/editor/tests/src/Functional/EditorUploadImageScaleTest.php, line 78

Class

EditorUploadImageScaleTest
Tests scaling of inline images.

Namespace

Drupal\Tests\editor\Functional

Code

public function testEditorUploadImageScale() {

  // Generate testing images.
  $testing_image_list = $this
    ->getTestFiles('image');

  // Case 1: no max dimensions set: uploaded image not scaled.
  $test_image = $testing_image_list[0];
  list($image_file_width, $image_file_height) = $this
    ->getTestImageInfo($test_image->uri);
  $max_width = NULL;
  $max_height = NULL;
  $this
    ->setMaxDimensions($max_width, $max_height);
  $this
    ->assertSavedMaxDimensions($max_width, $max_height);
  list($uploaded_image_file_width, $uploaded_image_file_height) = $this
    ->uploadImage($test_image->uri);
  $this
    ->assertEquals($image_file_width, $uploaded_image_file_width);
  $this
    ->assertEquals($image_file_height, $uploaded_image_file_height);
  $this
    ->assertSession()
    ->pageTextNotContains("The image was resized to fit within the maximum allowed dimensions of {$max_width}x{$max_height} pixels.");

  // Case 2: max width smaller than uploaded image: image scaled down.
  $test_image = $testing_image_list[1];
  list($image_file_width, $image_file_height) = $this
    ->getTestImageInfo($test_image->uri);
  $max_width = $image_file_width - 5;
  $max_height = $image_file_height;
  $this
    ->setMaxDimensions($max_width, $max_height);
  $this
    ->assertSavedMaxDimensions($max_width, $max_height);
  list($uploaded_image_file_width, $uploaded_image_file_height) = $this
    ->uploadImage($test_image->uri);
  $this
    ->assertEquals($max_width, $uploaded_image_file_width);
  $this
    ->assertEquals($uploaded_image_file_height * ($uploaded_image_file_width / $max_width), $uploaded_image_file_height);
  $this
    ->assertSession()
    ->pageTextContains("The image was resized to fit within the maximum allowed dimensions of {$max_width}x{$max_height} pixels.");

  // Case 3: max height smaller than uploaded image: image scaled down.
  $test_image = $testing_image_list[2];
  list($image_file_width, $image_file_height) = $this
    ->getTestImageInfo($test_image->uri);
  $max_width = $image_file_width;
  $max_height = $image_file_height - 5;
  $this
    ->setMaxDimensions($max_width, $max_height);
  $this
    ->assertSavedMaxDimensions($max_width, $max_height);
  list($uploaded_image_file_width, $uploaded_image_file_height) = $this
    ->uploadImage($test_image->uri);
  $this
    ->assertEquals($uploaded_image_file_width * ($uploaded_image_file_height / $max_height), $uploaded_image_file_width);
  $this
    ->assertEquals($max_height, $uploaded_image_file_height);
  $this
    ->assertSession()
    ->pageTextContains("The image was resized to fit within the maximum allowed dimensions of {$max_width}x{$max_height} pixels.");

  // Case 4: max dimensions greater than uploaded image: image not scaled.
  $test_image = $testing_image_list[3];
  list($image_file_width, $image_file_height) = $this
    ->getTestImageInfo($test_image->uri);
  $max_width = $image_file_width + 5;
  $max_height = $image_file_height + 5;
  $this
    ->setMaxDimensions($max_width, $max_height);
  $this
    ->assertSavedMaxDimensions($max_width, $max_height);
  list($uploaded_image_file_width, $uploaded_image_file_height) = $this
    ->uploadImage($test_image->uri);
  $this
    ->assertEquals($image_file_width, $uploaded_image_file_width);
  $this
    ->assertEquals($image_file_height, $uploaded_image_file_height);
  $this
    ->assertSession()
    ->pageTextNotContains("The image was resized to fit within the maximum allowed dimensions of {$max_width}x{$max_height} pixels.");

  // Case 5: only max width dimension was provided and it was smaller than
  // uploaded image: image scaled down.
  $test_image = $testing_image_list[4];
  list($image_file_width, $image_file_height) = $this
    ->getTestImageInfo($test_image->uri);
  $max_width = $image_file_width - 5;
  $max_height = NULL;
  $this
    ->setMaxDimensions($max_width, $max_height);
  $this
    ->assertSavedMaxDimensions($max_width, $max_height);
  list($uploaded_image_file_width, $uploaded_image_file_height) = $this
    ->uploadImage($test_image->uri);
  $this
    ->assertEquals($max_width, $uploaded_image_file_width);
  $this
    ->assertEquals($uploaded_image_file_height * ($uploaded_image_file_width / $max_width), $uploaded_image_file_height);
  $this
    ->assertSession()
    ->pageTextContains("The image was resized to fit within the maximum allowed width of {$max_width} pixels.");

  // Case 6: only max height dimension was provided and it was smaller than
  // uploaded image: image scaled down.
  $test_image = $testing_image_list[5];
  list($image_file_width, $image_file_height) = $this
    ->getTestImageInfo($test_image->uri);
  $max_width = NULL;
  $max_height = $image_file_height - 5;
  $this
    ->setMaxDimensions($max_width, $max_height);
  $this
    ->assertSavedMaxDimensions($max_width, $max_height);
  list($uploaded_image_file_width, $uploaded_image_file_height) = $this
    ->uploadImage($test_image->uri);
  $this
    ->assertEquals($uploaded_image_file_width * ($uploaded_image_file_height / $max_height), $uploaded_image_file_width);
  $this
    ->assertEquals($max_height, $uploaded_image_file_height);
  $this
    ->assertSession()
    ->pageTextContains("The image was resized to fit within the maximum allowed height of {$max_height} pixels.");
}