Make WordPress Core


Ignore:
Timestamp:
08/04/2022 12:09:03 PM (2 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/cache.php

    r53767 r53821  
    2727    }
    2828
     29    /**
     30     * @ticket 56198
     31     *
     32     * @covers WP_Object_Cache::is_valid_key
     33     * @dataProvider data_is_valid_key
     34     */
     35    public function test_is_valid_key( $key, $valid ) {
     36        if ( wp_using_ext_object_cache() ) {
     37            $this->markTestSkipped( 'This test requires that an external object cache is not in use.' );
     38        }
     39
     40        $val = 'val';
     41
     42        if ( $valid ) {
     43            $this->assertTrue( $this->cache->add( $key, $val ), 'WP_Object_Cache:add() should return true for valid keys.' );
     44            $this->assertSame( $val, $this->cache->get( $key ), 'The retrieved value should match the added value.' );
     45        } else {
     46            $this->setExpectedIncorrectUsage( 'WP_Object_Cache::add' );
     47            $this->assertFalse( $this->cache->add( $key, $val ), 'WP_Object_Cache:add() should return false for invalid keys.' );
     48        }
     49    }
     50
     51    /**
     52     * Data provider for test_is_valid_key().
     53     *
     54     * @return array[] Test parameters {
     55     *     @type mixed $key   Cache key value.
     56     *     @type bool  $valid Whether the key should be considered valid.
     57     * }
     58     */
     59    public function data_is_valid_key() {
     60        return array(
     61            array( false, false ),
     62            array( null, false ),
     63            array( '', false ),
     64            array( 0, true ),
     65            array( 1, true ),
     66            array( '0', true ),
     67            array( 'key', true ),
     68        );
     69    }
     70
    2971    public function test_miss() {
    3072        $this->assertFalse( $this->cache->get( 'test_miss' ) );
     
    113155
    114156    public function test_flush() {
    115         global $_wp_using_ext_object_cache;
    116 
    117         if ( $_wp_using_ext_object_cache ) {
     157        if ( wp_using_ext_object_cache() ) {
    118158            $this->markTestSkipped( 'This test requires that an external object cache is not in use.' );
    119159        }
Note: See TracChangeset for help on using the changeset viewer.