forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-post-terms.php
176 lines (156 loc) · 5.3 KB
/
test-timber-post-terms.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
<?php
/**
* @group posts-api
* @group terms-api
* @group post-terms
*/
class TestTimberPostTerms extends Timber_UnitTestCase
{
public function testPostTerms()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
// create a new tag and associate it with the post
$dummy_tag = wp_insert_term('whatever', 'post_tag');
wp_set_object_terms($pid, $dummy_tag['term_id'], 'post_tag', true);
$this->add_filter_temporarily('timber/term/classmap', function () {
return [
'post_tag' => MyTimberTerm::class,
];
});
$terms = $post->terms([
'query' => [
'taxonomy' => 'post_tag',
],
]);
$this->assertInstanceOf(MyTimberTerm::class, $terms[0]);
$post = Timber::get_post($pid);
$terms = $post->terms([
'query' => [
'taxonomy' => 'post_tag',
],
'merge' => true,
]);
$this->assertInstanceOf(MyTimberTerm::class, $terms[0]);
}
/**
* @ticket #2203
*/
public function testPostTermsUsingUsingFactories()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
// create a new tag and associate it with the post
$dummy_tag = wp_insert_term('whatever', 'post_tag');
$dummy_cat = wp_insert_term('news', 'category');
wp_set_object_terms($pid, $dummy_tag['term_id'], 'post_tag', true);
wp_set_object_terms($pid, $dummy_cat['term_id'], 'category', true);
$this->add_filter_temporarily('timber/term/classmap', function () {
return [
'post_tag' => MyTimberTerm::class,
];
});
$terms = $post->terms([
'taxonomy' => 'post_tag',
]);
$this->assertInstanceOf(MyTimberTerm::class, $terms[0]);
$post = Timber::get_post($pid);
$terms = $post->terms([], [
'merge' => false,
]);
$this->assertEquals('whatever', $terms['post_tag'][0]->name);
$terms = $post->terms([], [
'merge' => true,
]);
$this->assertSame(3, count($terms));
}
/**
* @ticket #2163
* This test confirms that term ordering works when sent through the query parameter of
* arguments.
*/
public function testPostTermOrder()
{
$pid = $this->factory->post->create();
register_taxonomy('cars', 'post');
$cars[] = $this->factory->term->create([
'name' => 'Honda Civic',
'taxonomy' => 'cars',
]);
$cars[] = $this->factory->term->create([
'name' => 'Toyota Corolla',
'taxonomy' => 'cars',
]);
$cars[] = $this->factory->term->create([
'name' => 'Toyota Camry',
'taxonomy' => 'cars',
]);
$cars[] = $this->factory->term->create([
'name' => 'Dodge Intrepid',
'taxonomy' => 'cars',
]);
foreach ($cars as $tid) {
$car = Timber::get_term($tid);
}
wp_set_object_terms($pid, $cars, 'cars', false);
$post = Timber::get_post($pid);
$template = "{% for term_item in post.terms({query : {taxonomy: 'cars', orderby: 'term_id', order: 'ASC'}}) %}{{ term_item.name }} {% endfor %}";
$str = Timber::compile_string($template, [
'post' => $post,
]);
$this->assertEquals('Honda Civic Toyota Corolla Toyota Camry Dodge Intrepid ', $str);
}
/**
* This should return an error because the "dfasdf" taxonomy doesn't exist
* NOTE: In Timber 1.x this returned a WP_Error.
*/
public function testTermExceptions()
{
self::enable_error_log(false);
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$terms = $post->terms('dfasdf');
$this->assertEmpty($terms);
self::enable_error_log(true);
}
/**
* This shouldn't return an error because the "foobar" taxonomy DOES exist
*/
public function testTermFromNonExistentTaxonomy()
{
self::enable_error_log(false);
register_taxonomy('foobar', 'post');
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$terms = $post->terms('foobar');
$this->assertEmpty($terms);
self::enable_error_log(true);
}
public function testTermNotMerged()
{
$pid = $this->factory->post->create();
// create a new tag and category and associate each with the post
$tag_id = $this->factory->term->create([
'name' => 'whatever',
'taxonomy' => 'post_tag',
]);
$cat_id = $this->factory->term->create([
'name' => 'thingy',
'taxonomy' => 'category',
]);
wp_set_object_terms($pid, $tag_id, 'post_tag', true);
wp_set_object_terms($pid, $cat_id, 'category', true);
$post = Timber::get_post($pid);
$terms = $post->terms([
'query' => [
'taxonomy' => 'all',
],
'merge' => false,
]);
$this->assertEquals($terms['post_tag'][0]->name, 'whatever');
$this->assertEquals($terms['category'][0]->name, 'thingy');
}
}
class MyTimberTerm extends Timber\Term
{
}