| 392 | |
| 393 | /** |
| 394 | * @ticket 26475 |
| 395 | */ |
| 396 | public function test_wp_terms_checklist() { |
| 397 | register_taxonomy( 'wptests_tax', 'post', array( |
| 398 | 'hierarchical' => true, |
| 399 | ) ); |
| 400 | |
| 401 | $t1 = $this->factory->term->create( array( |
| 402 | 'taxonomy' => 'wptests_tax', |
| 403 | ) ); |
| 404 | |
| 405 | $t2 = $this->factory->term->create( array( |
| 406 | 'taxonomy' => 'wptests_tax', |
| 407 | 'parent' => $t1 |
| 408 | ) ); |
| 409 | |
| 410 | $p1 = $this->factory->post->create( array( |
| 411 | 'post_type' => 'post', |
| 412 | ) ); |
| 413 | |
| 414 | wp_set_post_terms($p1, array($t1, $t2), 'wptests_tax'); |
| 415 | |
| 416 | $html = trim(get_echo('wp_terms_checklist', array($p1, array( |
| 417 | 'taxonomy' => 'wptests_tax', |
| 418 | 'descendants_and_self' => $t1, |
| 419 | 'selected_cats' => array($t1, $t2), |
| 420 | 'popular_cats' => array() |
| 421 | )))); |
| 422 | |
| 423 | // both the parent and child terms set to the post |
| 424 | $expected = '/<input value="' . $t1 . '".*checked=\'checked\'.*\/>.*<ul[^>]*>.*<input value="' . $t2 . '".*checked=\'checked\'.*\/>/s'; |
| 425 | $this->assertRegExp( $expected, $html ); |
| 426 | |
| 427 | wp_remove_object_terms($p1, array($t1), 'wptests_tax'); |
| 428 | |
| 429 | $html = trim(get_echo('wp_terms_checklist', array($p1, array( |
| 430 | 'taxonomy' => 'wptests_tax', |
| 431 | 'descendants_and_self' => $t2, |
| 432 | 'selected_cats' => array($t2), |
| 433 | 'popular_cats' => array() |
| 434 | )))); |
| 435 | |
| 436 | // the parent term was removed from the post |
| 437 | $expected = '/<input value="' . $t1 . '".* id="in-wptests_tax-' . $t1 . '" (?!checked=\'checked\').*\/>.*<ul[^>]*>.*<input value="' . $t2 . '".*checked=\'checked\'.*\/>/s'; |
| 438 | $this->assertRegExp( $expected, $html ); |
| 439 | |
| 440 | $t3 = $this->factory->term->create( array( |
| 441 | 'taxonomy' => 'wptests_tax', |
| 442 | 'parent' => $t1 |
| 443 | ) ); |
| 444 | |
| 445 | wp_set_post_terms($p1, array($t3), 'wptests_tax', true); |
| 446 | |
| 447 | $html = trim(get_echo('wp_terms_checklist', array($p1, array( |
| 448 | 'taxonomy' => 'wptests_tax', |
| 449 | 'descendants_and_self' => $t2, |
| 450 | 'selected_cats' => array($t2, $t3), |
| 451 | 'popular_cats' => array() |
| 452 | )))); |
| 453 | |
| 454 | // another child term was added to the same parent and assigned to the post |
| 455 | $expected = '/<input value="' . $t1 . '".* id="in-wptests_tax-' . $t1 . '" (?!checked=\'checked\').*\/>.*<ul[^>]*>.*<input value="' . $t2 . '".*checked=\'checked\'.*\/>.*<input value="' . $t3 . '".*checked=\'checked\'.*\/>.*<\/ul>/s'; |
| 456 | $this->assertRegExp( $expected, $html ); |
| 457 | } |