Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/wp-admin/includes/class-wp-filesystem-direct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
110 changes: 110 additions & 0 deletions tests/phpunit/tests/filesystem/wpFilesystemDirect/putContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

require_once __DIR__ . '/base.php';
require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php';

/**
* @group admin
Expand Down Expand Up @@ -39,4 +40,113 @@ 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.
*
* @ticket 66784
*
* @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.
*
* @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 );

$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.
*
* @ticket 66784
*
* @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;
}
}
Loading