3 namespace Drupal\editor\Tests;
5 use Drupal\editor\Entity\Editor;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\simpletest\WebTestBase;
10 * Tests scaling of inline images.
14 class EditorUploadImageScaleTest extends WebTestBase {
21 public static $modules = ['editor', 'editor_test'];
24 * A user with permission as administer for testing.
26 * @var \Drupal\user\Entity\User
33 protected function setUp() {
37 FilterFormat::create([
38 'format' => 'basic_html',
39 'name' => 'Basic HTML',
43 // Set up text editor.
45 'format' => 'basic_html',
46 'editor' => 'unicorn',
50 'directory' => 'inline-images',
60 $this->adminUser = $this->drupalCreateUser(['administer filters', 'use text format basic_html']);
61 $this->drupalLogin($this->adminUser);
65 * Tests scaling of inline images.
67 public function testEditorUploadImageScale() {
68 // Generate testing images.
69 $testing_image_list = $this->drupalGetTestFiles('image');
71 // Case 1: no max dimensions set: uploaded image not scaled.
72 $test_image = $testing_image_list[0];
73 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
76 $this->setMaxDimensions($max_width, $max_height);
77 $this->assertSavedMaxDimensions($max_width, $max_height);
78 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
79 $this->assertEqual($uploaded_image_file_width, $image_file_width);
80 $this->assertEqual($uploaded_image_file_height, $image_file_height);
81 $this->assertNoRaw(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', ['%dimensions' => $max_width . 'x' . $max_height]));
83 // Case 2: max width smaller than uploaded image: image scaled down.
84 $test_image = $testing_image_list[1];
85 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
86 $max_width = $image_file_width - 5;
87 $max_height = $image_file_height;
88 $this->setMaxDimensions($max_width, $max_height);
89 $this->assertSavedMaxDimensions($max_width, $max_height);
90 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
91 $this->assertEqual($uploaded_image_file_width, $max_width);
92 $this->assertEqual($uploaded_image_file_height, $uploaded_image_file_height * ($uploaded_image_file_width / $max_width));
93 $this->assertRaw(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', ['%dimensions' => $max_width . 'x' . $max_height]));
95 // Case 3: max height smaller than uploaded image: image scaled down.
96 $test_image = $testing_image_list[2];
97 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
98 $max_width = $image_file_width;
99 $max_height = $image_file_height - 5;
100 $this->setMaxDimensions($max_width, $max_height);
101 $this->assertSavedMaxDimensions($max_width, $max_height);
102 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
103 $this->assertEqual($uploaded_image_file_width, $uploaded_image_file_width * ($uploaded_image_file_height / $max_height));
104 $this->assertEqual($uploaded_image_file_height, $max_height);
105 $this->assertRaw(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', ['%dimensions' => $max_width . 'x' . $max_height]));
107 // Case 4: max dimensions greater than uploaded image: image not scaled.
108 $test_image = $testing_image_list[3];
109 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
110 $max_width = $image_file_width + 5;
111 $max_height = $image_file_height + 5;
112 $this->setMaxDimensions($max_width, $max_height);
113 $this->assertSavedMaxDimensions($max_width, $max_height);
114 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
115 $this->assertEqual($uploaded_image_file_width, $image_file_width);
116 $this->assertEqual($uploaded_image_file_height, $image_file_height);
117 $this->assertNoRaw(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', ['%dimensions' => $max_width . 'x' . $max_height]));
119 // Case 5: only max width dimension was provided and it was smaller than
120 // uploaded image: image scaled down.
121 $test_image = $testing_image_list[4];
122 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
123 $max_width = $image_file_width - 5;
125 $this->setMaxDimensions($max_width, $max_height);
126 $this->assertSavedMaxDimensions($max_width, $max_height);
127 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
128 $this->assertEqual($uploaded_image_file_width, $max_width);
129 $this->assertEqual($uploaded_image_file_height, $uploaded_image_file_height * ($uploaded_image_file_width / $max_width));
130 $this->assertRaw(t('The image was resized to fit within the maximum allowed width of %width pixels.', ['%width' => $max_width]));
132 // Case 6: only max height dimension was provided and it was smaller than
133 // uploaded image: image scaled down.
134 $test_image = $testing_image_list[5];
135 list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri);
137 $max_height = $image_file_height - 5;
138 $this->setMaxDimensions($max_width, $max_height);
139 $this->assertSavedMaxDimensions($max_width, $max_height);
140 list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri);
141 $this->assertEqual($uploaded_image_file_width, $uploaded_image_file_width * ($uploaded_image_file_height / $max_height));
142 $this->assertEqual($uploaded_image_file_height, $max_height);
143 $this->assertRaw(t('The image was resized to fit within the maximum allowed height of %height pixels.', ['%height' => $max_height]));
147 * Gets the dimensions of an uploaded image.
150 * The URI of the image.
153 * An array containing the uploaded image's width and height.
155 protected function getTestImageInfo($uri) {
156 $image_file = $this->container->get('image.factory')->get($uri);
158 (int) $image_file->getWidth(),
159 (int) $image_file->getHeight(),
164 * Sets the maximum dimensions and saves the configuration.
166 * @param string|int $width
167 * The width of the image.
168 * @param string|int $height
169 * The height of the image.
171 protected function setMaxDimensions($width, $height) {
172 $editor = Editor::load('basic_html');
173 $image_upload_settings = $editor->getImageUploadSettings();
174 $image_upload_settings['max_dimensions']['width'] = $width;
175 $image_upload_settings['max_dimensions']['height'] = $height;
176 $editor->setImageUploadSettings($image_upload_settings);
181 * Uploads an image via the editor dialog.
184 * The URI of the image.
187 * An array containing the uploaded image's width and height.
189 protected function uploadImage($uri) {
191 'files[fid]' => \Drupal::service('file_system')->realpath($uri),
193 $this->drupalGet('editor/dialog/image/basic_html');
194 $this->drupalPostForm('editor/dialog/image/basic_html', $edit, t('Upload'));
195 $uploaded_image_file = $this->container->get('image.factory')->get('public://inline-images/' . basename($uri));
197 (int) $uploaded_image_file->getWidth(),
198 (int) $uploaded_image_file->getHeight(),
203 * Asserts whether the saved maximum dimensions equal the ones provided.
205 * @param string $width
206 * The expected width of the uploaded image.
207 * @param string $height
208 * The expected height of the uploaded image.
212 protected function assertSavedMaxDimensions($width, $height) {
213 $image_upload_settings = Editor::load('basic_html')->getImageUploadSettings();
215 'width' => $image_upload_settings['max_dimensions']['width'],
216 'height' => $image_upload_settings['max_dimensions']['height'],
218 $same_width = $this->assertEqual($width, $expected['width'], 'Actual width of "' . $width . '" equals the expected width of "' . $expected['width'] . '"');
219 $same_height = $this->assertEqual($height, $expected['height'], 'Actual height of "' . $height . '" equals the expected width of "' . $expected['height'] . '"');
220 return $same_width && $same_height;