| 194 | |
| 195 | /** |
| 196 | * @ticket 40306 |
| 197 | */ |
| 198 | public function test_term_cache_should_be_invalidated_on_set_object_terms() { |
| 199 | register_taxonomy( 'wptests_tax', 'post' ); |
| 200 | |
| 201 | // Temporarily disable term counting, which performs its own cache invalidation. |
| 202 | wp_defer_term_counting( true ); |
| 203 | |
| 204 | // Create Test Category. |
| 205 | $term_id = self::factory()->term->create( array( |
| 206 | 'taxonomy' => 'wptests_tax', |
| 207 | ) ); |
| 208 | |
| 209 | $post_id = self::factory()->post->create(); |
| 210 | |
| 211 | // Prime cache. |
| 212 | get_the_terms( $post_id, 'wptests_tax' ); |
| 213 | |
| 214 | wp_set_object_terms( $post_id, $term_id, 'wptests_tax' ); |
| 215 | |
| 216 | $terms = get_the_terms( $post_id, 'wptests_tax' ); |
| 217 | |
| 218 | // Re-activate term counting so this doesn't affect other tests. |
| 219 | wp_defer_term_counting( false ); |
| 220 | |
| 221 | $this->assertTrue( is_array( $terms ) ); |
| 222 | $this->assertSame( array( $term_id ), wp_list_pluck( $terms, 'term_id' ) ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * @ticket 40306 |
| 227 | */ |
| 228 | public function test_term_cache_should_be_invalidated_on_remove_object_terms() { |
| 229 | register_taxonomy( 'wptests_tax', 'post' ); |
| 230 | |
| 231 | // Create Test Category. |
| 232 | $term_ids = self::factory()->term->create_many( 2, array( |
| 233 | 'taxonomy' => 'wptests_tax', |
| 234 | ) ); |
| 235 | |
| 236 | $post_id = self::factory()->post->create(); |
| 237 | |
| 238 | wp_set_object_terms( $post_id, $term_ids, 'wptests_tax' ); |
| 239 | |
| 240 | // Prime cache. |
| 241 | get_the_terms( $post_id, 'wptests_tax' ); |
| 242 | |
| 243 | // Temporarily disable term counting, which performs its own cache invalidation. |
| 244 | wp_defer_term_counting( true ); |
| 245 | |
| 246 | wp_remove_object_terms( $post_id, $term_ids[0], 'wptests_tax' ); |
| 247 | |
| 248 | $terms = get_the_terms( $post_id, 'wptests_tax' ); |
| 249 | |
| 250 | // Re-activate term counting so this doesn't affect other tests. |
| 251 | wp_defer_term_counting( false ); |
| 252 | |
| 253 | $this->assertTrue( is_array( $terms ) ); |
| 254 | $this->assertSame( array( $term_ids[1] ), wp_list_pluck( $terms, 'term_id' ) ); |
| 255 | } |