| 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 | $expected = '<li id=\'wptests_tax-2\'><label class="selectit"><input value="2" type="checkbox" name="tax_input[wptests_tax][]" id="in-wptests_tax-2" checked=\'checked\' disabled=\'disabled\' /> Term 1</label><ul class=\'children\'> |
| 424 | |
| 425 | <li id=\'wptests_tax-3\'><label class="selectit"><input value="3" type="checkbox" name="tax_input[wptests_tax][]" id="in-wptests_tax-3" checked=\'checked\' disabled=\'disabled\' /> Term 2</label></li> |
| 426 | </ul> |
| 427 | </li>'; |
| 428 | |
| 429 | // both the parent and child terms set to the post |
| 430 | $this->assertEquals( $expected, $html ); |
| 431 | |
| 432 | wp_remove_object_terms($p1, array($t1), 'wptests_tax'); |
| 433 | |
| 434 | $html = trim(get_echo('wp_terms_checklist', array($p1, array( |
| 435 | 'taxonomy' => 'wptests_tax', |
| 436 | 'descendants_and_self' => $t2, |
| 437 | 'selected_cats' => array($t2), |
| 438 | 'popular_cats' => array() |
| 439 | )))); |
| 440 | |
| 441 | $expected = '<li id=\'wptests_tax-2\'><label class="selectit"><input value="2" type="checkbox" name="tax_input[wptests_tax][]" id="in-wptests_tax-2" disabled=\'disabled\' /> Term 1</label><ul class=\'children\'> |
| 442 | |
| 443 | <li id=\'wptests_tax-3\'><label class="selectit"><input value="3" type="checkbox" name="tax_input[wptests_tax][]" id="in-wptests_tax-3" checked=\'checked\' disabled=\'disabled\' /> Term 2</label></li> |
| 444 | </ul> |
| 445 | </li>'; |
| 446 | |
| 447 | // the parent term was removed from the post |
| 448 | $this->assertEquals( $expected, $html ); |
| 449 | |
| 450 | $t3 = $this->factory->term->create( array( |
| 451 | 'taxonomy' => 'wptests_tax', |
| 452 | 'parent' => $t1 |
| 453 | ) ); |
| 454 | |
| 455 | wp_set_post_terms($p1, array($t3), 'wptests_tax', true); |
| 456 | |
| 457 | $html = trim(get_echo('wp_terms_checklist', array($p1, array( |
| 458 | 'taxonomy' => 'wptests_tax', |
| 459 | 'descendants_and_self' => $t2, |
| 460 | 'selected_cats' => array($t2, $t3), |
| 461 | 'popular_cats' => array() |
| 462 | )))); |
| 463 | |
| 464 | $expected = '<li id=\'wptests_tax-2\'><label class="selectit"><input value="2" type="checkbox" name="tax_input[wptests_tax][]" id="in-wptests_tax-2" disabled=\'disabled\' /> Term 1</label><ul class=\'children\'> |
| 465 | |
| 466 | <li id=\'wptests_tax-3\'><label class="selectit"><input value="3" type="checkbox" name="tax_input[wptests_tax][]" id="in-wptests_tax-3" checked=\'checked\' disabled=\'disabled\' /> Term 2</label></li> |
| 467 | |
| 468 | <li id=\'wptests_tax-4\'><label class="selectit"><input value="4" type="checkbox" name="tax_input[wptests_tax][]" id="in-wptests_tax-4" checked=\'checked\' disabled=\'disabled\' /> Term 3</label></li> |
| 469 | </ul> |
| 470 | </li>'; |
| 471 | |
| 472 | // another child term was added to the same parent and assigned to the post |
| 473 | $this->assertEquals( $expected, $html ); |
| 474 | } |