Skip to content

Commit d57569e

Browse files
committed
Tests: Add coverage for get_category().
Covers category lookup by ID and object, array output, edit-con filtering, and invalid or wrong-taxonomy inputs. Developed in: #14363 Props shailu25. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64456 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b877d72 commit d57569e

1 file changed

Lines changed: 187 additions & 0 deletions

File tree

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
/**
4+
* Tests for get_category().
5+
*
6+
* @group taxonomy
7+
* @group category
8+
*
9+
* @covers ::get_category
10+
*/
11+
class Tests_Category_GetCategory extends WP_UnitTestCase {
12+
13+
/**
14+
* Parent category ID.
15+
*
16+
* @var int
17+
*/
18+
private static $parent_id;
19+
20+
/**
21+
* Child category object.
22+
*
23+
* @var WP_Term
24+
*/
25+
private static $category;
26+
27+
/**
28+
* Creates shared category fixtures.
29+
*
30+
* @param WP_UnitTest_Factory $factory Test factory.
31+
*/
32+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
33+
self::$parent_id = $factory->category->create(
34+
array(
35+
'name' => 'Parent Category',
36+
'slug' => 'parent-category',
37+
)
38+
);
39+
40+
self::$category = $factory->category->create_and_get(
41+
array(
42+
'name' => 'Child Category',
43+
'slug' => 'child-category',
44+
'description' => 'Category description',
45+
'parent' => self::$parent_id,
46+
)
47+
);
48+
}
49+
50+
/**
51+
* Tests that get_category() returns a WP_Term object with backwards-compatible properties by default.
52+
*
53+
* @ticket 65819
54+
*/
55+
public function test_returns_category_object_with_compat_properties() {
56+
$category = get_category( self::$category->term_id );
57+
58+
$this->assertInstanceOf( WP_Term::class, $category );
59+
$this->assertSame( 'category', $category->taxonomy );
60+
$this->assertSame( 'raw', $category->filter );
61+
$this->assertSame( self::$category->term_id, $category->cat_ID );
62+
$this->assertSame( self::$category->count, $category->category_count );
63+
$this->assertSame( 'Category description', $category->category_description );
64+
$this->assertSame( 'Child Category', $category->cat_name );
65+
$this->assertSame( 'child-category', $category->category_nicename );
66+
$this->assertSame( self::$parent_id, $category->category_parent );
67+
}
68+
69+
/**
70+
* Tests that get_category() accepts a term object and populates backwards-compatible properties.
71+
*
72+
* @ticket 65819
73+
*/
74+
public function test_accepts_category_object() {
75+
$raw_object = (object) self::$category->to_array();
76+
77+
$category = get_category( $raw_object );
78+
79+
$this->assertInstanceOf( WP_Term::class, $category );
80+
$this->assertSame( self::$category->term_id, $category->cat_ID );
81+
$this->assertSame( 'Child Category', $category->cat_name );
82+
$this->assertSame( 'child-category', $category->category_nicename );
83+
$this->assertSame( self::$parent_id, $category->category_parent );
84+
}
85+
86+
/**
87+
* Tests that ARRAY_A output returns an associative array with backwards-compatible alias keys.
88+
*
89+
* @ticket 65819
90+
*/
91+
public function test_returns_associative_array_with_compat_keys() {
92+
$category = get_category( self::$category->term_id, ARRAY_A );
93+
94+
$this->assertIsArray( $category );
95+
$this->assertSame( self::$category->term_id, $category['term_id'] );
96+
$this->assertSame( self::$category->term_id, $category['cat_ID'] );
97+
$this->assertSame( self::$category->count, $category['category_count'] );
98+
$this->assertSame( 'Category description', $category['category_description'] );
99+
$this->assertSame( 'Child Category', $category['cat_name'] );
100+
$this->assertSame( 'child-category', $category['category_nicename'] );
101+
$this->assertSame( self::$parent_id, $category['category_parent'] );
102+
}
103+
104+
/**
105+
* Tests that ARRAY_N output returns a numerically indexed array of term values.
106+
*
107+
* @ticket 65819
108+
*/
109+
public function test_returns_numeric_array() {
110+
$category = get_category( self::$category->term_id, ARRAY_N );
111+
112+
$this->assertIsArray( $category );
113+
$this->assertSame( array_values( self::$category->to_array() ), $category );
114+
}
115+
116+
/**
117+
* Tests that the filter parameter sanitizes the returned category in the requested context.
118+
*
119+
* @ticket 65819
120+
*/
121+
public function test_applies_filter_context() {
122+
add_filter(
123+
'edit_category_name',
124+
static function ( $name ) {
125+
return $name . ' (edited)';
126+
}
127+
);
128+
129+
$category = get_category( self::$category->term_id, OBJECT, 'edit' );
130+
131+
$this->assertInstanceOf( WP_Term::class, $category );
132+
$this->assertSame( 'edit', $category->filter );
133+
$this->assertSame( 'Child Category (edited)', $category->name );
134+
$this->assertSame( 'Child Category (edited)', $category->cat_name );
135+
}
136+
137+
/**
138+
* Tests that empty category values return an invalid_term WP_Error.
139+
*
140+
* @ticket 65819
141+
*
142+
* @dataProvider data_empty_category_values
143+
*
144+
* @param mixed $empty_value Empty category input.
145+
*/
146+
public function test_returns_wp_error_for_empty_category( $empty_value ) {
147+
$result = get_category( $empty_value );
148+
149+
$this->assertWPError( $result );
150+
$this->assertSame( 'invalid_term', $result->get_error_code() );
151+
}
152+
153+
/**
154+
* Data provider.
155+
*
156+
* @return array[]
157+
*/
158+
public static function data_empty_category_values() {
159+
return array(
160+
'empty string' => array( '' ),
161+
'zero int' => array( 0 ),
162+
'zero string' => array( '0' ),
163+
'null' => array( null ),
164+
'false' => array( false ),
165+
);
166+
}
167+
168+
/**
169+
* Tests that a non-existent category ID returns null.
170+
*
171+
* @ticket 65819
172+
*/
173+
public function test_returns_null_for_non_existent_category() {
174+
$this->assertNull( get_category( 99999999 ) );
175+
}
176+
177+
/**
178+
* Tests that a term ID belonging to another taxonomy returns null.
179+
*
180+
* @ticket 65819
181+
*/
182+
public function test_returns_null_for_term_in_another_taxonomy() {
183+
$tag_id = self::factory()->tag->create();
184+
185+
$this->assertNull( get_category( $tag_id ) );
186+
}
187+
}

0 commit comments

Comments
 (0)