Make WordPress Core

Ticket #28399: getCommentsOperator.php

File getCommentsOperator.php, 14.6 KB (added by ramon fincken, 12 years ago)

Unit test (props to robert-john van doesburg)

Line 
1<?php
2/**
3 *
4 * @group comments
5 */
6class Tests_Comment_getCommentsOperator extends WP_UnitTestCase
7{
8 function setUp() {
9 parent::setUp();
10
11 $this->post_type = 'page';
12 $this->q = new WP_Query();
13 }
14
15 function tearDown() {
16 parent::tearDown();
17 unset( $this->q );
18 }
19
20 function test_operator_equals() {
21 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
22 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
23
24 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
25 for( $i = 0; $i < 4; $i++ ){
26 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
27 }
28
29 $args = http_build_query( array(
30 'post_type' => $this->post_type,
31 'posts_per_page' => -1,
32 'comments' => array(
33 'count' => 1,
34 'compare' => '='
35 )
36 ) );
37 $this->q->query( $args );
38
39 $this->assertEquals( 1, $this->q->found_posts );
40 }
41
42 function test_operator_greater_than() {
43 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
44 for( $i = 0; $i < 4; $i++ ){
45 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
46 }
47 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
48 for( $i = 0; $i < 5; $i++ ){
49 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
50 }
51 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
52 for( $i = 0; $i < 6; $i++ ){
53 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
54 }
55
56 $args = http_build_query( array(
57 'post_type' => $this->post_type,
58 'posts_per_page' => -1,
59 'comments' => array(
60 'count' => 4,
61 'compare' => '>'
62 )
63 ) );
64 $this->q->query( $args );
65
66 $this->assertEquals( 2, $this->q->found_posts );
67 }
68
69 function test_operator_greater_than_no_results() {
70 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
71 for( $i = 0; $i < 4; $i++ ){
72 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
73 }
74 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
75 for( $i = 0; $i < 5; $i++ ){
76 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
77 }
78 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
79 for( $i = 0; $i < 6; $i++ ){
80 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
81 }
82
83 $args = http_build_query( array(
84 'post_type' => $this->post_type,
85 'posts_per_page' => -1,
86 'comments' => array (
87 'count' => 6,
88 'compare' => '>'
89 )
90 ) );
91 $this->q->query( $args );
92
93 $this->assertEquals( 0, $this->q->found_posts );
94 }
95
96 function test_operator_lower_than() {
97 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
98 for( $i = 0; $i < 4; $i++ ){
99 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
100 }
101 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
102 for( $i = 0; $i < 5; $i++ ){
103 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
104 }
105 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
106 for( $i = 0; $i < 6; $i++ ){
107 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
108 }
109
110 $args = http_build_query( array(
111 'post_type' => $this->post_type,
112 'posts_per_page' => -1,
113 'comments' => array(
114 'count' => 6,
115 'compare' => '<'
116 )
117 ) );
118 $this->q->query( $args );
119
120 $this->assertEquals( 2, $this->q->found_posts );
121 }
122
123 function test_operator_lower_than_no_results() {
124 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
125 for( $i = 0; $i < 4; $i++ ){
126 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
127 }
128 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
129 for( $i = 0; $i < 5; $i++ ){
130 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
131 }
132 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
133 for( $i = 0; $i < 6; $i++ ){
134 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
135 }
136
137 $args = http_build_query( array(
138 'post_type' => $this->post_type,
139 'posts_per_page' => -1,
140 'comments' => array(
141 'count' => 2,
142 'compare' => '<'
143 )
144 ) );
145 $this->q->query( $args );
146
147 $this->assertEquals( 0, $this->q->found_posts );
148 }
149
150 function test_operator_not_equal() {
151 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
152 for( $i = 0; $i < 4; $i++ ){
153 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
154 }
155 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
156 for( $i = 0; $i < 5; $i++ ){
157 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
158 }
159 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
160 for( $i = 0; $i < 6; $i++ ){
161 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
162 }
163
164 $args = http_build_query( array(
165 'post_type' => $this->post_type,
166 'posts_per_page' => -1,
167 'comments' => array(
168 'count' => 5,
169 'compare' => '!='
170 )
171 ) );
172 $this->q->query( $args );
173
174 $this->assertEquals( 2, $this->q->found_posts );
175 }
176
177 function test_operator_equal_or_greater_than() {
178 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
179 for( $i = 0; $i < 4; $i++ ){
180 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
181 }
182 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
183 for( $i = 0; $i < 5; $i++ ){
184 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
185 }
186 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
187 for( $i = 0; $i < 6; $i++ ){
188 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
189 }
190
191 $args = http_build_query( array(
192 'post_type' => $this->post_type,
193 'posts_per_page' => -1,
194 'comments' => array(
195 'count' => 5,
196 'compare' => '>='
197 )
198 ) );
199 $this->q->query( $args );
200
201 $this->assertEquals( 2, $this->q->found_posts );
202 }
203
204 function test_operator_equal_or_greater_than_no_results() {
205 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
206 for( $i = 0; $i < 4; $i++ ){
207 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
208 }
209 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
210 for( $i = 0; $i < 5; $i++ ){
211 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
212 }
213 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
214 for( $i = 0; $i < 6; $i++ ){
215 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
216 }
217
218 $args = http_build_query( array(
219 'post_type' => $this->post_type,
220 'posts_per_page' => -1,
221 'comments' => array(
222 'count' => 7,
223 'compare' => '>='
224 )
225 ) );
226 $this->q->query( $args );
227
228 $this->assertEquals( 0, $this->q->found_posts );
229 }
230
231 function test_operator_equal_or_lower_than() {
232 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
233 for( $i = 0; $i < 4; $i++ ){
234 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
235 }
236 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
237 for( $i = 0; $i < 5; $i++ ){
238 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
239 }
240 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
241 for( $i = 0; $i < 6; $i++ ){
242 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
243 }
244
245 $args = http_build_query( array(
246 'post_type' => $this->post_type,
247 'posts_per_page' => -1,
248 'comments' => array(
249 'count' => 5,
250 'compare' => '<='
251 )
252 ) );
253 $this->q->query( $args );
254
255 $this->assertEquals( 2, $this->q->found_posts );
256 }
257
258 function test_operator_equal_or_lower_than_no_results() {
259 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
260 for( $i = 0; $i < 4; $i++ ){
261 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
262 }
263 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
264 for( $i = 0; $i < 5; $i++ ){
265 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
266 }
267 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
268 for( $i = 0; $i < 6; $i++ ){
269 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
270 }
271
272 $args = http_build_query( array(
273 'post_type' => $this->post_type,
274 'posts_per_page' => -1,
275 'comments' => array(
276 'count' => 3,
277 'compare' => '<='
278 )
279 ) );
280 $this->q->query( $args );
281
282 $this->assertEquals( 0, $this->q->found_posts );
283 }
284
285 function test_wrong_operator() {
286 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
287 for( $i = 0; $i < 4; $i++ ){
288 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
289 }
290 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
291 for( $i = 0; $i < 5; $i++ ){
292 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
293 }
294 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
295 for( $i = 0; $i < 6; $i++ ){
296 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
297 }
298
299 $args = http_build_query( array(
300 'post_type' => $this->post_type,
301 'posts_per_page' => -1,
302 'comments' => array(
303 'count' => 5,
304 'compare' => '@'
305 )
306 ) );
307 $this->q->query( $args );
308
309 $this->assertEquals( 1, $this->q->found_posts );
310 }
311
312 function test_wrong_count() {
313 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
314 for( $i = 0; $i < 4; $i++ ){
315 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
316 }
317 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
318 for( $i = 0; $i < 5; $i++ ){
319 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
320 }
321 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
322 for( $i = 0; $i < 6; $i++ ){
323 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
324 }
325
326 $args = http_build_query( array(
327 'post_type' => $this->post_type,
328 'posts_per_page' => -1,
329 'comments' => array(
330 'count' => 'abc',
331 'compare' => '='
332 )
333 ) );
334 $this->q->query( $args );
335
336 $this->assertEquals( 0, $this->q->found_posts );
337 }
338
339 function test_no_operator() {
340 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
341 for( $i = 0; $i < 4; $i++ ){
342 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
343 }
344 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
345 for( $i = 0; $i < 5; $i++ ){
346 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
347 }
348 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
349 for( $i = 0; $i < 6; $i++ ){
350 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
351 }
352
353 $args = http_build_query( array(
354 'post_type' => $this->post_type,
355 'posts_per_page' => -1,
356 'comments' => array(
357 'count' => 5
358 )
359 ) );
360 $this->q->query( $args );
361
362 $this->assertEquals( 1, $this->q->found_posts );
363 }
364
365 function test_simple_count() {
366 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
367 for( $i = 0; $i < 4; $i++ ){
368 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
369 }
370 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
371 for( $i = 0; $i < 5; $i++ ){
372 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
373 }
374 $org_post_id = $this->factory->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => $this->post_type ) );
375 for( $i = 0; $i < 6; $i++ ){
376 $this->factory->comment->create( array( 'comment_post_ID' => $org_post_id ) );
377 }
378
379 $args = http_build_query( array(
380 'post_type' => $this->post_type,
381 'posts_per_page' => -1,
382 'comment_count' => 5
383 ) );
384 $this->q->query( $args );
385
386 $this->assertEquals( 1, $this->q->found_posts );
387 }
388}