| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group comment |
| 5 | */ |
| 6 | class Tests_Comment_WpListComments extends WP_UnitTestCase { |
| 7 | /** |
| 8 | * @ticket 35175 |
| 9 | */ |
| 10 | public function test_should_respect_page_param() { |
| 11 | $p = self::factory()->post->create(); |
| 12 | |
| 13 | $comments = array(); |
| 14 | $now = time(); |
| 15 | for ( $i = 0; $i <= 5; $i++ ) { |
| 16 | $comments[] = self::factory()->comment->create( array( |
| 17 | 'comment_post_ID' => $p, |
| 18 | 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - $i ), |
| 19 | 'comment_author' => 'Commenter ' . $i, |
| 20 | ) ); |
| 21 | } |
| 22 | |
| 23 | update_option( 'page_comments', true ); |
| 24 | update_option( 'comments_per_page', 2 ); |
| 25 | |
| 26 | $this->go_to( get_permalink( $p ) ); |
| 27 | |
| 28 | // comments_template() populates $wp_query->comments |
| 29 | get_echo( 'comments_template' ); |
| 30 | |
| 31 | $found = wp_list_comments( array( |
| 32 | 'page' => 2, |
| 33 | 'echo' => false, |
| 34 | ) ); |
| 35 | |
| 36 | preg_match_all( '|id="comment\-([0-9]+)"|', $found, $matches ); |
| 37 | |
| 38 | $this->assertEqualSets( array( $comments[2], $comments[3] ), $matches[1] ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @ticket 35175 |
| 43 | */ |
| 44 | public function test_should_respect_per_page_param() { |
| 45 | $p = self::factory()->post->create(); |
| 46 | |
| 47 | $comments = array(); |
| 48 | $now = time(); |
| 49 | for ( $i = 0; $i <= 5; $i++ ) { |
| 50 | $comments[] = self::factory()->comment->create( array( |
| 51 | 'comment_post_ID' => $p, |
| 52 | 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - $i ), |
| 53 | 'comment_author' => 'Commenter ' . $i, |
| 54 | ) ); |
| 55 | } |
| 56 | |
| 57 | update_option( 'page_comments', true ); |
| 58 | update_option( 'comments_per_page', 2 ); |
| 59 | |
| 60 | $this->go_to( get_permalink( $p ) ); |
| 61 | |
| 62 | // comments_template() populates $wp_query->comments |
| 63 | get_echo( 'comments_template' ); |
| 64 | |
| 65 | $found = wp_list_comments( array( |
| 66 | 'per_page' => 3, |
| 67 | 'echo' => false, |
| 68 | ) ); |
| 69 | |
| 70 | preg_match_all( '|id="comment\-([0-9]+)"|', $found, $matches ); |
| 71 | |
| 72 | $this->assertEqualSets( array( $comments[0], $comments[1], $comments[2] ), $matches[1] ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @ticket 35175 |
| 77 | */ |
| 78 | public function test_should_respect_reverse_top_level_param() { |
| 79 | $p = self::factory()->post->create(); |
| 80 | |
| 81 | $comments = array(); |
| 82 | $now = time(); |
| 83 | for ( $i = 0; $i <= 5; $i++ ) { |
| 84 | $comments[] = self::factory()->comment->create( array( |
| 85 | 'comment_post_ID' => $p, |
| 86 | 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - $i ), |
| 87 | 'comment_author' => 'Commenter ' . $i, |
| 88 | ) ); |
| 89 | } |
| 90 | |
| 91 | update_option( 'page_comments', true ); |
| 92 | update_option( 'comments_per_page', 2 ); |
| 93 | |
| 94 | $this->go_to( get_permalink( $p ) ); |
| 95 | |
| 96 | // comments_template() populates $wp_query->comments |
| 97 | get_echo( 'comments_template' ); |
| 98 | |
| 99 | $found1 = wp_list_comments( array( |
| 100 | 'reverse_top_level' => true, |
| 101 | 'echo' => false, |
| 102 | ) ); |
| 103 | preg_match_all( '|id="comment\-([0-9]+)"|', $found1, $matches ); |
| 104 | $this->assertSame( array( $comments[0], $comments[1] ), array_map( 'intval', $matches[1] ) ); |
| 105 | |
| 106 | $found2 = wp_list_comments( array( |
| 107 | 'reverse_top_level' => false, |
| 108 | 'echo' => false, |
| 109 | ) ); |
| 110 | preg_match_all( '|id="comment\-([0-9]+)"|', $found2, $matches ); |
| 111 | $this->assertSame( array( $comments[1], $comments[0] ), array_map( 'intval', $matches[1] ) ); |
| 112 | } |
| 113 | } |