/web/core/modules/editor/tests/src/Functional/EditorUploadImageScaleTest.php

https://gitlab.com/andecode/theme-spark · PHP · 234 lines · 139 code · 23 blank · 72 comment · 0 complexity · 7525a53ec7838d130d0a0967e6a057ea MD5 · raw file

  1. <?php
  2. namespace Drupal\Tests\editor\Functional;
  3. use Drupal\editor\Entity\Editor;
  4. use Drupal\filter\Entity\FilterFormat;
  5. use Drupal\Tests\BrowserTestBase;
  6. use Drupal\Tests\TestFileCreationTrait;
  7. /**
  8. * Tests scaling of inline images.
  9. *
  10. * @group editor
  11. */
  12. class EditorUploadImageScaleTest extends BrowserTestBase {
  13. use TestFileCreationTrait;
  14. /**
  15. * Modules to enable.
  16. *
  17. * @var array
  18. */
  19. protected static $modules = ['editor', 'editor_test'];
  20. /**
  21. * {@inheritdoc}
  22. */
  23. protected $defaultTheme = 'stark';
  24. /**
  25. * A user with permission as administer for testing.
  26. *
  27. * @var \Drupal\user\Entity\User
  28. */
  29. protected $adminUser;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function setUp(): void {
  34. parent::setUp();
  35. // Add text format.
  36. FilterFormat::create([
  37. 'format' => 'basic_html',
  38. 'name' => 'Basic HTML',
  39. 'weight' => 0,
  40. ])->save();
  41. // Set up text editor.
  42. Editor::create([
  43. 'format' => 'basic_html',
  44. 'editor' => 'unicorn',
  45. 'image_upload' => [
  46. 'status' => TRUE,
  47. 'scheme' => 'public',
  48. 'directory' => 'inline-images',
  49. 'max_size' => '',
  50. 'max_dimensions' => [
  51. 'width' => NULL,
  52. 'height' => NULL,
  53. ],
  54. ],
  55. ])->save();
  56. // Create admin user.
  57. $this->adminUser = $this->drupalCreateUser([
  58. 'administer filters',
  59. 'use text format basic_html',
  60. ]);
  61. $this->drupalLogin($this->adminUser);
  62. }
  63. /**
  64. * Tests scaling of inline images.
  65. */
  66. public function testEditorUploadImageScale() {
  67. // Generate testing images.
  68. $testing_image_list = $this->getTestFiles('image');
  69. // Case 1: no max dimensions set: uploaded image not scaled.
  70. $test_image = $testing_image_list[0];
  71. [$image_file_width, $image_file_height] = $this->getTestImageInfo($test_image->uri);
  72. $max_width = NULL;
  73. $max_height = NULL;
  74. $this->setMaxDimensions($max_width, $max_height);
  75. $this->assertSavedMaxDimensions($max_width, $max_height);
  76. [$uploaded_image_file_width, $uploaded_image_file_height] = $this->uploadImage($test_image->uri);
  77. $this->assertEquals($image_file_width, $uploaded_image_file_width);
  78. $this->assertEquals($image_file_height, $uploaded_image_file_height);
  79. $this->assertSession()->pageTextNotContains("The image was resized to fit within the maximum allowed dimensions of {$max_width}x{$max_height} pixels.");
  80. // Case 2: max width smaller than uploaded image: image scaled down.
  81. $test_image = $testing_image_list[1];
  82. [$image_file_width, $image_file_height] = $this->getTestImageInfo($test_image->uri);
  83. $max_width = $image_file_width - 5;
  84. $max_height = $image_file_height;
  85. $this->setMaxDimensions($max_width, $max_height);
  86. $this->assertSavedMaxDimensions($max_width, $max_height);
  87. [$uploaded_image_file_width, $uploaded_image_file_height] = $this->uploadImage($test_image->uri);
  88. $this->assertEquals($max_width, $uploaded_image_file_width);
  89. $this->assertEquals($uploaded_image_file_height * ($uploaded_image_file_width / $max_width), $uploaded_image_file_height);
  90. $this->assertSession()->pageTextContains("The image was resized to fit within the maximum allowed dimensions of {$max_width}x{$max_height} pixels.");
  91. // Case 3: max height smaller than uploaded image: image scaled down.
  92. $test_image = $testing_image_list[2];
  93. [$image_file_width, $image_file_height] = $this->getTestImageInfo($test_image->uri);
  94. $max_width = $image_file_width;
  95. $max_height = $image_file_height - 5;
  96. $this->setMaxDimensions($max_width, $max_height);
  97. $this->assertSavedMaxDimensions($max_width, $max_height);
  98. [$uploaded_image_file_width, $uploaded_image_file_height] = $this->uploadImage($test_image->uri);
  99. $this->assertEquals($uploaded_image_file_width * ($uploaded_image_file_height / $max_height), $uploaded_image_file_width);
  100. $this->assertEquals($max_height, $uploaded_image_file_height);
  101. $this->assertSession()->pageTextContains("The image was resized to fit within the maximum allowed dimensions of {$max_width}x{$max_height} pixels.");
  102. // Case 4: max dimensions greater than uploaded image: image not scaled.
  103. $test_image = $testing_image_list[3];
  104. [$image_file_width, $image_file_height] = $this->getTestImageInfo($test_image->uri);
  105. $max_width = $image_file_width + 5;
  106. $max_height = $image_file_height + 5;
  107. $this->setMaxDimensions($max_width, $max_height);
  108. $this->assertSavedMaxDimensions($max_width, $max_height);
  109. [$uploaded_image_file_width, $uploaded_image_file_height] = $this->uploadImage($test_image->uri);
  110. $this->assertEquals($image_file_width, $uploaded_image_file_width);
  111. $this->assertEquals($image_file_height, $uploaded_image_file_height);
  112. $this->assertSession()->pageTextNotContains("The image was resized to fit within the maximum allowed dimensions of {$max_width}x{$max_height} pixels.");
  113. // Case 5: only max width dimension was provided and it was smaller than
  114. // uploaded image: image scaled down.
  115. $test_image = $testing_image_list[4];
  116. [$image_file_width, $image_file_height] = $this->getTestImageInfo($test_image->uri);
  117. $max_width = $image_file_width - 5;
  118. $max_height = NULL;
  119. $this->setMaxDimensions($max_width, $max_height);
  120. $this->assertSavedMaxDimensions($max_width, $max_height);
  121. [$uploaded_image_file_width, $uploaded_image_file_height] = $this->uploadImage($test_image->uri);
  122. $this->assertEquals($max_width, $uploaded_image_file_width);
  123. $this->assertEquals($uploaded_image_file_height * ($uploaded_image_file_width / $max_width), $uploaded_image_file_height);
  124. $this->assertSession()->pageTextContains("The image was resized to fit within the maximum allowed width of {$max_width} pixels.");
  125. // Case 6: only max height dimension was provided and it was smaller than
  126. // uploaded image: image scaled down.
  127. $test_image = $testing_image_list[5];
  128. [$image_file_width, $image_file_height] = $this->getTestImageInfo($test_image->uri);
  129. $max_width = NULL;
  130. $max_height = $image_file_height - 5;
  131. $this->setMaxDimensions($max_width, $max_height);
  132. $this->assertSavedMaxDimensions($max_width, $max_height);
  133. [$uploaded_image_file_width, $uploaded_image_file_height] = $this->uploadImage($test_image->uri);
  134. $this->assertEquals($uploaded_image_file_width * ($uploaded_image_file_height / $max_height), $uploaded_image_file_width);
  135. $this->assertEquals($max_height, $uploaded_image_file_height);
  136. $this->assertSession()->pageTextContains("The image was resized to fit within the maximum allowed height of {$max_height} pixels.");
  137. }
  138. /**
  139. * Gets the dimensions of an uploaded image.
  140. *
  141. * @param string $uri
  142. * The URI of the image.
  143. *
  144. * @return array
  145. * An array containing the uploaded image's width and height.
  146. */
  147. protected function getTestImageInfo($uri) {
  148. $image_file = $this->container->get('image.factory')->get($uri);
  149. return [
  150. (int) $image_file->getWidth(),
  151. (int) $image_file->getHeight(),
  152. ];
  153. }
  154. /**
  155. * Sets the maximum dimensions and saves the configuration.
  156. *
  157. * @param string|int $width
  158. * The width of the image.
  159. * @param string|int $height
  160. * The height of the image.
  161. */
  162. protected function setMaxDimensions($width, $height) {
  163. $editor = Editor::load('basic_html');
  164. $image_upload_settings = $editor->getImageUploadSettings();
  165. $image_upload_settings['max_dimensions']['width'] = $width;
  166. $image_upload_settings['max_dimensions']['height'] = $height;
  167. $editor->setImageUploadSettings($image_upload_settings);
  168. $editor->save();
  169. }
  170. /**
  171. * Uploads an image via the editor dialog.
  172. *
  173. * @param string $uri
  174. * The URI of the image.
  175. *
  176. * @return array
  177. * An array containing the uploaded image's width and height.
  178. */
  179. protected function uploadImage($uri) {
  180. $edit = [
  181. 'files[fid]' => \Drupal::service('file_system')->realpath($uri),
  182. ];
  183. $this->drupalGet('editor/dialog/image/basic_html');
  184. $this->drupalGet('editor/dialog/image/basic_html');
  185. $this->submitForm($edit, 'Upload');
  186. $uploaded_image_file = $this->container->get('image.factory')->get('public://inline-images/' . basename($uri));
  187. return [
  188. (int) $uploaded_image_file->getWidth(),
  189. (int) $uploaded_image_file->getHeight(),
  190. ];
  191. }
  192. /**
  193. * Asserts whether the saved maximum dimensions equal the ones provided.
  194. *
  195. * @param int|null $width
  196. * The expected width of the uploaded image.
  197. * @param int|null $height
  198. * The expected height of the uploaded image.
  199. *
  200. * @internal
  201. */
  202. protected function assertSavedMaxDimensions(?int $width, ?int $height): void {
  203. $image_upload_settings = Editor::load('basic_html')->getImageUploadSettings();
  204. $expected = [
  205. 'width' => $image_upload_settings['max_dimensions']['width'],
  206. 'height' => $image_upload_settings['max_dimensions']['height'],
  207. ];
  208. $this->assertEquals($expected['width'], $width, 'Actual width of "' . $width . '" equals the expected width of "' . $expected['width'] . '"');
  209. $this->assertEquals($expected['height'], $height, 'Actual height of "' . $height . '" equals the expected width of "' . $expected['height'] . '"');
  210. }
  211. }