Tests_Comment_Query::test_search()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
// Test the output of Comment Querying functions
4
5
/**
6
 * @group comment
7
 */
8
class Tests_Comment_Query extends WP_UnitTestCase
9
{
10
    protected static $post_id;
11
    protected $comment_id;
12
13
    public static function wpSetUpBeforeClass( $factory ) 
0 ignored issues
show
Coding Style introduced by
The function name wpSetUpBeforeClass is in camel caps, but expected wp_set_up_before_class instead as per the coding standard.
Loading history...
14
    {
15
        self::$post_id = $factory->post->create();
16
    }
17
18
    function setUp() 
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
19
    {
20
        parent::setUp();
21
    }
22
23
    public function test_query() 
24
    {
25
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
26
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
27
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
28
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
29
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ));
30
31
        $q = new WP_Comment_Query();
32
        $found = $q->query(
33
            array(
34
            'fields' => 'ids',
35
            ) 
36
        );
37
38
        $this->assertEqualSets(array( $c1, $c2, $c3, $c4, $c5 ), $found);
39
    }
40
41
    public function test_query_post_id_0() 
42
    {
43
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
44
45
        $q = new WP_Comment_Query();
46
        $found = $q->query(
47
            array(
48
            'post_id' => 0,
49
            'fields' => 'ids',
50
            ) 
51
        );
52
53
        $this->assertEqualSets(array( $c1 ), $found);
54
    }
55
56
    /**
57
     * @ticket 12668
58
     */
59 View Code Duplication
    public function test_query_type_empty_string() 
60
    {
61
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
62
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
63
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
64
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
65
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ));
66
67
        $q = new WP_Comment_Query();
68
        $found = $q->query(
69
            array(
70
            'type' => '',
71
            'fields' => 'ids',
72
            ) 
73
        );
74
75
        $this->assertEqualSets(array( $c1, $c2, $c3, $c4, $c5 ), $found);
76
    }
77
78
    /**
79
     * @ticket 12668
80
     */
81 View Code Duplication
    public function test_query_type_comment() 
82
    {
83
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
84
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
85
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
86
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
87
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ));
88
89
        $q = new WP_Comment_Query();
90
        $found = $q->query(
91
            array(
92
            'type' => 'comment',
93
            'fields' => 'ids',
94
            ) 
95
        );
96
97
        $this->assertEqualSets(array( $c1 ), $found);
98
    }
99
100 View Code Duplication
    public function test_query_type_pingback() 
101
    {
102
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
103
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
104
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
105
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
106
107
        $q = new WP_Comment_Query();
108
        $found = $q->query(
109
            array(
110
            'type' => 'pingback',
111
            'fields' => 'ids',
112
            ) 
113
        );
114
115
        $this->assertEqualSets(array( $c2, $c3 ), $found);
116
117
    }
118
119 View Code Duplication
    public function test_query_type_trackback() 
120
    {
121
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
122
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
123
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
124
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
125
126
        $q = new WP_Comment_Query();
127
        $found = $q->query(
128
            array(
129
            'type' => 'trackback',
130
            'fields' => 'ids',
131
            ) 
132
        );
133
134
        $this->assertEqualSets(array( $c2, $c3 ), $found);
135
136
    }
137
138
    /**
139
     * 'pings' is an alias for 'trackback' + 'pingback'.
140
     */
141 View Code Duplication
    public function test_query_type_pings() 
142
    {
143
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
144
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
145
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
146
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
147
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ));
148
149
        $q = new WP_Comment_Query();
150
        $found = $q->query(
151
            array(
152
            'type' => 'pings',
153
            'fields' => 'ids',
154
            ) 
155
        );
156
157
        $this->assertEqualSets(array( $c2, $c3 ), $found);
158
    }
159
160
    /**
161
     * Comments and custom
162
  *
163
     * @ticket 12668
164
     */
165 View Code Duplication
    public function test_type_array_comments_and_custom() 
166
    {
167
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
168
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
169
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
170
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
171
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ));
172
        $c6 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
173
174
        $q = new WP_Comment_Query();
175
        $found = $q->query(
176
            array(
177
            'type' => array( 'comments', 'mario' ),
178
            'fields' => 'ids',
179
            ) 
180
        );
181
182
        $this->assertEqualSets(array( $c1, $c4, $c6 ), $found);
183
    }
184
185
    /**
186
     * @ticket 12668
187
     */
188 View Code Duplication
    public function test_type_not__in_array_custom() 
189
    {
190
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
191
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
192
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
193
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
194
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ));
195
        $c6 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
196
197
        $q = new WP_Comment_Query();
198
        $found = $q->query(
199
            array(
200
            'type__not_in' => array( 'luigi' ),
201
            'fields' => 'ids',
202
            ) 
203
        );
204
205
        $this->assertEqualSets(array( $c1, $c2, $c3, $c4, $c6 ), $found);
206
    }
207
208
    /**
209
     * @ticket 12668
210
     */
211 View Code Duplication
    public function test_type__in_array_and_not_type_array_custom() 
212
    {
213
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
214
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
215
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
216
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
217
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ));
218
        $c6 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
219
220
        $q = new WP_Comment_Query();
221
        $found = $q->query(
222
            array(
223
            'type__in' => array( 'comments' ),
224
            'type__not_in' => array( 'luigi' ),
225
            'fields' => 'ids',
226
            ) 
227
        );
228
229
        $this->assertEqualSets(array( $c1 ), $found);
230
    }
231
232
    /**
233
     * @ticket 12668
234
     */
235 View Code Duplication
    public function test_type_array_and_type__not_in_array_custom() 
236
    {
237
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
238
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
239
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
240
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
241
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ));
242
        $c6 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
243
244
        $q = new WP_Comment_Query();
245
        $found = $q->query(
246
            array(
247
            'type' => array( 'pings' ),
248
            'type__not_in' => array( 'mario' ),
249
            'fields' => 'ids',
250
            ) 
251
        );
252
253
        $this->assertEqualSets(array( $c2, $c3 ), $found);
254
    }
255
256
    /**
257
     * @ticket 12668
258
     */
259 View Code Duplication
    public function test_type__not_in_custom() 
260
    {
261
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
262
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
263
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
264
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
265
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ));
266
        $c6 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
267
268
        $q = new WP_Comment_Query();
269
        $found = $q->query(
270
            array(
271
            'type__not_in' => 'luigi',
272
            'fields' => 'ids',
273
            ) 
274
        );
275
276
        $this->assertEqualSets(array( $c1, $c2, $c3, $c4, $c6 ), $found);
277
    }
278
279
    /**
280
     * @ticket 12668
281
     */
282 View Code Duplication
    public function test_type_array_comments_and_pings() 
283
    {
284
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
285
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
286
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
287
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ));
288
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ));
289
290
        $q = new WP_Comment_Query();
291
        $found = $q->query(
292
            array(
293
            'type' => array( 'comments', 'pings' ),
294
            'fields' => 'ids',
295
            ) 
296
        );
297
298
        $this->assertEqualSets(array( $c1, $c2, $c3 ), $found);
299
    }
300
301
    /**
302
     * @ticket 12668
303
     */
304
    public function test_type_array_comment_pings() 
305
    {
306
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
307
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
308
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
309
310
        $q = new WP_Comment_Query();
311
        $found = $q->query(
312
            array(
313
            'type' => array( 'comment', 'pings' ),
314
            'fields' => 'ids',
315
            ) 
316
        );
317
318
        $this->assertEqualSets(array( $c1, $c2, $c3 ), $found);
319
    }
320
321
    /**
322
     * @ticket 12668
323
     */
324
    public function test_type_array_pingback() 
325
    {
326
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
327
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
328
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
329
330
        $q = new WP_Comment_Query();
331
        $found = $q->query(
332
            array(
333
            'type' => array( 'pingback' ),
334
            'fields' => 'ids',
335
            ) 
336
        );
337
338
        $this->assertEquals(array( $c2 ), $found);
339
    }
340
341
    /**
342
     * @ticket 12668
343
     */
344
    public function test_type_array_custom_pingpack() 
345
    {
346
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
347
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
348
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
349
350
        $q = new WP_Comment_Query();
351
        $found = $q->query(
352
            array(
353
            'type' => array( 'peach', 'pingback' ),
354
            'fields' => 'ids',
355
            ) 
356
        );
357
358
        $this->assertEquals(array( $c2 ), $found);
359
    }
360
361
    /**
362
     * @ticket 12668
363
     */
364
    public function test_type_array_pings() 
365
    {
366
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
367
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
368
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
369
370
        $q = new WP_Comment_Query();
371
        $found = $q->query(
372
            array(
373
            'type' => array( 'pings' ),
374
            'fields' => 'ids',
375
            ) 
376
        );
377
378
        $this->assertEqualSets(array( $c2, $c3 ), $found);
379
    }
380
381
    /**
382
     * @ticket 12668
383
     */
384 View Code Duplication
    public function test_type_status_approved_array_comment_pings() 
385
    {
386
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
387
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
388
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
389
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '0', 'comment_type' => 'pingback' ));
390
391
        $q = new WP_Comment_Query();
392
        $found = $q->query(
393
            array(
394
            'status' => 'approve',
395
            'type' => array( 'pings' ),
396
            'fields' => 'ids',
397
            ) 
398
        );
399
400
        $this->assertEqualSets(array( $c3, $c2 ), $found);
401
    }
402
403
    /**
404
     * @ticket 12668
405
     */
406
    public function test_type_array_trackback() 
407
    {
408
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
409
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
410
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
411
412
        $q = new WP_Comment_Query();
413
        $found = $q->query(
414
            array(
415
            'type' => array( 'trackback' ),
416
            'fields' => 'ids',
417
            ) 
418
        );
419
420
        $this->assertEquals(array( $c2 ), $found);
421
    }
422
423
    /**
424
     * @ticket 12668
425
     */
426
    public function test_type_array_custom_trackback() 
427
    {
428
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
429
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
430
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ));
431
432
        $q = new WP_Comment_Query();
433
        $found = $q->query(
434
            array(
435
            'type' => array( 'toad', 'trackback' ),
436
            'fields' => 'ids',
437
            ) 
438
        );
439
440
        $this->assertEquals(array( $c2 ), $found);
441
    }
442
443
    /**
444
     * @ticket 12668
445
     */
446 View Code Duplication
    public function test_type_array_pings_approved() 
447
    {
448
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
449
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
450
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ));
451
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '0', 'comment_type' => 'trackback' ));
452
453
        $q = new WP_Comment_Query();
454
        $found = $q->query(
455
            array(
456
            'status' => 'approve',
457
            'type' => array( 'pings' ),
458
            'fields' => 'ids',
459
            ) 
460
        );
461
462
        $this->assertEqualSets(array( $c3, $c2 ), $found);
463
    }
464
465
    /**
466
     * @ticket 29612
467
     */
468 View Code Duplication
    public function test_status_empty_string() 
469
    {
470
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
471
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '0' ));
472
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => 'spam' ));
473
474
        $q = new WP_Comment_Query();
475
        $found = $q->query(
476
            array(
477
            'status' => '',
478
            'fields' => 'ids',
479
            ) 
480
        );
481
482
        $this->assertEqualSets(array( $c1, $c2 ), $found);
483
    }
484
485
    /**
486
     * @ticket 21101
487
     */
488 View Code Duplication
    public function test_status_hold() 
489
    {
490
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
491
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '0' ));
492
493
        $q = new WP_Comment_Query();
494
        $found = $q->query(
495
            array(
496
            'status' => 'hold',
497
            'fields' => 'ids',
498
            ) 
499
        );
500
501
        $this->assertEquals(array( $c2 ), $found);
502
    }
503
504
    /**
505
     * @ticket 21101
506
     */
507 View Code Duplication
    public function test_status_approve() 
508
    {
509
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
510
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '0' ));
511
512
        $q = new WP_Comment_Query();
513
        $found = $q->query(
514
            array(
515
            'status' => 'approve',
516
            'fields' => 'ids',
517
            ) 
518
        );
519
520
        $this->assertEquals(array( $c1 ), $found);
521
    }
522
523 View Code Duplication
    public function test_status_custom() 
524
    {
525
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
526
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => 'foo' ));
527
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => 'foo1' ));
528
529
        $q = new WP_Comment_Query();
530
        $found = $q->query(
531
            array(
532
            'status' => 'foo',
533
            'fields' => 'ids',
534
            ) 
535
        );
536
537
        $this->assertEquals(array( $c2 ), $found);
538
    }
539
540 View Code Duplication
    public function test_status_all() 
541
    {
542
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
543
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => 'foo' ));
544
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '0' ));
545
546
        $q = new WP_Comment_Query();
547
        $found = $q->query(
548
            array(
549
            'status' => 'all',
550
            'fields' => 'ids',
551
            ) 
552
        );
553
554
        $this->assertEqualSets(array( $c1, $c3 ), $found);
555
    }
556
557 View Code Duplication
    public function test_status_default_to_all() 
558
    {
559
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
560
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => 'foo' ));
561
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '0' ));
562
563
        $q = new WP_Comment_Query();
564
        $found = $q->query(
565
            array(
566
            'fields' => 'ids',
567
            ) 
568
        );
569
570
        $this->assertEqualSets(array( $c1, $c3 ), $found);
571
    }
572
573
    /**
574
     * @ticket 29612
575
     */
576 View Code Duplication
    public function test_status_comma_any() 
577
    {
578
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
579
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => 'foo' ));
580
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '0' ));
581
582
        $q = new WP_Comment_Query();
583
        $found = $q->query(
584
            array(
585
            'status' => 'any',
586
            'fields' => 'ids',
587
            ) 
588
        );
589
590
        $this->assertEqualSets(array( $c1, $c2, $c3 ), $found);
591
    }
592
593
    /**
594
     * @ticket 29612
595
     */
596 View Code Duplication
    public function test_status_comma_separated() 
597
    {
598
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
599
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => 'foo' ));
600
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '0' ));
601
602
        $q = new WP_Comment_Query();
603
        $found = $q->query(
604
            array(
605
            'status' => 'approve,foo,bar',
606
            'fields' => 'ids',
607
            ) 
608
        );
609
610
        $this->assertEqualSets(array( $c1, $c2 ), $found);
611
    }
612
613
    /**
614
     * @ticket 29612
615
     */
616
    public function test_status_array() 
617
    {
618
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
619
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => 'foo' ));
620
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '0' ));
621
622
        $q = new WP_Comment_Query();
623
        $found = $q->query(
624
            array(
625
            'status' => array( 'approve', 'foo', 'bar', ),
626
            'fields' => 'ids',
627
            ) 
628
        );
629
630
        $this->assertEqualSets(array( $c1, $c2 ), $found);
631
    }
632
633
    /**
634
     * @ticket 35478
635
     */
636
    public function test_multiple_post_fields_should_all_be_respected() 
637
    {
638
        $posts = array();
639
640
        $posts[] = self::factory()->post->create(
641
            array(
642
            'post_status' => 'publish',
643
            'post_author' => 3,
644
            ) 
645
        );
646
647
        $posts[] = self::factory()->post->create(
648
            array(
649
            'post_status' => 'draft',
650
            'post_author' => 4,
651
            ) 
652
        );
653
654
        $posts[] = self::factory()->post->create(
655
            array(
656
            'post_status' => 'draft',
657
            'post_author' => 3,
658
            ) 
659
        );
660
661
        $comments = array();
662
        foreach ( $posts as $post ) {
663
            $comments[] = self::factory()->comment->create(
664
                array(
665
                'comment_post_ID' => $post,
666
                ) 
667
            );
668
        }
669
670
        $q = new WP_Comment_Query(
671
            array(
672
            'post_status' => 'draft',
673
            'post_author' => 3,
674
            'fields' => 'ids',
675
            ) 
676
        );
677
678
        $this->assertSame(array( $comments[2] ), $q->comments);
679
    }
680
681
    function test_get_comments_for_post() 
682
    {
683
        $limit = 5;
684
685
        $post_id = self::factory()->post->create();
686
        self::factory()->comment->create_post_comments($post_id, $limit);
687
        $comments = get_comments(array( 'post_id' => $post_id ));
688
        $this->assertEquals($limit, count($comments));
689
        foreach ( $comments as $comment ) {
690
            $this->assertEquals($post_id, $comment->comment_post_ID);
691
        }
692
693
        $post_id2 = self::factory()->post->create();
694
        self::factory()->comment->create_post_comments($post_id2, $limit);
695
        $comments = get_comments(array( 'post_id' => $post_id2 ));
696
        $this->assertEquals($limit, count($comments));
697
        foreach ( $comments as $comment ) {
698
            $this->assertEquals($post_id2, $comment->comment_post_ID);
699
        }
700
701
        $post_id3 = self::factory()->post->create();
702
        self::factory()->comment->create_post_comments($post_id3, $limit, array( 'comment_approved' => '0' ));
703
        $comments = get_comments(array( 'post_id' => $post_id3 ));
704
        $this->assertEquals($limit, count($comments));
705
        foreach ( $comments as $comment ) {
706
            $this->assertEquals($post_id3, $comment->comment_post_ID);
707
        }
708
709
        $comments = get_comments(array( 'post_id' => $post_id3, 'status' => 'hold' ));
710
        $this->assertEquals($limit, count($comments));
711
        foreach ( $comments as $comment ) {
712
            $this->assertEquals($post_id3, $comment->comment_post_ID);
713
        }
714
715
        $comments = get_comments(array( 'post_id' => $post_id3, 'status' => 'approve' ));
716
        $this->assertEquals(0, count($comments));
717
718
        self::factory()->comment->create_post_comments($post_id3, $limit, array( 'comment_approved' => '1' ));
719
        $comments = get_comments(array( 'post_id' => $post_id3 ));
720
        $this->assertEquals($limit * 2, count($comments));
721
        foreach ( $comments as $comment ) {
722
            $this->assertEquals($post_id3, $comment->comment_post_ID);
723
        }
724
    }
725
726
    /**
727
     * @ticket 21003
728
     */
729
    function test_orderby_meta() 
730
    {
731
        $comment_id = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id ));
732
        $comment_id2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id ));
733
        $comment_id3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id ));
734
735
        add_comment_meta($comment_id, 'key', 'value1', true);
736
        add_comment_meta($comment_id, 'key1', 'value1', true);
737
        add_comment_meta($comment_id, 'key3', 'value3', true);
738
        add_comment_meta($comment_id2, 'key', 'value2', true);
739
        add_comment_meta($comment_id2, 'key2', 'value2', true);
740
        add_comment_meta($comment_id3, 'key3', 'value3', true);
741
742
        $comments = get_comments(array( 'meta_key' => 'key', 'orderby' => array( 'key' ) ));
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
743
        $this->assertEquals(2, count($comments));
744
        $this->assertEquals($comment_id2, $comments[0]->comment_ID);
745
        $this->assertEquals($comment_id, $comments[1]->comment_ID);
746
747
        $comments = get_comments(array( 'meta_key' => 'key', 'orderby' => array( 'meta_value' ) ));
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
748
        $this->assertEquals(2, count($comments));
749
        $this->assertEquals($comment_id2, $comments[0]->comment_ID);
750
        $this->assertEquals($comment_id, $comments[1]->comment_ID);
751
752
        $comments = get_comments(array( 'meta_key' => 'key', 'orderby' => array( 'key' ), 'order' => 'ASC' ));
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
753
        $this->assertEquals(2, count($comments));
754
        $this->assertEquals($comment_id, $comments[0]->comment_ID);
755
        $this->assertEquals($comment_id2, $comments[1]->comment_ID);
756
757
        $comments = get_comments(array( 'meta_key' => 'key', 'orderby' => array( 'meta_value' ), 'order' => 'ASC' ));
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
758
        $this->assertEquals(2, count($comments));
759
        $this->assertEquals($comment_id, $comments[0]->comment_ID);
760
        $this->assertEquals($comment_id2, $comments[1]->comment_ID);
761
762
        $comments = get_comments(array( 'meta_value' => 'value3', 'orderby' => array( 'key' ) ));
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
763
        $this->assertEquals(array( $comment_id3, $comment_id ), wp_list_pluck($comments, 'comment_ID'));
764
765
        $comments = get_comments(array( 'meta_value' => 'value3', 'orderby' => array( 'meta_value' ) ));
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
766
        $this->assertEquals(array( $comment_id3, $comment_id ), wp_list_pluck($comments, 'comment_ID'));
767
768
        // value1 is present on two different keys for $comment_id yet we should get only one instance
769
        // of that comment in the results
770
        $comments = get_comments(array( 'meta_value' => 'value1', 'orderby' => array( 'key' ) ));
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
771
        $this->assertEquals(1, count($comments));
772
773
        $comments = get_comments(array( 'meta_value' => 'value1', 'orderby' => array( 'meta_value' ) ));
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
774
        $this->assertEquals(1, count($comments));
775
    }
776
777
    /**
778
     * @ticket 30478
779
     */
780 View Code Duplication
    public function test_orderby_clause_key() 
781
    {
782
        $comments = self::factory()->comment->create_many(3);
783
        add_comment_meta($comments[0], 'foo', 'aaa');
784
        add_comment_meta($comments[1], 'foo', 'zzz');
785
        add_comment_meta($comments[2], 'foo', 'jjj');
786
787
        $q = new WP_Comment_Query();
788
        $found = $q->query(
789
            array(
790
            'fields' => 'ids',
791
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
792
            'foo_key' => array(
793
            'key' => 'foo',
794
            'compare' => 'EXISTS',
795
            ),
796
            ),
797
            'orderby' => 'foo_key',
798
            'order' => 'DESC',
799
            ) 
800
        );
801
802
        $this->assertEquals(array( $comments[1], $comments[2], $comments[0] ), $found);
803
    }
804
805
    /**
806
     * @ticket 30478
807
     */
808
    public function test_orderby_clause_key_as_secondary_sort() 
809
    {
810
        $c1 = self::factory()->comment->create(
811
            array(
812
            'comment_date' => '2015-01-28 03:00:00',
813
            ) 
814
        );
815
        $c2 = self::factory()->comment->create(
816
            array(
817
            'comment_date' => '2015-01-28 05:00:00',
818
            ) 
819
        );
820
        $c3 = self::factory()->comment->create(
821
            array(
822
            'comment_date' => '2015-01-28 03:00:00',
823
            ) 
824
        );
825
826
        add_comment_meta($c1, 'foo', 'jjj');
827
        add_comment_meta($c2, 'foo', 'zzz');
828
        add_comment_meta($c3, 'foo', 'aaa');
829
830
        $q = new WP_Comment_Query();
831
        $found = $q->query(
832
            array(
833
            'fields' => 'ids',
834
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
835
            'foo_key' => array(
836
            'key' => 'foo',
837
            'compare' => 'EXISTS',
838
            ),
839
            ),
840
            'orderby' => array(
841
            'comment_date' => 'asc',
842
            'foo_key' => 'asc',
843
            ),
844
            ) 
845
        );
846
847
        $this->assertEquals(array( $c3, $c1, $c2 ), $found);
848
    }
849
850
    /**
851
     * @ticket 30478
852
     */
853 View Code Duplication
    public function test_orderby_more_than_one_clause_key() 
854
    {
855
        $comments = self::factory()->comment->create_many(3);
856
857
        add_comment_meta($comments[0], 'foo', 'jjj');
858
        add_comment_meta($comments[1], 'foo', 'zzz');
859
        add_comment_meta($comments[2], 'foo', 'jjj');
860
        add_comment_meta($comments[0], 'bar', 'aaa');
861
        add_comment_meta($comments[1], 'bar', 'ccc');
862
        add_comment_meta($comments[2], 'bar', 'bbb');
863
864
        $q = new WP_Comment_Query();
865
        $found = $q->query(
866
            array(
867
            'fields' => 'ids',
868
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
869
            'foo_key' => array(
870
            'key' => 'foo',
871
            'compare' => 'EXISTS',
872
            ),
873
            'bar_key' => array(
874
            'key' => 'bar',
875
            'compare' => 'EXISTS',
876
            ),
877
            ),
878
            'orderby' => array(
879
            'foo_key' => 'asc',
880
            'bar_key' => 'desc',
881
            ),
882
            ) 
883
        );
884
885
        $this->assertEquals(array( $comments[2], $comments[0], $comments[1] ), $found);
886
    }
887
888
    /**
889
     * @ticket 32081
890
     */
891 View Code Duplication
    public function test_meta_query_should_work_with_comment__in() 
892
    {
893
        $comments = self::factory()->comment->create_many(3);
894
895
        add_comment_meta($comments[0], 'foo', 'jjj');
896
        add_comment_meta($comments[1], 'foo', 'zzz');
897
        add_comment_meta($comments[2], 'foo', 'jjj');
898
899
        $q = new WP_Comment_Query(
900
            array(
901
            'comment__in' => array( $comments[1], $comments[2] ),
902
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
903
            array(
904
            'key' => 'foo',
905
            'value' => 'jjj',
906
            ),
907
            ),
908
            'fields' => 'ids',
909
            ) 
910
        );
911
912
        $this->assertEquals(array( $comments[2] ), $q->get_comments());
913
    }
914
915
    /**
916
     * @ticket 32081
917
     */
918 View Code Duplication
    public function test_meta_query_should_work_with_comment__not_in() 
919
    {
920
        $comments = self::factory()->comment->create_many(3);
921
922
        add_comment_meta($comments[0], 'foo', 'jjj');
923
        add_comment_meta($comments[1], 'foo', 'zzz');
924
        add_comment_meta($comments[2], 'foo', 'jjj');
925
926
        $q = new WP_Comment_Query(
927
            array(
928
            'comment__not_in' => array( $comments[1], $comments[2] ),
929
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
930
            array(
931
            'key' => 'foo',
932
            'value' => 'jjj',
933
            ),
934
            ),
935
            'fields' => 'ids',
936
            ) 
937
        );
938
939
        $this->assertEquals(array( $comments[0] ), $q->get_comments());
940
    }
941
942
    /**
943
     * @ticket 27064
944
     */
945
    function test_get_comments_by_user() 
946
    {
947
        $users = self::factory()->user->create_many(2);
948
        self::factory()->comment->create(array( 'user_id' => $users[0], 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
949
        self::factory()->comment->create(array( 'user_id' => $users[0], 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
950
        self::factory()->comment->create(array( 'user_id' => $users[1], 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
951
952
        $comments = get_comments(
953
            array(
954
            'user_id' => $users[0],
955
            'orderby' => 'comment_ID',
956
            'order' => 'ASC',
957
            ) 
958
        );
959
960
        $this->assertCount(2, $comments);
961
        $this->assertEquals($users[0], $comments[0]->user_id);
962
        $this->assertEquals($users[0], $comments[1]->user_id);
963
964
        $comments = get_comments(
965
            array(
966
            'user_id' => $users,
967
            'orderby' => 'comment_ID',
968
            'order' => 'ASC',
969
            ) 
970
        );
971
972
        $this->assertCount(3, $comments);
973
        $this->assertEquals($users[0], $comments[0]->user_id);
974
        $this->assertEquals($users[0], $comments[1]->user_id);
975
        $this->assertEquals($users[1], $comments[2]->user_id);
976
977
    }
978
979
    /**
980
     * @ticket 35377
981
     */
982
    public function test_get_comments_by_author_url() 
983
    {
984
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_author' => 'bar', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://foo.bar' ));
985
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_author' => 'bar', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://foo.bar' ));
986
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_author' => 'bar', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://foo.bar/baz' ));
987
988
        $comments = get_comments(
989
            array(
990
            'author_url' => 'http://foo.bar',
991
            'fields' => 'ids',
992
            ) 
993
        );
994
995
        $this->assertEqualSets(array( $c1, $c2 ), $comments);
996
    }
997
998
    /**
999
     * @ticket 28434
1000
     */
1001
    function test_fields_ids_query() 
1002
    {
1003
        $comment_1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7, 'comment_approved' => '1' ));
1004
        $comment_2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '1' ));
1005
        $comment_3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '1' ));
1006
1007
        // Ensure we are dealing with integers, and not objects.
1008
        $this->assertInternalType('integer', $comment_1);
1009
        $this->assertInternalType('integer', $comment_2);
1010
        $this->assertInternalType('integer', $comment_3);
1011
1012
        $comment_ids = get_comments(array( 'fields' => 'ids' ));
1013
        $this->assertCount(3, $comment_ids);
1014
        $this->assertEqualSets(array( $comment_1, $comment_2, $comment_3 ), $comment_ids);
1015
    }
1016
1017
    /**
1018
     * @ticket 29189
1019
     */
1020 View Code Duplication
    function test_fields_comment__in() 
1021
    {
1022
        $comment_1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7, 'comment_approved' => '1' ));
1023
        $comment_2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '1' ));
1024
        $comment_3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '1' ));
1025
1026
        $comment_ids = get_comments(
1027
            array(
1028
            'fields' => 'ids',
1029
            'comment__in' => array( $comment_1, $comment_3 ),
1030
            ) 
1031
        );
1032
1033
        $this->assertEqualSets(array( $comment_1, $comment_3 ), $comment_ids);
1034
    }
1035
1036
    /**
1037
     * @ticket 29189
1038
     */
1039 View Code Duplication
    function test_fields_comment__not_in() 
1040
    {
1041
        $comment_1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7, 'comment_approved' => '1' ));
1042
        $comment_2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '1' ));
1043
        $comment_3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '1' ));
1044
1045
        $comment_ids = get_comments(
1046
            array(
1047
            'fields' => 'ids',
1048
            'comment__not_in' => array( $comment_2, $comment_3 ),
1049
            ) 
1050
        );
1051
1052
        $this->assertEqualSets(array( $comment_1 ), $comment_ids);
1053
    }
1054
1055
    /**
1056
     * @ticket 29189
1057
     */
1058 View Code Duplication
    function test_fields_post__in() 
1059
    {
1060
        $p1 = self::factory()->post->create();
1061
        $p2 = self::factory()->post->create();
1062
        $p3 = self::factory()->post->create();
1063
1064
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => $p1, 'user_id' => 7, 'comment_approved' => '1' ));
1065
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => $p2, 'user_id' => 1, 'comment_approved' => '1' ));
1066
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => $p3, 'user_id' => 1, 'comment_approved' => '1' ));
1067
1068
        $comment_ids = get_comments(
1069
            array(
1070
            'fields' => 'ids',
1071
            'post__in' => array( $p1, $p2 ),
1072
            ) 
1073
        );
1074
1075
        $this->assertEqualSets(array( $c1, $c2 ), $comment_ids);
1076
    }
1077
1078
    /**
1079
     * @ticket 29189
1080
     */
1081 View Code Duplication
    function test_fields_post__not_in() 
1082
    {
1083
        $p1 = self::factory()->post->create();
1084
        $p2 = self::factory()->post->create();
1085
        $p3 = self::factory()->post->create();
1086
1087
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => $p1, 'user_id' => 7, 'comment_approved' => '1' ));
1088
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => $p2, 'user_id' => 1, 'comment_approved' => '1' ));
1089
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => $p3, 'user_id' => 1, 'comment_approved' => '1' ));
1090
1091
        $comment_ids = get_comments(
1092
            array(
1093
            'fields' => 'ids',
1094
            'post__not_in' => array( $p1, $p2 ),
1095
            ) 
1096
        );
1097
1098
        $this->assertEqualSets(array( $c3 ), $comment_ids);
1099
    }
1100
1101
    /**
1102
     * @ticket 29885
1103
     */
1104 View Code Duplication
    function test_fields_post_author__in() 
1105
    {
1106
        $author_id1 = 105;
1107
        $author_id2 = 106;
1108
1109
        $p1 = self::factory()->post->create(array( 'post_author' => $author_id1    ));
1110
        $p2 = self::factory()->post->create(array( 'post_author' => $author_id1    ));
1111
        $p3 = self::factory()->post->create(array( 'post_author' => $author_id2    ));
1112
1113
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => $p1, 'user_id' => 1, 'comment_approved' => '1' ));
1114
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => $p2, 'user_id' => 1, 'comment_approved' => '1' ));
1115
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => $p3, 'user_id' => 1, 'comment_approved' => '1' ));
1116
1117
        $comment_ids = get_comments(
1118
            array(
1119
            'fields' => 'ids',
1120
            'post_author__in' => array( $author_id1 ),
1121
            ) 
1122
        );
1123
1124
        $this->assertEqualSets(array( $c1, $c2 ), $comment_ids);
1125
    }
1126
1127
    /**
1128
     * @ticket 29885
1129
     */
1130 View Code Duplication
    function test_fields_post_author__not_in() 
1131
    {
1132
        $author_id1 = 111;
1133
        $author_id2 = 112;
1134
1135
        $p1 = self::factory()->post->create(array( 'post_author' => $author_id1    ));
1136
        $p2 = self::factory()->post->create(array( 'post_author' => $author_id1    ));
1137
        $p3 = self::factory()->post->create(array( 'post_author' => $author_id2    ));
1138
1139
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => $p1, 'user_id' => 1, 'comment_approved' => '1' ));
1140
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => $p2, 'user_id' => 1, 'comment_approved' => '1' ));
1141
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => $p3, 'user_id' => 1, 'comment_approved' => '1' ));
1142
1143
        $comment_ids = get_comments(
1144
            array(
1145
            'fields' => 'ids',
1146
            'post_author__not_in' => array( $author_id1 ),
1147
            ) 
1148
        );
1149
1150
        $this->assertEqualSets(array( $c3 ), $comment_ids);
1151
    }
1152
1153
        /**
1154
         * @ticket 29885
1155
         */
1156 View Code Duplication
    function test_fields_author__in() 
1157
    {
1158
        $p1 = self::factory()->post->create();
1159
        $p2 = self::factory()->post->create();
1160
        $p3 = self::factory()->post->create();
1161
        $p4 = self::factory()->post->create();
1162
1163
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => $p1, 'user_id' => 1, 'comment_approved' => '1' ));
1164
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => $p1, 'user_id' => 2, 'comment_approved' => '1' ));
1165
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => $p2, 'user_id' => 3, 'comment_approved' => '1' ));
1166
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => $p4, 'user_id' => 4, 'comment_approved' => '1' ));
1167
1168
        $comment_ids = get_comments(
1169
            array(
1170
            'fields' => 'ids',
1171
            'author__in' => array( 1, 3 ),
1172
            ) 
1173
        );
1174
1175
        $this->assertEqualSets(array( $c1, $c3 ), $comment_ids);
1176
    }
1177
1178
        /**
1179
         * @ticket 29885
1180
         */
1181 View Code Duplication
    function test_fields_author__not_in() 
1182
    {
1183
        $p1 = self::factory()->post->create();
1184
        $p2 = self::factory()->post->create();
1185
        $p3 = self::factory()->post->create();
1186
        $p4 = self::factory()->post->create();
1187
1188
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => $p1, 'user_id' => 1, 'comment_approved' => '1' ));
1189
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => $p1, 'user_id' => 2, 'comment_approved' => '1' ));
1190
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => $p2, 'user_id' => 3, 'comment_approved' => '1' ));
1191
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => $p4, 'user_id' => 4, 'comment_approved' => '1' ));
1192
1193
        $comment_ids = get_comments(
1194
            array(
1195
            'fields' => 'ids',
1196
            'author__not_in' => array( 1, 2 ),
1197
            ) 
1198
        );
1199
1200
        $this->assertEqualSets(array( $c3, $c4 ), $comment_ids);
1201
    }
1202
1203
    /**
1204
     * @ticket 19623
1205
     */
1206 View Code Duplication
    public function test_get_comments_with_status_all() 
1207
    {
1208
        $comment_1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7, 'comment_approved' => '1' ));
1209
        $comment_2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '1' ));
1210
        $comment_3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '0' ));
1211
        $comments_approved_1 = get_comments(array( 'status' => 'all' ));
1212
1213
        $comment_ids = get_comments(array( 'fields' => 'ids' ));
1214
        $this->assertEqualSets(array( $comment_1, $comment_2, $comment_3 ), $comment_ids);
1215
    }
1216
1217
    /**
1218
     * @ticket 19623
1219
     */
1220
    public function test_get_comments_with_include_unapproved_user_id() 
1221
    {
1222
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7, 'comment_approved' => '1' ));
1223
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '1' ));
1224
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '0' ));
1225
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 6, 'comment_approved' => '0' ));
1226
1227
        $found = get_comments(
1228
            array(
1229
            'fields' => 'ids',
1230
            'include_unapproved' => 1,
1231
            'status' => 'approve',
1232
            ) 
1233
        );
1234
1235
        $this->assertEqualSets(array( $c1, $c2, $c3 ), $found);
1236
    }
1237
1238
    /**
1239
     * @ticket 19623
1240
     */
1241 View Code Duplication
    public function test_get_comments_with_include_unapproved_user_id_array() 
1242
    {
1243
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7, 'comment_approved' => '1' ));
1244
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '1' ));
1245
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '0' ));
1246
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 6, 'comment_approved' => '0' ));
1247
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 8, 'comment_approved' => '0' ));
1248
1249
        $found = get_comments(
1250
            array(
1251
            'fields' => 'ids',
1252
            'include_unapproved' => array( 1, 8 ),
1253
            'status' => 'approve',
1254
            ) 
1255
        );
1256
1257
        $this->assertEqualSets(array( $c1, $c2, $c3, $c5 ), $found);
1258
    }
1259
1260
    /**
1261
     * @ticket 19623
1262
     */
1263 View Code Duplication
    public function test_get_comments_with_include_unapproved_user_id_comma_separated() 
1264
    {
1265
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7, 'comment_approved' => '1' ));
1266
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '1' ));
1267
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 1, 'comment_approved' => '0' ));
1268
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 6, 'comment_approved' => '0' ));
1269
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 8, 'comment_approved' => '0' ));
1270
1271
        $found = get_comments(
1272
            array(
1273
            'fields' => 'ids',
1274
            'include_unapproved' => '1,8',
1275
            'status' => 'approve',
1276
            ) 
1277
        );
1278
1279
        $this->assertEqualSets(array( $c1, $c2, $c3, $c5 ), $found);
1280
    }
1281
1282
    /**
1283
     * @ticket 19623
1284
     */
1285
    public function test_get_comments_with_include_unapproved_author_email() 
1286
    {
1287
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7, 'comment_approved' => '1' ));
1288
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 0, 'comment_approved' => '1', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1289
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1290
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1291
1292
        $found = get_comments(
1293
            array(
1294
            'fields' => 'ids',
1295
            'include_unapproved' => '[email protected]',
1296
            'status' => 'approve',
1297
            ) 
1298
        );
1299
1300
        $this->assertEqualSets(array( $c1, $c2, $c3 ), $found);
1301
    }
1302
1303
    /**
1304
     * @ticket 19623
1305
     */
1306 View Code Duplication
    public function test_get_comments_with_include_unapproved_mixed_array() 
1307
    {
1308
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7, 'comment_approved' => '1' ));
1309
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 0, 'comment_approved' => '1', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1310
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1311
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1312
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1313
1314
        $found = get_comments(
1315
            array(
1316
            'fields' => 'ids',
1317
            'include_unapproved' => array( '[email protected]', 4 ),
1318
            'status' => 'approve',
1319
            ) 
1320
        );
1321
1322
        $this->assertEqualSets(array( $c1, $c2, $c3, $c5 ), $found);
1323
    }
1324
1325
    /**
1326
     * @ticket 19623
1327
     */
1328 View Code Duplication
    public function test_get_comments_with_include_unapproved_mixed_comma_separated() 
1329
    {
1330
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7, 'comment_approved' => '1' ));
1331
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 0, 'comment_approved' => '1', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1332
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1333
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1334
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1335
1336
        $found = get_comments(
1337
            array(
1338
            'fields' => 'ids',
1339
            'include_unapproved' => '[email protected], 4',
1340
            'status' => 'approve',
1341
            ) 
1342
        );
1343
1344
        $this->assertEqualSets(array( $c1, $c2, $c3, $c5 ), $found);
1345
    }
1346
1347
    public function test_search() 
1348
    {
1349
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => '[email protected]' ));
1350
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'bar', 'comment_author_email' => '[email protected]' ));
1351
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'bar', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://foo.bar' ));
1352
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'bar', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://example.com', 'comment_author_IP' => 'foo.bar' ));
1353
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'bar', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://example.com', 'comment_content' => 'Nice foo comment' ));
1354
        $c6 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'bar', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://example.com' ));
1355
1356
        $q = new WP_Comment_Query();
1357
        $found = $q->query(
1358
            array(
1359
            'search' => 'foo',
1360
            'fields' => 'ids',
1361
            ) 
1362
        );
1363
1364
        $this->assertEqualSets(array( $c1, $c2, $c3, $c4, $c5 ), $found);
1365
    }
1366
1367
    /**
1368
     * @ticket 35513
1369
     */
1370 View Code Duplication
    public function test_search_false_should_be_ignored() 
1371
    {
1372
        $q = new WP_Comment_Query();
1373
        $q->query(
1374
            array(
1375
            'search' => false,
1376
            ) 
1377
        );
1378
        $this->assertNotContains("comment_author LIKE", $q->request);
1379
    }
1380
1381
    /**
1382
     * @ticket 35513
1383
     */
1384 View Code Duplication
    public function test_search_null_should_be_ignored() 
1385
    {
1386
        $q = new WP_Comment_Query();
1387
        $q->query(
1388
            array(
1389
            'search' => null,
1390
            ) 
1391
        );
1392
        $this->assertNotContains("comment_author LIKE", $q->request);
1393
    }
1394
1395
    /**
1396
     * @ticket 35513
1397
     */
1398 View Code Duplication
    public function test_search_empty_string_should_be_ignored() 
1399
    {
1400
        $q = new WP_Comment_Query();
1401
        $q->query(
1402
            array(
1403
            'search' => false,
1404
            ) 
1405
        );
1406
        $this->assertNotContains("comment_author LIKE", $q->request);
1407
    }
1408
1409
    /**
1410
     * @ticket 35513
1411
     */
1412
    public function test_search_int_0_should_not_be_ignored() 
1413
    {
1414
        $q = new WP_Comment_Query();
1415
        $q->query(
1416
            array(
1417
            'search' => 0,
1418
            ) 
1419
        );
1420
        $this->assertContains("comment_author LIKE '%0%'", $q->request);
1421
    }
1422
1423
    /**
1424
     * @ticket 35513
1425
     */
1426
    public function test_search_string_0_should_not_be_ignored() 
1427
    {
1428
        $q = new WP_Comment_Query();
1429
        $q->query(
1430
            array(
1431
            'search' => '0',
1432
            ) 
1433
        );
1434
        $this->assertContains("comment_author LIKE '%0%'", $q->request);
1435
    }
1436
1437
    public function test_orderby_default() 
1438
    {
1439
        global $wpdb;
1440
1441
        $q = new WP_Comment_Query();
1442
        $q->query(array());
1443
1444
        $this->assertContains("ORDER BY $wpdb->comments.comment_date_gmt", $q->request);
1445
    }
1446
1447 View Code Duplication
    public function test_orderby_single() 
1448
    {
1449
        global $wpdb;
1450
1451
        $q = new WP_Comment_Query();
1452
        $q->query(
1453
            array(
1454
            'orderby' => 'comment_agent',
1455
            ) 
1456
        );
1457
1458
        $this->assertContains("ORDER BY $wpdb->comments.comment_agent", $q->request);
1459
    }
1460
1461 View Code Duplication
    public function test_orderby_single_invalid() 
1462
    {
1463
        global $wpdb;
1464
1465
        $q = new WP_Comment_Query();
1466
        $q->query(
1467
            array(
1468
            'orderby' => 'foo',
1469
            ) 
1470
        );
1471
1472
        $this->assertContains("ORDER BY $wpdb->comments.comment_date_gmt", $q->request);
1473
    }
1474
1475 View Code Duplication
    public function test_orderby_space_separated() 
1476
    {
1477
        global $wpdb;
1478
1479
        $q = new WP_Comment_Query();
1480
        $q->query(
1481
            array(
1482
            'orderby' => 'comment_agent comment_approved',
1483
            ) 
1484
        );
1485
1486
        $this->assertContains("ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request);
1487
    }
1488
1489 View Code Duplication
    public function test_orderby_comma_separated() 
1490
    {
1491
        global $wpdb;
1492
1493
        $q = new WP_Comment_Query();
1494
        $q->query(
1495
            array(
1496
            'orderby' => 'comment_agent, comment_approved',
1497
            ) 
1498
        );
1499
1500
        $this->assertContains("ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request);
1501
    }
1502
1503 View Code Duplication
    public function test_orderby_flat_array() 
1504
    {
1505
        global $wpdb;
1506
1507
        $q = new WP_Comment_Query();
1508
        $q->query(
1509
            array(
1510
            'orderby' => array( 'comment_agent', 'comment_approved' ),
1511
            ) 
1512
        );
1513
1514
        $this->assertContains("ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request);
1515
    }
1516
1517 View Code Duplication
    public function test_orderby_array_contains_invalid_item() 
1518
    {
1519
        global $wpdb;
1520
1521
        $q = new WP_Comment_Query();
1522
        $q->query(
1523
            array(
1524
            'orderby' => array( 'comment_agent', 'foo', 'comment_approved' ),
1525
            ) 
1526
        );
1527
1528
        $this->assertContains("ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request);
1529
    }
1530
1531 View Code Duplication
    public function test_orderby_array_contains_all_invalid_items() 
1532
    {
1533
        global $wpdb;
1534
1535
        $q = new WP_Comment_Query();
1536
        $q->query(
1537
            array(
1538
            'orderby' => array( 'foo', 'bar', 'baz' ),
1539
            ) 
1540
        );
1541
1542
        $this->assertContains("ORDER BY $wpdb->comments.comment_date_gmt", $q->request);
1543
    }
1544
1545
    /**
1546
     * @ticket 29902
1547
     */
1548
    public function test_orderby_none() 
1549
    {
1550
        $q = new WP_Comment_Query();
1551
        $q->query(
1552
            array(
1553
            'orderby' => 'none',
1554
            ) 
1555
        );
1556
1557
        $this->assertNotContains('ORDER BY', $q->request);
1558
    }
1559
1560
    /**
1561
     * @ticket 29902
1562
     */
1563
    public function test_orderby_empty_array() 
1564
    {
1565
        $q = new WP_Comment_Query();
1566
        $q->query(
1567
            array(
1568
            'orderby' => array(),
1569
            ) 
1570
        );
1571
1572
        $this->assertNotContains('ORDER BY', $q->request);
1573
    }
1574
1575
    /**
1576
     * @ticket 29902
1577
     */
1578 View Code Duplication
    public function test_orderby_false() 
1579
    {
1580
        $q = new WP_Comment_Query();
1581
        $q->query(
1582
            array(
1583
            'orderby' => false,
1584
            ) 
1585
        );
1586
1587
        $this->assertNotContains('ORDER BY', $q->request);
1588
    }
1589
1590
    /**
1591
     * @ticket 30478
1592
     */
1593 View Code Duplication
    public function test_orderby_array() 
1594
    {
1595
        global $wpdb;
1596
1597
        $q = new WP_Comment_Query();
1598
        $found = $q->query(
1599
            array(
1600
            'fields' => 'ids',
1601
            'orderby' => array(
1602
            'comment_agent' => 'DESC',
1603
            'comment_date_gmt' => 'ASC',
1604
            'comment_ID' => 'DESC',
1605
            ),
1606
            ) 
1607
        );
1608
1609
        $this->assertContains("ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt ASC, $wpdb->comments.comment_ID DESC", $q->request);
1610
    }
1611
1612
    /**
1613
     * @ticket 30478
1614
     */
1615 View Code Duplication
    public function test_orderby_array_should_discard_invalid_columns() 
1616
    {
1617
        global $wpdb;
1618
1619
        $q = new WP_Comment_Query();
1620
        $found = $q->query(
1621
            array(
1622
            'fields' => 'ids',
1623
            'orderby' => array(
1624
            'comment_agent' => 'DESC',
1625
            'foo' => 'ASC',
1626
            'comment_ID' => 'DESC',
1627
            ),
1628
            ) 
1629
        );
1630
1631
        $this->assertContains("ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_ID DESC", $q->request);
1632
    }
1633
1634
    /**
1635
     * @ticket 30478
1636
     */
1637 View Code Duplication
    public function test_orderby_array_should_convert_invalid_order_to_DESC() 
0 ignored issues
show
Coding Style introduced by
The function name test_orderby_array_shoul...t_invalid_order_to_DESC is in camel caps, but expected test_orderby_array_shoul...nvalid_order_to_d_e_s_c instead as per the coding standard.
Loading history...
1638
    {
1639
        global $wpdb;
1640
1641
        $q = new WP_Comment_Query();
1642
        $found = $q->query(
1643
            array(
1644
            'fields' => 'ids',
1645
            'orderby' => array(
1646
            'comment_agent' => 'DESC',
1647
            'comment_date_gmt' => 'foo',
1648
            'comment_ID' => 'DESC',
1649
            ),
1650
            ) 
1651
        );
1652
1653
        $this->assertContains("ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt DESC, $wpdb->comments.comment_ID DESC", $q->request);
1654
    }
1655
1656
    /**
1657
     * @ticket 30478
1658
     */
1659 View Code Duplication
    public function test_orderby_array_should_sort_by_comment_ID_as_fallback_and_should_inherit_order_from_comment_date_gmt() 
0 ignored issues
show
Coding Style introduced by
The function name test_orderby_array_shoul...r_from_comment_date_gmt is in camel caps, but expected test_orderby_array_shoul...r_from_comment_date_gmt instead as per the coding standard.
Loading history...
1660
    {
1661
        global $wpdb;
1662
1663
        $q = new WP_Comment_Query();
1664
        $found = $q->query(
1665
            array(
1666
            'fields' => 'ids',
1667
            'orderby' => array(
1668
            'comment_agent' => 'DESC',
1669
            'comment_date_gmt' => 'ASC',
1670
            ),
1671
            ) 
1672
        );
1673
1674
        $this->assertContains("ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt ASC, $wpdb->comments.comment_ID ASC", $q->request);
1675
    }
1676
1677
    /**
1678
     * @ticket 30478
1679
     */
1680 View Code Duplication
    public function test_orderby_array_should_sort_by_comment_ID_as_fallback_and_should_inherit_order_from_comment_date() 
0 ignored issues
show
Coding Style introduced by
The function name test_orderby_array_shoul...order_from_comment_date is in camel caps, but expected test_orderby_array_shoul...order_from_comment_date instead as per the coding standard.
Loading history...
1681
    {
1682
        global $wpdb;
1683
1684
        $q = new WP_Comment_Query();
1685
        $found = $q->query(
1686
            array(
1687
            'fields' => 'ids',
1688
            'orderby' => array(
1689
            'comment_agent' => 'DESC',
1690
            'comment_date' => 'ASC',
1691
            ),
1692
            ) 
1693
        );
1694
1695
        $this->assertContains("ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date ASC, $wpdb->comments.comment_ID ASC", $q->request);
1696
    }
1697
1698
    /**
1699
     * @ticket 30478
1700
     */
1701 View Code Duplication
    public function test_orderby_array_should_sort_by_comment_ID_DESC_as_fallback_when_not_sorted_by_date() 
0 ignored issues
show
Coding Style introduced by
The function name test_orderby_array_shoul...when_not_sorted_by_date is in camel caps, but expected test_orderby_array_shoul...when_not_sorted_by_date instead as per the coding standard.
Loading history...
1702
    {
1703
        global $wpdb;
1704
1705
        $q = new WP_Comment_Query();
1706
        $found = $q->query(
1707
            array(
1708
            'fields' => 'ids',
1709
            'orderby' => array(
1710
            'comment_agent' => 'ASC',
1711
            ),
1712
            ) 
1713
        );
1714
1715
        $this->assertContains("ORDER BY $wpdb->comments.comment_agent ASC, $wpdb->comments.comment_ID DESC", $q->request);
1716
    }
1717
1718
    /**
1719
     * @ticket 30478
1720
     */
1721 View Code Duplication
    public function test_orderby_date_modified_gmt_should_order_by_comment_ID_in_case_of_tie_ASC() 
0 ignored issues
show
Coding Style introduced by
The function name test_orderby_date_modifi...t_ID_in_case_of_tie_ASC is in camel caps, but expected test_orderby_date_modifi..._d_in_case_of_tie_a_s_c instead as per the coding standard.
Loading history...
1722
    {
1723
        $now = current_time('mysql', 1);
1724
        $comments = self::factory()->comment->create_many(
1725
            5, array(
1726
            'comment_post_ID' => self::$post_id,
1727
            'comment_date_gmt' => $now,
1728
            ) 
1729
        );
1730
1731
        $q = new WP_Comment_Query();
1732
        $found = $q->query(
1733
            array(
1734
            'orderby' => 'comment_date_gmt',
1735
            'order' => 'ASC',
1736
            ) 
1737
        );
1738
1739
        // $comments is ASC by default.
1740
        $this->assertEquals($comments, wp_list_pluck($found, 'comment_ID'));
1741
    }
1742
1743
    /**
1744
     * @ticket 30478
1745
     */
1746 View Code Duplication
    public function test_orderby_date_modified_gmt_should_order_by_comment_ID_in_case_of_tie_DESC() 
0 ignored issues
show
Coding Style introduced by
The function name test_orderby_date_modifi..._ID_in_case_of_tie_DESC is in camel caps, but expected test_orderby_date_modifi..._in_case_of_tie_d_e_s_c instead as per the coding standard.
Loading history...
1747
    {
1748
        $now = current_time('mysql', 1);
1749
        $comments = self::factory()->comment->create_many(
1750
            5, array(
1751
            'comment_post_ID' => self::$post_id,
1752
            'comment_date_gmt' => $now,
1753
            ) 
1754
        );
1755
1756
        $q = new WP_Comment_Query();
1757
        $found = $q->query(
1758
            array(
1759
            'orderby' => 'comment_date_gmt',
1760
            'order' => 'DESC',
1761
            ) 
1762
        );
1763
1764
        // $comments is ASC by default.
1765
        rsort($comments);
1766
1767
        $this->assertEquals($comments, wp_list_pluck($found, 'comment_ID'));
1768
    }
1769
1770
    public function test_meta_vars_should_be_converted_to_meta_query() 
1771
    {
1772
        $q = new WP_Comment_Query();
1773
        $q->query(
1774
            array(
1775
            'meta_key' => 'foo',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1776
            'meta_value' => '5',
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
1777
            'meta_compare' => '>',
1778
            'meta_type' => 'SIGNED',
1779
            ) 
1780
        );
1781
1782
        $this->assertSame('foo', $q->meta_query->queries[0]['key']);
1783
        $this->assertSame('5', $q->meta_query->queries[0]['value']);
1784
        $this->assertSame('>', $q->meta_query->queries[0]['compare']);
1785
        $this->assertSame('SIGNED', $q->meta_query->queries[0]['type']);
1786
    }
1787
1788
    public function test_count() 
1789
    {
1790
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7 ));
1791
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7 ));
1792
1793
        $q = new WP_Comment_Query();
1794
        $found = $q->query(
1795
            array(
1796
            'count' => true,
1797
            ) 
1798
        );
1799
1800
        $this->assertEquals(2, $found);
1801
    }
1802
1803
    /**
1804
     * @ticket 23369
1805
     */
1806
    public function test_count_with_meta_query() 
1807
    {
1808
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7 ));
1809
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7 ));
1810
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'user_id' => 7 ));
1811
        add_comment_meta($c1, 'foo', 'bar');
1812
        add_comment_meta($c3, 'foo', 'bar');
1813
1814
        $q = new WP_Comment_Query();
1815
        $found = $q->query(
1816
            array(
1817
            'count' => true,
1818
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1819
            array(
1820
            'key' => 'foo',
1821
            'value' => 'bar',
1822
            ),
1823
            ),
1824
            ) 
1825
        );
1826
1827
        $this->assertEquals(2, $found);
1828
    }
1829
1830 View Code Duplication
    public function test_post_type_single_value() 
1831
    {
1832
        register_post_type('post-type-1');
1833
        register_post_type('post-type-2');
1834
1835
        $p1 = self::factory()->post->create(array( 'post_type' => 'post-type-1' ));
1836
        $p2 = self::factory()->post->create(array( 'post_type' => 'post-type-2' ));
1837
1838
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
1839
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
1840
1841
        $q = new WP_Comment_Query();
1842
        $found = $q->query(
1843
            array(
1844
            'fields' => 'ids',
1845
            'post_type' => 'post-type-2',
1846
            ) 
1847
        );
1848
1849
        $this->assertEqualSets($c2, $found);
1850
1851
        _unregister_post_type('post-type-1');
1852
        _unregister_post_type('post-type-2');
1853
    }
1854
1855
    /**
1856
     * @ticket 20006
1857
     */
1858 View Code Duplication
    public function test_post_type_singleton_array() 
1859
    {
1860
        register_post_type('post-type-1');
1861
        register_post_type('post-type-2');
1862
1863
        $p1 = self::factory()->post->create(array( 'post_type' => 'post-type-1' ));
1864
        $p2 = self::factory()->post->create(array( 'post_type' => 'post-type-2' ));
1865
1866
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
1867
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
1868
1869
        $q = new WP_Comment_Query();
1870
        $found = $q->query(
1871
            array(
1872
            'fields' => 'ids',
1873
            'post_type' => array( 'post-type-2' ),
1874
            ) 
1875
        );
1876
1877
        $this->assertEqualSets($c2, $found);
1878
1879
        _unregister_post_type('post-type-1');
1880
        _unregister_post_type('post-type-2');
1881
    }
1882
1883
    /**
1884
     * @ticket 20006
1885
     */
1886
    public function test_post_type_array() 
1887
    {
1888
        register_post_type('post-type-1');
1889
        register_post_type('post-type-2');
1890
        register_post_type('post-type-3');
1891
1892
        $p1 = self::factory()->post->create(array( 'post_type' => 'post-type-1' ));
1893
        $p2 = self::factory()->post->create(array( 'post_type' => 'post-type-2' ));
1894
        $p3 = self::factory()->post->create(array( 'post_type' => 'post-type-3' ));
1895
1896
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
1897
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
1898
        $c3 = self::factory()->comment->create_post_comments($p3, 1);
1899
1900
        $q = new WP_Comment_Query();
1901
        $found = $q->query(
1902
            array(
1903
            'fields' => 'ids',
1904
            'post_type' => array( 'post-type-1', 'post-type-3' ),
1905
            ) 
1906
        );
1907
1908
        $this->assertEqualSets(array_merge($c1, $c3), $found);
1909
    }
1910
1911 View Code Duplication
    public function test_post_name_single_value() 
1912
    {
1913
        $p1 = self::factory()->post->create(array( 'post_name' => 'foo' ));
1914
        $p2 = self::factory()->post->create(array( 'post_name' => 'bar' ));
1915
1916
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
1917
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
1918
1919
        $q = new WP_Comment_Query();
1920
        $found = $q->query(
1921
            array(
1922
            'fields' => 'ids',
1923
            'post_name' => 'bar',
1924
            ) 
1925
        );
1926
1927
        $this->assertEqualSets($c2, $found);
1928
    }
1929
1930
    /**
1931
     * @ticket 20006
1932
     */
1933 View Code Duplication
    public function test_post_name_singleton_array() 
1934
    {
1935
        $p1 = self::factory()->post->create(array( 'post_name' => 'foo' ));
1936
        $p2 = self::factory()->post->create(array( 'post_name' => 'bar' ));
1937
1938
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
1939
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
1940
1941
        $q = new WP_Comment_Query();
1942
        $found = $q->query(
1943
            array(
1944
            'fields' => 'ids',
1945
            'post_name' => array( 'bar' ),
1946
            ) 
1947
        );
1948
1949
        $this->assertEqualSets($c2, $found);
1950
    }
1951
1952
    /**
1953
     * @ticket 20006
1954
     */
1955 View Code Duplication
    public function test_post_name_array() 
1956
    {
1957
        $p1 = self::factory()->post->create(array( 'post_name' => 'foo' ));
1958
        $p2 = self::factory()->post->create(array( 'post_name' => 'bar' ));
1959
        $p3 = self::factory()->post->create(array( 'post_name' => 'baz' ));
1960
1961
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
1962
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
1963
        $c3 = self::factory()->comment->create_post_comments($p3, 1);
1964
1965
        $q = new WP_Comment_Query();
1966
        $found = $q->query(
1967
            array(
1968
            'fields' => 'ids',
1969
            'post_name' => array( 'foo', 'baz' ),
1970
            ) 
1971
        );
1972
1973
        $this->assertEqualSets(array_merge($c1, $c3), $found);
1974
    }
1975
1976 View Code Duplication
    public function test_post_status_single_value() 
1977
    {
1978
        $p1 = self::factory()->post->create(array( 'post_status' => 'publish' ));
1979
        $p2 = self::factory()->post->create(array( 'post_status' => 'draft' ));
1980
1981
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
1982
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
1983
1984
        $q = new WP_Comment_Query();
1985
        $found = $q->query(
1986
            array(
1987
            'fields' => 'ids',
1988
            'post_status' => 'draft',
1989
            ) 
1990
        );
1991
1992
        $this->assertEqualSets($c2, $found);
1993
    }
1994
1995
    /**
1996
     * @ticket 20006
1997
     */
1998 View Code Duplication
    public function test_post_status_singleton_array() 
1999
    {
2000
        $p1 = self::factory()->post->create(array( 'post_status' => 'publish' ));
2001
        $p2 = self::factory()->post->create(array( 'post_status' => 'draft' ));
2002
2003
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
2004
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
2005
2006
        $q = new WP_Comment_Query();
2007
        $found = $q->query(
2008
            array(
2009
            'fields' => 'ids',
2010
            'post_status' => array( 'draft' ),
2011
            ) 
2012
        );
2013
2014
        $this->assertEqualSets($c2, $found);
2015
    }
2016
2017
    /**
2018
     * @ticket 20006
2019
     */
2020 View Code Duplication
    public function test_post_status_array() 
2021
    {
2022
        $p1 = self::factory()->post->create(array( 'post_status' => 'publish' ));
2023
        $p2 = self::factory()->post->create(array( 'post_status' => 'draft' ));
2024
        $p3 = self::factory()->post->create(array( 'post_status' => 'future' ));
2025
2026
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
2027
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
2028
        $c3 = self::factory()->comment->create_post_comments($p3, 1);
2029
2030
        $q = new WP_Comment_Query();
2031
        $found = $q->query(
2032
            array(
2033
            'fields' => 'ids',
2034
            'post_status' => array( 'publish', 'future' ),
2035
            ) 
2036
        );
2037
2038
        $this->assertEqualSets(array_merge($c1, $c3), $found);
2039
    }
2040
2041
    /**
2042
     * @ticket 35512
2043
     */
2044 View Code Duplication
    public function test_post_type_any_should_override_other_post_types() 
2045
    {
2046
        register_post_type('post-type-1', array( 'exclude_from_search' => false ));
2047
        register_post_type('post-type-2', array( 'exclude_from_search' => false ));
2048
2049
        $p1 = self::factory()->post->create(array( 'post_type' => 'post-type-1' ));
2050
        $p2 = self::factory()->post->create(array( 'post_type' => 'post-type-2' ));
2051
2052
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
2053
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
2054
2055
        $q = new WP_Comment_Query();
2056
        $found = $q->query(
2057
            array(
2058
            'fields' => 'ids',
2059
            'post_type' => array( 'any', 'post-type-1' ),
2060
            ) 
2061
        );
2062
        $this->assertEqualSets(array_merge($c1, $c2), $found);
2063
    }
2064
2065
    /**
2066
     * @ticket 35512
2067
     */
2068 View Code Duplication
    public function test_post_type_any_as_part_of_an_array_of_post_types() 
2069
    {
2070
        register_post_type('post-type-1', array( 'exclude_from_search' => false ));
2071
        register_post_type('post-type-2', array( 'exclude_from_search' => false ));
2072
2073
        $p1 = self::factory()->post->create(array( 'post_type' => 'post-type-1' ));
2074
        $p2 = self::factory()->post->create(array( 'post_type' => 'post-type-2' ));
2075
2076
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
2077
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
2078
2079
        $q = new WP_Comment_Query();
2080
        $found = $q->query(
2081
            array(
2082
            'fields' => 'ids',
2083
            'post_type' => array( 'any' ),
2084
            ) 
2085
        );
2086
        $this->assertEqualSets(array_merge($c1, $c2), $found);
2087
    }
2088
2089
    /**
2090
     * @ticket 35512
2091
     */
2092 View Code Duplication
    public function test_post_status_any_should_override_other_post_statuses() 
2093
    {
2094
        $p1 = self::factory()->post->create(array( 'post_status' => 'publish' ));
2095
        $p2 = self::factory()->post->create(array( 'post_status' => 'draft' ));
2096
2097
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
2098
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
2099
2100
        $q = new WP_Comment_Query();
2101
        $found = $q->query(
2102
            array(
2103
            'fields' => 'ids',
2104
            'post_status' => array( 'any', 'draft' ),
2105
            ) 
2106
        );
2107
        $this->assertEqualSets(array_merge($c1, $c2), $found);
2108
    }
2109
2110
    /**
2111
     * @ticket 35512
2112
     */
2113 View Code Duplication
    public function test_post_status_any_as_part_of_an_array_of_post_statuses() 
2114
    {
2115
        $p1 = self::factory()->post->create(array( 'post_status' => 'publish' ));
2116
        $p2 = self::factory()->post->create(array( 'post_status' => 'draft' ));
2117
2118
        $c1 = self::factory()->comment->create_post_comments($p1, 1);
2119
        $c2 = self::factory()->comment->create_post_comments($p2, 1);
2120
2121
        $q = new WP_Comment_Query();
2122
        $found = $q->query(
2123
            array(
2124
            'fields' => 'ids',
2125
            'post_status' => array( 'any' ),
2126
            ) 
2127
        );
2128
        $this->assertEqualSets(array_merge($c1, $c2), $found);
2129
    }
2130
2131
    /**
2132
     * @ticket 24826
2133
     */
2134
    public function test_comment_query_object() 
2135
    {
2136
        $comment_id = self::factory()->comment->create();
2137
2138
        $query1 = new WP_Comment_Query();
2139
        $this->assertNull($query1->query_vars);
2140
        $this->assertEmpty($query1->comments);
2141
        $comments = $query1->query(array( 'status' => 'all' ));
2142
        $this->assertInternalType('array', $query1->query_vars);
2143
        $this->assertNotEmpty($query1->comments);
2144
        $this->assertInternalType('array', $query1->comments);
2145
2146
        $query2 = new WP_Comment_Query(array( 'status' => 'all' ));
2147
        $this->assertNotEmpty($query2->query_vars);
2148
        $this->assertNotEmpty($query2->comments);
2149
        $this->assertEquals($query2->comments, $query1->get_comments());
2150
    }
2151
2152
    /**
2153
     * @ticket 22400
2154
     */
2155
    public function test_comment_cache_key_should_ignore_custom_params() 
2156
    {
2157
        global $wpdb;
2158
2159
        $p = self::factory()->post->create();
2160
        $c = self::factory()->comment->create(array( 'comment_post_ID' => $p ));
2161
2162
        $q1 = new WP_Comment_Query();
2163
        $q1->query(
2164
            array(
2165
            'post_id' => $p,
2166
            'fields' => 'ids',
2167
            ) 
2168
        );
2169
2170
        $num_queries = $wpdb->num_queries;
2171
2172
        $q2 = new WP_Comment_Query();
2173
        $q2->query(
2174
            array(
2175
            'post_id' => $p,
2176
            'fields' => 'ids',
2177
            'foo' => 'bar',
2178
            ) 
2179
        );
2180
2181
        $this->assertSame($num_queries, $wpdb->num_queries);
2182
    }
2183
2184
    /**
2185
     * @ticket 35677
2186
     */
2187 View Code Duplication
    public function test_cache_should_be_sensitive_to_parent__in() 
2188
    {
2189
        global $wpdb;
2190
2191
        $q1 = new WP_Comment_Query(
2192
            array(
2193
            'parent__in' => array( 1, 2, 3 ),
2194
            ) 
2195
        );
2196
2197
        $num_queries = $wpdb->num_queries;
2198
2199
        $q2 = new WP_Comment_Query(
2200
            array(
2201
            'parent__in' => array( 4, 5, 6 ),
2202
            ) 
2203
        );
2204
2205
        $this->assertNotEquals($num_queries, $wpdb->num_queries);
2206
    }
2207
2208
    /**
2209
     * @ticket 35677
2210
     */
2211 View Code Duplication
    public function test_cache_should_be_sensitive_to_parent__not_in() 
2212
    {
2213
        global $wpdb;
2214
2215
        $q1 = new WP_Comment_Query(
2216
            array(
2217
            'parent__not_in' => array( 1, 2, 3 ),
2218
            ) 
2219
        );
2220
2221
        $num_queries = $wpdb->num_queries;
2222
2223
        $q2 = new WP_Comment_Query(
2224
            array(
2225
            'parent__not_in' => array( 4, 5, 6 ),
2226
            ) 
2227
        );
2228
2229
        $this->assertNotEquals($num_queries, $wpdb->num_queries);
2230
    }
2231
2232
    /**
2233
     * @ticket 32762
2234
     */
2235 View Code Duplication
    public function test_it_should_be_possible_to_modify_meta_query_using_pre_get_comments_action() 
2236
    {
2237
        $comments = self::factory()->comment->create_many(
2238
            2, array(
2239
            'comment_post_ID' => self::$post_id,
2240
            ) 
2241
        );
2242
2243
        add_comment_meta($comments[1], 'foo', 'bar');
2244
2245
        add_action('pre_get_comments', array( $this, 'modify_meta_query' ));
2246
2247
        $q = new WP_Comment_Query(
2248
            array(
2249
            'comment_post_ID' => self::$post_id,
2250
            'fields' => 'ids',
2251
            ) 
2252
        );
2253
2254
        remove_action('pre_get_comments', array( $this, 'modify_meta_query' ));
2255
2256
        $this->assertEqualSets(array( $comments[1] ), $q->comments);
2257
    }
2258
2259
    public function modify_meta_query( $q ) 
2260
    {
2261
        $q->meta_query = new WP_Meta_Query(
2262
            array(
2263
            array(
2264
            'key' => 'foo',
2265
            'value' => 'bar',
2266
            ),
2267
            ) 
2268
        );
2269
    }
2270
2271
    /**
2272
     * @ticket 32762
2273
     */
2274 View Code Duplication
    public function test_it_should_be_possible_to_modify_meta_params_using_pre_get_comments_action() 
2275
    {
2276
        $comments = self::factory()->comment->create_many(
2277
            2, array(
2278
            'comment_post_ID' => self::$post_id,
2279
            ) 
2280
        );
2281
2282
        add_comment_meta($comments[1], 'foo', 'bar');
2283
2284
        add_action('pre_get_comments', array( $this, 'modify_meta_params' ));
2285
2286
        $q = new WP_Comment_Query(
2287
            array(
2288
            'comment_post_ID' => self::$post_id,
2289
            'fields' => 'ids',
2290
            ) 
2291
        );
2292
2293
        remove_action('pre_get_comments', array( $this, 'modify_meta_params' ));
2294
2295
        $this->assertEqualSets(array( $comments[1] ), $q->comments);
2296
    }
2297
2298
    public function modify_meta_params( $q ) 
2299
    {
2300
        $q->query_vars['meta_key'] = 'foo';
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
2301
        $q->query_vars['meta_value'] = 'bar';
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
2302
    }
2303
2304
    /**
2305
     * @ticket 33882
2306
     */
2307 View Code Duplication
    public function test_parent__in() 
2308
    {
2309
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
2310
        $c2 = self::factory()->comment->create(
2311
            array(
2312
            'comment_post_ID' => self::$post_id,
2313
            'comment_approved' => '1',
2314
            'comment_parent' => $c1,
2315
            ) 
2316
        );
2317
2318
        $ids = new WP_Comment_Query(
2319
            array(
2320
            'comment_post_ID' => self::$post_id,
2321
            'fields' => 'ids',
2322
            'parent__in' => array( $c1 )
2323
            ) 
2324
        );
2325
2326
        $this->assertEqualSets(array( $c2 ), $ids->comments);
2327
    }
2328
2329
    /**
2330
     * @ticket 33882
2331
     */
2332 View Code Duplication
    public function test_parent__in_commas() 
2333
    {
2334
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
2335
        $c2 = self::factory()->comment->create(
2336
            array(
2337
            'comment_post_ID' => self::$post_id,
2338
            'comment_approved' => '1'
2339
            ) 
2340
        );
2341
        $c3 = self::factory()->comment->create(
2342
            array(
2343
            'comment_post_ID' => self::$post_id,
2344
            'comment_approved' => '1',
2345
            'comment_parent' => $c1,
2346
            ) 
2347
        );
2348
        $c4 = self::factory()->comment->create(
2349
            array(
2350
            'comment_post_ID' => self::$post_id,
2351
            'comment_approved' => '1',
2352
            'comment_parent' => $c2,
2353
            ) 
2354
        );
2355
2356
        $ids = new WP_Comment_Query(
2357
            array(
2358
            'comment_post_ID' => self::$post_id,
2359
            'fields' => 'ids',
2360
            'parent__in' => "$c1,$c2"
2361
            ) 
2362
        );
2363
2364
        $this->assertEqualSets(array( $c3, $c4 ), $ids->comments);
2365
    }
2366
2367
    /**
2368
     * @ticket 33882
2369
     */
2370 View Code Duplication
    public function test_parent__not_in() 
2371
    {
2372
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
2373
2374
        self::factory()->comment->create(
2375
            array(
2376
            'comment_post_ID' => self::$post_id,
2377
            'comment_approved' => '1',
2378
            'comment_parent' => $c1,
2379
            ) 
2380
        );
2381
2382
        $ids = new WP_Comment_Query(
2383
            array(
2384
            'comment_post_ID' => self::$post_id,
2385
            'fields' => 'ids',
2386
            'parent__not_in' => array( $c1 )
2387
            ) 
2388
        );
2389
2390
        $this->assertEqualSets(array( $c1 ), $ids->comments);
2391
    }
2392
2393
    /**
2394
     * @ticket 33882
2395
     */
2396 View Code Duplication
    public function test_parent__not_in_commas() 
2397
    {
2398
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
2399
        $c2 = self::factory()->comment->create(
2400
            array(
2401
            'comment_post_ID' => self::$post_id,
2402
            'comment_approved' => '1'
2403
            ) 
2404
        );
2405
2406
        self::factory()->comment->create(
2407
            array(
2408
            'comment_post_ID' => self::$post_id,
2409
            'comment_approved' => '1',
2410
            'comment_parent' => $c1,
2411
            ) 
2412
        );
2413
        self::factory()->comment->create(
2414
            array(
2415
            'comment_post_ID' => self::$post_id,
2416
            'comment_approved' => '1',
2417
            'comment_parent' => $c2,
2418
            ) 
2419
        );
2420
2421
        $ids = new WP_Comment_Query(
2422
            array(
2423
            'comment_post_ID' => self::$post_id,
2424
            'fields' => 'ids',
2425
            'parent__not_in' => "$c1,$c2"
2426
            ) 
2427
        );
2428
2429
        $this->assertEqualSets(array( $c1, $c2 ), $ids->comments);
2430
    }
2431
2432
    /**
2433
     * @ticket 33883
2434
     */
2435
    public function test_orderby_comment__in() 
2436
    {
2437
        self::factory()->comment->create(
2438
            array(
2439
            'comment_post_ID' => self::$post_id,
2440
            'comment_approved' => '1'
2441
            ) 
2442
        );
2443
2444
        $c2 = self::factory()->comment->create(
2445
            array(
2446
            'comment_post_ID' => self::$post_id,
2447
            'comment_approved' => '1'
2448
            ) 
2449
        );
2450
        $c3 = self::factory()->comment->create(
2451
            array(
2452
            'comment_post_ID' => self::$post_id,
2453
            'comment_approved' => '1'
2454
            ) 
2455
        );
2456
2457
        self::factory()->comment->create(
2458
            array(
2459
            'comment_post_ID' => self::$post_id,
2460
            'comment_approved' => '1'
2461
            ) 
2462
        );
2463
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2464
2465
        $ids = new WP_Comment_Query(
2466
            array(
2467
            'fields' => 'ids',
2468
            'comment__in' => array( $c2, $c3 ),
2469
            'orderby' => 'comment__in'
2470
            ) 
2471
        );
2472
2473
        $this->assertEquals(array( $c2, $c3 ), $ids->comments);
2474
2475
    }
2476
2477
    /**
2478
     * @ticket 8071
2479
     */
2480 View Code Duplication
    public function test_no_found_rows_should_default_to_true() 
2481
    {
2482
        $comments = self::factory()->comment->create_many(3, array( 'comment_post_ID' => self::$post_id ));
2483
2484
        $q = new WP_Comment_Query(
2485
            array(
2486
            'post_id' => self::$post_id,
2487
            'number' => 2,
2488
            ) 
2489
        );
2490
2491
        $this->assertEquals(0, $q->found_comments);
2492
        $this->assertEquals(0, $q->max_num_pages);
2493
    }
2494
2495
    /**
2496
     * @ticket 8071
2497
     */
2498 View Code Duplication
    public function test_should_respect_no_found_rows_true() 
2499
    {
2500
        $comments = self::factory()->comment->create_many(3, array( 'comment_post_ID' => self::$post_id ));
2501
2502
        $q = new WP_Comment_Query(
2503
            array(
2504
            'post_id' => self::$post_id,
2505
            'number' => 2,
2506
            'no_found_rows' => true,
2507
            ) 
2508
        );
2509
2510
        $this->assertEquals(0, $q->found_comments);
2511
        $this->assertEquals(0, $q->max_num_pages);
2512
    }
2513
2514
    /**
2515
     * @ticket 8071
2516
     */
2517 View Code Duplication
    public function test_should_respect_no_found_rows_false() 
2518
    {
2519
        $comments = self::factory()->comment->create_many(3, array( 'comment_post_ID' => self::$post_id ));
2520
2521
        $q = new WP_Comment_Query(
2522
            array(
2523
            'post_id' => self::$post_id,
2524
            'number' => 2,
2525
            'no_found_rows' => false,
2526
            ) 
2527
        );
2528
2529
        $this->assertEquals(3, $q->found_comments);
2530
        $this->assertEquals(2, $q->max_num_pages);
2531
    }
2532
2533
    /**
2534
     * @ticket 37184
2535
     */
2536
    public function test_found_rows_should_be_fetched_from_the_cache() 
2537
    {
2538
        $comments = self::factory()->comment->create_many(3, array( 'comment_post_ID' => self::$post_id ));
2539
2540
        // Prime cache.
2541
        new WP_Comment_Query(
2542
            array(
2543
            'post_id' => self::$post_id,
2544
            'number' => 2,
2545
            'no_found_rows' => false,
2546
            ) 
2547
        );
2548
2549
        $q = new WP_Comment_Query(
2550
            array(
2551
            'post_id' => self::$post_id,
2552
            'number' => 2,
2553
            'no_found_rows' => false,
2554
            ) 
2555
        );
2556
2557
        $this->assertEquals(3, $q->found_comments);
2558
        $this->assertEquals(2, $q->max_num_pages);
2559
    }
2560
2561
    /**
2562
     * @ticket 8071
2563
     */
2564
    public function test_hierarchical_should_skip_child_comments_in_offset() 
2565
    {
2566
        $top_level_0 = self::factory()->comment->create(
2567
            array(
2568
            'comment_post_ID' => self::$post_id,
2569
            'comment_approved' => '1',
2570
            ) 
2571
        );
2572
2573
        $child_of_0 = self::factory()->comment->create(
2574
            array(
2575
            'comment_post_ID' => self::$post_id,
2576
            'comment_approved' => '1',
2577
            'comment_parent' => $top_level_0,
2578
            ) 
2579
        );
2580
2581
        $top_level_comments = self::factory()->comment->create_many(
2582
            3, array(
2583
            'comment_post_ID' => self::$post_id,
2584
            'comment_approved' => '1',
2585
            ) 
2586
        );
2587
2588
        $q = new WP_Comment_Query(
2589
            array(
2590
            'post_id' => self::$post_id,
2591
            'hierarchical' => 'flat',
2592
            'number' => 2,
2593
            'offset' => 1,
2594
            'orderby' => 'comment_ID',
2595
            'order' => 'ASC',
2596
            'fields' => 'ids',
2597
            ) 
2598
        );
2599
2600
        $this->assertEquals(array( $top_level_comments[0], $top_level_comments[1] ), $q->comments);
2601
    }
2602
2603
    /**
2604
     * @ticket 8071
2605
     */
2606
    public function test_hierarchical_should_not_include_child_comments_in_number() 
2607
    {
2608
        $top_level_0 = self::factory()->comment->create(
2609
            array(
2610
            'comment_post_ID' => self::$post_id,
2611
            'comment_approved' => '1',
2612
            ) 
2613
        );
2614
2615
        $child_of_0 = self::factory()->comment->create(
2616
            array(
2617
            'comment_post_ID' => self::$post_id,
2618
            'comment_approved' => '1',
2619
            'comment_parent' => $top_level_0,
2620
            ) 
2621
        );
2622
2623
        $top_level_comments = self::factory()->comment->create_many(
2624
            3, array(
2625
            'comment_post_ID' => self::$post_id,
2626
            'comment_approved' => '1',
2627
            ) 
2628
        );
2629
2630
        $q = new WP_Comment_Query(
2631
            array(
2632
            'post_id' => self::$post_id,
2633
            'hierarchical' => 'flat',
2634
            'number' => 2,
2635
            'orderby' => 'comment_ID',
2636
            'order' => 'ASC',
2637
            ) 
2638
        );
2639
2640
        $this->assertEqualSets(array( $top_level_0, $child_of_0, $top_level_comments[0] ), wp_list_pluck($q->comments, 'comment_ID'));
2641
    }
2642
2643
    /**
2644
     * @ticket 8071
2645
     */
2646
    public function test_hierarchical_threaded() 
2647
    {
2648
        $c1 = self::factory()->comment->create(
2649
            array(
2650
            'comment_post_ID' => self::$post_id,
2651
            'comment_approved' => '1',
2652
            ) 
2653
        );
2654
2655
        $c2 = self::factory()->comment->create(
2656
            array(
2657
            'comment_post_ID' => self::$post_id,
2658
            'comment_approved' => '1',
2659
            'comment_parent' => $c1,
2660
            ) 
2661
        );
2662
2663
        $c3 = self::factory()->comment->create(
2664
            array(
2665
            'comment_post_ID' => self::$post_id,
2666
            'comment_approved' => '1',
2667
            'comment_parent' => $c2,
2668
            ) 
2669
        );
2670
2671
        $c4 = self::factory()->comment->create(
2672
            array(
2673
            'comment_post_ID' => self::$post_id,
2674
            'comment_approved' => '1',
2675
            'comment_parent' => $c1,
2676
            ) 
2677
        );
2678
2679
        $c5 = self::factory()->comment->create(
2680
            array(
2681
            'comment_post_ID' => self::$post_id,
2682
            'comment_approved' => '1',
2683
            ) 
2684
        );
2685
2686
        $c6 = self::factory()->comment->create(
2687
            array(
2688
            'comment_post_ID' => self::$post_id,
2689
            'comment_approved' => '1',
2690
            'comment_parent' => $c5,
2691
            ) 
2692
        );
2693
2694
        $args = array(
2695
         'hierarchical' => 'threaded',
2696
         'orderby' => 'comment_ID',
2697
         'order' => 'ASC',
2698
        );
2699
2700
        $query_args = array_merge(
2701
            $args, array(
2702
            'post_id' => self::$post_id,
2703
            ) 
2704
        );
2705
2706
        $q = new WP_Comment_Query($query_args);
2707
2708
        // Top-level comments.
2709
        $this->assertEqualSets(array( $c1, $c5 ), array_values(wp_list_pluck($q->comments, 'comment_ID')));
2710
2711
        // Direct descendants of $c1.
2712
        $this->assertEqualSets(array( $c2, $c4 ), array_values(wp_list_pluck($q->comments[ $c1 ]->get_children($args), 'comment_ID')));
2713
2714
        // Direct descendants of $c2.
2715
        $this->assertEqualSets(array( $c3 ), array_values(wp_list_pluck($q->comments[ $c1 ]->get_child($c2)->get_children($args), 'comment_ID')));
2716
2717
        // Direct descendants of $c5.
2718
        $this->assertEqualSets(array( $c6 ), array_values(wp_list_pluck($q->comments[ $c5 ]->get_children($args), 'comment_ID')));
2719
    }
2720
2721
    /**
2722
     * @ticket 8071
2723
     */
2724
    public function test_hierarchical_threaded_approved() 
2725
    {
2726
        $c1 = self::factory()->comment->create(
2727
            array(
2728
            'comment_post_ID' => self::$post_id,
2729
            'comment_approved' => '1',
2730
            ) 
2731
        );
2732
2733
        $c2 = self::factory()->comment->create(
2734
            array(
2735
            'comment_post_ID' => self::$post_id,
2736
            'comment_approved' => '1',
2737
            'comment_parent' => $c1,
2738
            ) 
2739
        );
2740
2741
        $c3 = self::factory()->comment->create(
2742
            array(
2743
            'comment_post_ID' => self::$post_id,
2744
            'comment_approved' => '0',
2745
            'comment_parent' => $c2,
2746
            ) 
2747
        );
2748
2749
        $c4 = self::factory()->comment->create(
2750
            array(
2751
            'comment_post_ID' => self::$post_id,
2752
            'comment_approved' => '1',
2753
            'comment_parent' => $c1,
2754
            ) 
2755
        );
2756
2757
        $c5 = self::factory()->comment->create(
2758
            array(
2759
            'comment_post_ID' => self::$post_id,
2760
            'comment_approved' => '1',
2761
            ) 
2762
        );
2763
2764
        self::factory()->comment->create(
2765
            array(
2766
            'comment_post_ID' => self::$post_id,
2767
            'comment_approved' => '1',
2768
            'comment_parent' => $c5,
2769
            ) 
2770
        );
2771
2772
        $args = array(
2773
         'hierarchical' => 'threaded',
2774
         'status' => 'approve',
2775
         'orderby' => 'comment_ID',
2776
         'order' => 'ASC',
2777
        );
2778
2779
        $query_args = array_merge(
2780
            $args, array(
2781
            'post_id' => self::$post_id,
2782
            ) 
2783
        );
2784
2785
        $q = new WP_Comment_Query($query_args);
2786
2787
        // Top-level comments.
2788
        $this->assertEqualSets(array( $c1, $c5 ), array_values(wp_list_pluck($q->comments, 'comment_ID')));
2789
2790
        // Direct descendants of $c1.
2791
        $this->assertEqualSets(array( $c2, $c4 ), array_values(wp_list_pluck($q->comments[ $c1 ]->get_children($args), 'comment_ID')));
2792
2793
        // Direct descendants of $c2.
2794
        $this->assertEqualSets(array(), array_values(wp_list_pluck($q->comments[ $c1 ]->get_child($c2)->get_children($args), 'comment_ID')));
2795
    }
2796
2797
    /**
2798
     * @ticket 35192
2799
     */
2800 View Code Duplication
    public function test_comment_clauses_prepend_callback_should_be_respected_when_filling_descendants() 
2801
    {
2802
        $top_level_0 = self::factory()->comment->create(
2803
            array(
2804
            'comment_post_ID' => self::$post_id,
2805
            'comment_approved' => '1',
2806
            ) 
2807
        );
2808
2809
        $child1_of_0 = self::factory()->comment->create(
2810
            array(
2811
            'comment_post_ID' => self::$post_id,
2812
            'comment_approved' => '1',
2813
            'comment_parent' => $top_level_0,
2814
            ) 
2815
        );
2816
2817
        $child2_of_0 = self::factory()->comment->create(
2818
            array(
2819
            'comment_post_ID' => self::$post_id,
2820
            'comment_approved' => '1',
2821
            'comment_parent' => $top_level_0,
2822
            ) 
2823
        );
2824
2825
        $top_level_comments = self::factory()->comment->create_many(
2826
            3, array(
2827
            'comment_post_ID' => self::$post_id,
2828
            'comment_approved' => '1',
2829
            ) 
2830
        );
2831
2832
        $this->to_exclude = array( $child2_of_0, $top_level_comments[1] );
2833
2834
        add_filter('comments_clauses', array( $this, 'prepend_exclusions' ));
2835
        $q = new WP_Comment_Query(
2836
            array(
2837
            'post_id' => self::$post_id,
2838
            'hierarchical' => 'flat',
2839
            ) 
2840
        );
2841
        remove_filter('comments_clauses', array( $this, 'prepend_exclusions' ));
2842
2843
        unset($this->to_exclude);
2844
2845
        $this->assertEqualSets(array( $top_level_0, $child1_of_0, $top_level_comments[0], $top_level_comments[2] ), wp_list_pluck($q->comments, 'comment_ID'));
2846
    }
2847
2848
    public function prepend_exclusions( $clauses ) 
2849
    {
2850
        global $wpdb;
2851
        $clauses['where'] = $wpdb->prepare('comment_ID != %d AND comment_ID != %d AND ', $this->to_exclude[0], $this->to_exclude[1]) . $clauses['where'];
2852
        return $clauses;
2853
    }
2854
2855
    /**
2856
     * @ticket 35192
2857
     */
2858 View Code Duplication
    public function test_comment_clauses_append_callback_should_be_respected_when_filling_descendants() 
2859
    {
2860
        $top_level_0 = self::factory()->comment->create(
2861
            array(
2862
            'comment_post_ID' => self::$post_id,
2863
            'comment_approved' => '1',
2864
            ) 
2865
        );
2866
2867
        $child1_of_0 = self::factory()->comment->create(
2868
            array(
2869
            'comment_post_ID' => self::$post_id,
2870
            'comment_approved' => '1',
2871
            'comment_parent' => $top_level_0,
2872
            ) 
2873
        );
2874
2875
        $child2_of_0 = self::factory()->comment->create(
2876
            array(
2877
            'comment_post_ID' => self::$post_id,
2878
            'comment_approved' => '1',
2879
            'comment_parent' => $top_level_0,
2880
            ) 
2881
        );
2882
2883
        $top_level_comments = self::factory()->comment->create_many(
2884
            3, array(
2885
            'comment_post_ID' => self::$post_id,
2886
            'comment_approved' => '1',
2887
            ) 
2888
        );
2889
2890
        $this->to_exclude = array( $child2_of_0, $top_level_comments[1] );
2891
2892
        add_filter('comments_clauses', array( $this, 'append_exclusions' ));
2893
        $q = new WP_Comment_Query(
2894
            array(
2895
            'post_id' => self::$post_id,
2896
            'hierarchical' => 'flat',
2897
            ) 
2898
        );
2899
        remove_filter('comments_clauses', array( $this, 'append_exclusions' ));
2900
2901
        unset($this->to_exclude);
2902
2903
        $this->assertEqualSets(array( $top_level_0, $child1_of_0, $top_level_comments[0], $top_level_comments[2] ), wp_list_pluck($q->comments, 'comment_ID'));
2904
    }
2905
2906
    public function append_exclusions( $clauses ) 
2907
    {
2908
        global $wpdb;
2909
        $clauses['where'] .= $wpdb->prepare(' AND comment_ID != %d AND comment_ID != %d', $this->to_exclude[0], $this->to_exclude[1]);
2910
        return $clauses;
2911
    }
2912
2913
    /**
2914
     * @ticket 36487
2915
     */
2916
    public function test_cache_should_be_hit_when_querying_descendants() 
2917
    {
2918
        global $wpdb;
2919
2920
        $p = self::factory()->post->create();
2921
        $comment_1 = self::factory()->comment->create(
2922
            array(
2923
            'comment_post_ID' => $p,
2924
            'comment_approved' => '1',
2925
            ) 
2926
        );
2927
        $comment_2 = self::factory()->comment->create(
2928
            array(
2929
            'comment_post_ID' => $p,
2930
            'comment_approved' => '1',
2931
            'comment_parent' => $comment_1,
2932
            ) 
2933
        );
2934
        $comment_3 = self::factory()->comment->create(
2935
            array(
2936
            'comment_post_ID' => $p,
2937
            'comment_approved' => '1',
2938
            'comment_parent' => $comment_1,
2939
            ) 
2940
        );
2941
        $comment_4 = self::factory()->comment->create(
2942
            array(
2943
            'comment_post_ID' => $p,
2944
            'comment_approved' => '1',
2945
            'comment_parent' => $comment_2,
2946
            ) 
2947
        );
2948
2949
        $q1 = new WP_Comment_Query(
2950
            array(
2951
            'post_id' => $p,
2952
            'hierarchical' => true,
2953
            ) 
2954
        );
2955
        $q1_ids = wp_list_pluck($q1->comments, 'comment_ID');
2956
2957
        $num_queries = $wpdb->num_queries;
2958
        $q2 = new WP_Comment_Query(
2959
            array(
2960
            'post_id' => $p,
2961
            'hierarchical' => true,
2962
            ) 
2963
        );
2964
        $q2_ids = wp_list_pluck($q2->comments, 'comment_ID');
2965
2966
        $this->assertEqualSets($q1_ids, $q2_ids);
2967
        $this->assertSame($num_queries, $wpdb->num_queries);
2968
    }
2969
2970
    /**
2971
     * @ticket 37696
2972
     */
2973
    public function test_hierarchy_should_be_filled_when_cache_is_incomplete() 
2974
    {
2975
        global $wpdb;
2976
2977
        $p = self::factory()->post->create();
2978
        $comment_1 = self::factory()->comment->create(
2979
            array(
2980
            'comment_post_ID' => $p,
2981
            'comment_approved' => '1',
2982
            ) 
2983
        );
2984
        $comment_2 = self::factory()->comment->create(
2985
            array(
2986
            'comment_post_ID' => $p,
2987
            'comment_approved' => '1',
2988
            'comment_parent' => $comment_1,
2989
            ) 
2990
        );
2991
        $comment_3 = self::factory()->comment->create(
2992
            array(
2993
            'comment_post_ID' => $p,
2994
            'comment_approved' => '1',
2995
            'comment_parent' => $comment_1,
2996
            ) 
2997
        );
2998
        $comment_4 = self::factory()->comment->create(
2999
            array(
3000
            'comment_post_ID' => $p,
3001
            'comment_approved' => '1',
3002
            'comment_parent' => $comment_2,
3003
            ) 
3004
        );
3005
3006
        // Prime cache.
3007
        $q1 = new WP_Comment_Query(
3008
            array(
3009
            'post_id' => $p,
3010
            'hierarchical' => true,
3011
            ) 
3012
        );
3013
        $q1_ids = wp_list_pluck($q1->comments, 'comment_ID');
3014
        $this->assertEqualSets(array( $comment_1, $comment_2, $comment_3, $comment_4 ), $q1_ids);
3015
3016
        // Delete one of the parent caches.
3017
        $last_changed = wp_cache_get('last_changed', 'comment');
3018
        $key = md5(serialize(wp_array_slice_assoc($q1->query_vars, array_keys($q1->query_var_defaults))));
3019
        $cache_key = "get_comment_child_ids:$comment_2:$key:$last_changed";
3020
        wp_cache_delete($cache_key, 'comment');
3021
3022
        $q2 = new WP_Comment_Query(
3023
            array(
3024
            'post_id' => $p,
3025
            'hierarchical' => true,
3026
            ) 
3027
        );
3028
        $q2_ids = wp_list_pluck($q2->comments, 'comment_ID');
3029
        $this->assertEqualSets($q1_ids, $q2_ids);
3030
    }
3031
3032
    /**
3033
     * @ticket 37966
3034
     * @ticket 37696
3035
     */
3036
    public function test_fill_hierarchy_should_disregard_offset_and_number() 
3037
    {
3038
        $c0 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
3039
        $c1 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
3040
        $c2 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_parent' => $c1 ));
3041
        $c3 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ));
3042
        $c4 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_parent' => $c3 ));
3043
        $c5 = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_parent' => $c3 ));
3044
3045
        $q = new WP_Comment_Query();
3046
        $found = $q->query(
3047
            array(
3048
            'orderby' => 'comment_date_gmt',
3049
            'order' => 'ASC',
3050
            'status' => 'approve',
3051
            'post_id' => self::$post_id,
3052
            'no_found_rows' => false,
3053
            'hierarchical' => 'threaded',
3054
            'number' => 2,
3055
            'offset' => 1,
3056
            ) 
3057
        );
3058
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
3059
3060
        $found_1 = $found[ $c1 ];
3061
        $children_1 = $found_1->get_children();
3062
        $this->assertEqualSets(array( $c2 ), array_keys($children_1));
3063
3064
        $found_3 = $found[ $c3 ];
3065
        $children_3 = $found_3->get_children();
3066
        $this->assertEqualSets(array( $c4, $c5 ), array_keys($children_3));
3067
    }
3068
3069
    /**
3070
     * @ticket 27571
3071
     */
3072 View Code Duplication
    public function test_update_comment_post_cache_should_be_disabled_by_default() 
3073
    {
3074
        global $wpdb;
3075
3076
        $p = self::factory()->post->create();
3077
        $c = self::factory()->comment->create(array( 'comment_post_ID' => $p ));
3078
3079
        $q = new WP_Comment_Query(
3080
            array(
3081
            'post_ID' => $p,
3082
            ) 
3083
        );
3084
3085
        $num_queries = $wpdb->num_queries;
3086
        $this->assertTrue(isset($q->comments[0]->post_name));
3087
        $this->assertSame($num_queries + 1, $wpdb->num_queries);
3088
    }
3089
3090
    /**
3091
     * @ticket 27571
3092
     */
3093 View Code Duplication
    public function test_should_respect_update_comment_post_cache_true() 
3094
    {
3095
        global $wpdb;
3096
3097
        $p = self::factory()->post->create();
3098
        $c = self::factory()->comment->create(array( 'comment_post_ID' => $p ));
3099
3100
        $q = new WP_Comment_Query(
3101
            array(
3102
            'post_ID' => $p,
3103
            'update_comment_post_cache' => true,
3104
            ) 
3105
        );
3106
3107
        $num_queries = $wpdb->num_queries;
3108
        $this->assertTrue(isset($q->comments[0]->post_name));
3109
        $this->assertSame($num_queries, $wpdb->num_queries);
3110
    }
3111
3112
    /**
3113
     * @ticket 34138
3114
     */
3115
    public function test_comment_objects_should_be_filled_from_cache() 
3116
    {
3117
        global $wpdb;
3118
3119
        $comments = self::factory()->comment->create_many(3, array( 'comment_post_ID' => self::$post_id ));
3120
        clean_comment_cache($comments);
3121
3122
        $num_queries = $wpdb->num_queries;
3123
        $q = new WP_Comment_Query(
3124
            array(
3125
            'post_id' => self::$post_id,
3126
            'no_found_rows' => true,
3127
            'update_comment_post_cache' => false,
3128
            'update_comment_meta_cache' => false,
3129
            ) 
3130
        );
3131
3132
        // 2 queries should have been fired: one for IDs, one to prime comment caches.
3133
        $num_queries += 2;
3134
3135
        $found = wp_list_pluck($q->comments, 'comment_ID');
3136
        $this->assertEqualSets($comments, $found);
3137
3138
        $this->assertSame($num_queries, $wpdb->num_queries);
3139
    }
3140
3141
    /**
3142
     * @ticket 34138
3143
     */
3144
    public function test_comment_objects_should_be_fetched_from_database_when_suspend_cache_addition() 
3145
    {
3146
        $suspend = wp_suspend_cache_addition();
3147
        wp_suspend_cache_addition(true);
3148
3149
        $c = self::factory()->comment->create(array( 'comment_post_ID' => self::$post_id ));
3150
3151
        $q = new WP_Comment_Query(
3152
            array(
3153
            'post_id' => self::$post_id,
3154
            ) 
3155
        );
3156
3157
        wp_suspend_cache_addition($suspend);
3158
3159
        $found = wp_list_pluck($q->comments, 'comment_ID');
3160
        $this->assertEqualSets(array( $c ), $found);
3161
    }
3162
3163
    public function test_comment_query_should_be_cached() 
3164
    {
3165
        global $wpdb;
3166
3167
        $q = new WP_Comment_Query(
3168
            array(
3169
            'post_id' => self::$post_id,
3170
            'fields' => 'ids',
3171
            ) 
3172
        );
3173
3174
        $c = wp_insert_comment(
3175
            array(
3176
            'comment_author' => 'Foo',
3177
            'comment_author_email' => '[email protected]',
3178
            'comment_post_ID' => self::$post_id,
3179
            ) 
3180
        );
3181
3182
        $num_queries = $wpdb->num_queries;
3183
    }
3184
3185
    public function test_created_comment_should_invalidate_query_cache() 
3186
    {
3187
        global $wpdb;
3188
3189
        $c = self::factory()->comment->create(
3190
            array(
3191
            'comment_post_ID' => self::$post_id,
3192
            'comment_approved' => '1',
3193
            ) 
3194
        );
3195
3196
        $q = new WP_Comment_Query(
3197
            array(
3198
            'post_id' => self::$post_id,
3199
            'fields' => 'ids',
3200
            ) 
3201
        );
3202
3203
        $num_queries = $wpdb->num_queries;
3204
3205
        $q = new WP_Comment_Query(
3206
            array(
3207
            'post_id' => self::$post_id,
3208
            'fields' => 'ids',
3209
            ) 
3210
        );
3211
3212
        $this->assertSame($num_queries, $wpdb->num_queries);
3213
        $this->assertEqualSets(array( $c ), $q->comments);
3214
    }
3215
3216
    public function test_updated_comment_should_invalidate_query_cache() 
3217
    {
3218
        global $wpdb;
3219
3220
        $c = self::factory()->comment->create(
3221
            array(
3222
            'comment_post_ID' => self::$post_id,
3223
            'comment_approved' => '1',
3224
            ) 
3225
        );
3226
3227
        $q = new WP_Comment_Query(
3228
            array(
3229
            'post_id' => self::$post_id,
3230
            'fields' => 'ids',
3231
            ) 
3232
        );
3233
3234
        wp_update_comment(
3235
            array(
3236
            'comment_ID' => $c,
3237
            'comment_author' => 'Foo',
3238
            'comment_author_email' => '[email protected]',
3239
            'comment_post_ID' => self::$post_id,
3240
            ) 
3241
        );
3242
3243
        $num_queries = $wpdb->num_queries;
3244
3245
        $q = new WP_Comment_Query(
3246
            array(
3247
            'post_id' => self::$post_id,
3248
            'fields' => 'ids',
3249
            ) 
3250
        );
3251
3252
        $num_queries++;
3253
        $this->assertSame($num_queries, $wpdb->num_queries);
3254
        $this->assertEqualSets(array( $c ), $q->comments);
3255
    }
3256
3257 View Code Duplication
    public function test_deleted_comment_should_invalidate_query_cache() 
3258
    {
3259
        global $wpdb;
3260
3261
        $c = self::factory()->comment->create(
3262
            array(
3263
            'comment_post_ID' => self::$post_id,
3264
            'comment_approved' => '1',
3265
            ) 
3266
        );
3267
3268
        $q = new WP_Comment_Query(
3269
            array(
3270
            'post_id' => self::$post_id,
3271
            'fields' => 'ids',
3272
            ) 
3273
        );
3274
3275
        wp_delete_comment($c);
3276
3277
        $num_queries = $wpdb->num_queries;
3278
3279
        $q = new WP_Comment_Query(
3280
            array(
3281
            'post_id' => self::$post_id,
3282
            'fields' => 'ids',
3283
            ) 
3284
        );
3285
3286
        $num_queries++;
3287
        $this->assertSame($num_queries, $wpdb->num_queries);
3288
        $this->assertEqualSets(array(), $q->comments);
3289
    }
3290
3291 View Code Duplication
    public function test_trashed_comment_should_invalidate_query_cache() 
3292
    {
3293
        global $wpdb;
3294
3295
        $c = self::factory()->comment->create(
3296
            array(
3297
            'comment_post_ID' => self::$post_id,
3298
            'comment_approved' => '1',
3299
            ) 
3300
        );
3301
3302
        $q = new WP_Comment_Query(
3303
            array(
3304
            'post_id' => self::$post_id,
3305
            'fields' => 'ids',
3306
            ) 
3307
        );
3308
3309
        wp_trash_comment($c);
3310
3311
        $num_queries = $wpdb->num_queries;
3312
3313
        $q = new WP_Comment_Query(
3314
            array(
3315
            'post_id' => self::$post_id,
3316
            'fields' => 'ids',
3317
            ) 
3318
        );
3319
3320
        $num_queries++;
3321
        $this->assertSame($num_queries, $wpdb->num_queries);
3322
        $this->assertEqualSets(array(), $q->comments);
3323
    }
3324
3325 View Code Duplication
    public function test_untrashed_comment_should_invalidate_query_cache() 
3326
    {
3327
        global $wpdb;
3328
3329
        $c = self::factory()->comment->create(
3330
            array(
3331
            'comment_post_ID' => self::$post_id,
3332
            'comment_approved' => '1',
3333
            ) 
3334
        );
3335
3336
        wp_trash_comment($c);
3337
3338
        $q = new WP_Comment_Query(
3339
            array(
3340
            'post_id' => self::$post_id,
3341
            'fields' => 'ids',
3342
            ) 
3343
        );
3344
3345
        wp_untrash_comment($c);
3346
3347
        $num_queries = $wpdb->num_queries;
3348
3349
        $q = new WP_Comment_Query(
3350
            array(
3351
            'post_id' => self::$post_id,
3352
            'fields' => 'ids',
3353
            ) 
3354
        );
3355
3356
        $num_queries++;
3357
        $this->assertSame($num_queries, $wpdb->num_queries);
3358
        $this->assertEqualSets(array( $c ), $q->comments);
3359
    }
3360
3361 View Code Duplication
    public function test_spammed_comment_should_invalidate_query_cache() 
3362
    {
3363
        global $wpdb;
3364
3365
        $c = self::factory()->comment->create(
3366
            array(
3367
            'comment_post_ID' => self::$post_id,
3368
            'comment_approved' => '1',
3369
            ) 
3370
        );
3371
3372
        $q = new WP_Comment_Query(
3373
            array(
3374
            'post_id' => self::$post_id,
3375
            'fields' => 'ids',
3376
            ) 
3377
        );
3378
3379
        wp_spam_comment($c);
3380
3381
        $num_queries = $wpdb->num_queries;
3382
3383
        $q = new WP_Comment_Query(
3384
            array(
3385
            'post_id' => self::$post_id,
3386
            'fields' => 'ids',
3387
            ) 
3388
        );
3389
3390
        $num_queries++;
3391
        $this->assertSame($num_queries, $wpdb->num_queries);
3392
        $this->assertEqualSets(array(), $q->comments);
3393
    }
3394
3395 View Code Duplication
    public function test_unspammed_comment_should_invalidate_query_cache() 
3396
    {
3397
        global $wpdb;
3398
3399
        $c = self::factory()->comment->create(
3400
            array(
3401
            'comment_post_ID' => self::$post_id,
3402
            'comment_approved' => '1',
3403
            ) 
3404
        );
3405
3406
        wp_spam_comment($c);
3407
3408
        $q = new WP_Comment_Query(
3409
            array(
3410
            'post_id' => self::$post_id,
3411
            'fields' => 'ids',
3412
            ) 
3413
        );
3414
3415
        wp_unspam_comment($c);
3416
3417
        $num_queries = $wpdb->num_queries;
3418
3419
        $q = new WP_Comment_Query(
3420
            array(
3421
            'post_id' => self::$post_id,
3422
            'fields' => 'ids',
3423
            ) 
3424
        );
3425
3426
        $num_queries++;
3427
        $this->assertSame($num_queries, $wpdb->num_queries);
3428
        $this->assertEqualSets(array( $c ), $q->comments);
3429
    }
3430
}
3431