Make WordPress Core

Ticket #20633: getCommentsPagesCount.php

File getCommentsPagesCount.php, 6.8 KB (added by mdbitz, 10 years ago)

Unit Tests of get_comment_pages_count function

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