Skip to content
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
98 changes: 97 additions & 1 deletion WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 );
}
10 changes: 10 additions & 0 deletions WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading