3 namespace Drupal\Tests\editor\Functional;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\editor\Entity\Editor;
7 use Drupal\filter\Entity\FilterFormat;
8 use Drupal\Tests\BrowserTestBase;
9 use Drupal\Tests\TestFileCreationTrait;
12 * Tests scaling of inline images.
16 class EditorUploadImageScaleTest extends BrowserTestBase {
18 use TestFileCreationTrait;
25 public static $modules = ['editor', 'editor_test'];
28 * A user with permission as administer for testing.
30 * @var \Drupal\user\Entity\User
37 protected function setUp() {
41 FilterFormat::create([
42 'format' => 'basic_html',
43 'name' => 'Basic HTML',
47 // Set up text editor.
49 'format' => 'basic_html',
50 'editor' => 'unicorn',
54 'directory' => 'inline-images',
64 $this->adminUser = $this->drupalCreateUser(['administer filters', 'use text format basic_html']);
65 $this->drupalLogin($this->adminUser);
69 * Tests scaling of inline images.
71 public function testEditorUploadImageScale() {
72 // Generate testing images.
73 $testing_image_list = $this->getTestFiles('image');
75 // Case 1: no max dimensions set: uploaded image not scaled.
76 $test_image = $testing_image_list[0];
77 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
80 $this->setMaxDimensions($max_width, $max_height);
81 $this->assertSavedMaxDimensions($max_width, $max_height);
82 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
83 $this->assertEqual($uploaded_image_file_width, $image_file_width);
84 $this->assertEqual($uploaded_image_file_height, $image_file_height);
85 $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]));
87 // Case 2: max width smaller than uploaded image: image scaled down.
88 $test_image = $testing_image_list[1];
89 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
90 $max_width = $image_file_width - 5;
91 $max_height = $image_file_height;
92 $this->setMaxDimensions($max_width, $max_height);
93 $this->assertSavedMaxDimensions($max_width, $max_height);
94 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
95 $this->assertEqual($uploaded_image_file_width, $max_width);
96 $this->assertEqual($uploaded_image_file_height, $uploaded_image_file_height * ($uploaded_image_file_width / $max_width));
97 $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]));
99 // Case 3: max height smaller than uploaded image: image scaled down.
100 $test_image = $testing_image_list[2];
101 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
102 $max_width = $image_file_width;
103 $max_height = $image_file_height - 5;
104 $this->setMaxDimensions($max_width, $max_height);
105 $this->assertSavedMaxDimensions($max_width, $max_height);
106 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
107 $this->assertEqual($uploaded_image_file_width, $uploaded_image_file_width * ($uploaded_image_file_height / $max_height));
108 $this->assertEqual($uploaded_image_file_height, $max_height);
109 $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]));
111 // Case 4: max dimensions greater than uploaded image: image not scaled.
112 $test_image = $testing_image_list[3];
113 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
114 $max_width = $image_file_width + 5;
115 $max_height = $image_file_height + 5;
116 $this->setMaxDimensions($max_width, $max_height);
117 $this->assertSavedMaxDimensions($max_width, $max_height);
118 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
119 $this->assertEqual($uploaded_image_file_width, $image_file_width);
120 $this->assertEqual($uploaded_image_file_height, $image_file_height);
121 $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]));
123 // Case 5: only max width dimension was provided and it was smaller than
124 // uploaded image: image scaled down.
125 $test_image = $testing_image_list[4];
126 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
127 $max_width = $image_file_width - 5;
129 $this->setMaxDimensions($max_width, $max_height);
130 $this->assertSavedMaxDimensions($max_width, $max_height);
131 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
132 $this->assertEqual($uploaded_image_file_width, $max_width);
133 $this->assertEqual($uploaded_image_file_height, $uploaded_image_file_height * ($uploaded_image_file_width / $max_width));
134 $this->assertRaw((string) new FormattableMarkup('The image was resized to fit within the maximum allowed width of %width pixels.', ['%width' => $max_width]));
136 // Case 6: only max height dimension was provided and it was smaller than
137 // uploaded image: image scaled down.
138 $test_image = $testing_image_list[5];
139 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
141 $max_height = $image_file_height - 5;
142 $this->setMaxDimensions($max_width, $max_height);
143 $this->assertSavedMaxDimensions($max_width, $max_height);
144 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
145 $this->assertEqual($uploaded_image_file_width, $uploaded_image_file_width * ($uploaded_image_file_height / $max_height));
146 $this->assertEqual($uploaded_image_file_height, $max_height);
147 $this->assertRaw((string) new FormattableMarkup('The image was resized to fit within the maximum allowed height of %height pixels.', ['%height' => $max_height]));
151 * Gets the dimensions of an uploaded image.
154 * The URI of the image.
157 * An array containing the uploaded image's width and height.
159 protected function getTestImageInfo($uri) {
160 $image_file = $this->container->get('image.factory')->get($uri);
162 (int) $image_file->getWidth(),
163 (int) $image_file->getHeight(),
168 * Sets the maximum dimensions and saves the configuration.
170 * @param string|int $width
171 * The width of the image.
172 * @param string|int $height
173 * The height of the image.
175 protected function setMaxDimensions($width, $height) {
176 $editor = Editor::load('basic_html');
177 $image_upload_settings = $editor->getImageUploadSettings();
178 $image_upload_settings['max_dimensions']['width'] = $width;
179 $image_upload_settings['max_dimensions']['height'] = $height;
180 $editor->setImageUploadSettings($image_upload_settings);
185 * Uploads an image via the editor dialog.
188 * The URI of the image.
191 * An array containing the uploaded image's width and height.
193 protected function uploadImage($uri) {
195 'files[fid]' => \Drupal::service('file_system')->realpath($uri),
197 $this->drupalGet('editor/dialog/image/basic_html');
198 $this->drupalPostForm('editor/dialog/image/basic_html', $edit, t('Upload'));
199 $uploaded_image_file = $this->container->get('image.factory')->get('public://inline-images/' . basename($uri));
201 (int) $uploaded_image_file->getWidth(),
202 (int) $uploaded_image_file->getHeight(),
207 * Asserts whether the saved maximum dimensions equal the ones provided.
209 * @param string $width
210 * The expected width of the uploaded image.
211 * @param string $height
212 * The expected height of the uploaded image.
216 protected function assertSavedMaxDimensions($width, $height) {
217 $image_upload_settings = Editor::load('basic_html')->getImageUploadSettings();
219 'width' => $image_upload_settings['max_dimensions']['width'],
220 'height' => $image_upload_settings['max_dimensions']['height'],
222 $same_width = $this->assertEqual($width, $expected['width'], 'Actual width of "' . $width . '" equals the expected width of "' . $expected['width'] . '"');
223 $same_height = $this->assertEqual($height, $expected['height'], 'Actual height of "' . $height . '" equals the expected width of "' . $expected['height'] . '"');
224 return $same_width && $same_height;