Make WordPress Core

Ticket #44127: MediaEditBackupSizes-1.php

File MediaEditBackupSizes-1.php, 10.3 KB (added by pbiron, 7 years ago)

This fixes the bug I just mentioned. Do not use MediaEditBackupSizes.php!!! The bug was that the $_REQUEST array wasn't being emptied before setting up each new request; as a result, the testScaleImageImageEditOverwrite() and testMultipleScaleImageImageEditOverwrite() were actually doing the crop from the previous test instead of the scale they were supposed to be doing and I had constructed the $expected value for that "incorrect" operation rather than what it should it should have been. This version corrects those tests as well as fixing _setUpRequest().

Line 
1<?php
2/**
3 * Admin ajax functions to be tested
4 */
5require_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 */
28class 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                // make sure the $_REQUEST array is cleared from any previous tests
88                $_REQUEST = array();
89
90                $defaults = array(
91                        'action' => 'image-editor',
92                        'context' => 'edit-attachment',
93                        'postid' => $this->attachment_id,
94                );
95                $args = wp_parse_args( $args, $defaults );
96
97                foreach ( $args as $arg => $value ) {
98                        $_REQUEST[$arg] = $value;
99                }
100        }
101
102        /**
103         * @ticket 44127
104         */
105        public function testScaleImage() {
106                if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
107                        // skip test
108                        $this->markTestSkipped( 'testScaleImage() skipped because IMAGE_EDIT_OVERWRITE is defined and is true' );
109                }
110
111                $file = '33772.jpg';
112                $this->_insert_attachment( $file );
113
114                // setup the expected backup sizes
115                $orig_metadata = wp_get_attachment_metadata( $this->attachment_id );
116                $expected = array(
117                        'full-orig' => array(
118                                'width' => $orig_metadata['width'],
119                                'height' => $orig_metadata['height'],
120                                'file' => $file,
121                        ),
122                );
123
124                $this->_setUpRequest( array( 'do' => 'scale', 'fwidth' => '1200', 'fheight' => '675' ) );
125                wp_save_image( $this->attachment_id );
126
127                $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true );
128                $this->assertEquals( $expected, $actual );
129        }
130
131        /**
132         * @ticket 44127
133         */
134        public function testMultipleScaleImage() {
135                if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
136                        // skip test
137                        $this->markTestSkipped( 'testMultipleScaleImage() skipped because IMAGE_EDIT_OVERWRITE is defined and is true' );
138                }
139
140                $file = '33772.jpg';
141                $this->_insert_attachment( $file );
142
143                // setup the initial expected backup sizes, we'll append to this
144                // each time thru the loop below
145                $orig_metadata = wp_get_attachment_metadata( $this->attachment_id );
146                $expected = array(
147                        'full-orig' => array(
148                                'width' => $orig_metadata['width'],
149                                'height' => $orig_metadata['height'],
150                                'file' => $file,
151                        ),
152                );
153
154                // save basename for the suffix hash
155                $basename = basename( $file, '.jpg' ) . '-e';
156
157                $scaled_sizes = array(
158                        array( 'width' => '1200', 'height' => '675' ),
159                        array( 'width' => '800', 'height' => '450' ),
160                        array( 'width' => '400', 'height' => '225' ),
161                        array( 'width' => '300', 'height' => '169' ),// same size as 'medium' size
162                );
163
164                // loop thru all the scaled_sizes and scale the image to each
165                foreach ( $scaled_sizes as $idx => $size ) {
166                        $this->_setUpRequest( array( 'do' => 'scale', 'fwidth' => $size['width'], 'fheight' => $size['height'] ) );
167                         wp_save_image( $this->attachment_id );
168
169                        $metadata = wp_get_attachment_metadata( $this->attachment_id );
170
171                        if ( 0 !== $idx ) {
172                                // get the filename hash
173                                $suffix = str_replace( $basename, '', basename( $metadata['file'], '.jpg' ) );
174                                // append to the expected backup size
175                                $expected['full-' . $suffix] = array(
176                                        'width' => $previous_size['width'],
177                                        'height' => $previous_size['height'],
178                                        'file' => $previous_filename,
179                                );
180                        }
181
182                        // save these for the expected value the next time through the loop
183                        $previous_filename = basename( $metadata['file'] );
184                        $previous_size = $size;
185                }
186
187                $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true );
188                $this->assertEquals( $expected, $actual );
189        }
190
191        /**
192         * @ticket 44127
193         */
194        public function testCropImageAll() {
195                $file = '33772.jpg';
196                $this->_insert_attachment( $file );
197
198                // setup the expected backup sizes
199                $orig_metadata = wp_get_attachment_metadata( $this->attachment_id );
200                $expected = array(
201                        'full-orig' => array(
202                                'width' => $orig_metadata['width'],
203                                'height' => $orig_metadata['height'],
204                                'file' => $file,
205                        ),
206                );
207                foreach ( $orig_metadata['sizes'] as $size => $data ) {
208                        $expected[$size . '-orig'] = $data;
209                }
210
211                $this->_setUpRequest( array( 'do' => 'save', 'target' => 'all', 'history' => '[{\"c\":{\"x\":35,\"y\":23,\"w\":345,\"h\":186}}]' ) );
212                wp_save_image( $this->attachment_id );
213
214                $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true );
215                $this->assertEquals( $expected, $actual );
216        }
217
218        /**
219         * @ticket 44127
220         */
221        public function testCropImageThumbnail() {
222                $file = '33772.jpg';
223                $this->_insert_attachment( $file );
224
225                // setup the expected backup sizes
226                $orig_metadata = wp_get_attachment_metadata( $this->attachment_id );
227                $expected = array(
228                        'thumbnail-orig' => $orig_metadata['sizes']['thumbnail'],
229                );
230
231                $this->_setUpRequest( array( 'do' => 'save', 'target' => 'thumbnail', 'history' => '[{\"c\":{\"x\":35,\"y\":23,\"w\":345,\"h\":186}}]' ) );
232                wp_save_image( $this->attachment_id );
233
234                $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true );
235                $this->assertEquals( $expected, $actual );
236        }
237
238        /**
239         * @ticket 44127
240         */
241        public function testCropImageNoThumbnail() {
242                $file = '33772.jpg';
243                $this->_insert_attachment( $file );
244
245                // setup the expected backup sizes
246                $orig_metadata = wp_get_attachment_metadata( $this->attachment_id );
247                $expected = array(
248                        'full-orig' => array(
249                                'width' => $orig_metadata['width'],
250                                'height' => $orig_metadata['height'],
251                                'file' => $file,
252                        ),
253                );
254                foreach ( $orig_metadata['sizes'] as $size => $data ) {
255                        if ( 'thumbnail' !== $size ) {
256                                $expected[$size . '-orig'] = $data;
257                        }
258                }
259
260                $this->_setUpRequest( array( 'do' => 'save', 'target' => 'nothumb', 'history' => '[{\"c\":{\"x\":35,\"y\":23,\"w\":345,\"h\":186}}]' ) );
261                wp_save_image( $this->attachment_id );
262
263                $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true );
264                $this->assertEquals( $expected, $actual );
265        }
266
267
268        /**
269         * @ticket 44127
270         */
271        public function testScaleImageImageEditOverwrite() {
272                if ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) {
273                        // skip test
274                        $this->markTestSkipped( 'testScaleImageImageEditOverwrite() skipped because IMAGE_EDIT_OVERWRITE is not defined or is false' );
275                }
276
277                $file = '33772.jpg';
278                $this->_insert_attachment( $file );
279
280                // setup the expected backup sizes
281                $orig_metadata = wp_get_attachment_metadata( $this->attachment_id );
282                $expected = array(
283                        'full-orig' => array(
284                                'width' => $orig_metadata['width'],
285                                'height' => $orig_metadata['height'],
286                                'file' => $file,
287                        ),
288                );
289
290                $this->_setUpRequest( array( 'do' => 'scale', 'fwidth' => '1200', 'fheight' => '675' ) );
291                wp_save_image( $this->attachment_id );
292
293                $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true );
294                $this->assertEquals( $expected, $actual );
295        }
296
297        /**
298         * @ticket 44127
299         */
300        public function testMultipleScaleImageImageEditOverwrite() {
301                if ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) {
302                        // skip test
303                        $this->markTestSkipped( 'testMultipleScaleImageImageEditOverwrite() skipped because IMAGE_EDIT_OVERWRITE is not defined or is false' );
304                }
305
306                $file = '33772.jpg';
307                $this->_insert_attachment( $file );
308
309                // setup the expected backup sizes
310                $orig_metadata = wp_get_attachment_metadata( $this->attachment_id );
311                $expected = array(
312                        'full-orig' => array(
313                                'width' => $orig_metadata['width'],
314                                'height' => $orig_metadata['height'],
315                                'file' => $file,
316                        ),
317                );
318
319                // save basename for the suffix hash
320                $basename = basename( $file, '.jpg' ) . '-e';
321
322                $scaled_sizes = array(
323                        array( 'width' => '1200', 'height' => '675' ),
324                        array( 'width' => '800', 'height' => '450' ),
325                        array( 'width' => '400', 'height' => '225' ),
326                        array( 'width' => '300', 'height' => '169' ),// same size as 'medium' size
327                );
328
329                // loop thru all the scaled_sizes and scale the image to each
330                foreach ( $scaled_sizes as $idx => $size ) {
331                        $this->_setUpRequest( array( 'do' => 'scale', 'fwidth' => $size['width'], 'fheight' => $size['height'] ) );
332                        wp_save_image( $this->attachment_id );
333                }
334
335                $actual = get_post_meta( $this->attachment_id, '_wp_attachment_backup_sizes', true );
336                $this->assertEquals( $expected, $actual );
337        }
338}