From 2d75548250e5460e10a62a48e6d33bc426c1c2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Fri, 17 Jul 2026 20:39:40 +0200 Subject: [PATCH] Build/Test Tools: Use strict assertions in various test cases. Replace `assertEquals()`/`assertNotEquals()` with `assertSame()`/`assertNotSame()` across several test files. Strict assertions also verify the value type, catching unintended type coercion. For assertions comparing values that may differ in type (e.g. `ceil()` returning a float), the loose comparison is intentionally retained. --- tests/phpunit/tests/customize/manager.php | 2 +- tests/phpunit/tests/customize/setting.php | 4 ++-- tests/phpunit/tests/functions.php | 2 +- tests/phpunit/tests/icons/wpRestIconsController.php | 2 +- tests/phpunit/tests/image/functions.php | 6 +++--- tests/phpunit/tests/post.php | 4 ++-- tests/phpunit/tests/rest-api/rest-users-controller.php | 6 +++--- .../rest-api/wpRestAbilitiesV1CategoriesController.php | 2 +- .../tests/rest-api/wpRestAbilitiesV1ListController.php | 2 +- tests/phpunit/tests/script-modules/wpScriptModules.php | 2 +- tests/phpunit/tests/template.php | 2 +- tests/phpunit/tests/term/wpInsertTerm.php | 2 +- tests/phpunit/tests/theme/themeDir.php | 2 +- 13 files changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index bedf0b39b3e95..57da418055dcb 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -3041,7 +3041,7 @@ public function test_return_url() { $url = 'http://badreferer.example.com/'; $_SERVER['HTTP_REFERER'] = wp_slash( $url ); - $this->assertNotEquals( $url, $this->manager->get_return_url() ); + $this->assertNotSame( $url, $this->manager->get_return_url() ); $this->assertSame( $preview_url, $this->manager->get_return_url() ); $this->manager->set_return_url( admin_url( 'edit.php?trashed=1' ) ); diff --git a/tests/phpunit/tests/customize/setting.php b/tests/phpunit/tests/customize/setting.php index 8addb229d4edc..2ec20b040f976 100644 --- a/tests/phpunit/tests/customize/setting.php +++ b/tests/phpunit/tests/customize/setting.php @@ -188,14 +188,14 @@ public function test_preview_standard_types_non_multidimensional() { $message = 'Initial value should be overridden because initial preview() was no-op due to setting having existing value and/or post value was absent.'; $this->assertSame( $overridden_value, call_user_func( $type_options['getter'], $name ), $message ); $this->assertSame( $overridden_value, $setting->value(), $message ); - $this->assertNotEquals( $initial_value, $setting->value(), $message ); + $this->assertNotSame( $initial_value, $setting->value(), $message ); // Non-multidimensional: Ensure that setting a post value *after* preview() is called results in the post value being seen (deferred preview). $post_value = "post_value_for_{$setting->id}_set_after_preview_called"; $this->assertSame( 0, did_action( "customize_post_value_set_{$setting->id}" ) ); $this->manager->set_post_value( $setting->id, $post_value ); $this->assertSame( 1, did_action( "customize_post_value_set_{$setting->id}" ) ); - $this->assertNotEquals( $overridden_value, $setting->value() ); + $this->assertNotSame( $overridden_value, $setting->value() ); $this->assertSame( $post_value, call_user_func( $type_options['getter'], $name ) ); $this->assertSame( $post_value, $setting->value() ); diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 19c721aeaa46a..63f8d8cc5af50 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -2153,7 +2153,7 @@ public function data_wp_is_json_media_type() { */ public function test_wp_get_default_extension_for_mime_type() { $this->assertSame( 'jpg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpg not returned as default extension for "image/jpeg"' ); - $this->assertNotEquals( 'jpeg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpeg should not be returned as default extension for "image/jpeg"' ); + $this->assertNotSame( 'jpeg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpeg should not be returned as default extension for "image/jpeg"' ); $this->assertSame( 'png', wp_get_default_extension_for_mime_type( 'image/png' ), 'png not returned as default extension for "image/png"' ); $this->assertFalse( wp_get_default_extension_for_mime_type( 'wibble/wobble' ), 'false not returned for unrecognized mime type' ); $this->assertFalse( wp_get_default_extension_for_mime_type( '' ), 'false not returned when empty string as mime type supplied' ); diff --git a/tests/phpunit/tests/icons/wpRestIconsController.php b/tests/phpunit/tests/icons/wpRestIconsController.php index dc899ce2bd7be..27471538b3aaf 100644 --- a/tests/phpunit/tests/icons/wpRestIconsController.php +++ b/tests/phpunit/tests/icons/wpRestIconsController.php @@ -402,7 +402,7 @@ public function test_get_items_search_includes_label() { $data = $response->get_data(); $this->assertSame( 200, $response->get_status() ); - $this->assertEquals( array( 'core/at-symbol' ), array_column( $data, 'name' ) ); + $this->assertSame( array( 'core/at-symbol' ), array_column( $data, 'name' ) ); } /** diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 94cc0111f0d4c..9d8b34c24363c 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -1014,12 +1014,12 @@ public function test_pdf_preview_doesnt_overwrite_existing_jpeg() { $preview_path = $temp_dir . $metadata['sizes']['full']['file']; // PDF preview didn't overwrite PDF. - $this->assertNotEquals( $pdf_path, $preview_path ); + $this->assertNotSame( $pdf_path, $preview_path ); // PDF preview didn't overwrite JPG with same name. - $this->assertNotEquals( $jpg1_path, $preview_path ); + $this->assertNotSame( $jpg1_path, $preview_path ); $this->assertSame( 'asdf', file_get_contents( $jpg1_path ) ); // PDF preview didn't overwrite PDF preview with same name. - $this->assertNotEquals( $jpg2_path, $preview_path ); + $this->assertNotSame( $jpg2_path, $preview_path ); $this->assertSame( 'fdsa', file_get_contents( $jpg2_path ) ); // Cleanup. diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index e609fa0d3003d..a058bd628dff8 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -273,7 +273,7 @@ public function test_wp_count_posts_insert_invalidation() { $post = get_post( $post_ids[ $key ] ); $this->assertSame( 'draft', $post->post_status ); - $this->assertNotEquals( 'publish', $post->post_status ); + $this->assertNotSame( 'publish', $post->post_status ); $after_draft_counts = wp_count_posts(); $this->assertSame( '1', $after_draft_counts->draft ); @@ -294,7 +294,7 @@ public function test_wp_count_posts_trash_invalidation() { $post = get_post( $post_ids[ $key ] ); $this->assertSame( 'trash', $post->post_status ); - $this->assertNotEquals( 'publish', $post->post_status ); + $this->assertNotSame( 'publish', $post->post_status ); $after_trash_counts = wp_count_posts(); $this->assertSame( '1', $after_trash_counts->trash ); diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index b78e95b95f48d..38f47093472f5 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -1995,7 +1995,7 @@ public function test_update_user_role() { $new_data = $response->get_data(); $this->assertSame( 'editor', $new_data['roles'][0] ); - $this->assertNotEquals( 'administrator', $new_data['roles'][0] ); + $this->assertNotSame( 'administrator', $new_data['roles'][0] ); $user = get_userdata( $user_id ); $this->assertArrayHasKey( 'editor', $user->caps ); @@ -2090,7 +2090,7 @@ public function test_update_user_role_privilege_deescalation_multisite() { $new_data = $response->get_data(); $this->assertSame( 'editor', $new_data['roles'][0] ); - $this->assertNotEquals( 'administrator', $new_data['roles'][0] ); + $this->assertNotSame( 'administrator', $new_data['roles'][0] ); $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); @@ -2104,7 +2104,7 @@ public function test_update_user_role_privilege_deescalation_multisite() { $new_data = $response->get_data(); $this->assertSame( 'editor', $new_data['roles'][0] ); - $this->assertNotEquals( 'administrator', $new_data['roles'][0] ); + $this->assertNotSame( 'administrator', $new_data['roles'][0] ); } diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php index f52fa32543d47..e706d9e0c5ccf 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php @@ -277,7 +277,7 @@ public function test_pagination_headers(): void { $this->assertArrayHasKey( 'X-WP-TotalPages', $headers ); $total_categories = count( wp_get_ability_categories() ); - $this->assertEquals( $total_categories, (int) $headers['X-WP-Total'] ); + $this->assertSame( $total_categories, (int) $headers['X-WP-Total'] ); $this->assertEquals( ceil( $total_categories / 10 ), (int) $headers['X-WP-TotalPages'] ); } diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php index 8361c750b266b..67bd6c4d75882 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php @@ -506,7 +506,7 @@ public function test_pagination_headers(): void { $this->assertArrayHasKey( 'X-WP-TotalPages', $headers ); $total_abilities = count( wp_get_abilities() ) - 1; // Exclude the one that doesn't show in REST. - $this->assertEquals( $total_abilities, (int) $headers['X-WP-Total'] ); + $this->assertSame( $total_abilities, (int) $headers['X-WP-Total'] ); $this->assertEquals( ceil( $total_abilities / 10 ), (int) $headers['X-WP-TotalPages'] ); } diff --git a/tests/phpunit/tests/script-modules/wpScriptModules.php b/tests/phpunit/tests/script-modules/wpScriptModules.php index fb78d61d145ee..2f85e63b6f72b 100644 --- a/tests/phpunit/tests/script-modules/wpScriptModules.php +++ b/tests/phpunit/tests/script-modules/wpScriptModules.php @@ -2539,7 +2539,7 @@ public function test_static_import_dependency_with_dynamic_imports_depending_on_ $preload_links = get_echo( array( wp_script_modules(), 'print_script_module_preloads' ) ); $script_modules = get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) ); - $this->assertEquals( + $this->assertSame( array( 'static1' => '/static1.js', 'dynamic1' => '/dynamic1.js', diff --git a/tests/phpunit/tests/template.php b/tests/phpunit/tests/template.php index a79702554dc64..9500895713eff 100644 --- a/tests/phpunit/tests/template.php +++ b/tests/phpunit/tests/template.php @@ -747,7 +747,7 @@ static function ( string $buffer ): string { $processed_output = ob_get_clean(); // Obtain the output via the wrapper output buffer. $this->assertIsString( $processed_output ); - $this->assertNotEquals( $original_output, $processed_output ); + $this->assertNotSame( $original_output, $processed_output ); $this->assertStringContainsString( '', $processed_output, 'Expected processed output to contain string.' ); $this->assertStringContainsString( '', $processed_output, 'Expected processed output to contain string.' ); diff --git a/tests/phpunit/tests/term/wpInsertTerm.php b/tests/phpunit/tests/term/wpInsertTerm.php index 0bf95b9a10279..58576096c36e7 100644 --- a/tests/phpunit/tests/term/wpInsertTerm.php +++ b/tests/phpunit/tests/term/wpInsertTerm.php @@ -844,7 +844,7 @@ public function test_wp_insert_term_with_and_without_accents() { $this->assertIsInt( $t1 ); $this->assertIsInt( $t2 ); - $this->assertNotEquals( $t1, $t2 ); + $this->assertNotSame( $t1, $t2 ); $term_2 = get_term( $t2, 'wptests_tax' ); $this->assertSame( $t2, $term_2->term_id ); diff --git a/tests/phpunit/tests/theme/themeDir.php b/tests/phpunit/tests/theme/themeDir.php index a953a04bc5533..cc7ca52a43cd2 100644 --- a/tests/phpunit/tests/theme/themeDir.php +++ b/tests/phpunit/tests/theme/themeDir.php @@ -313,7 +313,7 @@ public function test_theme_dir_slashes() { $this->assertCount( $size + 1, $GLOBALS['wp_theme_directories'] ); foreach ( $GLOBALS['wp_theme_directories'] as $dir ) { - $this->assertNotEquals( '/', substr( $dir, -1 ) ); + $this->assertNotSame( '/', substr( $dir, -1 ) ); } rmdir( WP_CONTENT_DIR . '/themes/foo' );