size.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * @group image
  4. * @group media
  5. * @group upload
  6. */
  7. class Tests_Image_Size extends WP_UnitTestCase {
  8. function test_constrain_dims_zero() {
  9. // No constraint - should have no effect.
  10. $out = wp_constrain_dimensions( 640, 480, 0, 0 );
  11. $this->assertSame( array( 640, 480 ), $out );
  12. $out = wp_constrain_dimensions( 640, 480 );
  13. $this->assertSame( array( 640, 480 ), $out );
  14. $out = wp_constrain_dimensions( 0, 0, 0, 0 );
  15. $this->assertSame( array( 0, 0 ), $out );
  16. $out = wp_constrain_dimensions( 465, 700, 177, 177 );
  17. $this->assertSame( array( 118, 177 ), $out );
  18. }
  19. function test_constrain_dims_smaller() {
  20. // Image size is smaller than the constraint - no effect.
  21. $out = wp_constrain_dimensions( 500, 600, 1024, 768 );
  22. $this->assertSame( array( 500, 600 ), $out );
  23. $out = wp_constrain_dimensions( 500, 600, 0, 768 );
  24. $this->assertSame( array( 500, 600 ), $out );
  25. $out = wp_constrain_dimensions( 500, 600, 1024, 0 );
  26. $this->assertSame( array( 500, 600 ), $out );
  27. }
  28. function test_constrain_dims_equal() {
  29. // Image size is equal to the constraint - no effect.
  30. $out = wp_constrain_dimensions( 1024, 768, 1024, 768 );
  31. $this->assertSame( array( 1024, 768 ), $out );
  32. $out = wp_constrain_dimensions( 1024, 768, 0, 768 );
  33. $this->assertSame( array( 1024, 768 ), $out );
  34. $out = wp_constrain_dimensions( 1024, 768, 1024, 0 );
  35. $this->assertSame( array( 1024, 768 ), $out );
  36. }
  37. function test_constrain_dims_larger() {
  38. // Image size is larger than the constraint - result should be constrained.
  39. $out = wp_constrain_dimensions( 1024, 768, 500, 600 );
  40. $this->assertSame( array( 500, 375 ), $out );
  41. $out = wp_constrain_dimensions( 1024, 768, 0, 600 );
  42. $this->assertSame( array( 800, 600 ), $out );
  43. $out = wp_constrain_dimensions( 1024, 768, 500, 0 );
  44. $this->assertSame( array( 500, 375 ), $out );
  45. // Also try a portrait oriented image.
  46. $out = wp_constrain_dimensions( 300, 800, 500, 600 );
  47. $this->assertSame( array( 225, 600 ), $out );
  48. $out = wp_constrain_dimensions( 300, 800, 0, 600 );
  49. $this->assertSame( array( 225, 600 ), $out );
  50. $out = wp_constrain_dimensions( 300, 800, 200, 0 );
  51. $this->assertSame( array( 200, 533 ), $out );
  52. }
  53. function test_constrain_dims_boundary() {
  54. // One dimension is larger than the constraint, one smaller - result should be constrained.
  55. $out = wp_constrain_dimensions( 1024, 768, 500, 800 );
  56. $this->assertSame( array( 500, 375 ), $out );
  57. $out = wp_constrain_dimensions( 1024, 768, 2000, 700 );
  58. $this->assertSame( array( 933, 700 ), $out );
  59. // Portrait.
  60. $out = wp_constrain_dimensions( 768, 1024, 800, 500 );
  61. $this->assertSame( array( 375, 500 ), $out );
  62. $out = wp_constrain_dimensions( 768, 1024, 2000, 700 );
  63. $this->assertSame( array( 525, 700 ), $out );
  64. }
  65. /**
  66. * @expectedDeprecated wp_shrink_dimensions
  67. */
  68. function test_shrink_dimensions_default() {
  69. $out = wp_shrink_dimensions( 640, 480 );
  70. $this->assertSame( array( 128, 96 ), $out );
  71. $out = wp_shrink_dimensions( 480, 640 );
  72. $this->assertSame( array( 72, 96 ), $out );
  73. }
  74. /**
  75. * @expectedDeprecated wp_shrink_dimensions
  76. */
  77. function test_shrink_dimensions_smaller() {
  78. // Image size is smaller than the constraint - no effect.
  79. $out = wp_shrink_dimensions( 500, 600, 1024, 768 );
  80. $this->assertSame( array( 500, 600 ), $out );
  81. $out = wp_shrink_dimensions( 600, 500, 1024, 768 );
  82. $this->assertSame( array( 600, 500 ), $out );
  83. }
  84. /**
  85. * @expectedDeprecated wp_shrink_dimensions
  86. */
  87. function test_shrink_dimensions_equal() {
  88. // Image size is equal to the constraint - no effect.
  89. $out = wp_shrink_dimensions( 500, 600, 500, 600 );
  90. $this->assertSame( array( 500, 600 ), $out );
  91. $out = wp_shrink_dimensions( 600, 500, 600, 500 );
  92. $this->assertSame( array( 600, 500 ), $out );
  93. }
  94. /**
  95. * @expectedDeprecated wp_shrink_dimensions
  96. */
  97. function test_shrink_dimensions_larger() {
  98. // Image size is larger than the constraint - result should be constrained.
  99. $out = wp_shrink_dimensions( 1024, 768, 500, 600 );
  100. $this->assertSame( array( 500, 375 ), $out );
  101. $out = wp_shrink_dimensions( 300, 800, 500, 600 );
  102. $this->assertSame( array( 225, 600 ), $out );
  103. }
  104. /**
  105. * @expectedDeprecated wp_shrink_dimensions
  106. */
  107. function test_shrink_dimensions_boundary() {
  108. // One dimension is larger than the constraint, one smaller - result should be constrained.
  109. $out = wp_shrink_dimensions( 1024, 768, 500, 800 );
  110. $this->assertSame( array( 500, 375 ), $out );
  111. $out = wp_shrink_dimensions( 1024, 768, 2000, 700 );
  112. $this->assertSame( array( 933, 700 ), $out );
  113. // Portrait.
  114. $out = wp_shrink_dimensions( 768, 1024, 800, 500 );
  115. $this->assertSame( array( 375, 500 ), $out );
  116. $out = wp_shrink_dimensions( 768, 1024, 2000, 700 );
  117. $this->assertSame( array( 525, 700 ), $out );
  118. }
  119. function test_constrain_size_for_editor_thumb() {
  120. $out = image_constrain_size_for_editor( 600, 400, 'thumb' );
  121. $this->assertSame( array( 150, 100 ), $out );
  122. $out = image_constrain_size_for_editor( 64, 64, 'thumb' );
  123. $this->assertSame( array( 64, 64 ), $out );
  124. }
  125. function test_constrain_size_for_editor_medium() {
  126. // Default max width is 500, no constraint on height.
  127. global $content_width;
  128. $_content_width = $content_width;
  129. $content_width = 0;
  130. update_option( 'medium_size_w', 500 );
  131. update_option( 'medium_size_h', 0 );
  132. $out = image_constrain_size_for_editor( 600, 400, 'medium' );
  133. $this->assertSame( array( 500, 333 ), $out );
  134. $out = image_constrain_size_for_editor( 400, 600, 'medium' );
  135. $this->assertSame( array( 400, 600 ), $out );
  136. $out = image_constrain_size_for_editor( 64, 64, 'medium' );
  137. $this->assertSame( array( 64, 64 ), $out );
  138. // $content_width should be ignored.
  139. $content_width = 350;
  140. $out = image_constrain_size_for_editor( 600, 400, 'medium' );
  141. $this->assertSame( array( 500, 333 ), $out );
  142. $content_width = $_content_width;
  143. }
  144. function test_constrain_size_for_editor_full() {
  145. global $content_width;
  146. $_content_width = $content_width;
  147. $content_width = 400;
  148. $out = image_constrain_size_for_editor( 600, 400, 'full' );
  149. $this->assertSame( array( 600, 400 ), $out );
  150. $out = image_constrain_size_for_editor( 64, 64, 'full' );
  151. $this->assertSame( array( 64, 64 ), $out );
  152. // $content_width default is 500.
  153. $content_width = 0;
  154. $out = image_constrain_size_for_editor( 600, 400, 'full' );
  155. $this->assertSame( array( 600, 400 ), $out );
  156. $out = image_constrain_size_for_editor( 64, 64, 'full' );
  157. $this->assertSame( array( 64, 64 ), $out );
  158. $content_width = $_content_width;
  159. }
  160. }