Skip to content

Commit 6c132f5

Browse files
committed
Tests: Add coverage for get_tag().
Covers tag lookup by ID and object, array output, edit-con filtering, and invalid or wrong-taxonomy inputs. Developed in: #14375 Props rejaulalomkhan, mukesh27. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64467 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 795df58 commit 6c132f5

1 file changed

Lines changed: 189 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)