From aab43c6d5760263da97bd158f3171eae0f5da7af Mon Sep 17 00:00:00 2001 From: Shane Rounce Date: Mon, 23 Nov 2026 08:17:09 +0000 Subject: [PATCH 1/2] Filesystem API: Report failures when closing a direct write --- .../includes/class-wp-filesystem-direct.php | 4 +- .../wpFilesystemDirect/putContents.php | 104 ++++++++++++++++++ 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/src/wp-admin/includes/class-wp-filesystem-direct.php b/src/wp-admin/includes/class-wp-filesystem-direct.php index 33aa14ce47cb9..68e4ef34866db 100644 --- a/src/wp-admin/includes/class-wp-filesystem-direct.php +++ b/src/wp-admin/includes/class-wp-filesystem-direct.php @@ -80,9 +80,9 @@ public function put_contents( $file, $contents, $mode = false ) { reset_mbstring_encoding(); - fclose( $fp ); + $closed = fclose( $fp ); - if ( $data_length !== $bytes_written ) { + if ( $data_length !== $bytes_written || ! $closed ) { return false; } diff --git a/tests/phpunit/tests/filesystem/wpFilesystemDirect/putContents.php b/tests/phpunit/tests/filesystem/wpFilesystemDirect/putContents.php index 8eabfffdb2f6a..c76f6f37a3c3b 100644 --- a/tests/phpunit/tests/filesystem/wpFilesystemDirect/putContents.php +++ b/tests/phpunit/tests/filesystem/wpFilesystemDirect/putContents.php @@ -6,6 +6,7 @@ */ require_once __DIR__ . '/base.php'; +require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php'; /** * @group admin @@ -39,4 +40,107 @@ public function test_should_insert_contents_into_file() { $this->assertTrue( $actual, 'The contents were not inserted.' ); } + + /** + * Tests that writing to a stream preserves successful and empty writes. + * + * @dataProvider data_stream_contents + * + * @param string $contents Contents to write. + */ + public function test_should_write_contents_to_stream( $contents ) { + $actual = $this->put_contents_to_stream( 'WP_Test_Stream', $contents, true ); + + $this->assertTrue( $actual['result'], 'The stream write should succeed.' ); + $this->assertSame( $contents, $actual['contents'], 'The stream contents should match.' ); + } + + /** + * Provides contents for successful stream writes. + * + * @return array[] + */ + public function data_stream_contents() { + return array( + 'non-empty' => array( 'New content.' ), + 'empty' => array( '' ), + ); + } + + /** + * Tests that an incomplete stream write is reported as a failure. + */ + public function test_should_return_false_when_stream_write_fails() { + $actual = $this->put_contents_to_stream( 'WP_Test_Stream_Filesystem_Write_Failure', 'New content.', false ); + + $this->assertFalse( $actual['result'] ); + } + + /** + * Tests that a stream flush failure is reported even when all bytes were written. + * + * PHP 8.6 is the first version where fclose() reports flush and close failures. + * + * @requires PHP >= 8.6 + */ + public function test_should_return_false_when_stream_flush_fails() { + $actual = $this->put_contents_to_stream( 'WP_Test_Stream_Filesystem_Flush_Failure', 'New content.', false ); + + $this->assertFalse( $actual['result'] ); + $this->assertSame( 'New content.', $actual['contents'], 'The failure should occur after all bytes were written.' ); + } + + /** + * Writes through the actual filesystem method, observing permission changes. + * + * @param string $wrapper_class The stream wrapper class. + * @param string $contents The contents to write. + * @param bool $chmod Whether permissions should be changed. + * @return array The result and the contents accepted by the stream. + */ + private function put_contents_to_stream( $wrapper_class, $contents, $chmod ) { + $protocol = 'wptestfilesystem'; + $filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' ) + ->setConstructorArgs( array( null ) ) + ->onlyMethods( array( 'chmod' ) ) + ->getMock(); + + $filesystem->expects( $chmod ? $this->once() : $this->never() ) + ->method( 'chmod' ) + ->with( $protocol . '://WP_Filesystem_Direct/write.txt', 0644 ) + ->willReturn( true ); + + stream_wrapper_register( $protocol, $wrapper_class ); + WP_Test_Stream::$data = array(); + + try { + $result = $filesystem->put_contents( $protocol . '://WP_Filesystem_Direct/write.txt', $contents, 0644 ); + + return array( + 'result' => $result, + 'contents' => WP_Test_Stream::$data['WP_Filesystem_Direct']['/write.txt'] ?? '', + ); + } finally { + stream_wrapper_unregister( $protocol ); + WP_Test_Stream::$data = array(); + } + } +} + +/** + * A stream wrapper that does not accept data. + */ +class WP_Test_Stream_Filesystem_Write_Failure extends WP_Test_Stream { + public function stream_write( $data ) { + return 0; + } +} + +/** + * A stream wrapper that accepts data but cannot flush it. + */ +class WP_Test_Stream_Filesystem_Flush_Failure extends WP_Test_Stream { + public function stream_flush() { + return false; + } } From 92546346fe8e61f290d0efcda235f9dc43d021da Mon Sep 17 00:00:00 2001 From: Shane Rounce Date: Mon, 23 Nov 2026 13:03:51 +0000 Subject: [PATCH 2/2] Tests: Add the ticket reference for #66784. --- .../tests/filesystem/wpFilesystemDirect/putContents.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/phpunit/tests/filesystem/wpFilesystemDirect/putContents.php b/tests/phpunit/tests/filesystem/wpFilesystemDirect/putContents.php index c76f6f37a3c3b..b4efbcb74974d 100644 --- a/tests/phpunit/tests/filesystem/wpFilesystemDirect/putContents.php +++ b/tests/phpunit/tests/filesystem/wpFilesystemDirect/putContents.php @@ -44,6 +44,8 @@ public function test_should_insert_contents_into_file() { /** * Tests that writing to a stream preserves successful and empty writes. * + * @ticket 66784 + * * @dataProvider data_stream_contents * * @param string $contents Contents to write. @@ -69,6 +71,8 @@ public function data_stream_contents() { /** * Tests that an incomplete stream write is reported as a failure. + * + * @ticket 66784 */ public function test_should_return_false_when_stream_write_fails() { $actual = $this->put_contents_to_stream( 'WP_Test_Stream_Filesystem_Write_Failure', 'New content.', false ); @@ -81,6 +85,8 @@ public function test_should_return_false_when_stream_write_fails() { * * PHP 8.6 is the first version where fclose() reports flush and close failures. * + * @ticket 66784 + * * @requires PHP >= 8.6 */ public function test_should_return_false_when_stream_flush_fails() {