@@ -586,6 +586,116 @@ public function test_flip() {
586586 $ this ->assertSame ( $ color_top_left , imagecolorat ( $ property ->getValue ( $ gd_image_editor ), 0 , 99 ) );
587587 }
588588
589+ /**
590+ * Tests that flipping moves every pixel to the expected position.
591+ *
592+ * @ticket 66113
593+ *
594+ * @dataProvider data_flip
595+ *
596+ * @covers WP_Image_Editor_GD::flip
597+ *
598+ * @param bool $horz Whether to flip along the horizontal axis.
599+ * @param bool $vert Whether to flip along the vertical axis.
600+ */
601+ public function test_flip_moves_pixels ( $ horz , $ vert ) {
602+ $ gd_image_editor = new WP_Image_Editor_GD ( DIR_TESTDATA . '/images/gradient-square.jpg ' );
603+ $ gd_image_editor ->load ();
604+
605+ $ property = new ReflectionProperty ( $ gd_image_editor , 'image ' );
606+ if ( PHP_VERSION_ID < 80100 ) {
607+ $ property ->setAccessible ( true );
608+ }
609+
610+ // A 3x2 image in which every pixel has a distinct color.
611+ $ width = 3 ;
612+ $ height = 2 ;
613+ $ image = imagecreatetruecolor ( $ width , $ height );
614+ for ( $ x = 0 ; $ x < $ width ; $ x ++ ) {
615+ for ( $ y = 0 ; $ y < $ height ; $ y ++ ) {
616+ imagesetpixel ( $ image , $ x , $ y , ( $ x + 1 ) * 0x100000 + ( $ y + 1 ) * 0x10 );
617+ }
618+ }
619+ $ original = imagecreatetruecolor ( $ width , $ height );
620+ imagecopy ( $ original , $ image , 0 , 0 , 0 , 0 , $ width , $ height );
621+
622+ $ property ->setValue ( $ gd_image_editor , $ image );
623+
624+ $ size_property = new ReflectionProperty ( $ gd_image_editor , 'size ' );
625+ if ( PHP_VERSION_ID < 80100 ) {
626+ $ size_property ->setAccessible ( true );
627+ }
628+ $ size_property ->setValue (
629+ $ gd_image_editor ,
630+ array (
631+ 'width ' => $ width ,
632+ 'height ' => $ height ,
633+ )
634+ );
635+
636+ $ this ->assertTrue ( $ gd_image_editor ->flip ( $ horz , $ vert ), 'flip() should return true on success. ' );
637+
638+ $ flipped = $ property ->getValue ( $ gd_image_editor );
639+ for ( $ x = 0 ; $ x < $ width ; $ x ++ ) {
640+ for ( $ y = 0 ; $ y < $ height ; $ y ++ ) {
641+ $ this ->assertSame (
642+ imagecolorat ( $ original , $ x , $ y ),
643+ imagecolorat ( $ flipped , $ vert ? $ width - 1 - $ x : $ x , $ horz ? $ height - 1 - $ y : $ y ),
644+ "Pixel ( $ x, $ y) was not moved to the expected position. "
645+ );
646+ }
647+ }
648+ }
649+
650+ /**
651+ * Tests that flipping returns a WP_Error, not false, when the image cannot be flipped.
652+ *
653+ * @ticket 66113
654+ *
655+ * @covers WP_Image_Editor_GD::flip
656+ */
657+ public function test_flip_returns_wp_error_on_failure () {
658+ $ gd_image_editor = new WP_Image_Editor_GD ( DIR_TESTDATA . '/images/gradient-square.jpg ' );
659+ $ gd_image_editor ->load ();
660+
661+ $ property = new ReflectionProperty ( $ gd_image_editor , 'image ' );
662+ if ( PHP_VERSION_ID < 80100 ) {
663+ $ property ->setAccessible ( true );
664+ }
665+ $ property ->setValue ( $ gd_image_editor , false );
666+
667+ $ result = $ gd_image_editor ->flip ( true , false );
668+
669+ $ this ->assertWPError ( $ result , 'flip() should return a WP_Error on failure. ' );
670+ $ this ->assertSame ( 'image_flip_error ' , $ result ->get_error_code (), 'The error code should be image_flip_error. ' );
671+ }
672+
673+ /**
674+ * Data provider.
675+ *
676+ * @return array<string, array{ horz: bool, vert: bool }>
677+ */
678+ public function data_flip () {
679+ return array (
680+ 'horizontal axis ' => array (
681+ 'horz ' => true ,
682+ 'vert ' => false ,
683+ ),
684+ 'vertical axis ' => array (
685+ 'horz ' => false ,
686+ 'vert ' => true ,
687+ ),
688+ 'both axes ' => array (
689+ 'horz ' => true ,
690+ 'vert ' => true ,
691+ ),
692+ 'neither axis ' => array (
693+ 'horz ' => false ,
694+ 'vert ' => false ,
695+ ),
696+ );
697+ }
698+
589699 /**
590700 * Tests that an image created with WP_Image_Editor_GD preserves alpha with no resizing.
591701 *
0 commit comments