Skip to content

Commit 0650e51

Browse files
committed
Tests: Cover comment types and author IP output.
Verify comment-type normalization, global comment lookup, filters, and escaped author IP output. Developed in: #15253, #15254 Props shailu25. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@65131 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e7ee700 commit 0650e51

2 files changed

Lines changed: 219 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* Tests for get_comment_author_IP() and comment_author_IP().
5+
*
6+
* @group comment
7+
*/
8+
class Tests_Comment_GetCommentAuthorIP 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_IP' => '192.0.2.10',
30+
)
31+
);
32+
}
33+
34+
/**
35+
* @ticket 65819
36+
*
37+
* @covers ::get_comment_author_IP
38+
*/
39+
public function test_returns_comment_author_ip() {
40+
$this->assertSame( '192.0.2.10', get_comment_author_IP( self::$comment->comment_ID ) );
41+
}
42+
43+
/**
44+
* @ticket 65819
45+
*
46+
* @covers ::get_comment_author_IP
47+
*/
48+
public function test_filter_can_modify_ip() {
49+
add_filter(
50+
'get_comment_author_IP',
51+
static function () {
52+
return '203.0.113.5';
53+
}
54+
);
55+
56+
$this->assertSame( '203.0.113.5', get_comment_author_IP( self::$comment->comment_ID ) );
57+
}
58+
59+
/**
60+
* @ticket 65819
61+
*
62+
* @covers ::comment_author_IP
63+
* @covers ::get_comment_author_IP
64+
*/
65+
public function test_comment_author_ip_echoes_escaped_value() {
66+
add_filter(
67+
'get_comment_author_IP',
68+
static function () {
69+
return '<b>192.0.2.10</b>';
70+
}
71+
);
72+
73+
$this->expectOutputString( '&lt;b&gt;192.0.2.10&lt;/b&gt;' );
74+
comment_author_IP( self::$comment->comment_ID );
75+
}
76+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
/**
4+
* Tests for get_comment_type() and comment_type().
5+
*
6+
* @group comment
7+
*/
8+
class Tests_Comment_GetCommentType extends WP_UnitTestCase {
9+
10+
/**
11+
* Post fixture.
12+
*
13+
* @var int
14+
*/
15+
private static $post_id;
16+
17+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
18+
self::$post_id = $factory->post->create();
19+
}
20+
21+
public function tear_down() {
22+
unset( $GLOBALS['comment'] );
23+
parent::tear_down();
24+
}
25+
26+
/**
27+
* @ticket 65819
28+
*
29+
* @covers ::get_comment_type
30+
*/
31+
public function test_empty_type_normalizes_to_comment() {
32+
$comment = self::factory()->comment->create_and_get(
33+
array(
34+
'comment_post_ID' => self::$post_id,
35+
'comment_type' => 'pingback',
36+
)
37+
);
38+
39+
// Bypass wp_insert_comment() normalization so get_comment_type() is exercised.
40+
$comment->comment_type = '';
41+
42+
$this->assertSame( 'comment', get_comment_type( $comment ) );
43+
}
44+
45+
/**
46+
* @ticket 65819
47+
*
48+
* @covers ::get_comment_type
49+
*/
50+
public function test_filter_can_modify_comment_type() {
51+
$comment_id = self::factory()->comment->create(
52+
array(
53+
'comment_post_ID' => self::$post_id,
54+
'comment_type' => 'comment',
55+
)
56+
);
57+
58+
add_filter(
59+
'get_comment_type',
60+
static function () {
61+
return 'filtered-type';
62+
}
63+
);
64+
65+
$this->assertSame( 'filtered-type', get_comment_type( $comment_id ) );
66+
}
67+
68+
/**
69+
* @ticket 65819
70+
*
71+
* @covers ::comment_type
72+
* @covers ::get_comment_type
73+
*
74+
* @dataProvider data_default_labels
75+
*
76+
* @param string $comment_type Comment type fixture.
77+
* @param string $expected Expected echoed label.
78+
*/
79+
public function test_comment_type_echoes_default_labels( $comment_type, $expected ) {
80+
$comment = self::factory()->comment->create_and_get(
81+
array(
82+
'comment_post_ID' => self::$post_id,
83+
'comment_type' => $comment_type,
84+
)
85+
);
86+
87+
$GLOBALS['comment'] = $comment;
88+
89+
$this->expectOutputString( $expected );
90+
comment_type();
91+
}
92+
93+
/**
94+
* Data provider.
95+
*
96+
* @return array[]
97+
*/
98+
public static function data_default_labels() {
99+
return array(
100+
'comment' => array( 'comment', 'Comment' ),
101+
'pingback' => array( 'pingback', 'Pingback' ),
102+
'trackback' => array( 'trackback', 'Trackback' ),
103+
);
104+
}
105+
106+
/**
107+
* @ticket 65819
108+
*
109+
* @covers ::comment_type
110+
* @covers ::get_comment_type
111+
*
112+
* @dataProvider data_custom_labels
113+
*
114+
* @param string $comment_type Comment type fixture.
115+
* @param string $expected Expected echoed label.
116+
*/
117+
public function test_comment_type_echoes_custom_labels( $comment_type, $expected ) {
118+
$comment = self::factory()->comment->create_and_get(
119+
array(
120+
'comment_post_ID' => self::$post_id,
121+
'comment_type' => $comment_type,
122+
)
123+
);
124+
125+
$GLOBALS['comment'] = $comment;
126+
127+
$this->expectOutputString( $expected );
128+
comment_type( 'Custom Comment', 'Custom Trackback', 'Custom Pingback' );
129+
}
130+
131+
/**
132+
* Data provider.
133+
*
134+
* @return array[]
135+
*/
136+
public static function data_custom_labels() {
137+
return array(
138+
'comment' => array( 'comment', 'Custom Comment' ),
139+
'pingback' => array( 'pingback', 'Custom Pingback' ),
140+
'trackback' => array( 'trackback', 'Custom Trackback' ),
141+
);
142+
}
143+
}

0 commit comments

Comments
 (0)