/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php

https://gitlab.com/guillaumev/alkarama · PHP · 223 lines · 133 code · 21 blank · 69 comment · 1 complexity · 6b368731b8ced6fc29a5d1c3ae4afce5 MD5 · raw file

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