Changeset 29875
- Timestamp:
- 10/11/2014 04:36:44 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/term.php
r29867 r29875 632 632 } 633 633 634 public function test_wp_update_term_taxonomy_does_not_exist() { 635 $found = wp_update_term( 1, 'bar' ); 636 637 $this->assertTrue( is_wp_error( $found ) ); 638 $this->assertSame( 'invalid_taxonomy', $found->get_error_code() ); 639 } 640 641 public function test_wp_update_term_term_empty_string_should_return_wp_error() { 642 $found = wp_update_term( '', 'post_tag' ); 643 644 $this->assertTrue( is_wp_error( $found ) ); 645 $this->assertSame( 'invalid_term', $found->get_error_code() ); 646 } 647 648 public function test_wp_update_term_unslash_name() { 649 register_taxonomy( 'wptests_tax', 'post' ); 650 $t = $this->factory->term->create( array( 651 'taxonomy' => 'wptests_tax', 652 ) ); 653 654 $found = wp_update_term( $t, 'wptests_tax', array( 655 'name' => 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy', 656 ) ); 657 658 $term = get_term( $found['term_id'], 'wptests_tax' ); 659 _unregister_taxonomy( 'wptests_tax' ); 660 661 $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->name ); 662 } 663 664 public function test_wp_update_term_unslash_description() { 665 register_taxonomy( 'wptests_tax', 'post' ); 666 $t = $this->factory->term->create( array( 667 'taxonomy' => 'wptests_tax', 668 ) ); 669 670 $found = wp_update_term( $t, 'wptests_tax', array( 671 'description' => 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy', 672 ) ); 673 674 $term = get_term( $found['term_id'], 'wptests_tax' ); 675 _unregister_taxonomy( 'wptests_tax' ); 676 677 $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->description ); 678 } 679 680 public function test_wp_update_term_name_empty_string() { 681 register_taxonomy( 'wptests_tax', 'post' ); 682 $t = $this->factory->term->create( array( 683 'taxonomy' => 'wptests_tax', 684 ) ); 685 686 $found = wp_update_term( $t, 'wptests_tax', array( 687 'name' => '', 688 ) ); 689 690 $this->assertTrue( is_wp_error( $found ) ); 691 $this->assertSame( 'empty_term_name', $found->get_error_code() ); 692 _unregister_taxonomy( 'wptests_tax' ); 693 } 694 634 695 /** 635 696 * @ticket 29614 … … 656 717 $term = get_term( $t, 'wptests_tax' ); 657 718 $this->assertEquals( 0, $term->parent ); 719 _unregister_taxonomy( 'wptests_tax' ); 720 } 721 722 public function test_wp_update_term_slug_empty_string_while_not_updating_name() { 723 register_taxonomy( 'wptests_tax', 'post' ); 724 $t = $this->factory->term->create( array( 725 'taxonomy' => 'wptests_tax', 726 'name' => 'Foo Bar', 727 ) ); 728 729 $found = wp_update_term( $t, 'wptests_tax', array( 730 'slug' => '', 731 ) ); 732 733 $term = get_term( $t, 'wptests_tax' ); 734 $this->assertSame( 'foo-bar', $term->slug ); 735 _unregister_taxonomy( 'wptests_tax' ); 736 } 737 738 public function test_wp_update_term_slug_empty_string_while_updating_name() { 739 register_taxonomy( 'wptests_tax', 'post' ); 740 $t = $this->factory->term->create( array( 741 'taxonomy' => 'wptests_tax', 742 ) ); 743 744 $found = wp_update_term( $t, 'wptests_tax', array( 745 'name' => 'Foo Bar', 746 'slug' => '', 747 ) ); 748 749 $term = get_term( $t, 'wptests_tax' ); 750 $this->assertSame( 'foo-bar', $term->slug ); 751 _unregister_taxonomy( 'wptests_tax' ); 752 } 753 754 public function test_wp_update_term_slug_set_slug() { 755 register_taxonomy( 'wptests_tax', 'post' ); 756 $t = $this->factory->term->create( array( 757 'taxonomy' => 'wptests_tax', 758 ) ); 759 760 $found = wp_update_term( $t, 'wptests_tax', array( 761 'slug' => 'foo-bar', 762 ) ); 763 764 $term = get_term( $t, 'wptests_tax' ); 765 $this->assertSame( 'foo-bar', $term->slug ); 766 _unregister_taxonomy( 'wptests_tax' ); 658 767 } 659 768 … … 714 823 $this->assertSame( 0, $created_term->term_group ); 715 824 } 825 826 public function test_wp_update_term_slug_same_as_old_slug() { 827 register_taxonomy( 'wptests_tax', 'post' ); 828 $t = $this->factory->term->create( array( 829 'taxonomy' => 'wptests_tax', 830 'slug' => 'foo', 831 ) ); 832 833 $found = wp_update_term( $t, 'wptests_tax', array( 834 'slug' => 'foo', 835 ) ); 836 837 $term = get_term( $t, 'wptests_tax' ); 838 839 $this->assertSame( $t, $found['term_id'] ); 840 $this->assertSame( 'foo', $term->slug ); 841 _unregister_taxonomy( 'wptests_tax' ); 842 } 843 844 public function test_wp_update_term_duplicate_slug_generated_due_to_empty_slug_param() { 845 register_taxonomy( 'wptests_tax', 'post' ); 846 $t1 = $this->factory->term->create( array( 847 'taxonomy' => 'wptests_tax', 848 'slug' => 'foo-bar', 849 ) ); 850 $t2 = $this->factory->term->create( array( 851 'taxonomy' => 'wptests_tax', 852 'name' => 'not foo bar', 853 ) ); 854 855 $found = wp_update_term( $t2, 'wptests_tax', array( 856 'slug' => '', 857 'name' => 'Foo? Bar!', // Will sanitize to 'foo-bar'. 858 ) ); 859 860 $term = get_term( $t2, 'wptests_tax' ); 861 862 $this->assertSame( $t2, $found['term_id'] ); 863 $this->assertSame( 'foo-bar-2', $term->slug ); 864 _unregister_taxonomy( 'wptests_tax' ); 865 } 866 867 public function test_wp_update_term_duplicate_slug_with_changed_parent() { 868 register_taxonomy( 'wptests_tax', 'post', array( 869 'hierarchical' => true, 870 ) ); 871 $p = $this->factory->term->create( array( 872 'taxonomy' => 'wptests_tax', 873 ) ); 874 $t1 = $this->factory->term->create( array( 875 'taxonomy' => 'wptests_tax', 876 'slug' => 'foo-bar', 877 ) ); 878 $t2 = $this->factory->term->create( array( 879 'taxonomy' => 'wptests_tax', 880 ) ); 881 882 $found = wp_update_term( $t2, 'wptests_tax', array( 883 'parent' => $p, 884 'slug' => 'foo-bar', 885 ) ); 886 887 $term = get_term( $t2, 'wptests_tax' ); 888 $parent_term = get_term( $p, 'wptests_tax' ); 889 890 $this->assertSame( $t2, $found['term_id'] ); 891 $this->assertSame( 'foo-bar-' . $parent_term->slug, $term->slug ); 892 _unregister_taxonomy( 'wptests_tax' ); 893 } 894 895 public function test_wp_update_term_duplicate_slug_failure() { 896 register_taxonomy( 'wptests_tax', 'post' ); 897 $t1 = $this->factory->term->create( array( 898 'taxonomy' => 'wptests_tax', 899 'slug' => 'foo-bar', 900 ) ); 901 $t2 = $this->factory->term->create( array( 902 'taxonomy' => 'wptests_tax', 903 'slug' => 'my-old-slug', 904 ) ); 905 906 $found = wp_update_term( $t2, 'wptests_tax', array( 907 'slug' => 'foo-bar', 908 ) ); 909 910 $term = get_term( $t2, 'wptests_tax' ); 911 912 $this->assertWPError( $found ); 913 $this->assertSame( 'duplicate_term_slug', $found->get_error_code() ); 914 $this->assertSame( 'my-old-slug', $term->slug ); 915 _unregister_taxonomy( 'wptests_tax' ); 916 } 917 918 public function test_wp_update_term_should_return_term_id_and_term_taxonomy_id() { 919 register_taxonomy( 'wptests_tax', 'post' ); 920 $t = $this->factory->term->create( array( 921 'taxonomy' => 'wptests_tax', 922 ) ); 923 $found = wp_update_term( $t, 'wptests_tax', array( 924 'slug' => 'foo', 925 ) ); 926 927 $term_by_id = get_term( $found['term_id'], 'wptests_tax' ); 928 $term_by_slug = get_term_by( 'slug', 'foo', 'wptests_tax' ); 929 $term_by_ttid = get_term_by( 'term_taxonomy_id', $found['term_taxonomy_id'], 'wptests_tax' ); 930 931 _unregister_taxonomy( 'wptests_tax' ); 932 933 $this->assertInternalType( 'array', $found ); 934 $this->assertNotEmpty( $found['term_id'] ); 935 $this->assertNotEmpty( $found['term_taxonomy_id'] ); 936 $this->assertNotEmpty( $term_by_id ); 937 $this->assertEquals( $term_by_id, $term_by_slug ); 938 $this->assertEquals( $term_by_id, $term_by_ttid ); 939 } 940 941 public function test_wp_update_term_should_clean_object_term_cache() { 942 register_taxonomy( 'wptests_tax_for_post', 'post' ); 943 register_taxonomy( 'wptests_tax_for_page', 'page' ); 944 $post = $this->factory->post->create(); 945 $page = $this->factory->post->create( array( 946 'post_type' => 'page', 947 ) ); 948 949 $t_for_post = $this->factory->term->create( array( 950 'taxonomy' => 'wptests_tax_for_post', 951 ) ); 952 $t_for_page = $this->factory->term->create( array( 953 'taxonomy' => 'wptests_tax_for_page', 954 ) ); 955 956 wp_set_post_terms( $post, array( $t_for_post ), 'wptests_tax_for_post' ); 957 wp_set_post_terms( $page, array( $t_for_page ), 'wptests_tax_for_page' ); 958 959 // Prime caches and verify. 960 update_object_term_cache( array( $post ), 'post' ); 961 update_object_term_cache( array( $page ), 'page' ); 962 $this->assertNotEmpty( wp_cache_get( $post, 'wptests_tax_for_post_relationships' ) ); 963 $this->assertNotEmpty( wp_cache_get( $page, 'wptests_tax_for_page_relationships' ) ); 964 965 // Update a term in just one of the taxonomies. 966 $found = wp_update_term( $t_for_post, 'wptests_tax_for_post', array( 967 'slug' => 'foo', 968 ) ); 969 970 // Only the relevant cache should have been cleared. 971 $this->assertFalse( wp_cache_get( $post, 'wptests_tax_for_post_relationships' ) ); 972 $this->assertNotEmpty( wp_cache_get( $page, 'wptests_tax_for_page_relationships' ) ); 973 } 974 975 public function test_wp_update_term_should_clean_term_cache() { 976 register_taxonomy( 'wptests_tax', 'post', array( 977 'hierarchical' => true, 978 ) ); 979 980 $t1 = $this->factory->term->create( array( 981 'taxonomy' => 'wptests_tax', 982 ) ); 983 $t2 = $this->factory->term->create( array( 984 'taxonomy' => 'wptests_tax', 985 ) ); 986 987 /* 988 * It doesn't appear that WordPress itself ever sets these 989 * caches, but we should ensure that they're being cleared for 990 * compatibility with third-party addons. Prime the caches 991 * manually. 992 */ 993 wp_cache_set( 'all_ids', array( 1, 2, 3 ), 'wptests_tax' ); 994 wp_cache_set( 'get', array( 1, 2, 3 ), 'wptests_tax' ); 995 996 $found = wp_update_term( $t1, 'wptests_tax', array( 997 'parent' => $t2, 998 ) ); 999 _unregister_taxonomy( 'wptests_tax' ); 1000 1001 $this->assertSame( false, wp_cache_get( 'all_ids', 'wptests_tax' ) ); 1002 $this->assertSame( false, wp_cache_get( 'get', 'wptests_tax' ) ); 1003 1004 $cached_children = get_option( 'wptests_tax_children' ); 1005 $this->assertNotEmpty( $cached_children[ $t2 ] ); 1006 $this->assertTrue( in_array( $found['term_id'], $cached_children[ $t2 ] ) ); 1007 } 1008 716 1009 /** 717 1010 * @ticket 5381
Note: See TracChangeset
for help on using the changeset viewer.