Make WordPress Core

Ticket #26381: 26381.3.diff

File 26381.3.diff, 3.9 KB (added by Mista-Flo, 2 years ago)

Adding some unit test

  • src/js/_enqueues/lib/image-edit.js

    diff --git a/src/js/_enqueues/lib/image-edit.js b/src/js/_enqueues/lib/image-edit.js
    index 0d4f7b015a..c48848c341 100644
    a b  
    226226         */
    227227        scaleChanged : function( postid, x, el ) {
    228228                var w = $('#imgedit-scale-width-' + postid), h = $('#imgedit-scale-height-' + postid),
    229                 warn = $('#imgedit-scale-warn-' + postid), w1 = '', h1 = '';
     229                warn = $('#imgedit-scale-warn-' + postid), w1 = '', h1 = '',
     230                scaleBtn = $('#imgedit-scale-button');
    230231
    231232                if ( false === this.validateNumeric( el ) ) {
    232233                        return;
     
    242243
    243244                if ( ( h1 && h1 > this.hold.oh ) || ( w1 && w1 > this.hold.ow ) ) {
    244245                        warn.css('visibility', 'visible');
     246                        scaleBtn.prop('disabled', true);
    245247                } else {
    246248                        warn.css('visibility', 'hidden');
     249                        scaleBtn.prop('disabled', false);
    247250                }
    248251        },
    249252
  • src/wp-admin/includes/image-edit.php

    diff --git a/src/wp-admin/includes/image-edit.php b/src/wp-admin/includes/image-edit.php
    index 746c0bafbd..dd65fb4203 100644
    a b function wp_save_image( $post_id ) { 
    845845        $target  = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : '';
    846846        $scale   = ! empty( $_REQUEST['do'] ) && 'scale' === $_REQUEST['do'];
    847847
    848         if ( $scale && $fwidth > 0 && $fheight > 0 ) {
     848        if ( $scale ) {
    849849                $size = $img->get_size();
    850850                $sX   = $size['width'];
    851851                $sY   = $size['height'];
    852852
    853                 // Check if it has roughly the same w / h ratio.
    854                 $diff = round( $sX / $sY, 2 ) - round( $fwidth / $fheight, 2 );
    855                 if ( -0.1 < $diff && $diff < 0.1 ) {
    856                         // Scale the full size image.
    857                         if ( $img->resize( $fwidth, $fheight ) ) {
    858                                 $scaled = true;
    859                         }
     853                if ( $sX < $fwidth || $sY < $fheight ) {
     854                        $return->error = esc_js( __( 'Images cannot be scaled to a size larger than the original.' ) );
     855                        return $return;
    860856                }
    861857
    862                 if ( ! $scaled ) {
    863                         $return->error = esc_js( __( 'Error while saving the scaled image. Please reload the page and try again.' ) );
    864                         return $return;
     858                if ( $fwidth > 0 && $fheight > 0 ) {
     859                        // Check if it has roughly the same w / h ratio.
     860                        $diff = round( $sX / $sY, 2 ) - round( $fwidth / $fheight, 2 );
     861                        if ( -0.1 < $diff && $diff < 0.1 ) {
     862                                // Scale the full size image.
     863                                if ( $img->resize( $fwidth, $fheight ) ) {
     864                                        $scaled = true;
     865                                }
     866                        }
     867
     868                        if ( ! $scaled ) {
     869                                $return->error = esc_js( __( 'Error while saving the scaled image. Please reload the page and try again.' ) );
     870                                return $return;
     871                        }
    865872                }
    866873        } elseif ( ! empty( $_REQUEST['history'] ) ) {
    867874                $changes = json_decode( wp_unslash( $_REQUEST['history'] ) );
  • tests/phpunit/tests/ajax/wpAjaxImageEditor.php

    diff --git a/tests/phpunit/tests/ajax/wpAjaxImageEditor.php b/tests/phpunit/tests/ajax/wpAjaxImageEditor.php
    index f09721b74f..5fd7fffa3d 100644
    a b class Tests_Ajax_wpAjaxImageEditor extends WP_Ajax_UnitTestCase { 
    6161                $this->assertArrayHasKey( 'medium', $media_meta['sizes'], 'cropped attachment should have data for medium size' );
    6262        }
    6363
     64        /**
     65         * @ticket 26381
     66         * @requires function imagejpeg
     67         *
     68         * @covers ::wp_save_image
     69         */
     70        public function testCropImageIntoLargerOne() {
     71                require_once ABSPATH . 'wp-admin/includes/image-edit.php';
     72
     73                $filename = DIR_TESTDATA . '/images/canola.jpg';
     74                $contents = file_get_contents( $filename );
     75
     76                $upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
     77                $id     = $this->_make_attachment( $upload );
     78
     79                $_REQUEST['action']  = 'image-editor';
     80                $_REQUEST['postid']  = $id;
     81                $_REQUEST['do']      = 'scale';
     82                $_REQUEST['fwidth']  = 700;
     83                $_REQUEST['fheight'] = 500;
     84
     85                $ret = wp_save_image( $id );
     86
     87                $this->assertObjectHasAttribute( 'error', $ret );
     88                $this->assertEquals( 'Images cannot be scaled to a size larger than the original.', $ret->error );
     89        }
     90
    6491        /**
    6592         * @ticket 32171
    6693         * @requires function imagejpeg