From c17a4760b7482f0859329fdf6e94f6c7d6c468f7 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 14 Jan 2023 02:06:02 +0000 Subject: [PATCH] Tests: Move the test for `wp_save_image_file()` with a GD resource to a more appropriate place. When passed a GD resource as `$image`, `wp_save_image_file()` should throw a deprecated argument notice: {{{ Function wp_save_image_file was called with an argument that is deprecated since version 3.5.0! $image needs to be a WP_Image_Editor object. }}} The test verifies that the notice is thrown as expected. Includes: * Removing the `Tests_Functions_Deprecated` class. It appears to be initially intended for testing deprecated functions or arguments, but this was later superseded by the `@expectedDeprecated` annotation. * Removing a redundant test for `wp_save_image_file()` **not** throwing a deprecation notice when passed a `WP_Image_Editor` instance. This is already covered by `test_wp_save_image_file()`, which would fail if there is an unexpected deprecation notice. Follow-up to [1061/tests], [25408], [25409], [53529]. See #56793. git-svn-id: https://develop.svn.wordpress.org/trunk@55066 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/functions/deprecated.php | 185 ------------------- tests/phpunit/tests/image/functions.php | 23 +++ 2 files changed, 23 insertions(+), 185 deletions(-) delete mode 100644 tests/phpunit/tests/functions/deprecated.php diff --git a/tests/phpunit/tests/functions/deprecated.php b/tests/phpunit/tests/functions/deprecated.php deleted file mode 100644 index 3d932011fb..0000000000 --- a/tests/phpunit/tests/functions/deprecated.php +++ /dev/null @@ -1,185 +0,0 @@ -_deprecated_functions = array(); - $this->_deprecated_arguments = array(); - $this->_deprecated_files = array(); - add_action( 'deprecated_function_run', array( $this, 'deprecated_function' ), 10, 3 ); - add_action( 'deprecated_function_trigger_error', '__return_false' ); - add_action( 'deprecated_argument_run', array( $this, 'deprecated_argument' ), 10, 3 ); - add_action( 'deprecated_argument_trigger_error', '__return_false' ); - add_action( 'deprecated_file_included', array( $this, 'deprecated_file' ), 10, 4 ); - add_action( 'deprecated_file_trigger_error', '__return_false' ); - } - - /** - * Tears down the test fixture. - */ - public function tear_down() { - remove_action( 'deprecated_function_run', array( $this, 'deprecated_function' ), 10, 3 ); - remove_action( 'deprecated_function_trigger_error', '__return_false' ); - remove_action( 'deprecated_argument_run', array( $this, 'deprecated_argument' ), 10, 3 ); - remove_action( 'deprecated_argument_trigger_error', '__return_false' ); - remove_action( 'deprecated_file_included', array( $this, 'deprecated_argument' ), 10, 4 ); - remove_action( 'deprecated_file_trigger_error', '__return_false' ); - parent::tear_down(); - } - - /** - * Catches functions that have passed through _deprecated_function(). - * - * @param string $function_name - * @param string $replacement - * @param float $version - */ - public function deprecated_function( $function_name, $replacement, $version ) { - $this->_deprecated_functions[] = array( - 'function' => $function_name, - 'replacement' => $replacement, - 'version' => $version, - ); - } - - /** - * Catches arguments that have passed through _deprecated_argument(). - * - * @param string $argument - * @param string $message - * @param float $version - */ - public function deprecated_argument( $argument, $message, $version ) { - $this->_deprecated_arguments[] = array( - 'argument' => $argument, - 'message' => $message, - 'version' => $version, - ); - } - - /** - * Catches arguments that have passed through _deprecated_argument(). - * - * @param string $argument - * @param string $message - * @param float $version - */ - public function deprecated_file( $file, $version, $replacement, $message ) { - $this->_deprecated_files[] = array( - 'file' => $file, - 'version' => $version, - 'replacement' => $replacement, - 'message' => $message, - ); - } - - /** - * Checks if something was deprecated. - * - * @param string $type argument|function|file - * @param string $name - * @return array|false - */ - protected function was_deprecated( $type, $name ) { - switch ( $type ) { - case 'argument': - $search = $this->_deprecated_arguments; - $key = 'argument'; - break; - case 'function': - $search = $this->_deprecated_functions; - $key = 'function'; - break; - default: - $search = $this->_deprecated_files; - $key = 'file'; - } - foreach ( $search as $v ) { - if ( $name === $v[ $key ] ) { - return $v; - } - } - return false; - } - - /** - * Tests that wp_save_image_file() has a deprecated argument when passed a GD resource. - * - * @ticket 6821 - * @expectedDeprecated wp_save_image_file - * @requires function imagejpeg - * - * @covers ::wp_save_image_file - */ - public function test_wp_save_image_file_deprecated_with_gd_resource() { - // Call wp_save_image_file(). - require_once ABSPATH . 'wp-admin/includes/image-edit.php'; - $file = wp_tempnam(); - $img = imagecreatefromjpeg( DIR_TESTDATA . '/images/canola.jpg' ); - wp_save_image_file( $file, $img, 'image/jpeg', 1 ); - imagedestroy( $img ); - unlink( $file ); - - // Check if the arg was deprecated. - $check = $this->was_deprecated( 'argument', 'wp_save_image_file' ); - $this->assertNotEmpty( $check ); - } - - /** - * Tests that wp_save_image_file() doesn't have a deprecated argument when passed a WP_Image_Editor. - * - * @ticket 6821 - * @requires function imagejpeg - * - * @covers ::wp_save_image_file - */ - public function test_wp_save_image_file_not_deprecated_with_wp_image_editor() { - // Call wp_save_image_file(). - require_once ABSPATH . 'wp-admin/includes/image-edit.php'; - $file = wp_tempnam(); - $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' ); - wp_save_image_file( $file, $img, 'image/jpeg', 1 ); - unset( $img ); - unlink( $file ); - - // Check if the arg was deprecated. - $check = $this->was_deprecated( 'argument', 'wp_save_image_file' ); - $this->assertFalse( $check ); - } -} diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 38ef2892a6..7ed944b439 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -327,6 +327,29 @@ class Tests_Image_Functions extends WP_UnitTestCase { return $data; } + /** + * Tests that wp_save_image_file() throws a deprecated argument notice when passed a GD resource. + * + * @ticket 6821 + * @expectedDeprecated wp_save_image_file + * @requires function imagejpeg + * + * @covers ::wp_save_image_file + */ + public function test_wp_save_image_file_deprecated_argument_with_gd_resource() { + require_once ABSPATH . 'wp-admin/includes/image-edit.php'; + + // Call wp_save_image_file(). + $file = wp_tempnam(); + $img = imagecreatefromjpeg( DIR_TESTDATA . '/images/canola.jpg' ); + $ret = wp_save_image_file( $file, $img, 'image/jpeg', 1 ); + + imagedestroy( $img ); + unlink( $file ); + + $this->assertTrue( $ret, 'Image failed to save.' ); + } + /** * Tests that a passed mime type overrides the extension in the filename when saving an image. *