From 22f24125a8c151309cdb0575797ef97815401d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Tue, 14 Jul 2026 09:21:19 +0200 Subject: [PATCH 1/2] Tests: Use more specific assertions instead of `assertTrue()`/`assertFalse()`. Replace boolean assertions wrapping other checks with the dedicated PHPUnit assertions: * `assertContains()`/`assertNotContains()` instead of `in_array()` or `array_search()` wrapped in `assertTrue()`/`assertFalse()`. * `assertEmpty()` instead of `assertTrue( empty( ... ) )`. * `assertDirectoryExists()` instead of `assertTrue( is_dir( ... ) )`. This clarifies intent and produces more descriptive failure messages. --- tests/phpunit/tests/compat/mbStrlen.php | 5 +++-- tests/phpunit/tests/compat/mbSubstr.php | 5 +++-- tests/phpunit/tests/db.php | 6 +++--- tests/phpunit/tests/option/networkOption.php | 2 +- tests/phpunit/tests/post/types.php | 6 +++--- tests/phpunit/tests/post/wpPostType.php | 4 ++-- tests/phpunit/tests/taxonomy.php | 2 +- tests/phpunit/tests/term/wpTaxonomy.php | 4 ++-- tests/phpunit/tests/theme.php | 2 +- 9 files changed, 19 insertions(+), 17 deletions(-) diff --git a/tests/phpunit/tests/compat/mbStrlen.php b/tests/phpunit/tests/compat/mbStrlen.php index 8d9d73108d83a..4be968e3f39ce 100644 --- a/tests/phpunit/tests/compat/mbStrlen.php +++ b/tests/phpunit/tests/compat/mbStrlen.php @@ -13,8 +13,9 @@ class Tests_Compat_mbStrlen extends WP_UnitTestCase { * Test that the native mb_strlen() is available. */ public function test_mb_strlen_availability() { - $this->assertTrue( - in_array( 'mb_strlen', get_defined_functions()['internal'], true ), + $this->assertContains( + 'mb_strlen', + get_defined_functions()['internal'], 'Test runner should have `mbstring` extension active but doesn’t.' ); } diff --git a/tests/phpunit/tests/compat/mbSubstr.php b/tests/phpunit/tests/compat/mbSubstr.php index 5cc4b3d6778a7..5aee9616b16d1 100644 --- a/tests/phpunit/tests/compat/mbSubstr.php +++ b/tests/phpunit/tests/compat/mbSubstr.php @@ -13,8 +13,9 @@ class Tests_Compat_mbSubstr extends WP_UnitTestCase { * Test that mb_substr() is always available (either from PHP or WP). */ public function test_mb_substr_availability() { - $this->assertTrue( - in_array( 'mb_substr', get_defined_functions()['internal'], true ), + $this->assertContains( + 'mb_substr', + get_defined_functions()['internal'], 'Test runner should have `mbstring` extension active but doesn’t.' ); } diff --git a/tests/phpunit/tests/db.php b/tests/phpunit/tests/db.php index 22eeb235e4819..0a813f74b513e 100644 --- a/tests/phpunit/tests/db.php +++ b/tests/phpunit/tests/db.php @@ -243,7 +243,7 @@ public function test_wpdb_supposedly_protected_properties() { $this->assertNotEmpty( $dbh ); $this->assertTrue( isset( $wpdb->dbh ) ); // Test __isset(). unset( $wpdb->dbh ); - $this->assertTrue( empty( $wpdb->dbh ) ); + $this->assertEmpty( $wpdb->dbh ); $wpdb->dbh = $dbh; $this->assertNotEmpty( $wpdb->dbh ); } @@ -269,12 +269,12 @@ public function test_wpdb_actually_protected_properties() { public function test_wpdb_nonexistent_properties() { global $wpdb; - $this->assertTrue( empty( $wpdb->nonexistent_property ) ); + $this->assertEmpty( $wpdb->nonexistent_property ); $wpdb->nonexistent_property = true; $this->assertTrue( $wpdb->nonexistent_property ); $this->assertTrue( isset( $wpdb->nonexistent_property ) ); unset( $wpdb->nonexistent_property ); - $this->assertTrue( empty( $wpdb->nonexistent_property ) ); + $this->assertEmpty( $wpdb->nonexistent_property ); } /** diff --git a/tests/phpunit/tests/option/networkOption.php b/tests/phpunit/tests/option/networkOption.php index b2cb99412ca38..ed36d12754e8d 100644 --- a/tests/phpunit/tests/option/networkOption.php +++ b/tests/phpunit/tests/option/networkOption.php @@ -76,7 +76,7 @@ public function test_check_delete_network_option_updates_notoptions() { if ( ! is_multisite() ) { $network_notoptions = wp_cache_get( '1:notoptions', 'site-options' ); - $this->assertTrue( empty( $network_notoptions['foo'] ), 'The deleted option is not expected to be in network notoptions on a non-multisite.' ); + $this->assertEmpty( $network_notoptions['foo'], 'The deleted option is not expected to be in network notoptions on a non-multisite.' ); } $before = get_num_queries(); diff --git a/tests/phpunit/tests/post/types.php b/tests/phpunit/tests/post/types.php index 96519586d6cff..2c8564f22aeab 100644 --- a/tests/phpunit/tests/post/types.php +++ b/tests/phpunit/tests/post/types.php @@ -343,7 +343,7 @@ public function test_unregister_post_type_removes_query_vars() { $this->assertIsInt( array_search( 'bar', $wp->public_query_vars, true ) ); $this->assertTrue( unregister_post_type( 'foo' ) ); - $this->assertFalse( array_search( 'bar', $wp->public_query_vars, true ) ); + $this->assertNotContains( 'bar', $wp->public_query_vars ); } /** @@ -463,8 +463,8 @@ public function test_unregister_post_type_removes_post_type_from_taxonomies() { $this->assertIsInt( array_search( 'foo', $wp_taxonomies['category']->object_type, true ) ); $this->assertIsInt( array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) ); $this->assertTrue( unregister_post_type( 'foo' ) ); - $this->assertFalse( array_search( 'foo', $wp_taxonomies['category']->object_type, true ) ); - $this->assertFalse( array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) ); + $this->assertNotContains( 'foo', $wp_taxonomies['category']->object_type ); + $this->assertNotContains( 'foo', $wp_taxonomies['post_tag']->object_type ); $this->assertEmpty( get_object_taxonomies( 'foo' ) ); } diff --git a/tests/phpunit/tests/post/wpPostType.php b/tests/phpunit/tests/post/wpPostType.php index 23ec325a65a54..361ed0343ad0d 100644 --- a/tests/phpunit/tests/post/wpPostType.php +++ b/tests/phpunit/tests/post/wpPostType.php @@ -173,8 +173,8 @@ public function test_adds_rewrite_rules() { $post_type_object->remove_rewrite_rules(); $rewrite_tags_after = $wp_rewrite->rewritecode; - $this->assertNotFalse( array_search( "%$post_type%", $rewrite_tags, true ) ); - $this->assertFalse( array_search( "%$post_type%", $rewrite_tags_after, true ) ); + $this->assertContains( "%$post_type%", $rewrite_tags ); + $this->assertNotContains( "%$post_type%", $rewrite_tags_after ); } public function test_register_meta_boxes() { diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index 13528c3015c6b..39a1a6d90ed5b 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -895,7 +895,7 @@ public function test_unregister_taxonomy_removes_query_vars() { $this->assertIsInt( array_search( 'bar', $wp->public_query_vars, true ) ); $this->assertTrue( unregister_taxonomy( 'foo' ) ); - $this->assertFalse( array_search( 'bar', $wp->public_query_vars, true ) ); + $this->assertNotContains( 'bar', $wp->public_query_vars ); } /** diff --git a/tests/phpunit/tests/term/wpTaxonomy.php b/tests/phpunit/tests/term/wpTaxonomy.php index 845497ad0672a..a919e095aa8eb 100644 --- a/tests/phpunit/tests/term/wpTaxonomy.php +++ b/tests/phpunit/tests/term/wpTaxonomy.php @@ -76,8 +76,8 @@ public function test_adds_rewrite_rules() { $taxonomy_object->remove_rewrite_rules(); $rewrite_tags_after = $wp_rewrite->rewritecode; - $this->assertNotFalse( array_search( "%$taxonomy%", $rewrite_tags, true ) ); - $this->assertFalse( array_search( "%$taxonomy%", $rewrite_tags_after, true ) ); + $this->assertContains( "%$taxonomy%", $rewrite_tags ); + $this->assertNotContains( "%$taxonomy%", $rewrite_tags_after ); } public function test_adds_ajax_callback() { diff --git a/tests/phpunit/tests/theme.php b/tests/phpunit/tests/theme.php index aa67f7189c64d..e3b16212650de 100644 --- a/tests/phpunit/tests/theme.php +++ b/tests/phpunit/tests/theme.php @@ -457,7 +457,7 @@ public function test_switch_theme() { $this->assertSame( $theme['Stylesheet'], get_stylesheet() ); $root_fs = $theme->get_theme_root(); - $this->assertTrue( is_dir( $root_fs ) ); + $this->assertDirectoryExists( $root_fs ); $root_uri = $theme->get_theme_root_uri(); $this->assertNotEmpty( $root_uri ); From 3e0f7ee02c120c0cf5c52998fd54acf9bcc45c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Tue, 14 Jul 2026 14:38:12 +0200 Subject: [PATCH 2/2] Tests: Keep `assertTrue( empty( ... ) )` for magic property and array key access. `assertEmpty()` evaluates its argument directly, triggering "Undefined property" / "Undefined array key" warnings that `empty()` suppresses. The `wpdb::$dbh` case aborted the test before the handle was restored, cascading into ~490 follow-up failures. --- tests/phpunit/tests/db.php | 6 +++--- tests/phpunit/tests/option/networkOption.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/db.php b/tests/phpunit/tests/db.php index 0a813f74b513e..22eeb235e4819 100644 --- a/tests/phpunit/tests/db.php +++ b/tests/phpunit/tests/db.php @@ -243,7 +243,7 @@ public function test_wpdb_supposedly_protected_properties() { $this->assertNotEmpty( $dbh ); $this->assertTrue( isset( $wpdb->dbh ) ); // Test __isset(). unset( $wpdb->dbh ); - $this->assertEmpty( $wpdb->dbh ); + $this->assertTrue( empty( $wpdb->dbh ) ); $wpdb->dbh = $dbh; $this->assertNotEmpty( $wpdb->dbh ); } @@ -269,12 +269,12 @@ public function test_wpdb_actually_protected_properties() { public function test_wpdb_nonexistent_properties() { global $wpdb; - $this->assertEmpty( $wpdb->nonexistent_property ); + $this->assertTrue( empty( $wpdb->nonexistent_property ) ); $wpdb->nonexistent_property = true; $this->assertTrue( $wpdb->nonexistent_property ); $this->assertTrue( isset( $wpdb->nonexistent_property ) ); unset( $wpdb->nonexistent_property ); - $this->assertEmpty( $wpdb->nonexistent_property ); + $this->assertTrue( empty( $wpdb->nonexistent_property ) ); } /** diff --git a/tests/phpunit/tests/option/networkOption.php b/tests/phpunit/tests/option/networkOption.php index ed36d12754e8d..b2cb99412ca38 100644 --- a/tests/phpunit/tests/option/networkOption.php +++ b/tests/phpunit/tests/option/networkOption.php @@ -76,7 +76,7 @@ public function test_check_delete_network_option_updates_notoptions() { if ( ! is_multisite() ) { $network_notoptions = wp_cache_get( '1:notoptions', 'site-options' ); - $this->assertEmpty( $network_notoptions['foo'], 'The deleted option is not expected to be in network notoptions on a non-multisite.' ); + $this->assertTrue( empty( $network_notoptions['foo'] ), 'The deleted option is not expected to be in network notoptions on a non-multisite.' ); } $before = get_num_queries();