1 | <?php |
---|
2 | |
---|
3 | namespace FeedsByTag\test; |
---|
4 | |
---|
5 | class TermObjectCache extends \WP_UnitTestCase { |
---|
6 | |
---|
7 | public function testObjectTermCache() { |
---|
8 | $post_factory = new \WP_UnitTest_Factory_For_Post( $this ); |
---|
9 | $term_factory = new \WP_UnitTest_Factory_For_Term( $this ); |
---|
10 | |
---|
11 | register_post_type( 'sandwich', array( 'public' => true ) ); |
---|
12 | register_taxonomy( 'condiment', array( 'sandwich' ), array( 'label' => 'Condiments' ) ); |
---|
13 | |
---|
14 | $sauerkraut = $term_factory->create([ |
---|
15 | 'taxonomy' => 'condiment', |
---|
16 | 'name' => 'Sauerkraut' |
---|
17 | ]); |
---|
18 | |
---|
19 | $reuben = $post_factory->create([ |
---|
20 | 'post_title' => 'Reuben', |
---|
21 | 'post_type' => 'sandwich', |
---|
22 | ]); |
---|
23 | |
---|
24 | wp_set_object_terms( $reuben, array( $sauerkraut ), 'condiment' ); |
---|
25 | |
---|
26 | add_filter( 'get_term', array( $this, 'noTermsForGuests' ), 10, 2 ); |
---|
27 | |
---|
28 | $sandwiches = get_posts( [ 'post_type' => 'sandwich', 'update_post_term_cache' => false ] ); |
---|
29 | $this->assertCount( 1, $sandwiches ); // This is fine |
---|
30 | |
---|
31 | $sandwiches = get_posts( [ 'post_type' => 'sandwich', 'update_post_term_cache' => true ] ); |
---|
32 | $this->assertCount( 1, $sandwiches ); // This throws an |
---|
33 | |
---|
34 | remove_filter( 'get_term', array( $this, 'noTermsForGuests' ) ); |
---|
35 | |
---|
36 | |
---|
37 | } |
---|
38 | |
---|
39 | public function noTermsForGuests( $term, $taxonomy ) { |
---|
40 | if ( ! is_user_logged_in() ) { |
---|
41 | $term = null; |
---|
42 | } |
---|
43 | return $term; |
---|
44 | } |
---|
45 | |
---|
46 | } |
---|