Skip to content

Commit 476aabf

Browse files
committed
Tests: Cover comment author and ID helper output.
Verify comment email, URL, and ID retrieval and display, including global comment handling and filtered output. Developed in: #15274, #15276, #15280 Props shailu25. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@65137 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b10c571 commit 476aabf

3 files changed

Lines changed: 327 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
/**
4+
* Tests for comment_author_url() and get_comment_author_url().
5+
*
6+
* @group comment
7+
*/
8+
class Tests_Comment_CommentAuthorUrl extends WP_UnitTestCase {
9+
10+
/**
11+
* Post fixture.
12+
*
13+
* @var int
14+
*/
15+
private static $post_id;
16+
17+
/**
18+
* Comment fixture.
19+
*
20+
* @var WP_Comment
21+
*/
22+
private static $comment;
23+
24+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
25+
self::$post_id = $factory->post->create();
26+
self::$comment = $factory->comment->create_and_get(
27+
array(
28+
'comment_post_ID' => self::$post_id,
29+
'comment_author_url' => 'https://example.com/author',
30+
)
31+
);
32+
}
33+
34+
public function tear_down() {
35+
unset( $GLOBALS['comment'] );
36+
parent::tear_down();
37+
}
38+
39+
/**
40+
* @ticket 65819
41+
*
42+
* @covers ::get_comment_author_url
43+
*/
44+
public function test_returns_comment_author_url() {
45+
$this->assertSame( 'https://example.com/author', get_comment_author_url( self::$comment->comment_ID ) );
46+
}
47+
48+
/**
49+
* @ticket 65819
50+
*
51+
* @covers ::get_comment_author_url
52+
*/
53+
public function test_accepts_comment_object() {
54+
$this->assertSame( 'https://example.com/author', get_comment_author_url( self::$comment ) );
55+
}
56+
57+
/**
58+
* @ticket 65819
59+
*
60+
* @covers ::get_comment_author_url
61+
*/
62+
public function test_uses_global_comment_when_unspecified() {
63+
$GLOBALS['comment'] = self::$comment;
64+
65+
$this->assertSame( 'https://example.com/author', get_comment_author_url() );
66+
}
67+
68+
/**
69+
* @ticket 65819
70+
*
71+
* @covers ::get_comment_author_url
72+
*/
73+
public function test_returns_empty_string_when_url_missing() {
74+
$comment_id = self::factory()->comment->create(
75+
array(
76+
'comment_post_ID' => self::$post_id,
77+
'comment_author_url' => '',
78+
)
79+
);
80+
81+
$this->assertSame( '', get_comment_author_url( $comment_id ) );
82+
}
83+
84+
/**
85+
* @ticket 65819
86+
*
87+
* @covers ::get_comment_author_url
88+
*/
89+
public function test_filter_can_modify_url() {
90+
add_filter(
91+
'get_comment_author_url',
92+
static function () {
93+
return 'https://filtered.example.com';
94+
}
95+
);
96+
97+
$this->assertSame( 'https://filtered.example.com', get_comment_author_url( self::$comment->comment_ID ) );
98+
}
99+
100+
/**
101+
* @ticket 65819
102+
*
103+
* @covers ::comment_author_url
104+
* @covers ::get_comment_author_url
105+
*/
106+
public function test_comment_author_url_echoes_value() {
107+
add_filter(
108+
'comment_url',
109+
function ( $url ) {
110+
$this->assertSame( 'https://example.com/author', $url );
111+
return 'https://display.example.com';
112+
}
113+
);
114+
115+
$this->expectOutputString( 'https://display.example.com' );
116+
comment_author_url( self::$comment->comment_ID );
117+
}
118+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
/**
4+
* Tests for get_comment_author_email() and comment_author_email().
5+
*
6+
* @group comment
7+
*/
8+
class Tests_Comment_GetCommentAuthorEmail extends WP_UnitTestCase {
9+
10+
/**
11+
* Post fixture.
12+
*
13+
* @var int
14+
*/
15+
private static $post_id;
16+
17+
/**
18+
* Comment fixture.
19+
*
20+
* @var WP_Comment
21+
*/
22+
private static $comment;
23+
24+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
25+
self::$post_id = $factory->post->create();
26+
self::$comment = $factory->comment->create_and_get(
27+
array(
28+
'comment_post_ID' => self::$post_id,
29+
'comment_author_email' => 'author@example.com',
30+
)
31+
);
32+
}
33+
34+
public function tear_down() {
35+
unset( $GLOBALS['comment'] );
36+
parent::tear_down();
37+
}
38+
39+
/**
40+
* @ticket 65819
41+
*
42+
* @covers ::get_comment_author_email
43+
*/
44+
public function test_returns_comment_author_email() {
45+
$this->assertSame( 'author@example.com', get_comment_author_email( self::$comment->comment_ID ) );
46+
}
47+
48+
/**
49+
* @ticket 65819
50+
*
51+
* @covers ::get_comment_author_email
52+
*/
53+
public function test_accepts_comment_object() {
54+
$this->assertSame( 'author@example.com', get_comment_author_email( self::$comment ) );
55+
}
56+
57+
/**
58+
* @ticket 65819
59+
*
60+
* @covers ::get_comment_author_email
61+
*/
62+
public function test_uses_global_comment_when_unspecified() {
63+
$GLOBALS['comment'] = self::$comment;
64+
65+
$this->assertSame( 'author@example.com', get_comment_author_email() );
66+
}
67+
68+
/**
69+
* @ticket 65819
70+
*
71+
* @covers ::get_comment_author_email
72+
*/
73+
public function test_returns_empty_string_when_email_missing() {
74+
$comment_id = self::factory()->comment->create(
75+
array(
76+
'comment_post_ID' => self::$post_id,
77+
'comment_author_email' => '',
78+
)
79+
);
80+
81+
$this->assertSame( '', get_comment_author_email( $comment_id ) );
82+
}
83+
84+
/**
85+
* @ticket 65819
86+
*
87+
* @covers ::get_comment_author_email
88+
*/
89+
public function test_filter_can_modify_email() {
90+
add_filter(
91+
'get_comment_author_email',
92+
static function () {
93+
return 'filtered@example.com';
94+
}
95+
);
96+
97+
$this->assertSame( 'filtered@example.com', get_comment_author_email( self::$comment->comment_ID ) );
98+
}
99+
100+
/**
101+
* @ticket 65819
102+
*
103+
* @covers ::comment_author_email
104+
* @covers ::get_comment_author_email
105+
*/
106+
public function test_comment_author_email_echoes_value() {
107+
add_filter(
108+
'author_email',
109+
function ( $email ) {
110+
$this->assertSame( 'author@example.com', $email );
111+
112+
return 'display@example.com';
113+
}
114+
);
115+
116+
$this->expectOutputString( 'display@example.com' );
117+
comment_author_email( self::$comment->comment_ID );
118+
}
119+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/**
4+
* Tests for get_comment_ID() and comment_ID().
5+
*
6+
* @group comment
7+
*/
8+
class Tests_Comment_GetCommentID extends WP_UnitTestCase {
9+
10+
/**
11+
* Post fixture.
12+
*
13+
* @var int
14+
*/
15+
private static $post_id;
16+
17+
/**
18+
* Comment fixture.
19+
*
20+
* @var WP_Comment
21+
*/
22+
private static $comment;
23+
24+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
25+
self::$post_id = $factory->post->create();
26+
self::$comment = $factory->comment->create_and_get(
27+
array(
28+
'comment_post_ID' => self::$post_id,
29+
)
30+
);
31+
}
32+
33+
public function tear_down() {
34+
unset( $GLOBALS['comment'] );
35+
parent::tear_down();
36+
}
37+
38+
/**
39+
* @ticket 65819
40+
*
41+
* @covers ::get_comment_ID
42+
*/
43+
public function test_returns_comment_id_from_global_comment() {
44+
$GLOBALS['comment'] = self::$comment;
45+
46+
$this->assertSame( (string) self::$comment->comment_ID, get_comment_ID() );
47+
}
48+
49+
/**
50+
* @ticket 65819
51+
*
52+
* @covers ::get_comment_ID
53+
*/
54+
public function test_returns_zero_when_no_comment() {
55+
unset( $GLOBALS['comment'] );
56+
57+
$this->assertSame( '0', get_comment_ID() );
58+
}
59+
60+
/**
61+
* @ticket 65819
62+
*
63+
* @covers ::get_comment_ID
64+
*/
65+
public function test_filter_can_modify_comment_id() {
66+
$GLOBALS['comment'] = self::$comment;
67+
68+
add_filter(
69+
'get_comment_ID',
70+
static function () {
71+
return '999';
72+
}
73+
);
74+
75+
$this->assertSame( '999', get_comment_ID() );
76+
}
77+
78+
/**
79+
* @ticket 65819
80+
*
81+
* @covers ::comment_ID
82+
* @covers ::get_comment_ID
83+
*/
84+
public function test_comment_id_echoes_value() {
85+
$GLOBALS['comment'] = self::$comment;
86+
87+
$this->expectOutputString( (string) self::$comment->comment_ID );
88+
comment_ID();
89+
}
90+
}

0 commit comments

Comments
 (0)