Skip to content

Commit

Permalink
Tests_Admin_includesFile: add two tests for the download_url() function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Aug 11, 2021
1 parent 9b565f7 commit 8e58f75
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/phpunit/tests/admin/includesFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,49 @@ public function _fake_download_url_non_200_response_code( $response, $args, $url
public function __return_5() {
return 5;
}

/**
* Verify that a WP_Error object is returned when invalid input is passed as the $url parameter.
*
* @covers ::download_url
*
* @dataProvider data_download_url_empty_url
*
* @param mixed $url Input URL.
*/
public function test_download_url_empty_url( $url ) {
$error = download_url( $url );
$this->assertWPError( $error );
$this->assertSame( 'http_no_url', $error->get_error_code() );
$this->assertSame( 'Invalid URL Provided.', $error->get_error_message() );
}

/**
* Data provider.
*
* @return array
*/
public function data_download_url_empty_url() {
return array(
'null' => array( null ),
'false' => array( false ),
'integer 0' => array( 0 ),
'empty string' => array( '' ),
'string 0' => array( '0' ),
);
}

/**
* Verify that no "passing null to non-nullable" error is thrown on PHP 8.1 when the $url does not have a path component.
*
* @covers ::download_url
*
* @ticket 53635
*/
public function test_download_url_when_passed_url_without_path() {
$result = download_url( 'https://example.com' );

$this->assertIsString( $result );
$this->assertNotEmpty( $result ); // File path will be generated, but will never be empty.
}
}

0 comments on commit 8e58f75

Please sign in to comment.