forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-post.php
1150 lines (998 loc) · 39.5 KB
/
test-timber-post.php
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once 'php/timber-term-subclass.php';
/**
* @group posts-api
* @group users-api
* @group terms-api
* @group post-terms
*/
class TestTimberPost extends Timber_UnitTestCase
{
public function testGetPostWithNoPosts()
{
$this->assertNull(Timber::get_post());
}
public function testPostObject()
{
$post_id = $this->factory->post->create();
$post = Timber::get_post($post_id);
$this->assertEquals('Timber\Post', get_class($post));
$this->assertEquals($post_id, $post->ID);
}
public function testIDDataType()
{
$uid = $this->factory->post->create([
'title' => 'Air Force Once',
]);
$post = Timber::get_post($uid);
$this->assertEquals('integer', gettype($post->id));
$this->assertEquals('integer', gettype($post->ID));
}
public function testPostPasswordReqd()
{
$post_id = $this->factory->post->create();
$post = Timber::get_post($post_id);
$this->assertFalse($post->password_required());
$post_id = $this->factory->post->create([
'post_password' => 'jiggypoof',
]);
$post = Timber::get_post($post_id);
$this->assertTrue($post->password_required());
}
public function testNameMethod()
{
$post_id = $this->factory->post->create([
'post_title' => 'Battlestar Galactica',
]);
$post = Timber::get_post($post_id);
$this->assertEquals('Battlestar Galactica', $post->name());
}
public function testGetImageViaPostMeta()
{
$post_id = $this->factory->post->create([
'post_title' => 'St. Louis History',
]);
$filename = TestTimberImage::copyTestAttachment('arch.jpg');
$attachment = [
'post_title' => 'The Arch',
'post_content' => '',
];
$iid = wp_insert_attachment($attachment, $filename, $post_id);
update_post_meta($post_id, 'landmark', $iid);
$post = Timber::get_post($post_id);
$image = Timber::get_post($post->meta('landmark'));
$this->assertEquals('The Arch', $image->title());
}
public function testPostString()
{
$post_id = $this->factory->post->create([
'post_title' => 'Gobbles',
]);
$post = Timber::get_post($post_id);
$str = Timber::compile_string('<h1>{{post}}</h1>', [
'post' => $post,
]);
$this->assertEquals('<h1>Gobbles</h1>', $str);
}
public function testFalseParent()
{
$pid = $this->factory->post->create();
$filename = TestTimberImage::copyTestAttachment('arch.jpg');
$attachment = [
'post_title' => 'The Arch',
'post_content' => '',
];
$iid = wp_insert_attachment($attachment, $filename, $pid);
update_post_meta($iid, 'architect', 'Eero Saarinen');
$image = Timber::get_post($iid);
$parent = $image->parent();
$this->assertEquals($pid, $parent->ID);
$this->assertFalse($parent->parent());
}
public function testPostOnSingle()
{
$post_id = $this->factory->post->create();
$this->go_to(home_url('/?p=' . $post_id));
$post = Timber::get_post();
$this->assertEquals($post_id, $post->ID);
}
public function testPostOnSingleQuery()
{
$this->factory->post->create();
$post_id = $this->factory->post->create();
$this->go_to(home_url('/?p=' . $post_id));
$post = Timber::get_post($post_id);
$this->assertEquals($post_id, $post->ID);
}
public function testPostOnSingleQueryNoParams()
{
$post_id = $this->factory->post->create();
$this->go_to(home_url('/?p=' . $post_id));
$post = Timber::get_post();
$this->assertEquals($post_id, $post->ID);
$this->assertEquals($post_id, get_the_ID());
}
public function testNonexistentProperty()
{
$post_id = $this->factory->post->create();
$post = Timber::get_post($post_id);
$this->assertFalse($post->zebra);
}
public function testNonexistentMethod()
{
$post_id = $this->factory->post->create();
$post = Timber::get_post($post_id);
$template = '{{post.donkey}}';
$str = Timber::compile_string($template, [
'post' => $post,
]);
$this->assertSame('', $str);
}
public function testNext()
{
$posts = [];
for ($i = 0; $i < 2; $i++) {
$j = $i + 1;
$posts[] = $this->factory->post->create([
'post_date' => '2014-02-0' . $j . ' 12:00:00',
]);
}
$firstPost = Timber::get_post($posts[0]);
$nextPost = Timber::get_post($posts[1]);
$this->assertEquals($firstPost->next()->ID, $nextPost->ID);
}
public function testNextCategory()
{
$posts = [];
for ($i = 0; $i < 4; $i++) {
$j = $i + 1;
$posts[] = $this->factory->post->create([
'post_date' => '2014-02-0' . $j . ' 12:00:00',
]);
}
wp_set_object_terms($posts[0], 'TestMe', 'category', false);
wp_set_object_terms($posts[2], 'TestMe', 'category', false);
$firstPost = Timber::get_post($posts[0]);
$nextPost = Timber::get_post($posts[2]);
$this->assertEquals($firstPost->next('category')->ID, $nextPost->ID);
}
public function testNextCustomTax()
{
register_taxonomy('pizza', 'post');
$posts = [];
for ($i = 0; $i < 4; $i++) {
$j = $i + 1;
$posts[] = $this->factory->post->create([
'post_date' => '2014-02-0' . $j . ' 12:00:00',
]);
}
wp_set_object_terms($posts[0], 'Cheese', 'pizza', false);
wp_set_object_terms($posts[2], 'Cheese', 'pizza', false);
wp_set_object_terms($posts[3], 'Mushroom', 'pizza', false);
$firstPost = Timber::get_post($posts[0]);
$nextPost = Timber::get_post($posts[2]);
$this->assertEquals($firstPost->next('pizza')->ID, $nextPost->ID);
}
public function testPrev()
{
$posts = [];
for ($i = 0; $i < 2; $i++) {
$j = $i + 1;
$posts[] = $this->factory->post->create([
'post_date' => '2014-02-0' . $j . ' 12:00:00',
]);
}
$lastPost = Timber::get_post($posts[1]);
$prevPost = Timber::get_post($posts[0]);
$this->assertEquals($lastPost->prev()->ID, $prevPost->ID);
}
public function testPrevCustomTax()
{
register_taxonomy('pizza', 'post');
$posts = [];
for ($i = 0; $i < 3; $i++) {
$j = $i + 1;
$posts[] = $this->factory->post->create([
'post_date' => '2014-02-0' . $j . ' 12:00:00',
'post_title' => "Pizza $j is so good!",
]);
}
$cat = wp_insert_term('Cheese', 'pizza');
self::set_object_terms($posts[0], $cat, 'pizza', false);
self::set_object_terms($posts[2], $cat, 'pizza', false);
$lastPost = Timber::get_post($posts[2]);
$this->assertEquals($posts[0], $lastPost->prev('pizza')->ID);
}
public function testPrevCategory()
{
$posts = [];
for ($i = 0; $i < 3; $i++) {
$j = $i + 1;
$posts[] = $this->factory->post->create([
'post_date' => '2014-02-0' . $j . ' 12:00:00',
]);
}
$cat = wp_insert_term('TestMe', 'category');
self::set_object_terms($posts[0], $cat, 'category', false);
self::set_object_terms($posts[2], $cat, 'category', false);
$lastPost = Timber::get_post($posts[2]);
$prevPost = Timber::get_post($posts[0]);
$this->assertEquals($lastPost->prev('category')->ID, $prevPost->ID);
}
public function testNextWithDraftAndFallover()
{
$posts = [];
for ($i = 0; $i < 3; $i++) {
$j = $i + 1;
$posts[] = $this->factory->post->create([
'post_date' => '2014-02-0' . $j . ' 12:00:00',
]);
}
$firstPost = Timber::get_post($posts[0]);
$nextPost = Timber::get_post($posts[1]);
$nextPostAfter = Timber::get_post($posts[2]);
wp_update_post([
'ID' => $nextPost->ID,
'post_status' => 'draft',
]);
$this->assertEquals($nextPostAfter->ID, $firstPost->next()->ID);
}
public function testNextWithDraft()
{
$posts = [];
for ($i = 0; $i < 2; $i++) {
$j = $i + 1;
$posts[] = $this->factory->post->create([
'post_date' => '2014-02-0' . $j . ' 12:00:00',
]);
}
$firstPost = Timber::get_post($posts[0]);
$nextPost = Timber::get_post($posts[1]);
$nextPost->post_status = 'draft';
wp_update_post($nextPost);
$nextPostTest = $firstPost->next();
// because $nextPost has a status of "draft" now (and thus isn't public)
// it should not be retured when we call $firstPost->next();
$this->assertFalse($nextPostTest);
}
public function testPostInitObject()
{
$post_id = $this->factory->post->create();
$post = get_post($post_id);
$post = Timber::get_post($post);
$this->assertEquals($post->ID, $post_id);
}
public function testPostBySlug()
{
$post_id = $this->factory->post->create();
$slug = Timber::get_post($post_id)->post_name;
$postFromSlug = Timber::get_post_by('slug', $slug);
$this->assertEquals($postFromSlug->id, $post_id);
}
public function testCanEdit()
{
$subscriber_id = $this->factory->user->create([
'display_name' => 'Subscriber Sam',
'user_login' => 'subsam',
'role' => 'subscriber',
]);
// Test admin role.
wp_set_current_user(1);
$post_id = $this->factory->post->create([
'post_author' => 1,
]);
$post = Timber::get_post($post_id);
$this->assertTrue($post->can_edit());
// Test subscriber role.
wp_set_current_user($subscriber_id);
$this->assertFalse($post->can_edit());
wp_set_current_user(0);
}
public function testTitle()
{
$title = 'Fifteen Million Merits';
$post_id = $this->factory->post->create();
$post = Timber::get_post($post_id);
$post->post_title = $title;
wp_update_post($post);
$this->assertEquals($title, trim(strip_tags($post->title())));
}
public function testCustomFieldExcerptRevision()
{
global $current_user;
global $wp_query;
$post_id = $this->factory->post->create([
'post_author' => 5,
]);
update_field('test_field', 'The custom field content', $post_id);
$assertCustomFieldVal = 'This has been revised';
$revision_id = $this->factory->post->create([
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => $post_id,
]);
update_field('test_field', $assertCustomFieldVal, $revision_id);
$uid = $this->factory->user->create([
'user_login' => 'timber',
'user_pass' => 'timber',
]);
$user = wp_set_current_user($uid);
$user->add_role('administrator');
$wp_query->queried_object_id = $post_id;
$wp_query->queried_object = get_post($post_id);
$_GET['preview'] = true;
$_GET['preview_nonce'] = wp_create_nonce('post_preview_' . $post_id);
$post = Timber::get_post($post_id);
$str_direct = Timber::compile_string('{{post.test_field}}', [
'post' => $post,
]);
$str_getfield = Timber::compile_string('{{post.meta(\'test_field\')}}', [
'post' => $post,
]);
$this->assertEquals($assertCustomFieldVal, $str_direct);
$this->assertEquals($assertCustomFieldVal, $str_getfield);
}
public function testCustomFieldExcerptNotRevision()
{
global $current_user;
global $wp_query;
$original_content = 'The custom field content';
$post_id = $this->factory->post->create([
'post_author' => 5,
]);
update_field('test_field', $original_content, $post_id);
$assertCustomFieldVal = 'This has been revised';
$revision_id = $this->factory->post->create([
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => $post_id,
]);
update_field('test_field', $assertCustomFieldVal, $revision_id);
$uid = $this->factory->user->create([
'user_login' => 'timber',
'user_pass' => 'timber',
]);
$user = wp_set_current_user($uid);
$user->add_role('administrator');
$wp_query->queried_object_id = $post_id;
$wp_query->queried_object = get_post($post_id);
$post = Timber::get_post($post_id);
$str_direct = Timber::compile_string('{{post.test_field}}', [
'post' => $post,
]);
$str_getfield = Timber::compile_string('{{post.meta(\'test_field\')}}', [
'post' => $post,
]);
$this->assertEquals($original_content, $str_direct);
$this->assertEquals($original_content, $str_getfield);
}
public function testContent()
{
$quote = 'The way to do well is to do well.';
$post_id = $this->factory->post->create();
$post = Timber::get_post($post_id);
$post->post_content = $quote;
wp_update_post($post);
$this->assertEquals($quote, trim(strip_tags($post->content())));
}
public function testContentPaged()
{
$quote = $page1 = 'The way to do well is to do well.';
$quote .= '<!--nextpage-->';
$quote .= $page2 = "And do not let your tongue get ahead of your mind.";
$post_id = $this->factory->post->create();
$post = Timber::get_post($post_id);
$post->post_content = $quote;
wp_update_post($post);
$this->assertEquals($page1, trim(strip_tags($post->content(1))));
$this->assertEquals($page2, trim(strip_tags($post->content(2))));
}
public function testPagedContent()
{
$quote = $page1 = 'Named must your fear be before banish it you can.';
$quote .= '<!--nextpage-->';
$quote .= $page2 = "No, try not. Do or do not. There is no try.";
$post_id = $this->factory->post->create([
'post_content' => $quote,
]);
$this->go_to(get_permalink($post_id));
setup_postdata(get_post($post_id));
$post = Timber::get_post();
$this->assertEquals($page1, trim(strip_tags($post->paged_content())));
$pagination = $post->pagination();
$this->go_to($pagination['pages'][1]['link']);
setup_postdata(get_post($post_id));
$post = Timber::get_post();
$this->assertEquals($page2, trim(strip_tags($post->paged_content())));
}
public function testPostParent()
{
$parent_id = $this->factory->post->create();
$child_id = $this->factory->post->create([
'post_parent' => $parent_id,
]);
$child_post = Timber::get_post($child_id);
$this->assertEquals($parent_id, $child_post->parent()->ID);
}
public function testPostSlug()
{
$pid = $this->factory->post->create([
'post_name' => 'the-adventures-of-tom-sawyer',
]);
$post = Timber::get_post($pid);
$this->assertEquals('the-adventures-of-tom-sawyer', $post->slug);
}
public function testPostAuthor()
{
$author_id = $this->factory->user->create([
'display_name' => 'Jared Novack',
'user_login' => 'jared-novack',
]);
$pid = $this->factory->post->create([
'post_author' => $author_id,
]);
$post = Timber::get_post($pid);
$this->assertEquals('jared-novack', $post->author()->slug());
$this->assertEquals('Jared Novack', $post->author()->name());
$template = 'By {{post.author}}';
$authorCompile = Timber::compile_string($template, [
'post' => $post,
]);
$template = 'By {{post.author.name}}';
$authorNameCompile = Timber::compile_string($template, [
'post' => $post,
]);
$this->assertEquals($authorCompile, $authorNameCompile);
$this->assertEquals('By Jared Novack', $authorCompile);
}
public function testPostAuthorInTwig()
{
$author_id = $this->factory->user->create([
'display_name' => 'Jon Stewart',
'user_login' => 'jon-stewart',
]);
$pid = $this->factory->post->create([
'post_author' => $author_id,
]);
$post = Timber::get_post($pid);
$this->assertEquals('jon-stewart', $post->author()->slug());
$this->assertEquals('Jon Stewart', $post->author()->name());
$template = 'By {{post.author}}';
$authorCompile = Timber::compile_string($template, [
'post' => $post,
]);
$template = 'By {{post.author.name}}';
$authorNameCompile = Timber::compile_string($template, [
'post' => $post,
]);
$this->assertEquals($authorCompile, $authorNameCompile);
$this->assertEquals('By Jon Stewart', $authorCompile);
}
public function testPostModifiedAuthor()
{
$author_id = $this->factory->user->create([
'display_name' => 'Woodward',
'user_login' => 'bob-woodward',
]);
$mod_author_id = $this->factory->user->create([
'display_name' => 'Bernstein',
'user_login' => 'carl-bernstein',
]);
$pid = $this->factory->post->create([
'post_author' => $author_id,
]);
$post = Timber::get_post($pid);
$this->assertEquals('bob-woodward', $post->author()->slug());
$this->assertEquals('bob-woodward', $post->modified_author()->slug());
$this->assertEquals('Woodward', $post->author()->name());
$this->assertEquals('Woodward', $post->modified_author()->name());
update_post_meta($pid, '_edit_last', $mod_author_id);
$this->assertEquals('bob-woodward', $post->author()->slug());
$this->assertEquals('carl-bernstein', $post->modified_author()->slug());
$this->assertEquals('Woodward', $post->author()->name());
$this->assertEquals('Bernstein', $post->modified_author()->name());
}
public function tear_down()
{
global $wpdb;
$query = "DELETE from $wpdb->users WHERE ID > 1";
$wpdb->query($query);
$this->truncate('term_relationships');
$this->truncate('term_taxonomy');
$this->truncate('terms');
$this->truncate('termmeta');
$this->truncate('posts');
}
public function testPostFormat()
{
add_theme_support('post-formats', ['aside', 'gallery']);
$pid = $this->factory->post->create();
set_post_format($pid, 'aside');
$post = Timber::get_post($pid);
$this->assertEquals('aside', $post->format());
}
public function testPostClassInTwig()
{
$pid = $this->factory->post->create();
$category = wp_insert_term('Uncategorized', 'category');
self::set_object_terms($pid, $category, 'category', true);
$post = Timber::get_post($pid);
$str = Timber::compile_string("{{ post.class }}", [
'post' => $post,
]);
$this->assertEquals('post-' . $pid . ' post type-post status-publish format-standard hentry category-uncategorized', $str);
}
public function testPostClass()
{
$pid = $this->factory->post->create();
$category = wp_insert_term('Uncategorized', 'category');
self::set_object_terms($pid, $category, 'category', true);
$post = Timber::get_post($pid);
$this->assertEquals('post-' . $pid . ' post type-post status-publish format-standard hentry category-uncategorized', $post->post_class());
}
public function testCssClass()
{
$pid = $this->factory->post->create();
$category = wp_insert_term('Uncategorized', 'category');
self::set_object_terms($pid, $category, 'category', true);
$post = Timber::get_post($pid);
$this->assertEquals('post-' . $pid . ' post type-post status-publish format-standard hentry category-uncategorized', $post->css_class());
$this->assertEquals('post-' . $pid . ' post type-post status-publish format-standard hentry category-uncategorized additional-css-class', $post->css_class('additional-css-class'));
}
public function testCssClassMagicCall()
{
$pid = $this->factory->post->create();
$category = wp_insert_term('Uncategorized', 'category');
self::set_object_terms($pid, $category, 'category', true);
$post = Timber::get_post($pid);
$this->assertEquals('post-' . $pid . ' post type-post status-publish format-standard hentry category-uncategorized', $post->class());
$this->assertEquals('post-' . $pid . ' post type-post status-publish format-standard hentry category-uncategorized additional-css-class', $post->class('additional-css-class'));
}
public function testCssClassMagicGet()
{
$pid = $this->factory->post->create();
$category = wp_insert_term('Uncategorized', 'category');
self::set_object_terms($pid, $category, 'category', true);
$post = Timber::get_post($pid);
$this->assertEquals('post-' . $pid . ' post type-post status-publish format-standard hentry category-uncategorized', $post->class);
}
public function testPostChildren()
{
$parent_id = $this->factory->post->create();
$children = $this->factory->post->create_many(8, [
'post_parent' => $parent_id,
]);
$parent = Timber::get_post($parent_id);
$this->assertSame(8, count($parent->children()));
}
public function testPostChildrenOfInheritStatus()
{
$parent_id = $this->factory->post->create();
$children = $this->factory->post->create_many(4, [
'post_parent' => $parent_id,
]);
$children = $this->factory->post->create_many(4, [
'post_parent' => $parent_id,
'post_status' => 'inherit',
]);
$parent = Timber::get_post($parent_id);
$this->assertSame(8, count($parent->children()));
}
public function testPostChildrenOfParentType()
{
$parent_id = $this->factory->post->create([
'post_type' => 'foo',
]);
$children = $this->factory->post->create_many(8, [
'post_parent' => $parent_id,
]);
$children = $this->factory->post->create_many(4, [
'post_parent' => $parent_id,
'post_type' => 'foo',
]);
$parent = Timber::get_post($parent_id);
$this->assertSame(4, count($parent->children('parent')));
}
public function testPostChildrenWithArray()
{
$parent_id = $this->factory->post->create([
'post_type' => 'foo',
]);
$children = $this->factory->post->create_many(8, [
'post_parent' => $parent_id,
'post_type' => 'bar',
]);
$children = $this->factory->post->create_many(4, [
'post_parent' => $parent_id,
'post_type' => 'foo',
]);
$parent = Timber::get_post($parent_id);
$this->assertSame(12, count($parent->children(['foo', 'bar'])));
}
public function testPostNoConstructorArgument()
{
$pid = $this->factory->post->create();
$this->go_to('?p=' . $pid);
$post = Timber::get_post();
$this->assertEquals($pid, $post->ID);
}
public function testPostPathUglyPermalinks()
{
update_option('permalink_structure', '');
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$this->assertEquals('http://example.org/?p=' . $pid, $post->link());
$this->assertEquals('/?p=' . $pid, $post->path());
}
public function testPostPathPrettyPermalinks()
{
$struc = '/blog/%year%/%monthnum%/%postname%/';
update_option('permalink_structure', $struc);
$pid = $this->factory->post->create([
'post_date' => '2014-05-28',
]);
$post = Timber::get_post($pid);
$this->assertStringStartsWith('http://example.org/blog/2014/05/post-title', $post->link());
$this->assertStringStartsWith('/blog/2014/05/post-title', $post->path());
}
public function testPostCategory()
{
$cat = wp_insert_term('News', 'category');
$pid = $this->factory->post->create();
self::set_object_terms($pid, $cat, 'category');
$post = Timber::get_post($pid);
$this->assertEquals('News', $post->category()->name);
}
public function testPostCategories()
{
$pid = $this->factory->post->create();
$cat = wp_insert_term('Uncategorized', 'category');
self::set_object_terms($pid, $cat, 'category');
$post = Timber::get_post($pid);
$category_names = ['News', 'Sports', 'Obits'];
// Uncategorized is applied by default
$default_categories = $post->categories();
$this->assertEquals('uncategorized', $default_categories[0]->slug);
foreach ($category_names as $category_name) {
$category_name = wp_insert_term($category_name, 'category');
self::set_object_terms($pid, $category_name, 'category');
}
$this->assertEquals(count($default_categories) + count($category_names), count($post->categories()));
}
public function testPostTags()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$tag_names = ['News', 'Sports', 'Obits'];
foreach ($tag_names as $tag_name) {
$tag = wp_insert_term($tag_name, 'post_tag');
wp_set_object_terms($pid, $tag['term_id'], 'post_tag', true);
}
$this->assertEquals(count($tag_names), count($post->tags()));
}
public function testPostTerms()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$category = wp_insert_term('Uncategorized', 'category');
self::set_object_terms($pid, $category, 'category');
// create a new tag and associate it with the post
$dummy_tag = wp_insert_term('whatever', 'post_tag');
self::set_object_terms($pid, $dummy_tag, 'post_tag');
// test expected tags
$timber_tags = $post->terms('post_tag');
$dummy_timber_tag = Timber::get_term($dummy_tag['term_id'], 'post_tag');
$this->assertEquals('whatever', $timber_tags[0]->slug);
$this->assertEquals($dummy_timber_tag, $timber_tags[0]);
// register a custom taxonomy, create some terms in it and associate to post
register_taxonomy('team', 'post');
$team_names = ['Patriots', 'Bills', 'Dolphins', 'Jets'];
foreach ($team_names as $team_name) {
$team_term = wp_insert_term($team_name, 'team');
self::set_object_terms($pid, $team_term, 'team');
}
$this->assertEquals(count($team_names), count($post->terms('team')));
// check presence of specific terms
$this->assertTrue($post->has_term('Uncategorized'));
$this->assertTrue($post->has_term('whatever'));
$this->assertTrue($post->has_term('Dolphins'));
$this->assertTrue($post->has_term('Patriots', 'team'));
// 4 teams + 1 tag + default category (Uncategorized)
$this->assertSame(6, count($post->terms()));
// test tags method - wrapper for $this->get_terms('tags')
$this->assertEquals($post->tags(), $post->terms('post_tag'));
// test categories method - wrapper for $this->get_terms('category')
$this->assertEquals($post->categories(), $post->terms('category'));
// test using an array of taxonomies
$post_tag_terms = $post->terms(['post_tag']);
$this->assertSame(1, count($post_tag_terms));
$post_team_terms = $post->terms(['team']);
$this->assertEquals(count($team_names), count($post_team_terms));
// test multiple taxonomies
$post_tag_and_team_terms = $post->terms(['post_tag', 'team']);
$this->assertEquals(count($post_tag_terms) + count($post_team_terms), count($post_tag_and_team_terms));
}
public function testPostTermsArgumentStyle()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$category = wp_insert_term('Uncategorized', 'category');
self::set_object_terms($pid, $category, 'category');
// create a new tag and associate it with the post
$dummy_tag = wp_insert_term('whatever', 'post_tag');
self::set_object_terms($pid, $dummy_tag, 'post_tag');
// test expected tags
$timber_tags = $post->terms([
'query' => [
'taxonomy' => 'post_tag',
],
]);
$dummy_timber_tag = Timber::get_term($dummy_tag['term_id'], 'post_tag');
$this->assertEquals('whatever', $timber_tags[0]->slug);
$this->assertEquals($dummy_timber_tag, $timber_tags[0]);
// register a custom taxonomy, create some terms in it and associate to post
register_taxonomy('team', 'post');
$team_names = ['Patriots', 'Bills', 'Dolphins', 'Jets'];
foreach ($team_names as $team_name) {
$team_term = wp_insert_term($team_name, 'team');
self::set_object_terms($pid, $team_term, 'team');
}
$this->assertEquals(count($team_names), count($post->terms([
'query' => [
'taxonomy' => 'team',
],
])));
// test tags method - wrapper for $this->get_terms('tags')
$this->assertEquals($post->tags(), $post->terms([
'query' => [
'taxonomy' => 'post_tag',
],
]));
// test categories method - wrapper for $this->get_terms('category')
$this->assertEquals($post->categories(), $post->terms([
'query' => [
'taxonomy' => 'category',
],
]));
// test using an array of taxonomies
$post_tag_terms = $post->terms([
'query' => [
'taxonomy' => ['post_tag'],
],
]);
$this->assertSame(1, count($post_tag_terms));
$post_team_terms = $post->terms([
'query' => [
'taxonomy' => ['team'],
],
]);
$this->assertEquals(count($team_names), count($post_team_terms));
// test multiple taxonomies
$post_tag_and_team_terms = $post->terms([
'query' => [
'taxonomy' => ['post_tag', 'team'],
],
]);
$this->assertEquals(count($post_tag_terms) + count($post_team_terms), count($post_tag_and_team_terms));
}
public function testPostTermsMerge()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
// register a custom taxonomy, create some terms in it and associate to post
register_taxonomy('team', 'post');
$team_names = ['Patriots', 'Bills', 'Dolphins', 'Jets'];
foreach ($team_names as $team_name) {
$team_term = wp_insert_term($team_name, 'team');
self::set_object_terms($pid, $team_term, 'team');
}
register_taxonomy('book', 'post');
$book_names = ['Fall of Giants', 'Winter of the World', 'Edge of Eternity'];
foreach ($book_names as $book_name) {
$book_term = wp_insert_term($book_name, 'book');
self::set_object_terms($pid, $book_term, 'book');
}
$team_and_book_terms = $post->terms([
'query' => [
'taxonomy' => ['team', 'book'],
],
'merge' => false,
]);
$this->assertSame(4, count($team_and_book_terms['team']));
$this->assertSame(3, count($team_and_book_terms['book']));
}
public function testPostTermQueryArgs()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
// register a custom taxonomy, create some terms in it and associate to post
register_taxonomy('team', 'post');
$team_names = ['Patriots', 'Bills', 'Dolphins', 'Jets'];
foreach ($team_names as $team_name) {
$team_term = wp_insert_term($team_name, 'team');
self::set_object_terms($pid, $team_term, 'team');
}
register_taxonomy('book', 'post');
$book_names = ['Fall of Giants', 'Winter of the World', 'Edge of Eternity'];
foreach ($book_names as $book_name) {
$book_term = wp_insert_term($book_name, 'book');
self::set_object_terms($pid, $book_term, 'book');
}
// Test order.
$team_and_book_terms = $post->terms([
'query' => [
'taxonomy' => ['team', 'book'],
'orderby' => 'name',
],
]);
$this->assertEquals('Bills', $team_and_book_terms[0]->title);
$this->assertEquals('Edge of Eternity', $team_and_book_terms[2]->title);
// Test number of terms
$team_and_book_terms = $post->terms([
'query' => [
'taxonomy' => ['team', 'book'],
'number' => 3,
],
]);
$this->assertCount(3, $team_and_book_terms);
// Test query in Twig
$string = Timber::compile_string("{{
post.terms({
query: {
taxonomy: ['team', 'book'],
number: 3,
orderby: 'name'
}
})|join(', ') }}", [
'post' => $post,
]);
$this->assertEquals('Bills, Dolphins, Edge of Eternity', $string);
}
public function set_object_terms($pid, $term_info, $taxonomy = 'post_tag', $append = true)
{
$term_id = 0;
if (is_array($term_info)) {
$term_id = $term_info['term_id'];
} elseif (is_object($term_info) && get_class($term_info) == 'WP_Error') {
$term_id = $term_info->error_data['term_exists'];
}
if ($term_id) {
wp_set_object_terms($pid, $term_id, $taxonomy, $append);
}
}
public function testPostTermClass()
{
// create new post
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
// create a new tag, associate with post
$dummy_tag = wp_insert_term('whatever', 'post_tag');
self::set_object_terms($pid, $dummy_tag, 'post_tag');
$this->add_filter_temporarily('timber/term/classmap', function () {