Skip to content

Commit 45d6c69

Browse files
committed
Tests: Add coverage for separate_comments().
Cover default groups, comment types, combined pings, and empty and custom types. Verify that grouped elements retain references to the input array. Developed in: #14330 Props rejaulalomkhan. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64426 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b7d8232 commit 45d6c69

1 file changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/**
4+
* Tests for separate_comments().
5+
*
6+
* @group comment
7+
*
8+
* @covers ::separate_comments
9+
*/
10+
class Tests_Comment_SeparateComments extends WP_UnitTestCase {
11+
12+
/**
13+
* Post the comments belong to.
14+
*
15+
* @var int
16+
*/
17+
protected static $post_id;
18+
19+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
20+
self::$post_id = $factory->post->create();
21+
}
22+
23+
/**
24+
* Creates a comment of the given type and returns it.
25+
*
26+
* @param string $type Comment type.
27+
* @return WP_Comment The created comment.
28+
*/
29+
private function create_comment( $type ) {
30+
return self::factory()->comment->create_and_get(
31+
array(
32+
'comment_post_ID' => self::$post_id,
33+
'comment_type' => $type,
34+
)
35+
);
36+
}
37+
38+
/**
39+
* Tests that an empty list returns the four default groups, all empty.
40+
*
41+
* @ticket 65819
42+
*/
43+
public function test_empty_list_returns_default_groups() {
44+
$comments = array();
45+
46+
$this->assertSame(
47+
array(
48+
'comment' => array(),
49+
'trackback' => array(),
50+
'pingback' => array(),
51+
'pings' => array(),
52+
),
53+
separate_comments( $comments )
54+
);
55+
}
56+
57+
/**
58+
* Tests that comments are grouped by type, and trackbacks and pingbacks are also grouped as pings.
59+
*
60+
* @ticket 65819
61+
*/
62+
public function test_groups_comments_by_type() {
63+
$comment = $this->create_comment( 'comment' );
64+
$trackback = $this->create_comment( 'trackback' );
65+
$pingback = $this->create_comment( 'pingback' );
66+
$comments = array( $comment, $trackback, $pingback );
67+
68+
$grouped = separate_comments( $comments );
69+
70+
$this->assertSame( array( $comment ), $grouped['comment'], 'The comment should be in the comment group only.' );
71+
$this->assertSame( array( $trackback ), $grouped['trackback'], 'The trackback should be in the trackback group.' );
72+
$this->assertSame( array( $pingback ), $grouped['pingback'], 'The pingback should be in the pingback group.' );
73+
$this->assertSame( array( $trackback, $pingback ), $grouped['pings'], 'Trackbacks and pingbacks should both be in the pings group, in order.' );
74+
}
75+
76+
/**
77+
* Tests that a comment with an empty type is grouped as a comment.
78+
*
79+
* @ticket 65819
80+
*/
81+
public function test_empty_type_is_grouped_as_comment() {
82+
$comment = $this->create_comment( 'comment' );
83+
$comment->comment_type = '';
84+
$comments = array( $comment );
85+
86+
$grouped = separate_comments( $comments );
87+
88+
$this->assertSame( array( $comment ), $grouped['comment'], 'A comment with an empty type should be in the comment group.' );
89+
$this->assertArrayNotHasKey( '', $grouped, 'There should be no group for an empty type.' );
90+
}
91+
92+
/**
93+
* Tests that a custom comment type gets its own group and is not added to pings.
94+
*
95+
* @ticket 65819
96+
*/
97+
public function test_custom_type_gets_its_own_group() {
98+
$note = $this->create_comment( 'note' );
99+
$comments = array( $note );
100+
101+
$grouped = separate_comments( $comments );
102+
103+
$this->assertSame( array( $note ), $grouped['note'], 'A custom type should get its own group.' );
104+
$this->assertSame( array(), $grouped['comment'], 'A custom type should not be in the comment group.' );
105+
$this->assertSame( array(), $grouped['pings'], 'A custom type should not be in the pings group.' );
106+
}
107+
108+
/**
109+
* Tests that the groups hold references to the elements of the passed array.
110+
*
111+
* @ticket 65819
112+
*/
113+
public function test_groups_hold_references_to_the_passed_array() {
114+
$comments = array( $this->create_comment( 'pingback' ) );
115+
116+
$grouped = separate_comments( $comments );
117+
118+
$replacement = $this->create_comment( 'comment' );
119+
$grouped['pingback'][0] = $replacement;
120+
121+
$this->assertSame( $replacement, $comments[0], 'Replacing an element in a group should replace it in the passed array.' );
122+
$this->assertSame( $replacement, $grouped['pings'][0], 'The pings group should reference the same element.' );
123+
}
124+
}

0 commit comments

Comments
 (0)