| 1 | <?php |
| 2 | /** |
| 3 | * Validate the logic of get_comments_pages_count |
| 4 | * @group comment |
| 5 | */ |
| 6 | class Tests_Comment_GetCommentsPagesCount extends WP_UnitTestCase { |
| 7 | protected $option_page_comments; |
| 8 | protected $option_comments_per_page; |
| 9 | protected $option_thread_comments; |
| 10 | protected $option_posts_per_rss; |
| 11 | |
| 12 | /** |
| 13 | * setUp options |
| 14 | */ |
| 15 | function setUp() { |
| 16 | parent::setUp(); |
| 17 | $this->option_page_comments = get_option( 'page_comments' ); |
| 18 | $this->option_page_comments = get_option( 'comments_per_page' ); |
| 19 | $this->option_page_comments = get_option( 'thread_comments' ); |
| 20 | $this->option_posts_per_rss = get_option( 'posts_per_rss' ); |
| 21 | |
| 22 | update_option( 'page_comments', true ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * tearDown options |
| 27 | */ |
| 28 | function tearDown() { |
| 29 | parent::tearDown(); |
| 30 | update_option( 'page_comments', $this->option_page_comments ); |
| 31 | update_option( 'comments_per_page', $this->option_page_comments ); |
| 32 | update_option( 'thread_comments', $this->option_page_comments ); |
| 33 | update_option( 'posts_per_rss', $this->option_posts_per_rss ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Validate get_comments_pages_count for empty comments |
| 38 | */ |
| 39 | function test_empty() { |
| 40 | //setup post and comments |
| 41 | $post_id = $this->factory->post->create( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) ); |
| 42 | $this->go_to( '/?p=' . $post_id ); |
| 43 | |
| 44 | global $wp_query; |
| 45 | unset( $wp_query->comments ); |
| 46 | |
| 47 | $comments = get_comments( array( 'post_id' => $post_id ) ); |
| 48 | |
| 49 | $this->assertEquals( 0, get_comment_pages_count( $comments, 10, false ) ); |
| 50 | $this->assertEquals( 0, get_comment_pages_count( $comments, 1, false ) ); |
| 51 | $this->assertEquals( 0, get_comment_pages_count( $comments, 0, false ) ); |
| 52 | $this->assertEquals( 0, get_comment_pages_count( $comments, 10, true ) ); |
| 53 | $this->assertEquals( 0, get_comment_pages_count( $comments, 5 ) ); |
| 54 | $this->assertEquals( 0, get_comment_pages_count( $comments ) ); |
| 55 | $this->assertequals( 0, get_comment_pages_count( null, 1 ) ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Validate get_comments_pages_count for treaded comments |
| 60 | */ |
| 61 | function test_threaded_comments( ) { |
| 62 | //setup post and comments |
| 63 | $post = $this->factory->post->create_and_get( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) ); |
| 64 | $comments = $this->factory->comment->create_post_comments( $post->ID, 15 ); |
| 65 | $this->factory->comment->create_post_comments( $post->ID, 6, array( 'comment_parent' => $comments[0] ) ); |
| 66 | $comments = get_comments( array( 'post_id' => $post->ID ) ); |
| 67 | |
| 68 | $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) ); |
| 69 | $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) ); |
| 70 | $this->assertEquals( 4, get_comment_pages_count( $comments, 4, true ) ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Validate get_comments_pages_count for option page_comments |
| 75 | */ |
| 76 | function test_option_page_comments() { |
| 77 | //setup post and comments |
| 78 | $post = $this->factory->post->create_and_get( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) ); |
| 79 | $comments = $this->factory->comment->create_post_comments( $post->ID, 15 ); |
| 80 | |
| 81 | // comment paging disabled |
| 82 | update_option( 'page_comments', false ); |
| 83 | |
| 84 | $this->assertEquals( 1, get_comment_pages_count( $comments, 10, false ) ); |
| 85 | $this->assertEquals( 1, get_comment_pages_count( $comments, 0, true ) ); |
| 86 | $this->assertEquals( 1, get_comment_pages_count( $comments, 2, true ) ); |
| 87 | |
| 88 | // comment paging enabled |
| 89 | update_option( 'page_comments', true ); |
| 90 | |
| 91 | $this->assertEquals( 2, get_comment_pages_count( $comments, 10, false ) ); |
| 92 | $this->assertEquals( 3, get_comment_pages_count( $comments, 5, false ) ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Validate get_comments_pages_count for option tread_comments |
| 97 | */ |
| 98 | function test_option_thread_comments() { |
| 99 | |
| 100 | //setup post and comments |
| 101 | $post = $this->factory->post->create_and_get( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) ); |
| 102 | $comments = $this->factory->comment->create_post_comments( $post->ID, 15 ); |
| 103 | $this->factory->comment->create_post_comments( $post->ID, 6, array('comment_parent' => $comments[0] ) ); |
| 104 | $comments = get_comments( array( 'post_id' => $post->ID ) ); |
| 105 | |
| 106 | update_option( 'thread_comments', false ); |
| 107 | |
| 108 | $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) ); |
| 109 | $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) ); |
| 110 | $this->assertEquals( 3, get_comment_pages_count( $comments, 10, null ) ); |
| 111 | $this->assertEquals( 3, get_comment_pages_count( $comments, 10 ) ); |
| 112 | |
| 113 | update_option( 'thread_comments', true ); |
| 114 | |
| 115 | $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) ); |
| 116 | $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) ); |
| 117 | $this->assertEquals( 2, get_comment_pages_count( $comments, 10, null ) ); |
| 118 | $this->assertEquals( 2, get_comment_pages_count( $comments, 10 ) ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Validate $wp_query logic of get_comment_pages_count |
| 123 | */ |
| 124 | function test_wp_query_comments_per_page() { |
| 125 | global $wp_query; |
| 126 | |
| 127 | update_option( 'posts_per_rss', 100 ); |
| 128 | |
| 129 | $post = $this->factory->post->create_and_get( array( 'post_title' => 'comment-post', 'post_type' => 'post' ) ); |
| 130 | $comments = $this->factory->comment->create_post_comments( $post->ID, 25 ); |
| 131 | |
| 132 | $wp_query = new WP_Query( array( 'p' => $post->ID, 'comments_per_page' => 10, 'feed' =>'comments-' ) ); |
| 133 | |
| 134 | update_option( 'comments_per_page', 25 ); |
| 135 | |
| 136 | $this->assertEquals( 3, get_comment_pages_count() ); |
| 137 | $this->assertEquals( 2, get_comment_pages_count( null, 20 ) ); |
| 138 | |
| 139 | $wp_query = new WP_Query( array( 'p' => $post->ID,'comments_per_page' => null, 'feed' =>'comments-' ) ); |
| 140 | |
| 141 | $this->assertEquals( 1, get_comment_pages_count() ); |
| 142 | $this->assertEquals( 5, get_comment_pages_count( null, 5 ) ); |
| 143 | |
| 144 | $wp_query->query_vars['comments_per_page'] = null; |
| 145 | |
| 146 | update_option( 'comments_per_page', 5 ); |
| 147 | |
| 148 | $this->assertEquals( 5, get_comment_pages_count() ); |
| 149 | $this->assertEquals( 3, get_comment_pages_count( null, 11 ) ); |
| 150 | $this->assertEquals( 5, get_comment_pages_count( null, 0 ) ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Validate max_num_comment_pages logic of get_comment_pages_count |
| 155 | */ |
| 156 | function test_max_num_comment_pages() { |
| 157 | global $wp_query; |
| 158 | $wp_query = new WP_Query(); |
| 159 | |
| 160 | $org_max_num_comment_pages = $wp_query->max_num_comment_pages; |
| 161 | |
| 162 | $wp_query->max_num_comment_pages = 7; |
| 163 | |
| 164 | $this->assertEquals( 7, get_comment_pages_count() ); |
| 165 | $this->assertEquals( 7, get_comment_pages_count( null, null, null ) ); |
| 166 | $this->assertEquals( 0, get_comment_pages_count( array(), null, null ) ); |
| 167 | |
| 168 | $wp_query->max_num_comment_pages = $org_max_num_comment_pages; |
| 169 | } |
| 170 | } |
| 171 | No newline at end of file |