From 9d8e1ae0689ac5bb1b78d67a90364814b04abc81 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 7 Feb 2023 03:15:49 +0000 Subject: [PATCH] Filesystem API: Add test for uncovered `WP_Error` in `move_dir()`. Introduces a test for the `WP_Error` object `destination_not_deleted_move_dir` in the `move_dir()` function. Follow up to [55226]. Props costdev, mukesh27. Fixes #57375. git-svn-id: https://develop.svn.wordpress.org/trunk@55244 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/filesystem/moveDir.php | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/phpunit/tests/filesystem/moveDir.php b/tests/phpunit/tests/filesystem/moveDir.php index fb487c7338..4cc013453d 100644 --- a/tests/phpunit/tests/filesystem/moveDir.php +++ b/tests/phpunit/tests/filesystem/moveDir.php @@ -276,4 +276,37 @@ class Tests_Filesystem_MoveDir extends WP_UnitTestCase { ); } + /** + * Tests that `move_dir()` returns a WP_Error object when overwriting + * is enabled, the destination exists, but cannot be deleted. + * + * @ticket 57375 + */ + public function test_should_return_wp_error_when_overwriting_is_enabled_the_destination_exists_but_cannot_be_deleted() { + global $wp_filesystem; + $wpfilesystem_backup = $wp_filesystem; + + // Force failure conditions. + $filesystem_mock = $this->getMockBuilder( 'WP_Filesystem_Direct' )->setConstructorArgs( array( null ) )->getMock(); + $filesystem_mock->expects( $this->once() )->method( 'exists' )->willReturn( true ); + $filesystem_mock->expects( $this->once() )->method( 'delete' )->willReturn( false ); + $wp_filesystem = $filesystem_mock; + + $actual = move_dir( self::$existing_from, self::$existing_from_subdir, true ); + + // Restore the filesystem. + $wp_filesystem = $wpfilesystem_backup; + + $this->assertWPError( + $actual, + 'A WP_Error object was not returned.' + ); + + $this->assertSame( + 'destination_not_deleted_move_dir', + $actual->get_error_code(), + 'An unexpected error code was returned.' + ); + } + }