Skip to content

Commit ffa4c0a

Browse files
committed
Tests: Cover has_tag().
Verify matching by ID, name, slug, and arrays, explicit and global posts, empty tags, nonmatches, and numeric-name versus integer-ID behavior. Developed in: #14428 Props rejaulalomkhan. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64523 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a3f014c commit ffa4c0a

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
/**
4+
* Tests for has_tag().
5+
*
6+
* @group taxonomy
7+
* @group post_tag
8+
*
9+
* @covers ::has_tag
10+
*/
11+
class Tests_Category_HasTag extends WP_UnitTestCase {
12+
13+
/**
14+
* Tag object assigned to post.
15+
*
16+
* @var WP_Term
17+
*/
18+
private static $tag;
19+
20+
/**
21+
* Unassigned tag object.
22+
*
23+
* @var WP_Term
24+
*/
25+
private static $other_tag;
26+
27+
/**
28+
* Post object assigned to self::$tag.
29+
*
30+
* @var WP_Post
31+
*/
32+
private static $post;
33+
34+
/**
35+
* Creates shared tag and post fixtures.
36+
*
37+
* @param WP_UnitTest_Factory $factory Test factory.
38+
*/
39+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
40+
self::$tag = $factory->tag->create_and_get(
41+
array(
42+
'name' => 'Assigned Tag',
43+
'slug' => 'assigned-tag',
44+
)
45+
);
46+
47+
self::$other_tag = $factory->tag->create_and_get(
48+
array(
49+
'name' => 'Other Tag',
50+
'slug' => 'other-tag',
51+
)
52+
);
53+
54+
self::$post = $factory->post->create_and_get();
55+
wp_set_object_terms( self::$post->ID, self::$tag->term_id, 'post_tag' );
56+
}
57+
58+
/**
59+
* Tests that has_tag() returns true when the post has the specified tag
60+
* by ID, name, slug, or an array of values, and accepts a post ID or WP_Post object.
61+
*
62+
* @ticket 65819
63+
*/
64+
public function test_returns_true_when_post_has_matching_tag() {
65+
$this->assertTrue( has_tag( self::$tag->term_id, self::$post->ID ) );
66+
$this->assertTrue( has_tag( self::$tag->name, self::$post->ID ) );
67+
$this->assertTrue( has_tag( self::$tag->slug, self::$post->ID ) );
68+
$this->assertTrue( has_tag( array( self::$other_tag->slug, self::$tag->slug ), self::$post->ID ) );
69+
$this->assertTrue( has_tag( self::$tag->term_id, self::$post ) );
70+
}
71+
72+
/**
73+
* Tests that has_tag() with an empty tag check returns true when the post
74+
* has at least one tag and false when it has no tags.
75+
*
76+
* @ticket 65819
77+
*/
78+
public function test_empty_tag_checks_if_post_has_any_tag() {
79+
$this->assertTrue( has_tag( '', self::$post->ID ) );
80+
81+
$untagged_post_id = self::factory()->post->create();
82+
83+
$this->assertFalse( has_tag( '', $untagged_post_id ) );
84+
}
85+
86+
/**
87+
* Tests that has_tag() defaults to the global post when no post parameter is passed.
88+
*
89+
* @ticket 65819
90+
*/
91+
public function test_defaults_to_global_post_when_post_is_omitted() {
92+
$GLOBALS['post'] = self::$post;
93+
94+
$this->assertTrue( has_tag( self::$tag->term_id ) );
95+
$this->assertFalse( has_tag( self::$other_tag->term_id ) );
96+
}
97+
98+
/**
99+
* Tests that has_tag() returns false when the post does not have the tag
100+
* or when the post does not exist.
101+
*
102+
* @ticket 65819
103+
*/
104+
public function test_returns_false_for_non_matching_tag_or_invalid_post() {
105+
$this->assertFalse( has_tag( self::$other_tag->term_id, self::$post->ID ) );
106+
$this->assertFalse( has_tag( self::$other_tag->name, self::$post->ID ) );
107+
$this->assertFalse( has_tag( self::$other_tag->slug, self::$post->ID ) );
108+
$this->assertFalse( has_tag( self::$tag->term_id, 99999999 ) );
109+
}
110+
111+
/**
112+
* Tests that an integer tag argument only checks against term IDs, not numeric names or slugs.
113+
*
114+
* @ticket 65819
115+
*/
116+
public function test_integer_tag_matches_only_term_id_not_numeric_name_or_slug() {
117+
$numeric_name = '12345';
118+
$tag = self::factory()->tag->create_and_get(
119+
array(
120+
'name' => $numeric_name,
121+
'slug' => $numeric_name,
122+
)
123+
);
124+
125+
$post_id = self::factory()->post->create();
126+
wp_set_object_terms( $post_id, $tag->term_id, 'post_tag' );
127+
128+
// String matches the name/slug.
129+
$this->assertTrue( has_tag( $numeric_name, $post_id ) );
130+
131+
// Integer does not match because term_id is different from the numeric name.
132+
$this->assertFalse( has_tag( (int) $numeric_name, $post_id ) );
133+
}
134+
}

0 commit comments

Comments
 (0)