Skip to content

Commit 9c7273c

Browse files
committed
Add test coverage for file read error
1 parent 9828bac commit 9c7273c

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/wp-includes/script-loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3130,7 +3130,7 @@ static function ( $a, $b ) {
31303130
}
31313131

31323132
// Get the styles if we don't already have them.
3133-
$style['css'] = file_get_contents( $style['path'] );
3133+
$style['css'] = @file_get_contents( $style['path'] );
31343134
if ( false === $style['css'] ) {
31353135
_doing_it_wrong(
31363136
__FUNCTION__,

tests/phpunit/tests/dependencies/styles.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,39 @@ public function test_wp_maybe_inline_styles_no_path() {
774774
* @covers ::wp_maybe_inline_styles
775775
* @expectedIncorrectUsage wp_maybe_inline_styles
776776
*/
777-
public function test_wp_maybe_inline_styles_bad_path() {
777+
public function test_wp_maybe_inline_styles_bad_path_at_file_size_check() {
778+
$handle = 'test-handle';
779+
$inc_path = '/css/ancient-themes.css'; // Does not exist.
780+
$url = '/' . WPINC . $inc_path;
781+
wp_register_style( $handle, $url );
782+
wp_style_add_data( $handle, 'path', ABSPATH . WPINC . $inc_path );
783+
wp_enqueue_style( $handle );
784+
785+
wp_maybe_inline_styles();
786+
787+
$this->assertSame( $GLOBALS['wp_styles']->registered[ $handle ]->src, $url );
788+
}
789+
790+
/**
791+
* @ticket 64447
792+
*
793+
* @covers ::wp_maybe_inline_styles
794+
* @expectedIncorrectUsage wp_maybe_inline_styles
795+
*/
796+
public function test_wp_maybe_inline_styles_bad_path_with_file_size_provided() {
797+
// This ensures the initial file size check is bypassed.
798+
add_filter(
799+
'pre_wp_filesize',
800+
static function ( $size, $path ) {
801+
if ( str_contains( $path, 'ancient-themes.css' ) ) {
802+
$size = 1000;
803+
}
804+
return $size;
805+
},
806+
10,
807+
2
808+
);
809+
778810
$handle = 'test-handle';
779811
$inc_path = '/css/ancient-themes.css'; // Does not exist.
780812
$url = '/' . WPINC . $inc_path;

0 commit comments

Comments
 (0)