Skip to content

Commit

Permalink
Tests: Restore the environment before performing assertions in `downl…
Browse files Browse the repository at this point in the history
…oad_url()` tests.

This aims to avoid affecting other tests in case of failure.

Follow-up to [42773], [51939].

See #62280.

git-svn-id: https://develop.svn.wordpress.org/trunk@59604 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Jan 14, 2025
1 parent 4777477 commit 83718be
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions tests/phpunit/tests/admin/includesFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function test_download_url_non_200_response_code() {
add_filter( 'pre_http_request', array( $this, '_fake_download_url_non_200_response_code' ), 10, 3 );

$error = download_url( 'test_download_url_non_200' );

$this->assertWPError( $error );
$this->assertSame(
array(
Expand All @@ -55,6 +56,10 @@ public function test_download_url_non_200_response_code() {
add_filter( 'download_url_error_max_body_size', array( $this, '__return_5' ) );

$error = download_url( 'test_download_url_non_200' );

remove_filter( 'download_url_error_max_body_size', array( $this, '__return_5' ) );
remove_filter( 'pre_http_request', array( $this, '_fake_download_url_non_200_response_code' ) );

$this->assertWPError( $error );
$this->assertSame(
array(
Expand All @@ -63,9 +68,6 @@ public function test_download_url_non_200_response_code() {
),
$error->get_error_data()
);

remove_filter( 'download_url_error_max_body_size', array( $this, '__return_5' ) );
remove_filter( 'pre_http_request', array( $this, '_fake_download_url_non_200_response_code' ) );
}

public function _fake_download_url_non_200_response_code( $response, $parsed_args, $url ) {
Expand Down Expand Up @@ -94,11 +96,13 @@ public function test_download_url_should_respect_filename_from_content_dispositi
add_filter( 'pre_http_request', array( $this, $filter ), 10, 3 );

$filename = download_url( 'url_with_content_disposition_header' );
$this->assertStringContainsString( 'filename-from-content-disposition-header', $filename );

remove_filter( 'pre_http_request', array( $this, $filter ) );

$this->assertFileExists( $filename );
$this->unlink( $filename );

remove_filter( 'pre_http_request', array( $this, $filter ) );
$this->assertStringContainsString( 'filename-from-content-disposition-header', $filename );
}

/**
Expand Down Expand Up @@ -126,10 +130,12 @@ public function test_save_to_temp_directory_when_getting_filename_from_content_d
add_filter( 'pre_http_request', array( $this, $filter ), 10, 3 );

$filename = download_url( 'url_with_content_disposition_header' );
$this->assertStringContainsString( get_temp_dir(), $filename );
$this->unlink( $filename );

remove_filter( 'pre_http_request', array( $this, $filter ) );

$this->unlink( $filename );

$this->assertStringContainsString( get_temp_dir(), $filename );
}

/**
Expand Down Expand Up @@ -209,10 +215,12 @@ public function test_download_url_should_reject_filename_from_invalid_content_di
add_filter( 'pre_http_request', array( $this, $filter ), 10, 3 );

$filename = download_url( 'url_with_content_disposition_header' );
$this->assertStringContainsString( 'url_with_content_disposition_header', $filename );
$this->unlink( $filename );

remove_filter( 'pre_http_request', array( $this, $filter ) );

$this->unlink( $filename );

$this->assertStringContainsString( 'url_with_content_disposition_header', $filename );
}

/**
Expand Down

0 comments on commit 83718be

Please sign in to comment.