forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtermCounts.php
More file actions
611 lines (552 loc) · 18.9 KB
/
termCounts.php
File metadata and controls
611 lines (552 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<?php
/**
* @group taxonomy
*/
class Tests_Term_termCount extends WP_UnitTestCase {
/**
* Term ID for testing attachment counts.
*
* @var int
*/
public static $attachment_term;
/**
* Post IDs of shared posts.
*
* @var int[]
*/
public static $post_ids;
/**
* Array of tag IDs.
*
* @var int[]
*/
public static $tag_ids;
/**
* Term ID for testing user counts.
*
* @var int
*/
public static $user_term;
/**
* User ID for testing user counts.
*
* @var int
*/
public static $user_id;
/**
* Create shared fixtures.
*
* @param WP_UnitTest_Factory $factory Test suite factory.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
$statuses = array( 'publish', 'auto-draft', 'draft', 'private' );
foreach ( $statuses as $status ) {
self::$post_ids[ $status ] = $factory->post->create( array( 'post_status' => $status ) );
}
// Extra published post.
self::$post_ids['publish_two'] = $factory->post->create( array( 'post_status' => 'publish' ) );
self::$user_id = $factory->user->create( array( 'role' => 'author' ) );
self::register_taxonomies();
self::$attachment_term = $factory->term->create( array( 'taxonomy' => 'wp_test_tax_counts' ) );
self::$user_term = $factory->term->create( array( 'taxonomy' => 'wp_test_user_tax_counts' ) );
self::$tag_ids = $factory->term->create_many( 5 );
}
public function setUp() {
parent::setUp();
self::register_taxonomies();
}
/**
* Register taxonomies used by tests.
*
* This is called both before class and before each test as the global is
* reset in each test's tearDown.
*/
public static function register_taxonomies() {
register_taxonomy( 'wp_test_tax_counts', array( 'post', 'attachment' ) );
register_taxonomy( 'wp_test_user_tax_counts', 'user' );
}
/**
* Term counts are incremented when post created.
*
* @covers ::wp_update_term_count
* @dataProvider data_term_count_changes_for_post_statuses
*
* @param string $post_status New post status.
* @param int $change Expected change.
*/
public function test_term_count_changes_for_post_statuses( $post_status, $change ) {
$term_count = get_term( get_option( 'default_category' ) )->count;
// Do not use shared fixture for this test as it relies on a new post.
$post_id = $this->factory()->post->create( array( 'post_status' => $post_status ) );
$expected = $term_count + $change;
$this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
}
/**
* Data provider for test_term_count_changes_for_post_statuses.
*
* @return array[] {
* @type string $post_status New post status.
* @type int $change Expected change.
* }
*/
function data_term_count_changes_for_post_statuses() {
return array(
// 0. Published post
array( 'publish', 1 ),
// 1. Auto draft
array( 'auto-draft', 0 ),
// 2. Draft
array( 'draft', 0 ),
// 3. Private post
array( 'private', 0 ),
);
}
/**
* Term counts increments correctly when post status becomes published.
*
* @covers ::wp_publish_post
* @covers ::wp_update_term_count
* @dataProvider data_term_counts_incremented_on_publish
* @ticket 51292
*
* @param string $original_post_status Post status prior to change to publish.
* @param int $change Expected change upon publish.
*/
public function test_term_counts_incremented_on_publish( $original_post_status, $change ) {
$post_id = self::$post_ids[ $original_post_status ];
$term_count = get_term( get_option( 'default_category' ) )->count;
wp_publish_post( $post_id );
$expected = $term_count + $change;
$this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
}
/**
* Data provider for test_term_count_changes_for_post_statuses.
*
* @return array[] {
* @type string $original_post_status Post status prior to change to publish.
* @type int $change Expected change upon publish.
* }
*/
function data_term_counts_incremented_on_publish() {
return array(
// 0. Published post
array( 'publish', 0 ),
// 1. Auto draft
array( 'auto-draft', 1 ),
// 2. Draft
array( 'draft', 1 ),
// 3. Private post
array( 'private', 1 ),
);
}
/**
* Test post status transition update term counts correctly.
*
* @covers ::wp_update_term_count
* @dataProvider data_term_count_transitions_update_term_counts
*
* @param string $original_post_status Post status upon create.
* @param string $new_post_status Post status after update.
* @param int $change Expected change upon publish.
*/
function test_term_count_transitions_update_term_counts( $original_post_status, $new_post_status, $change ) {
$post_id = self::$post_ids[ $original_post_status ];
$term_count = get_term( get_option( 'default_category' ) )->count;
wp_update_post(
array(
'ID' => $post_id,
'post_status' => $new_post_status,
)
);
$expected = $term_count + $change;
$this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
}
/**
* Data provider for test_term_count_transitions_update_term_counts.
*
* @return array[] {
* @type string $original_post_status Post status upon create.
* @type string $new_post_status Post status after update.
* @type int $change Expected change upon publish.
* }
*/
function data_term_count_transitions_update_term_counts() {
return array(
// 0. Draft -> published post
array( 'draft', 'publish', 1 ),
// 1. Auto draft -> published post
array( 'auto-draft', 'publish', 1 ),
// 2. Private -> published post
array( 'private', 'publish', 1 ),
// 3. Published -> published post
array( 'publish', 'publish', 0 ),
// 4. Draft -> private post
array( 'draft', 'private', 0 ),
// 5. Auto draft -> private post
array( 'auto-draft', 'private', 0 ),
// 6. Private -> private post
array( 'private', 'private', 0 ),
// 7. Published -> private post
array( 'publish', 'private', -1 ),
// 8. Draft -> draft post
array( 'draft', 'draft', 0 ),
// 9. Auto draft -> draft post
array( 'auto-draft', 'draft', 0 ),
// 10. Private -> draft post
array( 'private', 'draft', 0 ),
// 11. Published -> draft post
array( 'publish', 'draft', -1 ),
);
}
function add_custom_status_to_counted_statuses( $statuses ) {
array_push( $statuses, 'custom' );
return $statuses;
}
/**
* Term counts incremented correctly when the `update_post_term_count_statuses` filter is used.
*
* @covers ::wp_update_term_count
* @dataProvider data_term_count_changes_for_update_post_term_count_statuses_filter
* @ticket 38843
*
* @param string $post_status New post status.
* @param int $change Expected change.
*/
public function test_term_count_changes_for_update_post_term_count_statuses_filter( $post_status, $change ) {
$term_count = get_term( self::$attachment_term )->count;
add_filter( 'update_post_term_count_statuses', array( $this, 'add_custom_status_to_counted_statuses' ) );
$post_id = $this->factory()->post->create( array( 'post_status' => $post_status ) );
wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
$attachment_id = self::factory()->attachment->create_object(
array(
'file' => 'image.jpg',
'post_parent' => $post_id,
'post_status' => 'inherit',
)
);
wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
$expected = $term_count + $change;
$this->assertSame( $expected, get_term( self::$attachment_term )->count );
remove_filter( 'update_post_term_count_statuses', array( $this, 'add_custom_status_to_counted_statuses' ) );
}
/**
* Data provider for test_term_count_changes_for_update_post_term_count_statuses_filter.
*
* @return array[] {
* @type string $post_status New post status.
* @type int $change Expected change.
* }
*/
function data_term_count_changes_for_update_post_term_count_statuses_filter() {
return array(
// 0. Published post
array( 'publish', 2 ),
// 1. Auto draft
array( 'auto-draft', 0 ),
// 2. Draft
array( 'draft', 0 ),
// 3. Private post
array( 'private', 0 ),
// 4. Custom post status
array( 'custom', 2 ),
);
}
/**
* Term counts incremented correctly for posts with attachment.
*
* @covers ::wp_update_term_count
* @dataProvider data_term_count_changes_for_post_statuses_with_attachments
*
* @param string $post_status New post status.
* @param int $change Expected change.
*/
public function test_term_count_changes_for_post_statuses_with_attachments( $post_status, $change ) {
$term_count = get_term( self::$attachment_term )->count;
// Do not use shared fixture for this test as it relies on a new post.
$post_id = $this->factory()->post->create( array( 'post_status' => $post_status ) );
wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
$attachment_id = self::factory()->attachment->create_object(
array(
'file' => 'image.jpg',
'post_parent' => $post_id,
'post_status' => 'inherit',
)
);
wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
$expected = $term_count + $change;
$this->assertSame( $expected, get_term( self::$attachment_term )->count );
}
/**
* Data provider for test_term_count_changes_for_post_statuses_with_attachments.
*
* @return array[] {
* @type string $post_status New post status.
* @type int $change Expected change.
* }
*/
function data_term_count_changes_for_post_statuses_with_attachments() {
return array(
// 0. Published post
array( 'publish', 2 ),
// 1. Auto draft
array( 'auto-draft', 0 ),
// 2. Draft
array( 'draft', 0 ),
// 3. Private post
array( 'private', 0 ),
);
}
/**
* Term counts increments correctly when post with attachment becomes published.
*
* @covers ::wp_publish_post
* @covers ::wp_update_term_count
* @dataProvider data_term_counts_incremented_on_publish_with_attachments
* @ticket 51292
*
* @param string $original_post_status Post status prior to change to publish.
* @param int $change Expected change upon publish.
*/
public function test_term_counts_incremented_on_publish_with_attachments( $original_post_status, $change ) {
$post_id = self::$post_ids[ $original_post_status ];
wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
$attachment_id = self::factory()->attachment->create_object(
array(
'file' => 'image.jpg',
'post_parent' => $post_id,
'post_status' => 'inherit',
)
);
wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
$term_count = get_term( self::$attachment_term )->count;
wp_publish_post( $post_id );
$expected = $term_count + $change;
$this->assertSame( $expected, get_term( self::$attachment_term )->count );
}
/**
* Data provider for test_term_count_changes_for_post_statuses_with_attachments.
*
* @return array[] {
* @type string $original_post_status Post status prior to change to publish.
* @type int $change Expected change upon publish.
* }
*/
function data_term_counts_incremented_on_publish_with_attachments() {
return array(
// 0. Published post
array( 'publish', 0 ),
// 1. Auto draft
array( 'auto-draft', 2 ),
// 2. Draft
array( 'draft', 2 ),
// 3. Private post
array( 'private', 2 ),
);
}
/**
* Test post status transition update term counts correctly for posts with attachments.
*
* @covers ::wp_update_term_count
* @dataProvider data_term_count_transitions_update_term_counts_with_attachments
*
* @param string $original_post_status Post status upon create.
* @param string $new_post_status Post status after update.
* @param int $change Expected change upon publish.
*/
function test_term_count_transitions_update_term_counts_with_attachments( $original_post_status, $new_post_status, $change ) {
$post_id = self::$post_ids[ $original_post_status ];
wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
$attachment_id = self::factory()->attachment->create_object(
array(
'file' => 'image.jpg',
'post_parent' => $post_id,
'post_status' => 'inherit',
)
);
wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
$term_count = get_term( self::$attachment_term )->count;
wp_update_post(
array(
'ID' => $post_id,
'post_status' => $new_post_status,
)
);
$expected = $term_count + $change;
$this->assertSame( $expected, get_term( self::$attachment_term )->count );
}
/**
* Data provider for test_term_count_transitions_update_term_counts_with_attachments.
*
* @return array[] {
* @type string $original_post_status Post status upon create.
* @type string $new_post_status Post status after update.
* @type int $change Expected change upon publish.
* }
*/
function data_term_count_transitions_update_term_counts_with_attachments() {
return array(
// 0. Draft -> published post
array( 'draft', 'publish', 2 ),
// 1. Auto draft -> published post
array( 'auto-draft', 'publish', 2 ),
// 2. Private -> published post
array( 'private', 'publish', 2 ),
// 3. Published -> published post
array( 'publish', 'publish', 0 ),
// 4. Draft -> private post
array( 'draft', 'private', 0 ),
// 5. Auto draft -> private post
array( 'auto-draft', 'private', 0 ),
// 6. Private -> private post
array( 'private', 'private', 0 ),
// 7. Published -> private post
array( 'publish', 'private', -2 ),
// 8. Draft -> draft post
array( 'draft', 'draft', 0 ),
// 9. Auto draft -> draft post
array( 'auto-draft', 'draft', 0 ),
// 10. Private -> draft post
array( 'private', 'draft', 0 ),
// 11. Published -> draft post
array( 'publish', 'draft', -2 ),
);
}
/**
* Term counts increments correctly when post with attachment becomes published.
*
* @covers ::wp_update_term_count
* @covers ::wp_publish_post
* @dataProvider data_term_counts_incremented_on_publish_with_untermed_attachments
* @ticket 51292
*
* @param string $original_post_status Post status prior to change to publish.
* @param int $change Expected change upon publish.
*/
public function test_term_counts_incremented_on_publish_with_untermed_attachments( $original_post_status, $change ) {
$post_id = self::$post_ids[ $original_post_status ];
wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
$attachment_id = self::factory()->attachment->create_object(
array(
'file' => 'image.jpg',
'post_parent' => $post_id,
'post_status' => 'inherit',
)
);
$term_count = get_term( self::$attachment_term )->count;
wp_publish_post( $post_id );
$expected = $term_count + $change;
$this->assertSame( $expected, get_term( self::$attachment_term )->count );
}
/**
* Data provider for test_term_count_changes_for_post_statuses_with_untermed_attachments.
*
* @return array[] {
* @type string $original_post_status Post status prior to change to publish.
* @type int $change Expected change upon publish.
* }
*/
function data_term_counts_incremented_on_publish_with_untermed_attachments() {
return array(
// 0. Published post
array( 'publish', 0 ),
// 1. Auto draft
array( 'auto-draft', 1 ),
// 2. Draft
array( 'draft', 1 ),
// 3. Private post
array( 'private', 1 ),
);
}
/**
* Test post status transition update term counts correctly on post with attachment.
*
* @covers ::wp_update_term_count
* @dataProvider data_term_count_transitions_update_term_counts_with_untermed_attachments
*
* @param string $original_post_status Post status upon create.
* @param string $new_post_status Post status after update.
* @param int $change Expected change upon publish.
*/
function test_term_count_transitions_update_term_counts_with_untermed_attachments( $original_post_status, $new_post_status, $change ) {
$post_id = self::$post_ids[ $original_post_status ];
wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
$attachment_id = self::factory()->attachment->create_object(
array(
'file' => 'image.jpg',
'post_parent' => $post_id,
'post_status' => 'inherit',
)
);
$term_count = get_term( self::$attachment_term )->count;
wp_update_post(
array(
'ID' => $post_id,
'post_status' => $new_post_status,
)
);
$expected = $term_count + $change;
$this->assertSame( $expected, get_term( self::$attachment_term )->count );
}
/**
* Data provider for test_term_count_transitions_update_term_counts_with_untermed_attachments.
*
* @return array[] {
* @type string $original_post_status Post status upon create.
* @type string $new_post_status Post status after update.
* @type int $change Expected change upon publish.
* }
*/
function data_term_count_transitions_update_term_counts_with_untermed_attachments() {
return array(
// 0. Draft -> published post
array( 'draft', 'publish', 1 ),
// 1. Auto draft -> published post
array( 'auto-draft', 'publish', 1 ),
// 2. Private -> published post
array( 'private', 'publish', 1 ),
// 3. Published -> published post
array( 'publish', 'publish', 0 ),
// 4. Draft -> private post
array( 'draft', 'private', 0 ),
// 5. Auto draft -> private post
array( 'auto-draft', 'private', 0 ),
// 6. Private -> private post
array( 'private', 'private', 0 ),
// 7. Published -> private post
array( 'publish', 'private', -1 ),
// 8. Draft -> draft post
array( 'draft', 'draft', 0 ),
// 9. Auto draft -> draft post
array( 'auto-draft', 'draft', 0 ),
// 10. Private -> draft post
array( 'private', 'draft', 0 ),
// 11. Published -> draft post
array( 'publish', 'draft', -1 ),
);
}
/**
* User taxonomy term counts increments when added to an account.
*
* @covers ::wp_update_term_count
* @ticket 51292
*/
public function test_term_counts_user_adding_term() {
$term_count = get_term( self::$user_term )->count;
wp_add_object_terms( self::$user_id, self::$user_term, 'wp_test_user_tax_counts' );
$expected = $term_count + 1;
$this->assertSame( $expected, get_term( self::$user_term )->count );
}
/**
* User taxonomy term counts decrement when term deleted from user.
*
* @covers ::wp_update_term_count
* @ticket 51292
*/
public function test_term_counts_user_removing_term() {
wp_add_object_terms( self::$user_id, self::$user_term, 'wp_test_user_tax_counts' );
$term_count = get_term( self::$user_term )->count;
wp_remove_object_terms( self::$user_id, self::$user_term, 'wp_test_user_tax_counts' );
$expected = $term_count - 1;
$this->assertSame( $expected, get_term( self::$user_term )->count );
}
}