You are here

public function EditorUploadImageScaleTest::testEditorUploadImageScale in Drupal 8

Same name and namespace in other branches
  1. 9 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 79

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
    ->assertEqual($uploaded_image_file_width, $image_file_width);
  $this
    ->assertEqual($uploaded_image_file_height, $image_file_height);
  $this
    ->assertNoRaw((string) new FormattableMarkup('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', [
    '%dimensions' => $max_width . 'x' . $max_height,
  ]));

  // 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
    ->assertEqual($uploaded_image_file_width, $max_width);
  $this
    ->assertEqual($uploaded_image_file_height, $uploaded_image_file_height * ($uploaded_image_file_width / $max_width));
  $this
    ->assertRaw((string) new FormattableMarkup('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', [
    '%dimensions' => $max_width . 'x' . $max_height,
  ]));

  // 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
    ->assertEqual($uploaded_image_file_width, $uploaded_image_file_width * ($uploaded_image_file_height / $max_height));
  $this
    ->assertEqual($uploaded_image_file_height, $max_height);
  $this
    ->assertRaw((string) new FormattableMarkup('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', [
    '%dimensions' => $max_width . 'x' . $max_height,
  ]));

  // 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
    ->assertEqual($uploaded_image_file_width, $image_file_width);
  $this
    ->assertEqual($uploaded_image_file_height, $image_file_height);
  $this
    ->assertNoRaw((string) new FormattableMarkup('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', [
    '%dimensions' => $max_width . 'x' . $max_height,
  ]));

  // 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
    ->assertEqual($uploaded_image_file_width, $max_width);
  $this
    ->assertEqual($uploaded_image_file_height, $uploaded_image_file_height * ($uploaded_image_file_width / $max_width));
  $this
    ->assertRaw((string) new FormattableMarkup('The image was resized to fit within the maximum allowed width of %width pixels.', [
    '%width' => $max_width,
  ]));

  // 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
    ->assertEqual($uploaded_image_file_width, $uploaded_image_file_width * ($uploaded_image_file_height / $max_height));
  $this
    ->assertEqual($uploaded_image_file_height, $max_height);
  $this
    ->assertRaw((string) new FormattableMarkup('The image was resized to fit within the maximum allowed height of %height pixels.', [
    '%height' => $max_height,
  ]));
}