100fb4fdc54e01c3edb1d0d79188a16edba0f244
[yaffs-website] / web / core / modules / editor / src / Tests / EditorUploadImageScaleTest.php
1 <?php
2
3 namespace Drupal\editor\Tests;
4
5 use Drupal\editor\Entity\Editor;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\simpletest\WebTestBase;
8
9 /**
10  * Tests scaling of inline images.
11  *
12  * @group editor
13  */
14 class EditorUploadImageScaleTest extends WebTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['editor', 'editor_test'];
22
23   /**
24    * A user with permission as administer for testing.
25    *
26    * @var \Drupal\user\Entity\User
27    */
28   protected $adminUser;
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35
36     // Add text format.
37     FilterFormat::create([
38       'format' => 'basic_html',
39       'name' => 'Basic HTML',
40       'weight' => 0,
41     ])->save();
42
43     // Set up text editor.
44     Editor::create([
45       'format' => 'basic_html',
46       'editor' => 'unicorn',
47       'image_upload' => [
48         'status' => TRUE,
49         'scheme' => 'public',
50         'directory' => 'inline-images',
51         'max_size' => '',
52         'max_dimensions' => [
53           'width' => NULL,
54           'height' => NULL
55         ],
56       ]
57     ])->save();
58
59     // Create admin user.
60     $this->adminUser = $this->drupalCreateUser(['administer filters', 'use text format basic_html']);
61     $this->drupalLogin($this->adminUser);
62   }
63
64   /**
65    * Tests scaling of inline images.
66    */
67   public function testEditorUploadImageScale() {
68     // Generate testing images.
69     $testing_image_list = $this->drupalGetTestFiles('image');
70
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);
74     $max_width = NULL;
75     $max_height = NULL;
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]));
82
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]));
94
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]));
106
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]));
118
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;
124     $max_height = NULL;
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]));
131
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);
136     $max_width = NULL;
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]));
144   }
145
146   /**
147    * Gets the dimensions of an uploaded image.
148    *
149    * @param string $uri
150    *   The URI of the image.
151    *
152    * @return array
153    *   An array containing the uploaded image's width and height.
154    */
155   protected function getTestImageInfo($uri) {
156     $image_file = $this->container->get('image.factory')->get($uri);
157     return [
158       (int) $image_file->getWidth(),
159       (int) $image_file->getHeight(),
160     ];
161   }
162
163   /**
164    * Sets the maximum dimensions and saves the configuration.
165    *
166    * @param string|int $width
167    *   The width of the image.
168    * @param string|int $height
169    *   The height of the image.
170    */
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);
177     $editor->save();
178   }
179
180   /**
181    * Uploads an image via the editor dialog.
182    *
183    * @param string $uri
184    *   The URI of the image.
185    *
186    * @return array
187    *   An array containing the uploaded image's width and height.
188    */
189   protected function uploadImage($uri) {
190     $edit = [
191       'files[fid]' => \Drupal::service('file_system')->realpath($uri),
192     ];
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));
196     return [
197       (int) $uploaded_image_file->getWidth(),
198       (int) $uploaded_image_file->getHeight(),
199     ];
200   }
201
202   /**
203    * Asserts whether the saved maximum dimensions equal the ones provided.
204    *
205    * @param string $width
206    *   The expected width of the uploaded image.
207    * @param string $height
208    *   The expected height of the uploaded image.
209    *
210    * @return bool
211    */
212   protected function assertSavedMaxDimensions($width, $height) {
213     $image_upload_settings = Editor::load('basic_html')->getImageUploadSettings();
214     $expected = [
215       'width' => $image_upload_settings['max_dimensions']['width'],
216       'height' => $image_upload_settings['max_dimensions']['height'],
217     ];
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;
221   }
222
223 }