forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-term.php
656 lines (572 loc) · 20.5 KB
/
test-timber-term.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
<?php
use Timber\Post;
class TermTestPage extends Post
{
}
/**
* @group terms-api
*/
class TestTimberTerm extends Timber_UnitTestCase
{
public function testTermFrom()
{
register_taxonomy('baseball', ['post']);
register_taxonomy('hockey', ['post']);
$this->factory->term->create([
'name' => 'Rangers',
'taxonomy' => 'baseball',
]);
$this->factory->term->create([
'name' => 'Cardinals',
'taxonomy' => 'baseball',
]);
$this->factory->term->create([
'name' => 'Rangers',
'taxonomy' => 'hockey',
]);
$wp_terms = get_terms([
'taxonomy' => 'baseball',
'hide_empty' => false,
]);
$baseball_teams = Timber::get_terms($wp_terms);
$this->assertCount(2, $baseball_teams);
$this->assertEquals('Cardinals', $baseball_teams[0]->title());
$this->assertEquals('Rangers', $baseball_teams[1]->title());
}
/**
* @ticket #2362
*/
public function testMultiTermsWithSameSlug()
{
$post_tag_id = $this->factory->term->create([
'name' => 'Security',
'taxonomy' => 'post_tag',
]);
$category_id = $this->factory->term->create([
'name' => 'Security',
'taxonomy' => 'category',
]);
$post_id = $this->factory->post->create();
wp_set_object_terms($post_id, $post_tag_id, 'post_tag', true);
wp_set_object_terms($post_id, $category_id, 'category', true);
$term_default = Timber::get_term_by('slug', 'security');
$this->assertEquals('post_tag', $term_default->taxonomy);
$this->assertEquals('Security', $term_default->title());
$term_category = Timber::get_term_by('slug', 'security', 'category');
$this->assertEquals('category', $term_category->taxonomy);
$this->assertEquals('Security', $term_category->title());
}
public function testTermFromInvalidObject()
{
$this->expectException(\InvalidArgumentException::class);
register_taxonomy('baseball', ['post']);
$term_id = $this->factory->term->create([
'name' => 'Cardinals',
'taxonomy' => 'baseball',
]);
$post_id = $this->factory->post->create([
'post_title' => 'Test Post',
]);
$post = get_post($post_id);
$test = Timber::get_terms($post);
}
public function testGetTerm()
{
register_taxonomy('arts', ['post']);
$term_id = $this->factory->term->create([
'name' => 'Zong',
'taxonomy' => 'arts',
]);
$term = Timber::get_term($term_id);
$this->assertEquals('Zong', $term->title());
$template = '{% set zp_term = get_term("' . $term->ID . '", "arts") %}{{ zp_term.name }}';
$string = Timber::compile_string($template);
$this->assertEquals('Zong', $string);
}
public function testTerm()
{
$term_id = $this->factory->term->create();
$term = Timber::get_term($term_id);
$this->assertEquals('Timber\Term', get_class($term));
}
public function testGetTermWithObject()
{
$term_id = $this->factory->term->create([
'name' => 'Famous Commissioners',
]);
$term_data = get_term($term_id, 'post_tag');
$this->assertTrue(in_array(get_class($term_data), ['WP_Term', 'stdClass']));
$term = Timber::get_term($term_id);
$this->assertEquals('Famous Commissioners', $term->title());
$this->assertEquals('Timber\Term', get_class($term));
}
public function testTermToString()
{
$term_id = $this->factory->term->create([
'name' => 'New England Patriots',
]);
$term = Timber::get_term($term_id);
$str = Timber::compile_string('{{term}}', [
'term' => $term,
]);
$this->assertEquals('New England Patriots', $str);
}
public function testTermDescription()
{
$desc = 'An honest football team';
$term_id = $this->factory->term->create([
'name' => 'New England Patriots',
'description' => $desc,
]);
$term = Timber::get_term($term_id, 'post_tag');
$this->assertEquals($desc, $term->description());
}
public function testTermInitObject()
{
$term_id = $this->factory->term->create();
$term = get_term($term_id, 'post_tag');
$term = Timber::get_term($term);
$this->assertEquals($term->ID, $term_id);
}
public function testTermLink()
{
$term_id = $this->factory->term->create();
$term = Timber::get_term($term_id);
$this->assertStringContainsString('http://', $term->link());
}
public function testTermPath()
{
$term_id = $this->factory->term->create();
$term = Timber::get_term($term_id);
$this->assertFalse(strstr($term->path(), 'http://'));
}
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);
$term_id = $this->factory->term->create();
$term = Timber::get_term($term_id);
$this->assertTrue($term->can_edit());
// Test subscriber role.
wp_set_current_user($subscriber_id);
$this->assertFalse($term->can_edit());
wp_set_current_user(0);
}
/*
* Term::posts() tests
*/
public function testPostsDefault()
{
register_post_type('portfolio', [
'taxonomies' => ['arts'],
'public' => true,
]);
register_taxonomy('arts', ['portfolio', 'post']);
// Create a term, and some posts to assign it to.
$term_id = $this->factory->term->create([
'name' => 'Zong',
'taxonomy' => 'arts',
]);
// Create 12 posts total.
// NOTE: Neither post_type has enough to satisfy the assertion below on its own,
// but together they should exceed the default posts_per_page and we should get
// exactly posts_per_page (10) back.
$posts = array_merge(
$this->factory->post->create_many(5),
$this->factory->post->create_many(7, [
'post_type' => 'portfolio',
])
);
// assign the term to each of our new posts
foreach ($posts as $post_id) {
wp_set_object_terms($post_id, $term_id, 'arts', true);
}
$other_id = $this->factory->term->create([
'name' => 'Other',
'taxonomy' => 'arts',
]);
$other_posts = $this->factory->post->create_many(10);
foreach ($other_posts as $id) {
wp_set_object_terms($id, $other_id, 'arts', true);
}
$term = Timber::get_term($term_id);
// Expect the default posts_per_page, with posts of all types.
$this->assertCount(10, $term->posts());
// Passing an empty array should behave exactly the same.
$this->assertCount(10, $term->posts([]));
}
public function testPostsDefaultPostType()
{
register_post_type('portfolio', [
'taxonomies' => ['arts'],
'public' => true,
]);
register_taxonomy('arts', ['portfolio', 'post']);
// Create a term, and some posts to assign it to.
$term_id = $this->factory->term->create([
'name' => 'Zong',
'taxonomy' => 'arts',
]);
// Create 12 posts total.
// NOTE: Neither post_type has enough to satisfy the assertion below on its own,
// but together they should exceed the 8 we ask for so we should get exactly 8 back.
// This is because, according to the docs, post_type defaults to "any" when using
// tax_query.
// https://developer.wordpress.org/reference/classes/WP_Query/parse_query/
$posts = array_merge(
$this->factory->post->create_many(5),
$this->factory->post->create_many(7, [
'post_type' => 'portfolio',
])
);
// assign the term to each of our new posts
foreach ($posts as $post_id) {
wp_set_object_terms($post_id, $term_id, 'arts', true);
}
$term = Timber::get_term($term_id);
// Expect exactly the count we asked for.
$this->assertCount(8, $term->posts([
'posts_per_page' => 8,
]));
}
public function testPostsWithPostTypeQuery()
{
register_post_type('portfolio', [
'taxonomies' => ['arts'],
'public' => true,
]);
register_taxonomy('arts', ['portfolio', 'post']);
// Create a term, and some posts to assign it to.
$term_id = $this->factory->term->create([
'name' => 'Zong',
'taxonomy' => 'arts',
]);
// Create 12 posts total. But we should only get 7 back below even though posts_per_page
// defaults to 10, because we limit by post_type.
$posts = array_merge(
$this->factory->post->create_many(5),
$this->factory->post->create_many(7, [
'post_type' => 'portfolio',
])
);
// assign the term to each of our new posts
foreach ($posts as $post_id) {
wp_set_object_terms($post_id, $term_id, 'arts', true);
}
$term = Timber::get_term($term_id);
// Expect the default posts_per_page, with posts of all types.
$this->assertCount(7, $term->posts([
'post_type' => 'portfolio',
]));
}
public function testPostsWithTaxQuery()
{
register_post_type('portfolio', [
'taxonomies' => ['arts'],
'public' => true,
]);
register_taxonomy('arts', ['portfolio', 'post']);
// Create a term, and some posts to assign it to.
$term_id = $this->factory->term->create([
'taxonomy' => 'arts',
]);
// Create 12 posts total.
// NOTE: Neither post_type has enough to satisfy the assertion below on its own,
// but together they should exceed the 8 we ask for so we should get exactly 8 back.
// This is because, according to the docs, post_type defaults to "any" when using
// tax_query.
// https://developer.wordpress.org/reference/classes/WP_Query/parse_query/
$posts = array_merge(
$this->factory->post->create_many(5),
$this->factory->post->create_many(7, [
'post_type' => 'portfolio',
])
);
// assign the term to each of our new posts
foreach ($posts as $post_id) {
wp_set_object_terms($post_id, $term_id, 'arts', true);
}
// Tag one post and one portfolio with a special crafts term, too.
register_taxonomy('crafts', ['portfolio', 'post']);
$craft_id = $this->factory->term->create([
'taxonomy' => 'crafts',
]);
wp_set_object_terms($posts[0], $craft_id, 'crafts', true);
wp_set_object_terms($posts[5], $craft_id, 'crafts', true);
$term = Timber::get_term($term_id);
// Expect the intersection of arts & crafts.
$this->assertCount(2, $term->posts([
'tax_query' => [
[
'field' => 'id',
'terms' => $craft_id,
'taxonomy' => 'crafts',
],
// This should get overridden; we don't want users to be able to
// override the Term we're querying for.
'relation' => 'OR',
],
]));
}
/**
* @expectedIncorrectUsage Passing a query string to Term::posts()
*/
public function testGetPostsWithQueryString()
{
register_post_type('portfolio', [
'taxonomies' => ['post_tag'],
'public' => true,
]);
$term_id = $this->factory->term->create([
'name' => 'Zong',
]);
$this->factory->post->create_many(3, [
'post_type' => 'post',
'tags_input' => 'zong',
]);
$this->factory->post->create_many(5, [
'post_type' => 'portfolio',
'tags_input' => 'zong',
]);
// Count is mismatched because string-based queries default to a post_type of "post".
$term = Timber::get_term($term_id);
$this->assertFalse($term->posts('posts_per_page=8'));
}
/**
* @expectedDeprecated Passing post_type_or_class
* This test *partially* honors the logic described in
* https://github.com/timber/timber/issues/799#issuecomment-192445207,
* although that behavior is not deprecated.
*/
public function testGetPostsWithPostTypeArg()
{
register_post_type('portfolio', [
'taxonomies' => ['post_tag'],
'public' => true,
]);
$term_id = $this->factory->term->create([
'name' => 'Zong',
]);
$this->factory->post->create_many(3, [
'post_type' => 'post',
'tags_input' => 'zong',
]);
$this->factory->post->create_many(5, [
'post_type' => 'portfolio',
'tags_input' => 'zong',
]);
$term = Timber::get_term($term_id);
$this->assertCount(3, $term->posts([
'orderby' => 'menu_order',
], 'post'));
}
/**
* @expectedIncorrectUsage Passing a post class
*/
public function testGetPostsWithPostClassArg()
{
register_post_type('portfolio', [
'taxonomies' => ['post_tag'],
'public' => true,
]);
$term_id = $this->factory->term->create([
'name' => 'Zong',
]);
$this->factory->post->create_many(3, [
'post_type' => 'post',
'tags_input' => 'zong',
]);
$this->factory->post->create_many(5, [
'post_type' => 'portfolio',
'tags_input' => 'zong',
]);
$term = Timber::get_term($term_id);
$this->assertCount(3, $term->posts([
'orderby' => 'menu_order',
], null, 'INCORRECT'));
}
/**
* @expectedDeprecated {{ term.get_posts }}
*/
public function testGetPostsDeprecated()
{
$term_id = $this->factory->term->create([
'name' => 'Rad',
]);
$posts = $this->factory->post->create_many(3, [
'tags_input' => 'rad',
]);
$term = Timber::get_term($term_id);
$this->assertCount(3, $term->get_posts());
}
public function testPostsWithPostCount()
{
$term_id = $this->factory->term->create();
// Assign some pages to our post_tag Term.
$page_ids = $this->factory->post->create_many(3, [
'post_type' => 'page',
'post_date' => '2020-01-01',
]);
// Create some posts too.
$post_ids = $this->factory->post->create_many(3, [
'post_date' => '2019-01-01',
]);
// Tag all posts.
foreach (array_merge($page_ids, $post_ids) as $post_id) {
wp_set_object_terms($post_id, $term_id, 'post_tag', true);
}
$this->register_post_classmap_temporarily([
'page' => TermTestPage::class,
]);
// Get the first four posts from this term.
$term_posts = Timber::get_term($term_id)->posts(4);
$this->assertCount(4, $term_posts);
// Pages should come first due to later publish dates.
$this->assertInstanceOf(TermTestPage::class, $term_posts[0]);
$this->assertInstanceOf(TermTestPage::class, $term_posts[1]);
$this->assertInstanceOf(TermTestPage::class, $term_posts[2]);
$this->assertInstanceOf(Post::class, $term_posts[3]);
}
public function testPostsWithExtraQueryArgs()
{
$term_id = $this->factory->term->create([
'name' => 'Rad',
]);
$posts = [
$this->factory->post->create([
'post_title' => 'Earlier',
'post_date' => '2020-01-01',
'tags_input' => 'rad',
]),
$this->factory->post->create([
'post_title' => 'Later',
'post_date' => '2020-03-01',
'tags_input' => 'rad',
]),
$this->factory->post->create([
'post_title' => 'Much Later',
'post_date' => '2020-08-01',
'tags_input' => 'rad',
]),
];
$term = Timber::get_term($term_id);
$term_posts = $term->posts([
'posts_per_page' => 2,
'orderby' => 'post_date',
'order' => 'ASC',
]);
$this->assertCount(2, $term_posts);
$this->assertEquals('Earlier', $term_posts[0]->title());
$this->assertEquals('Later', $term_posts[1]->title());
}
public function testTermChildren()
{
$parent_id = $this->factory->term->create([
'name' => 'News',
'taxonomy' => 'category',
]);
$local = $this->factory->term->create([
'name' => 'Local',
'parent' => $parent_id,
'taxonomy' => 'category',
]);
$int = $this->factory->term->create([
'name' => 'International',
'parent' => $parent_id,
'taxonomy' => 'category',
]);
$term = Timber::get_term($parent_id);
$children = $term->children();
$this->assertSame(2, count($children));
$this->assertEquals('Local', $children[0]->name);
}
/**
* @ticket #824
*/
public function testTermWithNativeMeta()
{
$tid = $this->factory->term->create([
'name' => 'News',
'taxonomy' => 'category',
]);
add_term_meta($tid, 'foo', 'bar');
$term = Timber::get_term($tid);
$template = '{{term.foo}}';
$compiled = Timber::compile_string($template, [
'term' => $term,
]);
$this->assertEquals('bar', $compiled);
}
/**
* @ticket #824
*/
public function testTermWithNativeMetaFalse()
{
$tid = $this->factory->term->create([
'name' => 'News',
'taxonomy' => 'category',
]);
add_term_meta($tid, 'foo', false);
$term = Timber::get_term($tid);
$this->assertSame('', $term->meta('foo'));
}
/**
* @ticket #824
*/
public function testTermWithNativeMetaNotExisting()
{
$tid = $this->factory->term->create([
'name' => 'News',
'taxonomy' => 'category',
]);
add_term_meta($tid, 'bar', 'qux');
;
$wp_native_value = get_term_meta($tid, 'foo', true);
$acf_native_value = get_field('foo', 'category_' . $tid);
$valid_wp_native_value = get_term_meta($tid, 'bar', true);
$valid_acf_native_value = get_field('bar', 'category_' . $tid);
$term = Timber::get_term($tid);
//test baseline "bar" data
$this->assertEquals('qux', $valid_wp_native_value);
$this->assertEquals('qux', $valid_acf_native_value);
$this->assertEquals('qux', $term->bar);
//test the one that doesn't exist
$this->assertEquals('string', gettype($wp_native_value));
$this->assertEmpty($wp_native_value);
$this->assertNull($acf_native_value);
$this->assertNotTrue($term->meta('foo'));
}
public function testEditLink()
{
wp_set_current_user(1);
$tid = $this->factory->term->create([
'name' => 'News',
'taxonomy' => 'category',
]);
$term = Timber::get_term($tid);
$links = [];
$links[] = 'http://example.org/wp-admin/term.php?taxonomy=category&tag_ID=' . $tid . '&post_type=post';
$links[] = 'http://example.org/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=' . $tid . '&post_type=post';
$links[] = 'http://example.org/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=' . $tid;
$links[] = 'http://example.org/wp-admin/term.php?taxonomy=category&term_id=' . $tid . '&post_type=post';
$this->assertContains($term->edit_link(), $links);
}
public function testWPObject()
{
$term_id = $this->factory->term->create();
$term = Timber::get_term($term_id);
$this->assertInstanceOf('WP_Term', $term->wp_object());
}
}
class Arts extends Timber\Term
{
public function foobar()
{
return 'Zebra';
}
}