| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * |
| | 5 | * Test various query vars and make sure the WP_Query class is caching correctly |
| | 6 | * |
| | 7 | * @ticket 22176 |
| | 8 | * @group query |
| | 9 | */ |
| | 10 | class Tests_Query_Cache extends WP_UnitTestCase { |
| | 11 | protected $q; |
| | 12 | |
| | 13 | static $cat_ids = array(); |
| | 14 | static $tag_ids = array(); |
| | 15 | static $post_ids = array(); |
| | 16 | |
| | 17 | static $parent_one; |
| | 18 | static $parent_two; |
| | 19 | static $parent_three; |
| | 20 | static $child_one; |
| | 21 | static $child_two; |
| | 22 | static $child_three; |
| | 23 | static $child_four; |
| | 24 | |
| | 25 | public static function wpSetUpBeforeClass( $factory ) { |
| | 26 | self::$cat_ids[] = $cat_a = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) ); |
| | 27 | self::$cat_ids[] = $cat_b = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) ); |
| | 28 | self::$cat_ids[] = $cat_c = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-c' ) ); |
| | 29 | |
| | 30 | self::$tag_ids[] = $tag_a = $factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'tag-a' ) ); |
| | 31 | self::$tag_ids[] = $tag_b = $factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'tag-b' ) ); |
| | 32 | self::$tag_ids[] = $tag_c = $factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'tag-c' ) ); |
| | 33 | self::$tag_ids[] = $tag_nun = $factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'tag-נ' ) ); |
| | 34 | |
| | 35 | self::$post_ids[] = $factory->post->create( array( |
| | 36 | 'post_title' => 'tag-נ', |
| | 37 | 'tags_input' => array( 'tag-נ' ), |
| | 38 | 'post_date' => '2008-11-01 00:00:00' |
| | 39 | ) ); |
| | 40 | self::$post_ids[] = $factory->post->create( array( |
| | 41 | 'post_title' => 'cats-a-b-c', |
| | 42 | 'post_date' => '2008-12-01 00:00:00', |
| | 43 | 'post_category' => array( $cat_a, $cat_b, $cat_c ) |
| | 44 | ) ); |
| | 45 | self::$post_ids[] = $factory->post->create( array( |
| | 46 | 'post_title' => 'cats-a-and-b', |
| | 47 | 'post_date' => '2009-01-01 00:00:00', |
| | 48 | 'post_category' => array( $cat_a, $cat_b ) |
| | 49 | ) ); |
| | 50 | self::$post_ids[] = $factory->post->create( array( |
| | 51 | 'post_title' => 'cats-b-and-c', |
| | 52 | 'post_date' => '2009-02-01 00:00:00', |
| | 53 | 'post_category' => array( $cat_b, $cat_c ) |
| | 54 | ) ); |
| | 55 | self::$post_ids[] = $factory->post->create( array( |
| | 56 | 'post_title' => 'cats-a-and-c', |
| | 57 | 'post_date' => '2009-03-01 00:00:00', |
| | 58 | 'post_category' => array( $cat_a, $cat_c ) |
| | 59 | ) ); |
| | 60 | self::$post_ids[] = $factory->post->create( array( |
| | 61 | 'post_title' => 'cat-a', |
| | 62 | 'post_date' => '2009-04-01 00:00:00', |
| | 63 | 'post_category' => array( $cat_a ) |
| | 64 | ) ); |
| | 65 | self::$post_ids[] = $factory->post->create( array( |
| | 66 | 'post_title' => 'cat-b', |
| | 67 | 'post_date' => '2009-05-01 00:00:00', |
| | 68 | 'post_category' => array( $cat_b ) |
| | 69 | ) ); |
| | 70 | self::$post_ids[] = $factory->post->create( array( |
| | 71 | 'post_title' => 'cat-c', |
| | 72 | 'post_date' => '2009-06-01 00:00:00', |
| | 73 | 'post_category' => array( $cat_c ) |
| | 74 | ) ); |
| | 75 | self::$post_ids[] = $factory->post->create( array( |
| | 76 | 'post_title' => 'lorem-ipsum', |
| | 77 | 'post_date' => '2009-07-01 00:00:00' |
| | 78 | ) ); |
| | 79 | self::$post_ids[] = $factory->post->create( array( |
| | 80 | 'post_title' => 'comment-test', |
| | 81 | 'post_date' => '2009-08-01 00:00:00' |
| | 82 | ) ); |
| | 83 | self::$post_ids[] = $factory->post->create( array( |
| | 84 | 'post_title' => 'one-trackback', |
| | 85 | 'post_date' => '2009-09-01 00:00:00' |
| | 86 | ) ); |
| | 87 | self::$post_ids[] = $factory->post->create( array( |
| | 88 | 'post_title' => 'many-trackbacks', |
| | 89 | 'post_date' => '2009-10-01 00:00:00' |
| | 90 | ) ); |
| | 91 | self::$post_ids[] = $factory->post->create( array( |
| | 92 | 'post_title' => 'no-comments', |
| | 93 | 'post_date' => '2009-10-01 00:00:00' |
| | 94 | ) ); |
| | 95 | self::$post_ids[] = $factory->post->create( array( |
| | 96 | 'post_title' => 'one-comment', |
| | 97 | 'post_date' => '2009-11-01 00:00:00' |
| | 98 | ) ); |
| | 99 | self::$post_ids[] = $factory->post->create( array( |
| | 100 | 'post_title' => 'contributor-post-approved', |
| | 101 | 'post_date' => '2009-12-01 00:00:00' |
| | 102 | ) ); |
| | 103 | self::$post_ids[] = $factory->post->create( array( |
| | 104 | 'post_title' => 'embedded-video', |
| | 105 | 'post_date' => '2010-01-01 00:00:00' |
| | 106 | ) ); |
| | 107 | self::$post_ids[] = $factory->post->create( array( |
| | 108 | 'post_title' => 'simple-markup-test', |
| | 109 | 'post_date' => '2010-02-01 00:00:00' |
| | 110 | ) ); |
| | 111 | self::$post_ids[] = $factory->post->create( array( |
| | 112 | 'post_title' => 'raw-html-code', |
| | 113 | 'post_date' => '2010-03-01 00:00:00' |
| | 114 | ) ); |
| | 115 | self::$post_ids[] = $factory->post->create( array( |
| | 116 | 'post_title' => 'tags-a-b-c', |
| | 117 | 'tags_input' => array( 'tag-a', 'tag-b', 'tag-c' ), |
| | 118 | 'post_date' => '2010-04-01 00:00:00' |
| | 119 | ) ); |
| | 120 | self::$post_ids[] = $factory->post->create( array( |
| | 121 | 'post_title' => 'tag-a', |
| | 122 | 'tags_input' => array( 'tag-a' ), |
| | 123 | 'post_date' => '2010-05-01 00:00:00' |
| | 124 | ) ); |
| | 125 | self::$post_ids[] = $factory->post->create( array( |
| | 126 | 'post_title' => 'tag-b', |
| | 127 | 'tags_input' => array( 'tag-b' ), |
| | 128 | 'post_date' => '2010-06-01 00:00:00' |
| | 129 | ) ); |
| | 130 | self::$post_ids[] = $factory->post->create( array( |
| | 131 | 'post_title' => 'tag-c', |
| | 132 | 'tags_input' => array( 'tag-c' ), |
| | 133 | 'post_date' => '2010-07-01 00:00:00' |
| | 134 | ) ); |
| | 135 | self::$post_ids[] = $factory->post->create( array( |
| | 136 | 'post_title' => 'tags-a-and-b', |
| | 137 | 'tags_input' => array( 'tag-a', 'tag-b' ), |
| | 138 | 'post_date' => '2010-08-01 00:00:00' |
| | 139 | ) ); |
| | 140 | self::$post_ids[] = $factory->post->create( array( |
| | 141 | 'post_title' => 'tags-b-and-c', |
| | 142 | 'tags_input' => array( 'tag-b', 'tag-c' ), |
| | 143 | 'post_date' => '2010-09-01 00:00:00' |
| | 144 | ) ); |
| | 145 | self::$post_ids[] = $factory->post->create( array( |
| | 146 | 'post_title' => 'tags-a-and-c', |
| | 147 | 'tags_input' => array( 'tag-a', 'tag-c' ), |
| | 148 | 'post_date' => '2010-10-01 00:00:00' |
| | 149 | ) ); |
| | 150 | |
| | 151 | self::$post_ids[] = self::$parent_one = $factory->post->create( array( |
| | 152 | 'post_title' => 'parent-one', |
| | 153 | 'post_date' => '2007-01-01 00:00:00' |
| | 154 | ) ); |
| | 155 | self::$post_ids[] = self::$parent_two = $factory->post->create( array( |
| | 156 | 'post_title' => 'parent-two', |
| | 157 | 'post_date' => '2007-01-01 00:00:00' |
| | 158 | ) ); |
| | 159 | self::$post_ids[] = self::$parent_three = $factory->post->create( array( |
| | 160 | 'post_title' => 'parent-three', |
| | 161 | 'post_date' => '2007-01-01 00:00:00' |
| | 162 | ) ); |
| | 163 | self::$post_ids[] = self::$child_one = $factory->post->create( array( |
| | 164 | 'post_title' => 'child-one', |
| | 165 | 'post_parent' => self::$parent_one, |
| | 166 | 'post_date' => '2007-01-01 00:00:01' |
| | 167 | ) ); |
| | 168 | self::$post_ids[] = self::$child_two = $factory->post->create( array( |
| | 169 | 'post_title' => 'child-two', |
| | 170 | 'post_parent' => self::$parent_one, |
| | 171 | 'post_date' => '2007-01-01 00:00:02' |
| | 172 | ) ); |
| | 173 | self::$post_ids[] = self::$child_three = $factory->post->create( array( |
| | 174 | 'post_title' => 'child-three', |
| | 175 | 'post_parent' => self::$parent_two, |
| | 176 | 'post_date' => '2007-01-01 00:00:03' |
| | 177 | ) ); |
| | 178 | self::$post_ids[] = self::$child_four = $factory->post->create( array( |
| | 179 | 'post_title' => 'child-four', |
| | 180 | 'post_parent' => self::$parent_two, |
| | 181 | 'post_date' => '2007-01-01 00:00:04' |
| | 182 | ) ); |
| | 183 | } |
| | 184 | |
| | 185 | function setUp() { |
| | 186 | parent::setUp(); |
| | 187 | |
| | 188 | unset( $this->q ); |
| | 189 | $this->q = new WP_Query(); |
| | 190 | } |
| | 191 | |
| | 192 | function test_caching_no_more_queries_same_query() { |
| | 193 | global $wpdb; |
| | 194 | |
| | 195 | $posts1 = $this->q->query( array() ); |
| | 196 | $number_of_queries = $wpdb->num_queries; |
| | 197 | $posts2 = $this->q->query( array() ); |
| | 198 | $this->assertSame( $wpdb->num_queries, $number_of_queries ); |
| | 199 | $this->assertEquals( $posts1, $posts2 ); |
| | 200 | |
| | 201 | } |
| | 202 | |
| | 203 | function test_caching_no_more_queries_parent_id_query() { |
| | 204 | global $wpdb; |
| | 205 | |
| | 206 | $posts1 = $this->q->query( array( 'fields' => 'all' ) ); |
| | 207 | $number_of_queries = $wpdb->num_queries; |
| | 208 | $posts2 = $this->q->query( array( 'fields' => 'id=>parent' ) ); |
| | 209 | $this->assertSame( $wpdb->num_queries, $number_of_queries ); |
| | 210 | $this->assertNotEquals( $posts1, $posts2 ); |
| | 211 | } |
| | 212 | |
| | 213 | function test_caching_no_more_queries_id_query() { |
| | 214 | global $wpdb; |
| | 215 | |
| | 216 | $posts1 = $this->q->query( array( 'fields' => 'all' ) ); |
| | 217 | $number_of_queries = $wpdb->num_queries; |
| | 218 | $posts2 = $this->q->query( array( 'fields' => 'ids' ) ); |
| | 219 | $this->assertNotSame( $wpdb->num_queries, $number_of_queries ); |
| | 220 | $this->assertNotEquals( $posts1, $posts2 ); |
| | 221 | } |
| | 222 | |
| | 223 | function test_caching_invalidation_queries_same_query() { |
| | 224 | global $wpdb; |
| | 225 | |
| | 226 | $posts1 = $this->q->query( array() ); |
| | 227 | self::factory()->post->create(); |
| | 228 | $number_of_queries = $wpdb->num_queries; |
| | 229 | $posts2 = $this->q->query( array() ); |
| | 230 | $this->assertNotSame( $wpdb->num_queries, $number_of_queries ); |
| | 231 | $this->assertNotEquals( $posts1, $posts2 ); |
| | 232 | |
| | 233 | } |
| | 234 | |
| | 235 | function test_caching_invalidation_term_queries_same_query() { |
| | 236 | global $wpdb; |
| | 237 | |
| | 238 | $posts1 = $this->q->query( array() ); |
| | 239 | self::factory()->term->create(); |
| | 240 | $number_of_queries = $wpdb->num_queries; |
| | 241 | $posts2 = $this->q->query( array() ); |
| | 242 | $this->assertNotSame( $wpdb->num_queries, $number_of_queries ); |
| | 243 | $this->assertEquals( $posts1, $posts2 ); |
| | 244 | |
| | 245 | } |
| | 246 | |
| | 247 | public function test_tax_query_single_query_single_term_field_slug() { |
| | 248 | global $wpdb; |
| | 249 | $t = self::factory()->term->create( array( |
| | 250 | 'taxonomy' => 'category', |
| | 251 | 'slug' => 'foo', |
| | 252 | 'name' => 'Foo', |
| | 253 | ) ); |
| | 254 | $p1 = self::factory()->post->create(); |
| | 255 | $p2 = self::factory()->post->create(); |
| | 256 | |
| | 257 | $posts1 = new WP_Query( array( |
| | 258 | 'fields' => 'ids', |
| | 259 | 'update_post_meta_cache' => false, |
| | 260 | 'update_post_term_cache' => false, |
| | 261 | 'tax_query' => array( |
| | 262 | array( |
| | 263 | 'taxonomy' => 'category', |
| | 264 | 'terms' => array( 'foo' ), |
| | 265 | 'field' => 'slug', |
| | 266 | ), |
| | 267 | ), |
| | 268 | ) ); |
| | 269 | |
| | 270 | wp_set_post_terms( $p1, $t, 'category' ); |
| | 271 | $number_of_queries = $wpdb->num_queries; |
| | 272 | $posts2 = new WP_Query( array( |
| | 273 | 'fields' => 'ids', |
| | 274 | 'update_post_meta_cache' => false, |
| | 275 | 'update_post_term_cache' => false, |
| | 276 | 'tax_query' => array( |
| | 277 | array( |
| | 278 | 'taxonomy' => 'category', |
| | 279 | 'terms' => array( 'foo' ), |
| | 280 | 'field' => 'slug', |
| | 281 | ), |
| | 282 | ), |
| | 283 | ) ); |
| | 284 | |
| | 285 | $this->assertEquals( array( $p1 ), $posts2->posts ); |
| | 286 | $this->assertNotEquals( $posts1->posts, $posts2->posts ); |
| | 287 | $this->assertNotSame( $wpdb->num_queries, $number_of_queries ); |
| | 288 | } |
| | 289 | |
| | 290 | function test_disable_caching_query() { |
| | 291 | global $wpdb; |
| | 292 | |
| | 293 | $posts1 = $this->q->query( array( 'cache_results' => false ) ); |
| | 294 | $number_of_queries = $wpdb->num_queries; |
| | 295 | $posts2 = $this->q->query( array( 'cache_results' => false ) ); |
| | 296 | $this->assertNotSame( $wpdb->num_queries, $number_of_queries ); |
| | 297 | $this->assertEquals( $posts1, $posts2 ); |
| | 298 | } |
| | 299 | |
| | 300 | public function test_meta_query_single_query_compare_default() { |
| | 301 | global $wpdb; |
| | 302 | $p1 = self::factory()->post->create(); |
| | 303 | $p2 = self::factory()->post->create(); |
| | 304 | |
| | 305 | $query1 = new WP_Query( array( |
| | 306 | 'update_post_meta_cache' => false, |
| | 307 | 'update_post_term_cache' => false, |
| | 308 | 'fields' => 'ids', |
| | 309 | 'meta_query' => array( |
| | 310 | array( |
| | 311 | 'key' => 'foo', |
| | 312 | 'value' => 'bar', |
| | 313 | ), |
| | 314 | ), |
| | 315 | ) ); |
| | 316 | |
| | 317 | add_post_meta( $p1, 'foo', 'bar' ); |
| | 318 | $number_of_queries = $wpdb->num_queries; |
| | 319 | $query2 = new WP_Query( array( |
| | 320 | 'update_post_meta_cache' => false, |
| | 321 | 'update_post_term_cache' => false, |
| | 322 | 'fields' => 'ids', |
| | 323 | 'meta_query' => array( |
| | 324 | array( |
| | 325 | 'key' => 'foo', |
| | 326 | 'value' => 'bar', |
| | 327 | ), |
| | 328 | ), |
| | 329 | ) ); |
| | 330 | |
| | 331 | $expected = array( $p1 ); |
| | 332 | $this->assertEqualSets( $expected, $query2->posts ); |
| | 333 | $this->assertNotSame( $wpdb->num_queries, $number_of_queries ); |
| | 334 | } |
| | 335 | } |