From 282727a8ff9df986cbb151411d8085647cb1cbb7 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 7 Aug 2025 09:03:02 -0300 Subject: [PATCH] DB/DirectDatabaseQuery: add tests for namespaced names --- .../DB/DirectDatabaseQueryUnitTest.1.inc | 98 ++++++++++++++++++- .../Tests/DB/DirectDatabaseQueryUnitTest.php | 10 ++ 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc b/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc index 1a6be76a5a..517cf56476 100644 --- a/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc +++ b/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc @@ -93,7 +93,7 @@ function cache_add_instead_of_set() { $b = function () { global $wpdb; - if ( ! ( $listofthings = wp_cache_get( $foo ) ) ) { + if ( ! ( $listofthings = \Wp_Cache_Get( $foo ) ) ) { $listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning. wp_cache_set( 'foo', $listofthings ); } @@ -379,3 +379,99 @@ function methodNamesSameAsCacheFunctions() { return $listofthings; } + +/* + * Safeguard correct handling of namespaced function calls. The sniff deliberately does not distinguish between calls to + * WP global cache functions and calls to namespaced functions that mirror the name of the WP global cache functions as + * those are likely custom cache functions. This is consistent with the behavior for method calls (see the + * methodNamesSameAsCacheFunctions() test above). + */ +function callToFullyQualifiedCacheGetSet() { + global $wpdb; + + if ( ! ( $listofthings = \wp_cache_get( $foo ) ) ) { + $listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call. + \wp_cache_set( 'foo', $listofthings ); + } + + return $listofthings; +} + +function callToQualifiedNamespacedCacheGetSet() { + global $wpdb; + + if ( ! ( $listofthings = MyNamespace\wp_cache_get( $foo ) ) ) { + $listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call. + MyNamespace\wp_cache_add( 'foo', $listofthings ); + } + + return $listofthings; +} + +function callToFullyQualifiedNamespacedCacheGetSet() { + global $wpdb; + + if ( ! ( $listofthings = \MyNamespace\wp_cache_get( $foo ) ) ) { + $listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call. + \MyNamespace\wp_cache_set( 'foo', $listofthings ); + } + + return $listofthings; +} + +function callToRelativeNamespacedCacheGetSet() { + global $wpdb; + + if ( ! ( $listofthings = namespace\wp_cache_get( $foo ) ) ) { + $listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call. + namespace\wp_cache_add( 'foo', $listofthings ); + } + + return $listofthings; +} + +function callToMultiLevelNamespaceRelativeCacheGetSet() { + global $wpdb; + + if ( ! ( $listofthings = namespace\Sub\wp_cache_get( $foo ) ) ) { + $listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call. + namespace\Sub\wp_cache_set( 'foo', $listofthings ); + } + + return $listofthings; +} + +function callToFullyQualifiedCacheDelete() { + global $wpdb; + + $wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call. + \wp_cache_delete( 'key', 'group' ); +} + +function callToQualifiedNamespacedCacheDelete() { + global $wpdb; + + $wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call. + MyNamespace\clean_attachment_cache( 1 ); +} + +function callToFullyQualifiedNamespacedCacheDelete() { + global $wpdb; + + $wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call. + \MyNamespace\CLEAN_POST_CACHE( 1 ); +} + +function callToRelativeNamespacedCacheDelete() { + global $wpdb; + + $wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call. + namespace\clean_user_cache( 1 ); +} + +function callToMultiLevelNamespaceRelativeCacheDelete() { + global $wpdb; + + $wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call. + namespace\Sub\clean_blog_cache( 1 ); +} diff --git a/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php b/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php index e43fbb126b..709d66c0e6 100644 --- a/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php +++ b/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php @@ -99,6 +99,16 @@ public function getWarningList( $testFile = '' ) { 350 => 1, 364 => 2, 376 => 1, + 393 => 1, + 404 => 1, + 415 => 1, + 426 => 1, + 437 => 1, + 447 => 1, + 454 => 1, + 461 => 1, + 468 => 1, + 475 => 1, ); default: return array();