Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix order of cleanup actions in PHP unit tests #853

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public function test_run_with_invalid_requires_wp_header() {

$errors = $check_result->get_errors();

delete_transient( 'wp_plugin_check_latest_version_info' );

$this->assertNotEmpty( $errors );

$error_items = wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_requires_wp' ) );

$this->assertCount( 1, $error_items );
$this->assertStringContainsString( 'such as "6.5" or "6.4"', reset( $error_items )['message'] );

delete_transient( 'wp_plugin_check_latest_version_info' );
}

public function test_run_with_valid_requires_plugins_header() {
Expand Down
38 changes: 19 additions & 19 deletions tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ public function test_run_with_errors_parser_warnings_with_custom_set_transient_v

$warnings = $check_result->get_warnings();

delete_transient( 'wp_plugin_check_latest_wp_version' );

$this->assertNotEmpty( $warnings );
$this->assertArrayHasKey( 'readme.txt', $warnings );

Expand All @@ -359,8 +361,6 @@ public function test_run_with_errors_parser_warnings_with_custom_set_transient_v
$filtered_items = array_values( $filtered_items );

$this->assertStringContainsString( 'The "Tested up to" field was ignored. This field should only contain a valid WordPress version such as "' . $version . '"', $filtered_items[0]['message'] );

delete_transient( 'wp_plugin_check_latest_wp_version' );
}

public function test_run_with_errors_multiple_parser_warnings_and_empty_ignored_array() {
Expand All @@ -375,6 +375,8 @@ public function test_run_with_errors_multiple_parser_warnings_and_empty_ignored_
$errors = $check_result->get_errors();
$warnings = $check_result->get_warnings();

remove_filter( 'wp_plugin_check_ignored_readme_warnings', '__return_empty_array' );

$this->assertNotEmpty( $warnings );
$this->assertArrayHasKey( 'readme.txt', $warnings );

Expand All @@ -385,8 +387,6 @@ public function test_run_with_errors_multiple_parser_warnings_and_empty_ignored_
$this->assertEquals( 8, $check_result->get_warning_count() );
$this->assertEmpty( $errors );
$this->assertEquals( 0, $check_result->get_error_count() );

remove_filter( 'wp_plugin_check_ignored_readme_warnings', '__return_empty_array' );
}

public function test_filter_readme_warnings_ignored() {
Expand All @@ -406,15 +406,15 @@ static function () use ( $custom_ignores ) {

$result = apply_filters( $filter_name, array() );

$this->assertEquals( $custom_ignores, $result );

// Remove the filter to avoid interfering with other tests.
remove_filter(
$filter_name,
static function () use ( $custom_ignores ) {
return $custom_ignores;
}
);

$this->assertEquals( $custom_ignores, $result );
}

public function test_filter_wp_plugin_check_ignored_readme_warnings_will_return_no_error() {
Expand Down Expand Up @@ -442,18 +442,18 @@ static function () use ( $custom_ignores ) {
$errors = $check_result->get_errors();
$warnings = $check_result->get_warnings();

$this->assertEmpty( $errors );
$this->assertEmpty( $warnings );
$this->assertSame( 0, $check_result->get_error_count() );
$this->assertSame( 0, $check_result->get_warning_count() );

// Remove the filter to avoid interfering with other tests.
remove_filter(
$filter_name,
static function () use ( $custom_ignores ) {
return $custom_ignores;
}
);

$this->assertEmpty( $errors );
$this->assertEmpty( $warnings );
$this->assertSame( 0, $check_result->get_error_count() );
$this->assertSame( 0, $check_result->get_warning_count() );
}

public function test_run_with_errors_upgrade_notice() {
Expand Down Expand Up @@ -485,18 +485,18 @@ public function test_run_with_errors_tested_up_to_latest_plus_two_version() {

$errors = $check_result->get_errors();

delete_transient( 'wp_plugin_check_latest_wp_version' );

$this->assertNotEmpty( $errors );

$filtered_items = wp_list_filter( $errors['readme.md'][0][0], array( 'code' => 'nonexistent_tested_upto_header' ) );

$this->assertCount( 1, $filtered_items );
$this->assertStringContainsString( 'Tested up to: 6.1', $filtered_items[0]['message'] );
$this->assertStringContainsString( 'This version of WordPress does not exist (yet).', $filtered_items[0]['message'] );

delete_transient( 'wp_plugin_check_latest_wp_version' );
}

public function test_run_with_errors_tested_up_to_latest_plus_one_version() {
public function test_run_without_errors_tested_up_to_latest_plus_one_version() {
$version = '6.0'; // Target plugin has "6.1" is readme.

set_transient( 'wp_plugin_check_latest_wp_version', $version );
Expand All @@ -509,12 +509,12 @@ public function test_run_with_errors_tested_up_to_latest_plus_one_version() {

$errors = $check_result->get_errors();

$this->assertCount( 0, wp_list_filter( $errors['readme.md'][0][0], array( 'code' => 'nonexistent_tested_upto_header' ) ) );

delete_transient( 'wp_plugin_check_latest_wp_version' );

$this->assertCount( 0, wp_list_filter( $errors['readme.md'][0][0], array( 'code' => 'nonexistent_tested_upto_header' ) ) );
}

public function test_run_with_errors_tested_up_to_latest_stable_version() {
public function test_run_without_errors_tested_up_to_latest_stable_version() {
$version = '6.1'; // Target plugin has "6.1" is readme.

set_transient( 'wp_plugin_check_latest_wp_version', $version );
Expand All @@ -527,9 +527,9 @@ public function test_run_with_errors_tested_up_to_latest_stable_version() {

$errors = $check_result->get_errors();

$this->assertCount( 0, wp_list_filter( $errors['readme.md'][0][0], array( 'code' => 'nonexistent_tested_upto_header' ) ) );

delete_transient( 'wp_plugin_check_latest_wp_version' );

$this->assertCount( 0, wp_list_filter( $errors['readme.md'][0][0], array( 'code' => 'nonexistent_tested_upto_header' ) ) );
}

public function test_run_without_errors_readme_contributors_warning() {
Expand Down
32 changes: 16 additions & 16 deletions tests/phpunit/tests/Utilities/Plugin_Request_Utility_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,15 @@ static function () use ( $custom_ignore_directories ) {

$result = Plugin_Request_Utility::get_directories_to_ignore();

$this->assertEquals( $custom_ignore_directories, $result );

// Remove the filter to avoid interfering with other tests.
remove_filter(
$filter_name,
static function () use ( $custom_ignore_directories ) {
return $custom_ignore_directories;
}
);

$this->assertEquals( $custom_ignore_directories, $result );
}

public function test_filter_ignore_files() {
Expand All @@ -289,15 +289,15 @@ static function () use ( $custom_ignore_files ) {

$result = Plugin_Request_Utility::get_files_to_ignore();

$this->assertEquals( $custom_ignore_files, $result );

// Remove the filter to avoid interfering with other tests.
remove_filter(
$filter_name,
static function () use ( $custom_ignore_files ) {
return $custom_ignore_files;
}
);

$this->assertEquals( $custom_ignore_files, $result );
}

public function test_plugin_without_error_for_ignore_directories() {
Expand Down Expand Up @@ -331,17 +331,17 @@ static function () use ( $custom_ignore_directories ) {

$results = $checks->run_checks( $check_context, $checks_to_run );

$this->assertInstanceOf( Check_Result::class, $results );
$this->assertEmpty( $results->get_warnings() );
$this->assertEmpty( $results->get_errors() );

// Remove the filter to avoid interfering with other tests.
remove_filter(
$filter_name,
static function () use ( $custom_ignore_directories ) {
return $custom_ignore_directories;
}
);

$this->assertInstanceOf( Check_Result::class, $results );
$this->assertEmpty( $results->get_warnings() );
$this->assertEmpty( $results->get_errors() );
}

public function test_plugin_with_error_for_ignore_directories() {
Expand Down Expand Up @@ -464,6 +464,14 @@ static function () use ( $custom_ignore_files ) {

$results = $checks->run_checks( $check_context, $checks_to_run );

// Remove the filter to avoid interfering with other tests.
remove_filter(
$filter_name,
static function () use ( $custom_ignore_files ) {
return $custom_ignore_files;
}
);

$this->assertInstanceOf( Check_Result::class, $results );

$errors = $results->get_errors();
Expand All @@ -473,13 +481,5 @@ static function () use ( $custom_ignore_files ) {
$this->assertEmpty( $warnings );
$this->assertEquals( 2, $results->get_error_count() );
$this->assertEquals( 0, $results->get_warning_count() );

// Remove the filter to avoid interfering with other tests.
remove_filter(
$filter_name,
static function () use ( $custom_ignore_files ) {
return $custom_ignore_files;
}
);
}
}
Loading