Skip to content

Commit 8e58f75

Browse files
committed
Tests_Admin_includesFile: add two tests for the download_url() function
The first test is "girl-scouting" to make sure that the code up to the point where the error is expected is tested. The second test exposed a PHP 8.1 `basename(): Passing null to parameter #1 ($path) of type string is deprecated` error due to the call to `parse_url()` returning `null` when the component requested does not exist in the passed URL.
1 parent 9b565f7 commit 8e58f75

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/phpunit/tests/admin/includesFile.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,49 @@ public function _fake_download_url_non_200_response_code( $response, $args, $url
7777
public function __return_5() {
7878
return 5;
7979
}
80+
81+
/**
82+
* Verify that a WP_Error object is returned when invalid input is passed as the $url parameter.
83+
*
84+
* @covers ::download_url
85+
*
86+
* @dataProvider data_download_url_empty_url
87+
*
88+
* @param mixed $url Input URL.
89+
*/
90+
public function test_download_url_empty_url( $url ) {
91+
$error = download_url( $url );
92+
$this->assertWPError( $error );
93+
$this->assertSame( 'http_no_url', $error->get_error_code() );
94+
$this->assertSame( 'Invalid URL Provided.', $error->get_error_message() );
95+
}
96+
97+
/**
98+
* Data provider.
99+
*
100+
* @return array
101+
*/
102+
public function data_download_url_empty_url() {
103+
return array(
104+
'null' => array( null ),
105+
'false' => array( false ),
106+
'integer 0' => array( 0 ),
107+
'empty string' => array( '' ),
108+
'string 0' => array( '0' ),
109+
);
110+
}
111+
112+
/**
113+
* Verify that no "passing null to non-nullable" error is thrown on PHP 8.1 when the $url does not have a path component.
114+
*
115+
* @covers ::download_url
116+
*
117+
* @ticket 53635
118+
*/
119+
public function test_download_url_when_passed_url_without_path() {
120+
$result = download_url( 'https://example.com' );
121+
122+
$this->assertIsString( $result );
123+
$this->assertNotEmpty( $result ); // File path will be generated, but will never be empty.
124+
}
80125
}

0 commit comments

Comments
 (0)