1 | <?php |
---|
2 | /** |
---|
3 | * Admin ajax functions to be tested |
---|
4 | */ |
---|
5 | require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' ); |
---|
6 | |
---|
7 | /** |
---|
8 | * Testing `_wp_attachment_backup_sizes` meta for ajax media editing |
---|
9 | * |
---|
10 | * Does NOT test that the edits made are correct |
---|
11 | * ONLY tests the creation/modification of the `_wp_attachment_backup_sizes` meta |
---|
12 | * |
---|
13 | * @package WordPress |
---|
14 | * @subpackage UnitTests |
---|
15 | * @since x.y.z |
---|
16 | * @group ajax |
---|
17 | * |
---|
18 | * @todo I've tagged all these tests with {@ticket 44127} because I decided they |
---|
19 | * were needed while I was working on that ticket. However, I'm now thinking |
---|
20 | * I should open a new ticket specifically stating the need for these tests, |
---|
21 | * independent of that ticket. Opinions? |
---|
22 | * @todo Are tests needed for flip & rotate as well as crop? I don't think so |
---|
23 | * because I don't think the actual edit operation affects what is |
---|
24 | * stored in _wp_attachment_backup_sizes, but need confirmation of that. |
---|
25 | * @todo As far as I can tell, the tests that depend on IMAGE_EDIT_OVERWRITE |
---|
26 | * must be at the end of the file. Is there any way to avoid this? |
---|
27 | */ |
---|
28 | class Tests_Ajax_MediaEditBackupSizes extends WP_Ajax_UnitTestCase { |
---|
29 | /** |
---|
30 | * The attachment ID |
---|
31 | * |
---|
32 | * @var int |
---|
33 | */ |
---|
34 | public $attachment_id; |
---|
35 | |
---|
36 | /** |
---|
37 | * Uploaded file bits. |
---|
38 | * |
---|
39 | * Keys are filenames and values are the result of calling `wp_upload_bits()` for those files. |
---|
40 | * |
---|
41 | * @var array |
---|
42 | */ |
---|
43 | public $uploaded_bits = array(); |
---|
44 | |
---|
45 | /** |
---|
46 | * Setup the test fixture. |
---|
47 | */ |
---|
48 | public function setUp() { |
---|
49 | parent::setUp(); |
---|
50 | |
---|
51 | require_once( ABSPATH . 'wp-admin/includes/image-edit.php' ); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Tear down the test fixture. |
---|
56 | */ |
---|
57 | public function tearDown() { |
---|
58 | // Cleanup |
---|
59 | $this->remove_added_uploads(); |
---|
60 | parent::tearDown(); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Upload an image. |
---|
65 | * |
---|
66 | * @param string $file |
---|
67 | * @return int |
---|
68 | */ |
---|
69 | public function _insert_attachment( $file ) { |
---|
70 | if ( ! isset( $this->uploaded_bits[ $file ] ) ) { |
---|
71 | // only upload a given file once, regardless of how many tests it's used in |
---|
72 | $filename = DIR_TESTDATA . '/images/' . $file; |
---|
73 | $contents = file_get_contents( $filename ); |
---|
74 | |
---|
75 | $this->uploaded_bits[ $file ] = wp_upload_bits( basename( $filename ), null, $contents ); |
---|
76 | } |
---|
77 | |
---|
78 | $this->attachment_id = $this->_make_attachment( $this->uploaded_bits[ $file ] ); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Setup the `$_REQUEST` array for wp_save_image() |
---|
83 | * |
---|
84 | * @param array $args |
---|
85 | */ |
---|
86 | public function _setUpRequest( $args ) { |
---|
87 | $defaults = array( |
---|
88 | 'action' => 'image-editor', |
---|
89 | 'context' => 'edit-attachment', |
---|
90 | 'postid' => $this->attachment_id, |
---|
91 | ); |
---|
92 | |
---|
93 | $args = wp_parse_args( $args, $defaults ); |
---|
94 | foreach ( $args as $arg => $value ) { |
---|
95 | $_REQUEST[$arg] = $value; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * @ticket 44127 |
---|
101 | */ |
---|
102 | public function testScaleImage() { |
---|
103 | if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) { |
---|
104 | // skip test |
---|
105 | $this->markTestSkipped( 'testScaleImage() skipped because IMAGE_EDIT_OVERWRITE is defined and is true' ); |
---|
106 | } |
---|
107 | |
---|
108 | $file = '33772.jpg'; |
---|
109 | $this->_insert_attachment( $file ); |
---|
110 | |
---|
111 | // setup the expected backup sizes |
---|
112 | $orig_metadata = wp_get_attachment_metadata( $this->attachment_id ); |
---|
113 | $expected = array( |
---|
114 | 'full-orig' => array( |
---|
115 | 'width' => $orig_metadata['width'], |
---|
116 | 'height' => $orig_metadata['height'], |
---|
117 | 'file' => $file, |
---|
118 | ), |
---|
119 | ); |
---|
120 | |
---|
121 | $this->_setUpRequest( array( 'do' => 'scale', 'fwidth' => '1200', 'fheight' => '675' ) ); |
---|
122 | $ret = wp_save_image( $this->attachment_id ); |
---|
123 | |
---|
124 | $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true ); |
---|
125 | $this->assertEquals( $expected, $actual ); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * @ticket 44127 |
---|
130 | */ |
---|
131 | public function testMultipleScaleImage() { |
---|
132 | if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) { |
---|
133 | // skip test |
---|
134 | $this->markTestSkipped( 'testMultipleScaleImage() skipped because IMAGE_EDIT_OVERWRITE is defined and is true' ); |
---|
135 | } |
---|
136 | |
---|
137 | $file = '33772.jpg'; |
---|
138 | $this->_insert_attachment( $file ); |
---|
139 | |
---|
140 | // setup the initial expected backup sizes, we'll append to this |
---|
141 | // each time thru the loop below |
---|
142 | $orig_metadata = wp_get_attachment_metadata( $this->attachment_id ); |
---|
143 | $expected = array( |
---|
144 | 'full-orig' => array( |
---|
145 | 'width' => $orig_metadata['width'], |
---|
146 | 'height' => $orig_metadata['height'], |
---|
147 | 'file' => $file, |
---|
148 | ), |
---|
149 | ); |
---|
150 | |
---|
151 | // save basename for the suffix hash |
---|
152 | $basename = basename( $file, '.jpg' ) . '-e'; |
---|
153 | |
---|
154 | $scaled_sizes = array( |
---|
155 | array( 'width' => '1200', 'height' => '675' ), |
---|
156 | array( 'width' => '800', 'height' => '450' ), |
---|
157 | array( 'width' => '400', 'height' => '225' ), |
---|
158 | array( 'width' => '300', 'height' => '169' ),// same size as 'medium' size |
---|
159 | ); |
---|
160 | |
---|
161 | // loop thru all the scaled_sizes and scale the image to each |
---|
162 | foreach ( $scaled_sizes as $idx => $size ) { |
---|
163 | $this->_setUpRequest( array( 'do' => 'scale', 'fwidth' => $size['width'], 'fheight' => $size['height'] ) ); |
---|
164 | $ret = wp_save_image( $this->attachment_id ); |
---|
165 | |
---|
166 | $metadata = wp_get_attachment_metadata( $this->attachment_id ); |
---|
167 | |
---|
168 | if ( 0 !== $idx ) { |
---|
169 | // get the filename hash |
---|
170 | $suffix = str_replace( $basename, '', basename( $metadata['file'], '.jpg' ) ); |
---|
171 | // append to the expected backup size |
---|
172 | $expected['full-' . $suffix] = array( |
---|
173 | 'width' => $previous_size['width'], |
---|
174 | 'height' => $previous_size['height'], |
---|
175 | 'file' => $previous_filename, |
---|
176 | ); |
---|
177 | } |
---|
178 | |
---|
179 | // save these for the expected value the next time through the loop |
---|
180 | $previous_filename = basename( $metadata['file'] ); |
---|
181 | $previous_size = $size; |
---|
182 | } |
---|
183 | |
---|
184 | $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true ); |
---|
185 | $this->assertEquals( $expected, $actual ); |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * @ticket 44127 |
---|
190 | */ |
---|
191 | public function testCropImageAll() { |
---|
192 | $file = '33772.jpg'; |
---|
193 | $this->_insert_attachment( $file ); |
---|
194 | |
---|
195 | // setup the expected backup sizes |
---|
196 | $orig_metadata = wp_get_attachment_metadata( $this->attachment_id ); |
---|
197 | $expected = array( |
---|
198 | 'full-orig' => array( |
---|
199 | 'width' => $orig_metadata['width'], |
---|
200 | 'height' => $orig_metadata['height'], |
---|
201 | 'file' => $file, |
---|
202 | ), |
---|
203 | ); |
---|
204 | foreach ( $orig_metadata['sizes'] as $size => $data ) { |
---|
205 | $expected[$size . '-orig'] = $data; |
---|
206 | } |
---|
207 | |
---|
208 | $this->_setUpRequest( array( 'do' => 'save', 'target' => 'all', 'history' => '[{\"c\":{\"x\":35,\"y\":23,\"w\":345,\"h\":186}}]' ) ); |
---|
209 | $ret = wp_save_image( $this->attachment_id ); |
---|
210 | |
---|
211 | $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true ); |
---|
212 | $this->assertEquals( $expected, $actual ); |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * @ticket 44127 |
---|
217 | */ |
---|
218 | public function testCropImageThumbnail() { |
---|
219 | $file = '33772.jpg'; |
---|
220 | $this->_insert_attachment( $file ); |
---|
221 | |
---|
222 | // setup the expected backup sizes |
---|
223 | $orig_metadata = wp_get_attachment_metadata( $this->attachment_id ); |
---|
224 | $expected = array( |
---|
225 | 'thumbnail-orig' => $orig_metadata['sizes']['thumbnail'], |
---|
226 | ); |
---|
227 | |
---|
228 | $this->_setUpRequest( array( 'do' => 'save', 'target' => 'thumbnail', 'history' => '[{\"c\":{\"x\":35,\"y\":23,\"w\":345,\"h\":186}}]' ) ); |
---|
229 | $ret = wp_save_image( $this->attachment_id ); |
---|
230 | |
---|
231 | $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true ); |
---|
232 | $this->assertEquals( $expected, $actual ); |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | * @ticket 44127 |
---|
237 | */ |
---|
238 | public function testCropImageNoThumbnail() { |
---|
239 | $file = '33772.jpg'; |
---|
240 | $this->_insert_attachment( $file ); |
---|
241 | |
---|
242 | // setup the expected backup sizes |
---|
243 | $orig_metadata = wp_get_attachment_metadata( $this->attachment_id ); |
---|
244 | $expected = array( |
---|
245 | 'full-orig' => array( |
---|
246 | 'width' => $orig_metadata['width'], |
---|
247 | 'height' => $orig_metadata['height'], |
---|
248 | 'file' => $file, |
---|
249 | ), |
---|
250 | ); |
---|
251 | foreach ( $orig_metadata['sizes'] as $size => $data ) { |
---|
252 | if ( 'thumbnail' !== $size ) { |
---|
253 | $expected[$size . '-orig'] = $data; |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | $this->_setUpRequest( array( 'do' => 'save', 'target' => 'nothumb', 'history' => '[{\"c\":{\"x\":35,\"y\":23,\"w\":345,\"h\":186}}]' ) ); |
---|
258 | $ret = wp_save_image( $this->attachment_id ); |
---|
259 | |
---|
260 | $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true ); |
---|
261 | $this->assertEquals( $expected, $actual ); |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | /** |
---|
266 | * @ticket 44127 |
---|
267 | */ |
---|
268 | public function testScaleImageImageEditOverwrite() { |
---|
269 | if ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) { |
---|
270 | // skip test |
---|
271 | $this->markTestSkipped( 'testScaleImageImageEditOverwrite() skipped because IMAGE_EDIT_OVERWRITE is not defined or is false' ); |
---|
272 | } |
---|
273 | |
---|
274 | $file = '33772.jpg'; |
---|
275 | $this->_insert_attachment( $file ); |
---|
276 | |
---|
277 | // setup the expected backup sizes |
---|
278 | $orig_metadata = wp_get_attachment_metadata( $this->attachment_id ); |
---|
279 | $expected = array( |
---|
280 | 'full-orig' => array( |
---|
281 | 'width' => $orig_metadata['width'], |
---|
282 | 'height' => $orig_metadata['height'], |
---|
283 | 'file' => $file, |
---|
284 | ), |
---|
285 | ); |
---|
286 | foreach ( $orig_metadata['sizes'] as $size => $data ) { |
---|
287 | // @todo why isn't thumbnail-orig added to the backup sizes when IMAGE_EDIT_OVERWRITE is true? |
---|
288 | // is that a bug in wp_save_image()? |
---|
289 | // Note: this is NOT the same as skipping 'thumbnail-orig in testCropImageNoThumbnail(), |
---|
290 | // which is expected |
---|
291 | if ( 'thumbnail' !== $size ) { |
---|
292 | $expected[$size . '-orig'] = $data; |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | $this->_setUpRequest( array( 'do' => 'scale', 'fwidth' => '1200', 'fheight' => '675' ) ); |
---|
297 | $ret = wp_save_image( $this->attachment_id ); |
---|
298 | |
---|
299 | $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true ); |
---|
300 | $this->assertEquals( $expected, $actual ); |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * @ticket 44127 |
---|
305 | */ |
---|
306 | public function testMultipleScaleImageImageEditOverwrite() { |
---|
307 | if ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) { |
---|
308 | // skip test |
---|
309 | $this->markTestSkipped( 'testMultipleScaleImageImageEditOverwrite() skipped because IMAGE_EDIT_OVERWRITE is not defined or is false' ); |
---|
310 | } |
---|
311 | |
---|
312 | $file = '33772.jpg'; |
---|
313 | $this->_insert_attachment( $file ); |
---|
314 | |
---|
315 | // setup the expected backup sizes |
---|
316 | $orig_metadata = wp_get_attachment_metadata( $this->attachment_id ); |
---|
317 | $expected = array( |
---|
318 | 'full-orig' => array( |
---|
319 | 'width' => $orig_metadata['width'], |
---|
320 | 'height' => $orig_metadata['height'], |
---|
321 | 'file' => $file, |
---|
322 | ), |
---|
323 | ); |
---|
324 | foreach ( $orig_metadata['sizes'] as $size => $data ) { |
---|
325 | // @todo why isn't thumbnail-orig added to the backup sizes when IMAGE_EDIT_OVERWRITE is true? |
---|
326 | // is that a bug in wp_save_image()? |
---|
327 | // Note: this is NOT the same as skipping 'thumbnail-orig in testCropImageNoThumbnail(), |
---|
328 | // which is expected |
---|
329 | if ( 'thumbnail' !== $size ) { |
---|
330 | $expected[$size . '-orig'] = $data; |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | // save basename for the suffix hash |
---|
335 | $basename = basename( $file, '.jpg' ) . '-e'; |
---|
336 | |
---|
337 | $scaled_sizes = array( |
---|
338 | array( 'width' => '1200', 'height' => '675' ), |
---|
339 | array( 'width' => '800', 'height' => '450' ), |
---|
340 | array( 'width' => '400', 'height' => '225' ), |
---|
341 | array( 'width' => '300', 'height' => '169' ),// same size as 'medium' size |
---|
342 | ); |
---|
343 | |
---|
344 | // loop thru all the scaled_sizes and scale the image to each |
---|
345 | foreach ( $scaled_sizes as $idx => $size ) { |
---|
346 | $this->_setUpRequest( array( 'do' => 'scale', 'fwidth' => $size['width'], 'fheight' => $size['height'] ) ); |
---|
347 | $ret = wp_save_image( $this->attachment_id ); |
---|
348 | |
---|
349 | $metadata = wp_get_attachment_metadata( $this->attachment_id ); |
---|
350 | } |
---|
351 | |
---|
352 | $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true ); |
---|
353 | $this->assertEquals( $expected, $actual ); |
---|
354 | } |
---|
355 | } |
---|