Changeset 42343 for trunk/tests/phpunit/tests/term.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/term.php
r38398 r42343 5 5 */ 6 6 class Tests_Term extends WP_UnitTestCase { 7 protected $taxonomy = 'category';7 protected $taxonomy = 'category'; 8 8 protected static $post_ids = array(); 9 9 … … 16 16 */ 17 17 public function test_wp_delete_term_should_invalidate_cache_for_child_terms() { 18 register_taxonomy( 'wptests_tax', 'post', array( 19 'hierarchical' => true, 20 ) ); 21 22 $parent = self::factory()->term->create( array( 23 'taxonomy' => 'wptests_tax', 24 ) ); 25 26 $child = self::factory()->term->create( array( 27 'taxonomy' => 'wptests_tax', 28 'parent' => $parent, 29 'slug' => 'foo', 30 ) ); 18 register_taxonomy( 19 'wptests_tax', 'post', array( 20 'hierarchical' => true, 21 ) 22 ); 23 24 $parent = self::factory()->term->create( 25 array( 26 'taxonomy' => 'wptests_tax', 27 ) 28 ); 29 30 $child = self::factory()->term->create( 31 array( 32 'taxonomy' => 'wptests_tax', 33 'parent' => $parent, 34 'slug' => 'foo', 35 ) 36 ); 31 37 32 38 // Prime the cache. … … 45 51 // insert a term 46 52 $term = rand_str(); 47 $t = wp_insert_term( $term, $this->taxonomy );53 $t = wp_insert_term( $term, $this->taxonomy ); 48 54 $this->assertInternalType( 'array', $t ); 49 $term_obj = get_term_by( 'name', $term, $this->taxonomy);50 $this->assertEquals( $t['term_id'], term_exists( $term_obj->slug) );55 $term_obj = get_term_by( 'name', $term, $this->taxonomy ); 56 $this->assertEquals( $t['term_id'], term_exists( $term_obj->slug ) ); 51 57 52 58 // clean up 53 $this->assertTrue( wp_delete_term( $t['term_id'], $this->taxonomy) );59 $this->assertTrue( wp_delete_term( $t['term_id'], $this->taxonomy ) ); 54 60 } 55 61 … … 68 74 function test_wp_add_remove_object_terms() { 69 75 $posts = self::$post_ids; 70 $tags = self::factory()->tag->create_many( 5 );76 $tags = self::factory()->tag->create_many( 5 ); 71 77 72 78 $tt = wp_add_object_terms( $posts[0], $tags[1], 'post_tag' ); … … 75 81 76 82 $three_tags = array( $tags[0], $tags[1], $tags[2] ); 77 $tt = wp_add_object_terms( $posts[1], $three_tags, 'post_tag' );83 $tt = wp_add_object_terms( $posts[1], $three_tags, 'post_tag' ); 78 84 $this->assertEquals( 3, count( $tt ) ); 79 85 $this->assertEquals( $three_tags, wp_get_object_terms( $posts[1], 'post_tag', array( 'fields' => 'ids' ) ) ); … … 85 91 $this->assertEquals( 0, count( wp_get_object_terms( $posts[1], 'post_tag' ) ) ); 86 92 87 foreach ( $tags as $term_id ) 93 foreach ( $tags as $term_id ) { 88 94 $this->assertTrue( wp_delete_term( $term_id, 'post_tag' ) ); 89 90 foreach ( $posts as $post_id ) 95 } 96 97 foreach ( $posts as $post_id ) { 91 98 $this->assertTrue( (bool) wp_delete_post( $post_id ) ); 99 } 92 100 } 93 101 … … 95 103 * @group category.php 96 104 */ 97 function test_term_is_ancestor_of( 98 $term = rand_str();105 function test_term_is_ancestor_of() { 106 $term = rand_str(); 99 107 $term2 = rand_str(); 100 108 … … 107 115 $this->assertFalse( term_is_ancestor_of( $t2['term_id'], $t['term_id'], 'category' ) ); 108 116 } 109 $this->assertTrue( cat_is_ancestor_of( $t['term_id'], $t2['term_id'] ) );110 $this->assertFalse( cat_is_ancestor_of( $t2['term_id'], $t['term_id'] ) );111 112 wp_delete_term( $t['term_id'], 'category');113 wp_delete_term( $t2['term_id'], 'category');117 $this->assertTrue( cat_is_ancestor_of( $t['term_id'], $t2['term_id'] ) ); 118 $this->assertFalse( cat_is_ancestor_of( $t2['term_id'], $t['term_id'] ) ); 119 120 wp_delete_term( $t['term_id'], 'category' ); 121 wp_delete_term( $t2['term_id'], 'category' ); 114 122 } 115 123 … … 121 129 122 130 $t = wp_insert_category( array( 'cat_name' => $term ) ); 123 $this->assertTrue( is_numeric( $t) );131 $this->assertTrue( is_numeric( $t ) ); 124 132 $this->assertNotWPError( $t ); 125 133 $this->assertTrue( $t > 0 ); … … 127 135 128 136 // make sure the term exists 129 $this->assertTrue( term_exists( $term) > 0 );130 $this->assertTrue( term_exists( $t) > 0 );137 $this->assertTrue( term_exists( $term ) > 0 ); 138 $this->assertTrue( term_exists( $t ) > 0 ); 131 139 132 140 // now delete it 133 $this->assertTrue( wp_delete_category( $t) );134 $this->assertNull( term_exists( $term) );135 $this->assertNull( term_exists( $t) );136 $this->assertEquals( $initial_count, wp_count_terms( 'category') );141 $this->assertTrue( wp_delete_category( $t ) ); 142 $this->assertNull( term_exists( $term ) ); 143 $this->assertNull( term_exists( $t ) ); 144 $this->assertEquals( $initial_count, wp_count_terms( 'category' ) ); 137 145 } 138 146 … … 142 150 function test_wp_set_post_categories() { 143 151 $post_id = self::$post_ids[0]; 144 $post = get_post( $post_id );152 $post = get_post( $post_id ); 145 153 146 154 $this->assertInternalType( 'array', $post->post_category ); … … 152 160 wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) ); 153 161 $this->assertEquals( 2, count( $post->post_category ) ); 154 $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ) 162 $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ), $post->post_category ); 155 163 156 164 wp_set_post_categories( $post_id, $term3['term_id'], true ); 157 $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ) 165 $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ), $post->post_category ); 158 166 159 167 $term4 = wp_insert_term( 'Burrito', 'category' ); … … 179 187 $term = wp_insert_term( 'foo', $this->taxonomy ); 180 188 181 $this->assertEquals( 0, sanitize_term_field( 'parent', 182 $this->assertEquals( 1, sanitize_term_field( 'parent', 189 $this->assertEquals( 0, sanitize_term_field( 'parent', 0, $term['term_id'], $this->taxonomy, 'raw' ) ); 190 $this->assertEquals( 1, sanitize_term_field( 'parent', 1, $term['term_id'], $this->taxonomy, 'raw' ) ); 183 191 $this->assertEquals( 0, sanitize_term_field( 'parent', -1, $term['term_id'], $this->taxonomy, 'raw' ) ); 184 192 $this->assertEquals( 0, sanitize_term_field( 'parent', '', $term['term_id'], $this->taxonomy, 'raw' ) ); … … 186 194 187 195 private function assertPostHasTerms( $post_id, $expected_term_ids, $taxonomy ) { 188 $assigned_term_ids = wp_get_object_terms( $post_id, $taxonomy, array( 189 'fields' => 'ids' 190 ) ); 196 $assigned_term_ids = wp_get_object_terms( 197 $post_id, $taxonomy, array( 198 'fields' => 'ids', 199 ) 200 ); 191 201 192 202 $this->assertEquals( $expected_term_ids, $assigned_term_ids );
Note: See TracChangeset
for help on using the changeset viewer.