From a164d642759a6673ad832cdc3330856bb4084be8 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 4 Aug 2022 12:09:03 +0000 Subject: [PATCH] Tests: Add a unit test for `WP_Object_Cache::is_valid_key()`. A valid cache key for `wp_cache_*()` functions must be either an integer number or a non-empty string. Follow-up to [53818]. See #56198. git-svn-id: https://develop.svn.wordpress.org/trunk@53821 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/cache.php | 46 ++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/cache.php b/tests/phpunit/tests/cache.php index ac37ddc734..5da134f72b 100644 --- a/tests/phpunit/tests/cache.php +++ b/tests/phpunit/tests/cache.php @@ -26,6 +26,48 @@ class Tests_Cache extends WP_UnitTestCase { return $cache; } + /** + * @ticket 56198 + * + * @covers WP_Object_Cache::is_valid_key + * @dataProvider data_is_valid_key + */ + public function test_is_valid_key( $key, $valid ) { + if ( wp_using_ext_object_cache() ) { + $this->markTestSkipped( 'This test requires that an external object cache is not in use.' ); + } + + $val = 'val'; + + if ( $valid ) { + $this->assertTrue( $this->cache->add( $key, $val ), 'WP_Object_Cache:add() should return true for valid keys.' ); + $this->assertSame( $val, $this->cache->get( $key ), 'The retrieved value should match the added value.' ); + } else { + $this->setExpectedIncorrectUsage( 'WP_Object_Cache::add' ); + $this->assertFalse( $this->cache->add( $key, $val ), 'WP_Object_Cache:add() should return false for invalid keys.' ); + } + } + + /** + * Data provider for test_is_valid_key(). + * + * @return array[] Test parameters { + * @type mixed $key Cache key value. + * @type bool $valid Whether the key should be considered valid. + * } + */ + public function data_is_valid_key() { + return array( + array( false, false ), + array( null, false ), + array( '', false ), + array( 0, true ), + array( 1, true ), + array( '0', true ), + array( 'key', true ), + ); + } + public function test_miss() { $this->assertFalse( $this->cache->get( 'test_miss' ) ); } @@ -112,9 +154,7 @@ class Tests_Cache extends WP_UnitTestCase { } public function test_flush() { - global $_wp_using_ext_object_cache; - - if ( $_wp_using_ext_object_cache ) { + if ( wp_using_ext_object_cache() ) { $this->markTestSkipped( 'This test requires that an external object cache is not in use.' ); }