Skip to content

Commit 282727a

Browse files
committed
DB/DirectDatabaseQuery: add tests for namespaced names
1 parent 93c8284 commit 282727a

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function cache_add_instead_of_set() {
9393
$b = function () {
9494
global $wpdb;
9595

96-
if ( ! ( $listofthings = wp_cache_get( $foo ) ) ) {
96+
if ( ! ( $listofthings = \Wp_Cache_Get( $foo ) ) ) {
9797
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning.
9898
wp_cache_set( 'foo', $listofthings );
9999
}
@@ -379,3 +379,99 @@ function methodNamesSameAsCacheFunctions() {
379379

380380
return $listofthings;
381381
}
382+
383+
/*
384+
* Safeguard correct handling of namespaced function calls. The sniff deliberately does not distinguish between calls to
385+
* WP global cache functions and calls to namespaced functions that mirror the name of the WP global cache functions as
386+
* those are likely custom cache functions. This is consistent with the behavior for method calls (see the
387+
* methodNamesSameAsCacheFunctions() test above).
388+
*/
389+
function callToFullyQualifiedCacheGetSet() {
390+
global $wpdb;
391+
392+
if ( ! ( $listofthings = \wp_cache_get( $foo ) ) ) {
393+
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call.
394+
\wp_cache_set( 'foo', $listofthings );
395+
}
396+
397+
return $listofthings;
398+
}
399+
400+
function callToQualifiedNamespacedCacheGetSet() {
401+
global $wpdb;
402+
403+
if ( ! ( $listofthings = MyNamespace\wp_cache_get( $foo ) ) ) {
404+
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call.
405+
MyNamespace\wp_cache_add( 'foo', $listofthings );
406+
}
407+
408+
return $listofthings;
409+
}
410+
411+
function callToFullyQualifiedNamespacedCacheGetSet() {
412+
global $wpdb;
413+
414+
if ( ! ( $listofthings = \MyNamespace\wp_cache_get( $foo ) ) ) {
415+
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call.
416+
\MyNamespace\wp_cache_set( 'foo', $listofthings );
417+
}
418+
419+
return $listofthings;
420+
}
421+
422+
function callToRelativeNamespacedCacheGetSet() {
423+
global $wpdb;
424+
425+
if ( ! ( $listofthings = namespace\wp_cache_get( $foo ) ) ) {
426+
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call.
427+
namespace\wp_cache_add( 'foo', $listofthings );
428+
}
429+
430+
return $listofthings;
431+
}
432+
433+
function callToMultiLevelNamespaceRelativeCacheGetSet() {
434+
global $wpdb;
435+
436+
if ( ! ( $listofthings = namespace\Sub\wp_cache_get( $foo ) ) ) {
437+
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call.
438+
namespace\Sub\wp_cache_set( 'foo', $listofthings );
439+
}
440+
441+
return $listofthings;
442+
}
443+
444+
function callToFullyQualifiedCacheDelete() {
445+
global $wpdb;
446+
447+
$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
448+
\wp_cache_delete( 'key', 'group' );
449+
}
450+
451+
function callToQualifiedNamespacedCacheDelete() {
452+
global $wpdb;
453+
454+
$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
455+
MyNamespace\clean_attachment_cache( 1 );
456+
}
457+
458+
function callToFullyQualifiedNamespacedCacheDelete() {
459+
global $wpdb;
460+
461+
$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
462+
\MyNamespace\CLEAN_POST_CACHE( 1 );
463+
}
464+
465+
function callToRelativeNamespacedCacheDelete() {
466+
global $wpdb;
467+
468+
$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
469+
namespace\clean_user_cache( 1 );
470+
}
471+
472+
function callToMultiLevelNamespaceRelativeCacheDelete() {
473+
global $wpdb;
474+
475+
$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
476+
namespace\Sub\clean_blog_cache( 1 );
477+
}

WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ public function getWarningList( $testFile = '' ) {
9999
350 => 1,
100100
364 => 2,
101101
376 => 1,
102+
393 => 1,
103+
404 => 1,
104+
415 => 1,
105+
426 => 1,
106+
437 => 1,
107+
447 => 1,
108+
454 => 1,
109+
461 => 1,
110+
468 => 1,
111+
475 => 1,
102112
);
103113
default:
104114
return array();

0 commit comments

Comments
 (0)