|
6 | 6 | * @covers WP_Comment::get_instance |
7 | 7 | */ |
8 | 8 | class Tests_Comment_WpComment extends WP_UnitTestCase { |
9 | | - protected static $comment_id; |
| 9 | + protected static int $comment_id; |
10 | 10 |
|
11 | 11 | public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
12 | 12 | global $wpdb; |
@@ -63,6 +63,78 @@ public function test_get_instance_should_succeed_for_float_that_is_equal_to_post |
63 | 63 | $this->assertSame( '1', $found->comment_ID ); |
64 | 64 | } |
65 | 65 |
|
| 66 | + /** |
| 67 | + * Tests that a cached value which cannot be used as a comment is treated as a cache miss. |
| 68 | + * |
| 69 | + * @ticket 65962 |
| 70 | + * |
| 71 | + * @dataProvider data_get_instance_treats_a_poisoned_cache_value_as_a_cache_miss |
| 72 | + * |
| 73 | + * @param mixed $cache_value Value to poison the object cache with. |
| 74 | + */ |
| 75 | + public function test_get_instance_treats_a_poisoned_cache_value_as_a_cache_miss( $cache_value ): void { |
| 76 | + wp_cache_set( self::$comment_id, $cache_value, 'comment' ); |
| 77 | + |
| 78 | + $num_queries = get_num_queries(); |
| 79 | + |
| 80 | + $comment = WP_Comment::get_instance( self::$comment_id ); |
| 81 | + |
| 82 | + $this->assertInstanceOf( WP_Comment::class, $comment, 'A comment object was not returned.' ); |
| 83 | + $this->assertSame( (string) self::$comment_id, $comment->comment_ID, 'The wrong comment was returned.' ); |
| 84 | + $this->assertSame( $num_queries + 1, get_num_queries(), 'The comment was not fetched from the database.' ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Tests that the refetched comment replaces the poisoned cache value. |
| 89 | + * |
| 90 | + * Otherwise the poisoned value survives and every subsequent lookup queries the database again. |
| 91 | + * |
| 92 | + * @ticket 65962 |
| 93 | + * |
| 94 | + * @dataProvider data_get_instance_treats_a_poisoned_cache_value_as_a_cache_miss |
| 95 | + * |
| 96 | + * @param mixed $cache_value Value to poison the object cache with. |
| 97 | + */ |
| 98 | + public function test_get_instance_replaces_a_poisoned_cache_value( $cache_value ): void { |
| 99 | + wp_cache_set( self::$comment_id, $cache_value, 'comment' ); |
| 100 | + |
| 101 | + // Prime the object cache, replacing the poisoned value. |
| 102 | + WP_Comment::get_instance( self::$comment_id ); |
| 103 | + |
| 104 | + $num_queries = get_num_queries(); |
| 105 | + |
| 106 | + $comment = WP_Comment::get_instance( self::$comment_id ); |
| 107 | + |
| 108 | + $this->assertInstanceOf( WP_Comment::class, $comment, 'A comment object was not returned.' ); |
| 109 | + $this->assertSame( (string) self::$comment_id, $comment->comment_ID, 'The wrong comment was returned.' ); |
| 110 | + $this->assertSame( $num_queries, get_num_queries(), 'The database was queried again.' ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Data provider. |
| 115 | + * |
| 116 | + * @return array<non-falsy-string, array{ mixed }> |
| 117 | + */ |
| 118 | + public function data_get_instance_treats_a_poisoned_cache_value_as_a_cache_miss(): array { |
| 119 | + return array( |
| 120 | + 'true' => array( true ), |
| 121 | + 'a non-numeric string' => array( 'not-a-comment' ), |
| 122 | + 'an empty array' => array( array() ), |
| 123 | + 'an array of comment data' => array( |
| 124 | + array( |
| 125 | + 'comment_ID' => '1', |
| 126 | + 'comment_content' => 'Hello world.', |
| 127 | + ), |
| 128 | + ), |
| 129 | + 'an object without comment_ID' => array( |
| 130 | + (object) array( |
| 131 | + 'comment_content' => 'Hello world.', |
| 132 | + ), |
| 133 | + ), |
| 134 | + 'a WP_Comment without comment_ID' => array( new WP_Comment( new stdClass() ) ), |
| 135 | + ); |
| 136 | + } |
| 137 | + |
66 | 138 | /** |
67 | 139 | * @ticket 64898 |
68 | 140 | * |
|
0 commit comments