Skip to content

Commit 8491158

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 eb6b517 commit 8491158

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

tests/phpunit/tests/admin/includesFile.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,50 @@ 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,
114+
* when the $url does not have a path component.
115+
*
116+
* @covers ::download_url
117+
*
118+
* @ticket 53635
119+
*/
120+
public function test_download_url_no_warning_with_url_without_path() {
121+
$result = download_url( 'https://example.com' );
122+
123+
$this->assertIsString( $result );
124+
$this->assertNotEmpty( $result ); // File path will be generated, but will never be empty.
125+
}
80126
}

0 commit comments

Comments
 (0)