forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-context.php
270 lines (216 loc) · 7.81 KB
/
test-timber-context.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
<?php
use Timber\Post;
use Timber\PostQuery;
use Timber\Term;
use Timber\Timber;
use Timber\User;
/**
* @group posts-api
* @group post-collections
*/
class TestTimberContext extends Timber_UnitTestCase
{
/**
* This throws an infite loop if memorization isn't working
*/
public function testContextLoop()
{
$this->add_filter_temporarily('timber/context', function ($context) {
$context = Timber::context();
$context['zebra'] = 'silly horse';
return $context;
});
$context = Timber::context();
$this->assertEquals('http://example.org', $context['http_host']);
}
public function testPostContextSimple()
{
$post_id = $this->factory->post->create();
$this->go_to(get_permalink($post_id));
$context = Timber::context();
$post = Timber::get_post($post_id);
$this->assertArrayNotHasKey('posts', $context);
$this->assertEquals($post, $context['post']);
$context = Timber::context();
$this->assertEquals('http://example.org', $context['http_host']);
}
public function testPostsContextHomePosts()
{
update_option('show_on_front', 'posts');
$id = $this->factory->post->create([
'post_title' => 'Blog',
'post_type' => 'page',
]);
update_option('page_for_posts', $id);
$this->factory->post->create_many(3);
$this->go_to('/');
$context = Timber::context();
$this->assertInstanceOf(PostQuery::class, $context['posts']);
$this->assertCount(3, $context['posts']);
$this->assertInstanceOf(Post::class, $context['post']);
$this->assertEquals($context['post']->id, $context['posts'][0]->id);
}
/**
* @ticket https://github.com/timber/timber/issues/2470
*/
public function testPostsContextWithPostOnFrontAndNoPageForPosts()
{
update_option('show_on_front', 'posts');
update_option('page_for_posts', 0);
update_option('page_on_front', 0);
$this->go_to('/');
$context = Timber::context();
$this->assertNotContains('post', $context);
}
public function testPostsContextHomePage()
{
update_option('show_on_front', 'page');
$id = $this->factory->post->create([
'post_type' => 'page',
]);
update_option('page_on_front', $id);
$this->go_to('/');
$context = Timber::context();
$this->assertArrayNotHasKey('posts', $context);
$this->assertInstanceOf(Post::class, $context['post']);
$this->assertEquals($id, $context['post']->id);
}
public function testPostsContextSearch()
{
$this->factory->post->create_many(3, [
'post_content' => 'here are some things',
'post_status' => 'publish',
]);
$this->factory->post->create_many(3, [
'post_content' => 'here is some stuff',
'post_status' => 'publish',
]);
query_posts('s=stuff');
$context = Timber::context();
$this->assertArrayNotHasKey('post', $context);
$this->assertInstanceOf(PostQuery::class, $context['posts']);
$this->assertCount(3, $context['posts']);
$this->assertEquals('stuff', $context['search_query']);
}
public function testPostsContextAuthor()
{
$uid = $this->factory->user->create([
'user_login' => 'bob',
]);
$this->factory->post->create_many(3, [
'post_content' => 'here are some things',
'post_author' => $uid,
'post_status' => 'publish',
]);
query_posts('author=' . $uid);
$context = Timber::context();
$this->assertArrayNotHasKey('post', $context);
$this->assertInstanceOf(PostQuery::class, $context['posts']);
$this->assertCount(3, $context['posts']);
$this->assertInstanceOf(User::class, $context['author']);
$this->assertEquals($uid, $context['author']->id);
}
public function testPostsContextCategory()
{
$stuff = wp_insert_term('Stuff', 'category');
$cat_posts = $this->factory->post->create_many(3, [
'post_status' => 'publish',
]);
foreach ($cat_posts as $id) {
wp_set_object_terms($id, $stuff, 'category');
}
// 3 uncategorized posts
$this->factory->post->create_many(3, [
'post_status' => 'publish',
]);
query_posts('cat=' . $stuff['term_id']);
$context = Timber::context();
$this->assertArrayNotHasKey('post', $context);
$this->assertInstanceOf(PostQuery::class, $context['posts']);
$this->assertCount(3, $context['posts']);
$this->assertInstanceOf(Term::class, $context['term']);
$this->assertEquals('Stuff', $context['term']->title());
}
public function testPostsContextTag()
{
$stuff = wp_insert_term('Stuff', 'post_tag');
$cat_posts = $this->factory->post->create_many(3, [
'post_status' => 'publish',
]);
foreach ($cat_posts as $id) {
wp_set_object_terms($id, $stuff, 'post_tag');
}
// 3 untagged posts
$this->factory->post->create_many(3, [
'post_status' => 'publish',
]);
query_posts('tag=stuff');
$context = Timber::context();
$this->assertArrayNotHasKey('post', $context);
$this->assertInstanceOf(PostQuery::class, $context['posts']);
$this->assertCount(3, $context['posts']);
$this->assertInstanceOf(Term::class, $context['term']);
$this->assertEquals('Stuff', $context['term']->title());
}
public function testPostsContextTax()
{
register_taxonomy('thingy', ['post'], [
'public' => true,
]);
$stuff = wp_insert_term('Stuff', 'thingy');
$cat_posts = $this->factory->post->create_many(3, [
'post_status' => 'publish',
]);
foreach ($cat_posts as $id) {
wp_set_object_terms($id, $stuff, 'thingy');
}
// 3 non-thingy posts
$this->factory->post->create_many(3, [
'post_status' => 'publish',
]);
query_posts([
'tax_query' => [
[
'taxonomy' => 'thingy',
'terms' => [$stuff['term_id']],
'field' => 'term_id',
],
],
]);
$context = Timber::context();
$this->assertArrayNotHasKey('post', $context);
$this->assertInstanceOf(PostQuery::class, $context['posts']);
$this->assertCount(3, $context['posts']);
$this->assertInstanceOf(Term::class, $context['term']);
$this->assertEquals('Stuff', $context['term']->title());
}
public function testIfSetupFunctionIsRunInSingularTemplates()
{
$post_id = $this->factory->post->create();
$this->go_to(get_permalink($post_id));
global $wp_query;
$this->assertFalse($wp_query->in_the_loop);
Timber::context();
$this->assertTrue($wp_query->in_the_loop);
}
/**
* Tests whether 'the_post' action is called when a singular template is displayed.
*
* @see TestTimberPost::testPostConstructorAndThePostHook()
*/
public function testIfThePostHookIsRunInSingularTemplates()
{
add_action('the_post', function ($post) {
add_filter('touched_the_post_action', '__return_true');
});
$post_id = $this->factory()->post->create();
$this->go_to(get_permalink($post_id));
Timber::context();
$this->assertTrue(apply_filters('touched_the_post_action', false));
}
public function testContext()
{
$context = Timber::context();
$this->assertEquals('http://example.org', $context['http_host']);
}
}