Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / editor / tests / src / Functional / EditorUploadImageScaleTest.php
1 <?php
2
3 namespace Drupal\Tests\editor\Functional;
4
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;
10
11 /**
12  * Tests scaling of inline images.
13  *
14  * @group editor
15  */
16 class EditorUploadImageScaleTest extends BrowserTestBase {
17
18   use TestFileCreationTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['editor', 'editor_test'];
26
27   /**
28    * A user with permission as administer for testing.
29    *
30    * @var \Drupal\user\Entity\User
31    */
32   protected $adminUser;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39
40     // Add text format.
41     FilterFormat::create([
42       'format' => 'basic_html',
43       'name' => 'Basic HTML',
44       'weight' => 0,
45     ])->save();
46
47     // Set up text editor.
48     Editor::create([
49       'format' => 'basic_html',
50       'editor' => 'unicorn',
51       'image_upload' => [
52         'status' => TRUE,
53         'scheme' => 'public',
54         'directory' => 'inline-images',
55         'max_size' => '',
56         'max_dimensions' => [
57           'width' => NULL,
58           'height' => NULL,
59         ],
60       ],
61     ])->save();
62
63     // Create admin user.
64     $this->adminUser = $this->drupalCreateUser(['administer filters', 'use text format basic_html']);
65     $this->drupalLogin($this->adminUser);
66   }
67
68   /**
69    * Tests scaling of inline images.
70    */
71   public function testEditorUploadImageScale() {
72     // Generate testing images.
73     $testing_image_list = $this->getTestFiles('image');
74
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);
78     $max_width = NULL;
79     $max_height = NULL;
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]));
86
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]));
98
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]));
110
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]));
122
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;
128     $max_height = NULL;
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]));
135
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);
140     $max_width = NULL;
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]));
148   }
149
150   /**
151    * Gets the dimensions of an uploaded image.
152    *
153    * @param string $uri
154    *   The URI of the image.
155    *
156    * @return array
157    *   An array containing the uploaded image's width and height.
158    */
159   protected function getTestImageInfo($uri) {
160     $image_file = $this->container->get('image.factory')->get($uri);
161     return [
162       (int) $image_file->getWidth(),
163       (int) $image_file->getHeight(),
164     ];
165   }
166
167   /**
168    * Sets the maximum dimensions and saves the configuration.
169    *
170    * @param string|int $width
171    *   The width of the image.
172    * @param string|int $height
173    *   The height of the image.
174    */
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);
181     $editor->save();
182   }
183
184   /**
185    * Uploads an image via the editor dialog.
186    *
187    * @param string $uri
188    *   The URI of the image.
189    *
190    * @return array
191    *   An array containing the uploaded image's width and height.
192    */
193   protected function uploadImage($uri) {
194     $edit = [
195       'files[fid]' => \Drupal::service('file_system')->realpath($uri),
196     ];
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));
200     return [
201       (int) $uploaded_image_file->getWidth(),
202       (int) $uploaded_image_file->getHeight(),
203     ];
204   }
205
206   /**
207    * Asserts whether the saved maximum dimensions equal the ones provided.
208    *
209    * @param string $width
210    *   The expected width of the uploaded image.
211    * @param string $height
212    *   The expected height of the uploaded image.
213    *
214    * @return bool
215    */
216   protected function assertSavedMaxDimensions($width, $height) {
217     $image_upload_settings = Editor::load('basic_html')->getImageUploadSettings();
218     $expected = [
219       'width' => $image_upload_settings['max_dimensions']['width'],
220       'height' => $image_upload_settings['max_dimensions']['height'],
221     ];
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;
225   }
226
227 }