| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Class Tests_Term_wpGetTermCountForObjectType |
| | 5 | * |
| | 6 | * @group taxonomy |
| | 7 | * @ticket 38280 |
| | 8 | */ |
| | 9 | class Tests_Term_wpGetTermCountForObjectType extends WP_UnitTestCase { |
| | 10 | |
| | 11 | /** |
| | 12 | * Test when a taxonomy is invalid. |
| | 13 | */ |
| | 14 | public function test_wp_get_term_count_for_object_type_invalid_taxonomy() { |
| | 15 | $actual = wp_get_term_count_for_object_type( 0, 'does-not-exist', 'post' ); |
| | 16 | |
| | 17 | $this->assertWPError( $actual ); |
| | 18 | $this->assertSame( 'invalid_taxonomy', $actual->get_error_code() ); |
| | 19 | } |
| | 20 | |
| | 21 | /** |
| | 22 | * Test when an object type is not in a taxonomy. |
| | 23 | */ |
| | 24 | public function test_wp_get_term_count_for_object_type_object_not_in_taxonomy() { |
| | 25 | $this->assertWPError( wp_get_term_count_for_object_type( 0, 'category', 'page' ) ); |
| | 26 | } |
| | 27 | |
| | 28 | /** |
| | 29 | * Test when an invalid term is passed. |
| | 30 | */ |
| | 31 | public function test_wp_get_term_count_for_object_type_invalid_term() { |
| | 32 | $actual = wp_get_term_count_for_object_type( 0, 'category', 'post' ); |
| | 33 | |
| | 34 | $this->assertWPError( $actual ); |
| | 35 | $this->assertSame( 'invalid_term', $actual->get_error_code() ); |
| | 36 | } |
| | 37 | |
| | 38 | /** |
| | 39 | * Test when a taxonomy belongs to a single object type. No term meta should be |
| | 40 | * stored and the count property should be used. |
| | 41 | */ |
| | 42 | public function test_wp_get_term_count_for_object_type_single_object_type() { |
| | 43 | $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) ); |
| | 44 | $post_id = self::factory()->post->create( |
| | 45 | array( |
| | 46 | 'post_type' => 'post', |
| | 47 | 'post_category' => array( |
| | 48 | $term_id, |
| | 49 | ), |
| | 50 | ) |
| | 51 | ); |
| | 52 | |
| | 53 | $this->assertFalse( (bool) get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 54 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 55 | |
| | 56 | $term_object = get_term( $term_id, 'category' ); |
| | 57 | $this->assertEquals( 1, $term_object->count ); |
| | 58 | |
| | 59 | wp_remove_object_terms( $post_id, array( $term_id ), 'category' ); |
| | 60 | |
| | 61 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 62 | |
| | 63 | $term_object = get_term( $term_id, 'category' ); |
| | 64 | $this->assertEquals( 0, $term_object->count ); |
| | 65 | } |
| | 66 | |
| | 67 | /** |
| | 68 | * Test when a taxonomy belongs to more than one object, and at least one object is not a post type. |
| | 69 | */ |
| | 70 | public function test_wp_get_term_count_for_object_type_non_post_type() { |
| | 71 | register_taxonomy( |
| | 72 | 'wptests_tax', |
| | 73 | array( |
| | 74 | 'user', |
| | 75 | 'foo' |
| | 76 | ) |
| | 77 | ); |
| | 78 | |
| | 79 | $term_id = self::factory()->term->create( |
| | 80 | array( |
| | 81 | 'taxonomy' => 'wptests_tax', |
| | 82 | ) |
| | 83 | ); |
| | 84 | |
| | 85 | $foo_id = 99999; |
| | 86 | |
| | 87 | wp_set_object_terms( $foo_id, $term_id, 'wptests_tax' ); |
| | 88 | |
| | 89 | // 'foo' object doesn't have an update count callback, so the value is always 0. |
| | 90 | $count = wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'foo' ); |
| | 91 | $this->assertSame( 0, $count ); |
| | 92 | } |
| | 93 | |
| | 94 | /** |
| | 95 | * Test when a taxonomy belongs to multiple object types. |
| | 96 | */ |
| | 97 | public function test_wp_get_term_count_for_object_type_multiple_object_types() { |
| | 98 | register_post_type( 'wptests_cpt' ); |
| | 99 | register_taxonomy( |
| | 100 | 'wptests_tax', |
| | 101 | array( |
| | 102 | 'post', |
| | 103 | 'wptests_cpt', |
| | 104 | ) |
| | 105 | ); |
| | 106 | |
| | 107 | $term_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); |
| | 108 | $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); |
| | 109 | $custom_post_id = self::factory()->post->create( array( 'post_type' => 'wptests_cpt' ) ); |
| | 110 | |
| | 111 | // When a term has no relationships, no meta should be stored. |
| | 112 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) ); |
| | 113 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) ); |
| | 114 | $this->assertEmpty( get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 115 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) ); |
| | 116 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) ); |
| | 117 | |
| | 118 | wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' ); |
| | 119 | |
| | 120 | // Term has relationships, meta should be stored caching types counted and counts for each type > 0. |
| | 121 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) ); |
| | 122 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) ); |
| | 123 | $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 124 | $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) ); |
| | 125 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) ); |
| | 126 | |
| | 127 | wp_set_object_terms( $custom_post_id, array( $term_id ), 'wptests_tax' ); |
| | 128 | |
| | 129 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) ); |
| | 130 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) ); |
| | 131 | $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 132 | $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) ); |
| | 133 | $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) ); |
| | 134 | |
| | 135 | // Total count should be stored in the term's count property. |
| | 136 | $term_object = get_term( $term_id, 'wptests_tax' ); |
| | 137 | $this->assertEquals( 2, $term_object->count ); |
| | 138 | |
| | 139 | wp_remove_object_terms( $custom_post_id, array( $term_id ), 'wptests_tax' ); |
| | 140 | |
| | 141 | // Object count cache should be removed. |
| | 142 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) ); |
| | 143 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) ); |
| | 144 | $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 145 | $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) ); |
| | 146 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) ); |
| | 147 | |
| | 148 | wp_remove_object_terms( $post_id, array( $term_id ), 'wptests_tax' ); |
| | 149 | |
| | 150 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) ); |
| | 151 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) ); |
| | 152 | $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 153 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) ); |
| | 154 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) ); |
| | 155 | } |
| | 156 | |
| | 157 | /** |
| | 158 | * Term count must be generated on the fly (as for "legacy" terms). |
| | 159 | */ |
| | 160 | public function test_count_should_be_generated_for_legacy_terms() { |
| | 161 | register_post_type( 'wptests_cpt' ); |
| | 162 | register_taxonomy( |
| | 163 | 'wptests_tax', |
| | 164 | array( |
| | 165 | 'post', |
| | 166 | 'wptests_cpt', |
| | 167 | ) |
| | 168 | ); |
| | 169 | |
| | 170 | $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); |
| | 171 | $p = self::factory()->post->create( array( 'post_type' => 'post' ) ); |
| | 172 | |
| | 173 | wp_set_object_terms( $p, array( $t ), 'wptests_tax' ); |
| | 174 | |
| | 175 | // Mimic "legacy" terms, which will not have the proper counts. |
| | 176 | delete_term_meta( $t, '_wp_object_count_post' ); |
| | 177 | delete_term_meta( $t, '_wp_counted_object_types' ); |
| | 178 | |
| | 179 | $found = wp_get_term_count_for_object_type( $t, 'wptests_tax', 'post' ); |
| | 180 | $this->assertSame( 1, $found ); |
| | 181 | } |
| | 182 | |
| | 183 | /** |
| | 184 | * Test when a taxonomy belongs to multiple object types, one of which is attachments. |
| | 185 | */ |
| | 186 | public function test_wp_get_term_count_for_object_type_multiple_object_types_attachment() { |
| | 187 | register_taxonomy( |
| | 188 | 'wptests_tax', |
| | 189 | array( |
| | 190 | 'post', |
| | 191 | 'attachment', |
| | 192 | ) |
| | 193 | ); |
| | 194 | |
| | 195 | $term_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); |
| | 196 | $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); |
| | 197 | $attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg', $post_id ); |
| | 198 | |
| | 199 | // When a term has no relationships, no meta should be stored. |
| | 200 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) ); |
| | 201 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) ); |
| | 202 | $this->assertEmpty( get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 203 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) ); |
| | 204 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_attachment', true ) ); |
| | 205 | |
| | 206 | wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' ); |
| | 207 | |
| | 208 | // Term has relationships, meta should be stored caching types counted and counts for each type > 0. |
| | 209 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) ); |
| | 210 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) ); |
| | 211 | $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 212 | $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) ); |
| | 213 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_attachment', true ) ); |
| | 214 | |
| | 215 | wp_set_object_terms( $attachment_id, array( $term_id ), 'wptests_tax' ); |
| | 216 | |
| | 217 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) ); |
| | 218 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) ); |
| | 219 | $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 220 | $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) ); |
| | 221 | $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_attachment', true ) ); |
| | 222 | |
| | 223 | // Total count should be stored in the term's count property. |
| | 224 | $term_object = get_term( $term_id, 'wptests_tax' ); |
| | 225 | $this->assertEquals( 2, $term_object->count ); |
| | 226 | |
| | 227 | wp_remove_object_terms( $post_id, array( $term_id ), 'wptests_tax' ); |
| | 228 | |
| | 229 | // Object count cache should be removed. |
| | 230 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) ); |
| | 231 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) ); |
| | 232 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) ); |
| | 233 | $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_attachment', true ) ); |
| | 234 | wp_remove_object_terms( $attachment_id, array( $term_id ), 'wptests_tax' ); |
| | 235 | |
| | 236 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) ); |
| | 237 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) ); |
| | 238 | $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 239 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) ); |
| | 240 | $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_attachment', true ) ); |
| | 241 | } |
| | 242 | } |