Changeset 64612
- Timestamp:
- 10/20/2026 05:45:28 PM (3 weeks ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/category/catIsAncestorOf.php
r64457 r64612 4 4 * Tests for cat_is_ancestor_of(). 5 5 * 6 * @group category 6 7 * @group taxonomy 7 * @group category8 8 * 9 9 * @covers ::cat_is_ancestor_of … … 12 12 13 13 /** 14 * Grandparent category object. 15 * 16 * @var WP_Term 17 */ 18 private static $grandparent; 19 20 /** 21 * Parent category object. 22 * 23 * @var WP_Term 24 */ 25 private static $parent; 26 27 /** 28 * Child category object. 29 * 30 * @var WP_Term 31 */ 32 private static $child; 33 34 /** 35 * Unrelated category object. 36 * 37 * @var WP_Term 38 */ 39 private static $unrelated; 40 41 /** 42 * Creates shared category hierarchy fixtures. 14 * Grandparent category ID. 15 * 16 * @var int 17 */ 18 private static $grandparent_id; 19 20 /** 21 * Parent category ID. 22 * 23 * @var int 24 */ 25 private static $parent_id; 26 27 /** 28 * Child category ID. 29 * 30 * @var int 31 */ 32 private static $child_id; 33 34 /** 35 * Unrelated category ID. 36 * 37 * @var int 38 */ 39 private static $unrelated_id; 40 41 /** 42 * Post tag ID (different taxonomy). 43 * 44 * @var int 45 */ 46 private static $tag_id; 47 48 /** 49 * Creates category fixtures before tests run. 43 50 * 44 51 * @param WP_UnitTest_Factory $factory Test factory. 45 52 */ 46 53 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 47 self::$grandparent = $factory->category->create_and_get(54 self::$grandparent_id = $factory->category->create( 48 55 array( 49 56 'name' => 'Grandparent Category', 50 ) 51 ); 52 53 self::$parent = $factory->category->create_and_get( 57 'slug' => 'grandparent-category', 58 ) 59 ); 60 61 self::$parent_id = $factory->category->create( 54 62 array( 55 63 'name' => 'Parent Category', 56 'parent' => self::$grandparent->term_id, 57 ) 58 ); 59 60 self::$child = $factory->category->create_and_get( 64 'slug' => 'parent-category', 65 'parent' => self::$grandparent_id, 66 ) 67 ); 68 69 self::$child_id = $factory->category->create( 61 70 array( 62 71 'name' => 'Child Category', 63 'parent' => self::$parent->term_id, 64 ) 65 ); 66 67 self::$unrelated = $factory->category->create_and_get( 72 'slug' => 'child-category', 73 'parent' => self::$parent_id, 74 ) 75 ); 76 77 self::$unrelated_id = $factory->category->create( 68 78 array( 69 79 'name' => 'Unrelated Category', 70 ) 71 ); 72 } 73 74 /** 75 * Tests that cat_is_ancestor_of() returns true for direct and indirect descendants 76 * when given category IDs, WP_Term objects, or a mix of both. 77 * 78 * @ticket 65819 79 */ 80 public function test_returns_true_for_direct_and_indirect_descendants() { 81 // Category IDs. 82 $this->assertTrue( cat_is_ancestor_of( self::$parent->term_id, self::$child->term_id ) ); 83 $this->assertTrue( cat_is_ancestor_of( self::$grandparent->term_id, self::$child->term_id ) ); 84 $this->assertTrue( cat_is_ancestor_of( self::$grandparent->term_id, self::$parent->term_id ) ); 85 86 // Category objects. 87 $this->assertTrue( cat_is_ancestor_of( self::$parent, self::$child ) ); 88 $this->assertTrue( cat_is_ancestor_of( self::$grandparent, self::$child ) ); 89 90 // Mixed ID and object inputs. 91 $this->assertTrue( cat_is_ancestor_of( self::$parent->term_id, self::$child ) ); 92 $this->assertTrue( cat_is_ancestor_of( self::$grandparent, self::$child->term_id ) ); 93 } 94 95 /** 96 * Tests that cat_is_ancestor_of() returns false when the first category is not an ancestor. 97 * 98 * @ticket 65819 99 */ 100 public function test_returns_false_for_non_ancestors() { 101 // Child is not an ancestor of its parent or grandparent. 102 $this->assertFalse( cat_is_ancestor_of( self::$child->term_id, self::$parent->term_id ) ); 103 $this->assertFalse( cat_is_ancestor_of( self::$child->term_id, self::$grandparent->term_id ) ); 104 $this->assertFalse( cat_is_ancestor_of( self::$child, self::$parent ) ); 105 106 // A category is not an ancestor of itself. 107 $this->assertFalse( cat_is_ancestor_of( self::$parent->term_id, self::$parent->term_id ) ); 108 $this->assertFalse( cat_is_ancestor_of( self::$parent, self::$parent ) ); 109 110 // Unrelated categories. 111 $this->assertFalse( cat_is_ancestor_of( self::$unrelated->term_id, self::$child->term_id ) ); 112 $this->assertFalse( cat_is_ancestor_of( self::$parent->term_id, self::$unrelated->term_id ) ); 113 114 // Non-existent category IDs. 115 $this->assertFalse( cat_is_ancestor_of( 99999999, self::$child->term_id ) ); 116 $this->assertFalse( cat_is_ancestor_of( self::$parent->term_id, 99999999 ) ); 80 'slug' => 'unrelated-category', 81 ) 82 ); 83 84 self::$tag_id = $factory->tag->create( 85 array( 86 'name' => 'Test Tag', 87 'slug' => 'test-tag', 88 ) 89 ); 90 } 91 92 /** 93 * Tests that cat_is_ancestor_of() returns true for a direct parent category. 94 * 95 * @ticket 65819 96 */ 97 public function test_returns_true_for_direct_parent() { 98 $this->assertTrue( cat_is_ancestor_of( self::$parent_id, self::$child_id ) ); 99 $this->assertTrue( cat_is_ancestor_of( self::$grandparent_id, self::$parent_id ) ); 100 } 101 102 /** 103 * Tests that cat_is_ancestor_of() returns true for a grandparent category. 104 * 105 * @ticket 65819 106 */ 107 public function test_returns_true_for_grandparent() { 108 $this->assertTrue( cat_is_ancestor_of( self::$grandparent_id, self::$child_id ) ); 109 } 110 111 /** 112 * Tests that cat_is_ancestor_of() returns false when cat1 is not an ancestor of cat2. 113 * 114 * @ticket 65819 115 */ 116 public function test_returns_false_when_not_an_ancestor() { 117 // Child is not an ancestor of parent or grandparent. 118 $this->assertFalse( cat_is_ancestor_of( self::$child_id, self::$parent_id ) ); 119 $this->assertFalse( cat_is_ancestor_of( self::$child_id, self::$grandparent_id ) ); 120 121 // Unrelated category is not an ancestor. 122 $this->assertFalse( cat_is_ancestor_of( self::$unrelated_id, self::$child_id ) ); 123 $this->assertFalse( cat_is_ancestor_of( self::$child_id, self::$unrelated_id ) ); 124 } 125 126 /** 127 * Tests that cat_is_ancestor_of() returns false when comparing a category to itself. 128 * 129 * @ticket 65819 130 */ 131 public function test_returns_false_for_same_category() { 132 $this->assertFalse( cat_is_ancestor_of( self::$parent_id, self::$parent_id ) ); 133 } 134 135 /** 136 * Tests that cat_is_ancestor_of() accepts category IDs and WP_Term objects in any combination. 137 * 138 * @ticket 65819 139 * 140 * @dataProvider data_category_parameter_types 141 * 142 * @param string $parent_type Parameter type for parent category. 143 * @param string $child_type Parameter type for child category. 144 */ 145 public function test_accepts_category_objects_and_ids( $parent_type, $child_type ) { 146 $parent = ( 'object' === $parent_type ) ? get_term( self::$parent_id, 'category' ) : self::$parent_id; 147 $child = ( 'object' === $child_type ) ? get_term( self::$child_id, 'category' ) : self::$child_id; 148 149 $this->assertTrue( cat_is_ancestor_of( $parent, $child ) ); 150 } 151 152 /** 153 * Data provider for test_accepts_category_objects_and_ids(). 154 * 155 * @return array[] 156 */ 157 public static function data_category_parameter_types() { 158 return array( 159 'both category IDs' => array( 'id', 'id' ), 160 'both WP_Term objects' => array( 'object', 'object' ), 161 'parent ID and child WP_Term object' => array( 'id', 'object' ), 162 'parent WP_Term object and child ID' => array( 'object', 'id' ), 163 ); 164 } 165 166 /** 167 * Tests that cat_is_ancestor_of() returns false when either category does not exist. 168 * 169 * @ticket 65819 170 */ 171 public function test_returns_false_for_non_existent_category() { 172 $this->assertFalse( cat_is_ancestor_of( 999999, self::$child_id ) ); 173 $this->assertFalse( cat_is_ancestor_of( self::$parent_id, 999999 ) ); 174 $this->assertFalse( cat_is_ancestor_of( 999998, 999999 ) ); 175 } 176 177 /** 178 * Tests that cat_is_ancestor_of() returns false for invalid or empty inputs. 179 * 180 * @ticket 65819 181 * 182 * @dataProvider data_invalid_inputs 183 * 184 * @param mixed $cat1 Invalid category 1. 185 * @param mixed $cat2 Invalid category 2. 186 */ 187 public function test_returns_false_for_invalid_inputs( $cat1, $cat2 ) { 188 $this->assertFalse( cat_is_ancestor_of( $cat1, $cat2 ) ); 189 } 190 191 /** 192 * Data provider for test_returns_false_for_invalid_inputs(). 193 * 194 * @return array[] 195 */ 196 public static function data_invalid_inputs() { 197 return array( 198 'both integer zeros' => array( 0, 0 ), 199 'empty strings' => array( '', '' ), 200 'null values' => array( null, null ), 201 'boolean false values' => array( false, false ), 202 'valid parent and zero child' => array( 1, 0 ), 203 'zero parent and valid child' => array( 0, 1 ), 204 ); 205 } 206 207 /** 208 * Tests that cat_is_ancestor_of() returns false when passed a term from another taxonomy. 209 * 210 * @ticket 65819 211 */ 212 public function test_returns_false_for_term_from_different_taxonomy() { 213 $this->assertFalse( cat_is_ancestor_of( self::$tag_id, self::$child_id ) ); 214 $this->assertFalse( cat_is_ancestor_of( self::$parent_id, self::$tag_id ) ); 117 215 } 118 216
Note:
See TracChangeset
for help on using the changeset viewer.