Changeset 37643 for branches/4.4
- Timestamp:
- 06/06/2016 09:16:54 PM (8 years ago)
- Location:
- branches/4.4/tests/phpunit/tests
- Files:
-
- 1 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.4/tests/phpunit/tests/term.php
r37642 r37643 67 67 // there are 5 posts, all Uncategorized 68 68 $this->assertEquals( 1, $count ); 69 } 70 71 /** 72 * @ticket 26570 73 */ 74 function test_set_object_terms() { 75 $non_hier = rand_str( 10 ); 76 $hier = rand_str( 10 ); 77 78 // Register taxonomies 79 register_taxonomy( $non_hier, array() ); 80 register_taxonomy( $hier, array( 'hierarchical' => true ) ); 81 82 // Create a post. 83 $post_id = self::$post_ids[0]; 84 85 /* 86 * Set a single term (non-hierarchical) by ID. 87 */ 88 $tag = wp_insert_term( 'Foo', $non_hier ); 89 $this->assertFalse( has_term( $tag['term_id'], $non_hier, $post_id ) ); 90 91 wp_set_object_terms( $post_id, $tag['term_id'], $non_hier ); 92 $this->assertTrue( has_term( $tag['term_id'], $non_hier, $post_id ) ); 93 94 /* 95 * Set a single term (non-hierarchical) by slug. 96 */ 97 $tag = wp_insert_term( 'Bar', $non_hier ); 98 $tag = get_term( $tag['term_id'], $non_hier ); 99 100 $this->assertFalse( has_term( $tag->slug, $non_hier, $post_id ) ); 101 102 wp_set_object_terms( $post_id, $tag->slug, $non_hier ); 103 $this->assertTrue( has_term( $tag->slug, $non_hier, $post_id ) ); 104 105 /* 106 * Set a single term (hierarchical) by ID. 107 */ 108 $cat = wp_insert_term( 'Baz', $hier ); 109 $this->assertFalse( has_term( $cat['term_id'], $hier, $post_id ) ); 110 111 wp_set_object_terms( $post_id, $cat['term_id'], $hier ); 112 $this->assertTrue( has_term( $cat['term_id'], $hier, $post_id ) ); 113 114 /* 115 * Set a single term (hierarchical) by slug. 116 */ 117 $cat = wp_insert_term( 'Qux', $hier ); 118 $cat = get_term( $cat['term_id'], $hier ); 119 120 $this->assertFalse( has_term( $cat->slug, $hier, $post_id ) ); 121 122 wp_set_object_terms( $post_id, $cat->slug, $hier ); 123 $this->assertTrue( has_term( $cat->slug, $hier, $post_id ) ); 124 125 /* 126 * Set an array of term IDs (non-hierarchical) by ID. 127 */ 128 $tag1 = wp_insert_term( '_tag1', $non_hier ); 129 $this->assertFalse( has_term( $tag1['term_id'], $non_hier, $post_id ) ); 130 131 $tag2 = wp_insert_term( '_tag2', $non_hier ); 132 $this->assertFalse( has_term( $tag2['term_id'], $non_hier, $post_id ) ); 133 134 $tag3 = wp_insert_term( '_tag3', $non_hier ); 135 $this->assertFalse( has_term( $tag3['term_id'], $non_hier, $post_id ) ); 136 137 wp_set_object_terms( $post_id, array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier ); 138 $this->assertTrue( has_term( array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier, $post_id ) ); 139 140 /* 141 * Set an array of term slugs (hierarchical) by slug. 142 */ 143 $cat1 = wp_insert_term( '_cat1', $hier ); 144 $cat1 = get_term( $cat1['term_id'], $hier ); 145 $this->assertFalse( has_term( $cat1->slug, $hier, $post_id ) ); 146 147 $cat2 = wp_insert_term( '_cat2', $hier ); 148 $cat2 = get_term( $cat2['term_id'], $hier ); 149 $this->assertFalse( has_term( $cat2->slug, $hier, $post_id ) ); 150 151 $cat3 = wp_insert_term( '_cat3', $hier ); 152 $cat3 = get_term( $cat3['term_id'], $hier ); 153 $this->assertFalse( has_term( $cat3->slug, $hier, $post_id ) ); 154 155 wp_set_object_terms( $post_id, array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier ); 156 $this->assertTrue( has_term( array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier, $post_id ) ); 157 } 158 159 function test_set_object_terms_by_id() { 160 $ids = self::$post_ids; 161 162 $terms = array(); 163 for ($i=0; $i<3; $i++ ) { 164 $term = rand_str(); 165 $result = wp_insert_term( $term, $this->taxonomy ); 166 $this->assertInternalType( 'array', $result ); 167 $term_id[$term] = $result['term_id']; 168 } 169 170 foreach ($ids as $id) { 171 $tt = wp_set_object_terms( $id, array_values($term_id), $this->taxonomy ); 172 // should return three term taxonomy ids 173 $this->assertEquals( 3, count($tt) ); 174 } 175 176 // each term should be associated with every post 177 foreach ($term_id as $term=>$id) { 178 $actual = get_objects_in_term($id, $this->taxonomy); 179 $this->assertEquals( $ids, array_map('intval', $actual) ); 180 } 181 182 // each term should have a count of 5 183 foreach (array_keys($term_id) as $term) { 184 $t = get_term_by('name', $term, $this->taxonomy); 185 $this->assertEquals( 5, $t->count ); 186 } 187 } 188 189 function test_set_object_terms_by_name() { 190 $ids = self::$post_ids; 191 192 $terms = array( 193 rand_str(), 194 rand_str(), 195 rand_str() 196 ); 197 198 foreach ($ids as $id) { 199 $tt = wp_set_object_terms( $id, $terms, $this->taxonomy ); 200 // should return three term taxonomy ids 201 $this->assertEquals( 3, count($tt) ); 202 // remember which term has which term_id 203 for ($i=0; $i<3; $i++) { 204 $term = get_term_by('name', $terms[$i], $this->taxonomy); 205 $term_id[$terms[$i]] = intval($term->term_id); 206 } 207 } 208 209 // each term should be associated with every post 210 foreach ($term_id as $term=>$id) { 211 $actual = get_objects_in_term($id, $this->taxonomy); 212 $this->assertEquals( $ids, array_map('intval', $actual) ); 213 } 214 215 // each term should have a count of 5 216 foreach ($terms as $term) { 217 $t = get_term_by('name', $term, $this->taxonomy); 218 $this->assertEquals( 5, $t->count ); 219 } 220 } 221 222 function test_set_object_terms_invalid() { 223 // bogus taxonomy 224 $result = wp_set_object_terms( self::$post_ids[0], array(rand_str()), rand_str() ); 225 $this->assertTrue( is_wp_error($result) ); 226 } 227 228 public function test_wp_set_object_terms_append_true() { 229 register_taxonomy( 'wptests_tax', 'post' ); 230 $p = self::$post_ids[0]; 231 $t1 = self::factory()->term->create( array( 232 'taxonomy' => 'wptests_tax', 233 ) ); 234 $t2 = self::factory()->term->create( array( 235 'taxonomy' => 'wptests_tax', 236 ) ); 237 238 $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' ); 239 $this->assertNotEmpty( $added1 ); 240 $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) ); 241 242 $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax', true ); 243 $this->assertNotEmpty( $added2 ); 244 $this->assertEqualSets( array( $t1, $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) ); 245 246 _unregister_taxonomy( 'wptests_tax' ); 247 } 248 249 public function test_wp_set_object_terms_append_false() { 250 register_taxonomy( 'wptests_tax', 'post' ); 251 $p = self::$post_ids[0]; 252 $t1 = self::factory()->term->create( array( 253 'taxonomy' => 'wptests_tax', 254 ) ); 255 $t2 = self::factory()->term->create( array( 256 'taxonomy' => 'wptests_tax', 257 ) ); 258 259 $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' ); 260 $this->assertNotEmpty( $added1 ); 261 $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) ); 262 263 $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax', false ); 264 $this->assertNotEmpty( $added2 ); 265 $this->assertEqualSets( array( $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) ); 266 267 _unregister_taxonomy( 'wptests_tax' ); 268 } 269 270 public function test_wp_set_object_terms_append_default_to_false() { 271 register_taxonomy( 'wptests_tax', 'post' ); 272 $p = self::$post_ids[0]; 273 $t1 = self::factory()->term->create( array( 274 'taxonomy' => 'wptests_tax', 275 ) ); 276 $t2 = self::factory()->term->create( array( 277 'taxonomy' => 'wptests_tax', 278 ) ); 279 280 $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' ); 281 $this->assertNotEmpty( $added1 ); 282 $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) ); 283 284 $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax' ); 285 $this->assertNotEmpty( $added2 ); 286 $this->assertEqualSets( array( $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) ); 287 288 _unregister_taxonomy( 'wptests_tax' ); 289 } 290 291 function test_change_object_terms_by_id() { 292 // set some terms on an object; then change them while leaving one intact 293 294 $post_id = self::$post_ids[0]; 295 296 // first set: 3 terms 297 $terms_1 = array(); 298 for ($i=0; $i<3; $i++ ) { 299 $term = rand_str(); 300 $result = wp_insert_term( $term, $this->taxonomy ); 301 $this->assertInternalType( 'array', $result ); 302 $terms_1[$i] = $result['term_id']; 303 } 304 305 // second set: one of the original terms, plus one new term 306 $terms_2 = array(); 307 $terms_2[0] = $terms_1[1]; 308 309 $term = rand_str(); 310 $result = wp_insert_term( $term, $this->taxonomy ); 311 $terms_2[1] = $result['term_id']; 312 313 314 // set the initial terms 315 $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy ); 316 $this->assertEquals( 3, count($tt_1) ); 317 318 // make sure they're correct 319 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id')); 320 $this->assertEquals( $terms_1, $terms ); 321 322 // change the terms 323 $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy ); 324 $this->assertEquals( 2, count($tt_2) ); 325 326 // make sure they're correct 327 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id')); 328 $this->assertEquals( $terms_2, $terms ); 329 330 // make sure the tt id for 'bar' matches 331 $this->assertEquals( $tt_1[1], $tt_2[0] ); 332 333 } 334 335 function test_change_object_terms_by_name() { 336 // set some terms on an object; then change them while leaving one intact 337 338 $post_id = self::$post_ids[0]; 339 340 $terms_1 = array('foo', 'bar', 'baz'); 341 $terms_2 = array('bar', 'bing'); 342 343 // set the initial terms 344 $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy ); 345 $this->assertEquals( 3, count($tt_1) ); 346 347 // make sure they're correct 348 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id')); 349 $this->assertEquals( $terms_1, $terms ); 350 351 // change the terms 352 $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy ); 353 $this->assertEquals( 2, count($tt_2) ); 354 355 // make sure they're correct 356 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id')); 357 $this->assertEquals( $terms_2, $terms ); 358 359 // make sure the tt id for 'bar' matches 360 $this->assertEquals( $tt_1[1], $tt_2[0] ); 361 69 362 } 70 363
Note: See TracChangeset
for help on using the changeset viewer.