|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the `invalidates_query_cache` parameter of `register_meta()`. |
| 4 | + * |
| 5 | + * @group meta |
| 6 | + * @group cache |
| 7 | + * |
| 8 | + * @ticket 64696 |
| 9 | + */ |
| 10 | +class Tests_Meta_InvalidatesQueryCache extends WP_UnitTestCase { |
| 11 | + |
| 12 | + protected static $post_id; |
| 13 | + |
| 14 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 15 | + self::$post_id = $factory->post->create(); |
| 16 | + } |
| 17 | + |
| 18 | + public static function wpTearDownAfterClass() { |
| 19 | + wp_delete_post( self::$post_id, true ); |
| 20 | + } |
| 21 | + |
| 22 | + public function tear_down() { |
| 23 | + unregister_meta_key( 'post', 'nocache_meta' ); |
| 24 | + unregister_meta_key( 'post', 'normal_meta' ); |
| 25 | + parent::tear_down(); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * The `invalidates_query_cache` argument should default to true. |
| 30 | + */ |
| 31 | + public function test_default_value_is_true() { |
| 32 | + register_post_meta( '', 'normal_meta', array() ); |
| 33 | + |
| 34 | + $meta_keys = get_registered_meta_keys( 'post' ); |
| 35 | + $this->assertTrue( $meta_keys['normal_meta']['invalidates_query_cache'] ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * The `invalidates_query_cache` argument should be stored when set to false. |
| 40 | + */ |
| 41 | + public function test_registered_as_false() { |
| 42 | + register_post_meta( |
| 43 | + '', |
| 44 | + 'nocache_meta', |
| 45 | + array( 'invalidates_query_cache' => false ) |
| 46 | + ); |
| 47 | + |
| 48 | + $meta_keys = get_registered_meta_keys( 'post' ); |
| 49 | + $this->assertFalse( $meta_keys['nocache_meta']['invalidates_query_cache'] ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Adding post meta for a non-cacheable key should not bump last_changed. |
| 54 | + */ |
| 55 | + public function test_add_post_meta_does_not_invalidate_cache() { |
| 56 | + register_post_meta( |
| 57 | + '', |
| 58 | + 'nocache_meta', |
| 59 | + array( 'invalidates_query_cache' => false ) |
| 60 | + ); |
| 61 | + |
| 62 | + // Prime the last_changed value. |
| 63 | + wp_cache_set_last_changed( 'posts' ); |
| 64 | + $before = wp_cache_get_last_changed( 'posts' ); |
| 65 | + |
| 66 | + usleep( 1000 ); |
| 67 | + add_post_meta( self::$post_id, 'nocache_meta', 'value1' ); |
| 68 | + |
| 69 | + $after = wp_cache_get_last_changed( 'posts' ); |
| 70 | + $this->assertSame( $before, $after, 'last_changed should not change for non-cacheable meta.' ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Updating post meta for a non-cacheable key should not bump last_changed. |
| 75 | + */ |
| 76 | + public function test_update_post_meta_does_not_invalidate_cache() { |
| 77 | + register_post_meta( |
| 78 | + '', |
| 79 | + 'nocache_meta', |
| 80 | + array( 'invalidates_query_cache' => false ) |
| 81 | + ); |
| 82 | + |
| 83 | + add_post_meta( self::$post_id, 'nocache_meta', 'value1' ); |
| 84 | + |
| 85 | + wp_cache_set_last_changed( 'posts' ); |
| 86 | + $before = wp_cache_get_last_changed( 'posts' ); |
| 87 | + |
| 88 | + usleep( 1000 ); |
| 89 | + update_post_meta( self::$post_id, 'nocache_meta', 'value2' ); |
| 90 | + |
| 91 | + $after = wp_cache_get_last_changed( 'posts' ); |
| 92 | + $this->assertSame( $before, $after, 'last_changed should not change for non-cacheable meta.' ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Deleting post meta for a non-cacheable key should not bump last_changed. |
| 97 | + */ |
| 98 | + public function test_delete_post_meta_does_not_invalidate_cache() { |
| 99 | + register_post_meta( |
| 100 | + '', |
| 101 | + 'nocache_meta', |
| 102 | + array( 'invalidates_query_cache' => false ) |
| 103 | + ); |
| 104 | + |
| 105 | + add_post_meta( self::$post_id, 'nocache_meta', 'value1' ); |
| 106 | + |
| 107 | + wp_cache_set_last_changed( 'posts' ); |
| 108 | + $before = wp_cache_get_last_changed( 'posts' ); |
| 109 | + |
| 110 | + usleep( 1000 ); |
| 111 | + delete_post_meta( self::$post_id, 'nocache_meta' ); |
| 112 | + |
| 113 | + $after = wp_cache_get_last_changed( 'posts' ); |
| 114 | + $this->assertSame( $before, $after, 'last_changed should not change for non-cacheable meta.' ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Regular meta should still invalidate the cache as before. |
| 119 | + */ |
| 120 | + public function test_regular_meta_still_invalidates_cache() { |
| 121 | + wp_cache_set_last_changed( 'posts' ); |
| 122 | + $before = wp_cache_get_last_changed( 'posts' ); |
| 123 | + |
| 124 | + // Small sleep to ensure microtime differs. |
| 125 | + usleep( 1000 ); |
| 126 | + add_post_meta( self::$post_id, 'regular_unregistered_meta', 'value1' ); |
| 127 | + |
| 128 | + $after = wp_cache_get_last_changed( 'posts' ); |
| 129 | + $this->assertNotSame( $before, $after, 'last_changed should change for regular meta.' ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Meta registered with invalidates_query_cache true should still invalidate. |
| 134 | + */ |
| 135 | + public function test_registered_cacheable_meta_still_invalidates_cache() { |
| 136 | + register_post_meta( |
| 137 | + '', |
| 138 | + 'normal_meta', |
| 139 | + array( 'invalidates_query_cache' => true ) |
| 140 | + ); |
| 141 | + |
| 142 | + wp_cache_set_last_changed( 'posts' ); |
| 143 | + $before = wp_cache_get_last_changed( 'posts' ); |
| 144 | + |
| 145 | + usleep( 1000 ); |
| 146 | + add_post_meta( self::$post_id, 'normal_meta', 'value1' ); |
| 147 | + |
| 148 | + $after = wp_cache_get_last_changed( 'posts' ); |
| 149 | + $this->assertNotSame( $before, $after, 'last_changed should change for cacheable meta.' ); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * WP_Meta_Query should refuse to query by a non-cacheable meta key. |
| 154 | + * |
| 155 | + * @expectedIncorrectUsage WP_Meta_Query::get_sql_for_clause |
| 156 | + */ |
| 157 | + public function test_meta_query_refuses_non_cacheable_key() { |
| 158 | + register_post_meta( |
| 159 | + '', |
| 160 | + 'nocache_meta', |
| 161 | + array( 'invalidates_query_cache' => false ) |
| 162 | + ); |
| 163 | + |
| 164 | + $meta_query = new WP_Meta_Query( |
| 165 | + array( |
| 166 | + array( |
| 167 | + 'key' => 'nocache_meta', |
| 168 | + 'value' => 'test', |
| 169 | + ), |
| 170 | + ) |
| 171 | + ); |
| 172 | + |
| 173 | + $sql = $meta_query->get_sql( 'post', 'wp_posts', 'ID' ); |
| 174 | + |
| 175 | + $this->assertStringNotContainsString( 'nocache_meta', $sql['where'], 'Non-cacheable meta key should not appear in WHERE clause.' ); |
| 176 | + $this->assertStringNotContainsString( 'nocache_meta', $sql['join'], 'Non-cacheable meta key should not appear in JOIN clause.' ); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * WP_Meta_Query should work normally for regular meta keys. |
| 181 | + */ |
| 182 | + public function test_meta_query_allows_regular_key() { |
| 183 | + $meta_query = new WP_Meta_Query( |
| 184 | + array( |
| 185 | + array( |
| 186 | + 'key' => 'some_regular_key', |
| 187 | + 'value' => 'test', |
| 188 | + ), |
| 189 | + ) |
| 190 | + ); |
| 191 | + |
| 192 | + $sql = $meta_query->get_sql( 'post', 'wp_posts', 'ID' ); |
| 193 | + |
| 194 | + $this->assertStringContainsString( 'some_regular_key', $sql['where'], 'Regular meta key should appear in WHERE clause.' ); |
| 195 | + } |
| 196 | +} |
0 commit comments