forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-helper.php
391 lines (349 loc) · 12.1 KB
/
test-timber-helper.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
<?php
require_once(__DIR__ . '/php/timber-post-subclass.php');
/**
* @group posts-api
* @group terms-api
* @group users-api
*/
class TestTimberHelper extends Timber_UnitTestCase
{
public function testPluckArray()
{
$arr = [];
$arr[] = [
'name' => 'Bill',
'number' => 42,
];
$arr[] = [
'name' => 'Barack',
'number' => 44,
];
$arr[] = [
'name' => 'Hillary',
'number' => 45,
];
$names = \Timber\Helper::pluck($arr, 'name');
$this->assertEquals(['Bill', 'Barack', 'Hillary'], $names);
}
public function testPluckArrayMissing()
{
$arr = [];
$arr[] = [
'name' => 'Bill',
'number' => 42,
];
$arr[] = [
'name' => 'Barack',
'number' => 44,
];
$arr[] = [
'name' => 'Hillary',
'number' => 45,
];
$arr[] = [
'name' => 'Donald',
];
$names = \Timber\Helper::pluck($arr, 'number');
$this->assertEquals([42, 44, 45], $names);
}
public function testPluckObject()
{
$billy = new stdClass();
$billy->name = 'Billy Corgan';
$billy->instrument = 'guitar';
$jimmy = new stdClass();
$jimmy->name = 'Jimmy Chamberlin';
$jimmy->instrument = 'drums';
$pumpkins = [$billy, $jimmy];
$instruments = \Timber\Helper::pluck($pumpkins, 'instrument');
$this->assertEquals(['guitar', 'drums'], $instruments);
}
public function testPluckObjectWithMethod()
{
$this->register_post_classmap_temporarily([
'post' => TimberPostSubclass::class,
]);
$tps = Timber::get_post($this->factory->post->create());
$jimmy = new stdClass();
$jimmy->name = 'Jimmy';
$pumpkins = [$tps, $jimmy];
$bar = \Timber\Helper::pluck($pumpkins, 'foo');
$this->assertEquals(['bar'], $bar);
}
public function testTrimCharacters()
{
$text = "Sometimes you need to do such weird things like remove all comments from your project.";
$trimmed = \Timber\TextHelper::trim_characters($text, 20);
$this->assertEquals("Sometimes yo…", $trimmed);
}
public function testCloseTagsWithSelfClosingTags()
{
$p = '<p>My thing is this <hr>Whatever';
$html = \Timber\TextHelper::close_tags($p);
$this->assertEquals('<p>My thing is this <hr />Whatever</p>', $html);
}
public function testCommentForm()
{
$post_id = $this->factory->post->create();
global $post;
$post = get_post($post_id);
$form = Timber\Helper::ob_function('comment_form', [[], $post_id]);
$form = trim($form);
$this->assertStringStartsWith('<div id="respond"', $form);
}
public function testWPTitle()
{
//since we're testing with twentyfourteen -- need to remove its filters on wp_title
remove_all_filters('wp_title');
remove_theme_support('title-tag');
$this->assertSame('', Timber\Helper::get_wp_title());
}
public function testWPTitleSingle()
{
//since we're testing with twentyfourteen -- need to remove its filters on wp_title
remove_all_filters('wp_title');
$post_id = $this->factory->post->create([
'post_title' => 'My New Post',
]);
$post = get_post($post_id);
$this->go_to(site_url('?p=' . $post_id));
$this->assertEquals('My New Post', Timber\Helper::get_wp_title());
}
public function testCloseTags()
{
$str = '<a href="http://wordpress.org">Hi!';
$closed = Timber\TextHelper::close_tags($str);
$this->assertEquals($str . '</a>', $closed);
}
public function testArrayToObject()
{
$arr = [
'jared' => 'super cool',
];
$obj = Timber\Helper::array_to_object($arr);
$this->assertEquals('super cool', $obj->jared);
}
public function testArrayArrayToObject()
{
$arr = [
'jared' => 'super cool',
'prefs' => [
'food' => 'spicy',
'women' => 'spicier',
],
];
$obj = Timber\Helper::array_to_object($arr);
$this->assertEquals('spicy', $obj->prefs->food);
}
public function testGetObjectIndexByProperty()
{
$obj1 = new stdClass();
$obj1->name = 'mark';
$obj1->skill = 'acro yoga';
$obj2 = new stdClass();
$obj2->name = 'austin';
$obj2->skill = 'cooking';
$arr = [$obj1, $obj2];
$index = Timber\Helper::get_object_index_by_property($arr, 'skill', 'cooking');
$this->assertSame(1, $index);
$obj = Timber\Helper::get_object_by_property($arr, 'skill', 'cooking');
$this->assertEquals('austin', $obj->name);
}
public function testGetObjectByPropertyButNoMatch()
{
$obj1 = new stdClass();
$obj1->name = 'mark';
$obj1->skill = 'acro yoga';
$arr = [$obj1];
$result = Timber\Helper::get_object_by_property($arr, 'skill', 'cooking');
$this->assertFalse($result);
}
public function testGetArrayIndexByProperty()
{
$obj1 = [];
$obj1['name'] = 'mark';
$obj1['skill'] = 'acro yoga';
$obj2 = [];
$obj2['name'] = 'austin';
$obj2['skill'] = 'cooking';
$arr = [$obj1, $obj2];
$index = \Timber\Helper::get_object_index_by_property($arr, 'skill', 'cooking');
$this->assertSame(1, $index);
$this->assertFalse(\Timber\Helper::get_object_index_by_property('butts', 'skill', 'cooking'));
}
public function testGetObjectByPropertyButNo()
{
$this->expectException(InvalidArgumentException::class);
$obj1 = new stdClass();
$obj1->name = 'mark';
$obj1->skill = 'acro yoga';
$obj = Timber\Helper::get_object_by_property($obj1, 'skill', 'cooking');
}
public function testTimers()
{
$start = Timber\Helper::start_timer();
sleep(1);
$end = Timber\Helper::stop_timer($start);
$this->assertStringContainsString(' seconds.', $end);
$time = str_replace(' seconds.', '', $end);
$this->assertGreaterThan(1, $time);
}
public function testArrayTruncate()
{
$arr = ['Buster', 'GOB', 'Michael', 'Lindsay'];
$arr = Timber\Helper::array_truncate($arr, 2);
$this->assertContains('Buster', $arr);
$this->assertSame(2, count($arr));
$this->assertFalse(in_array('Lindsay', $arr));
}
public function testIsTrue()
{
$true = Timber\Helper::is_true('true');
$this->assertTrue($true);
$false = Timber\Helper::is_true('false');
$this->assertFalse($false);
$estelleGetty = Timber\Helper::is_true('Estelle Getty');
$this->assertTrue($estelleGetty);
}
public function testIsEven()
{
$this->assertTrue(Timber\Helper::iseven(2));
$this->assertFalse(Timber\Helper::iseven(7));
}
public function testIsOdd()
{
$this->assertFalse(Timber\Helper::isodd(2));
$this->assertTrue(Timber\Helper::isodd(7));
}
public function testErrorLog()
{
ob_start();
$this->assertTrue(Timber\Helper::error_log('foo'));
$this->assertTrue(Timber\Helper::error_log(['Dark Helmet', 'Barf']));
$data = ob_get_flush();
}
public function testOSort()
{
$michael = new stdClass();
$michael->name = 'Michael';
$michael->year = 1981;
$lauren = new stdClass();
$lauren->name = 'Lauren';
$lauren->year = 1984;
$boo = new stdClass();
$boo->name = 'Robbie';
$boo->year = 1989;
$people = [$lauren, $michael, $boo];
Timber\Helper::osort($people, 'year');
$this->assertEquals('Michael', $people[0]->name);
$this->assertEquals('Lauren', $people[1]->name);
$this->assertEquals('Robbie', $people[2]->name);
$this->assertSame(1984, $people[1]->year);
}
/**
* Updated to new syntax
* @ticket #2124
*/
public function testNewArrayFilter()
{
$posts = [];
$posts[] = $this->factory->post->create([
'post_title' => 'Stringer Bell',
'post_content' => 'Idris Elba',
]);
$posts[] = $this->factory->post->create([
'post_title' => 'Snoop',
'post_content' => 'Felicia Pearson',
]);
$posts[] = $this->factory->post->create([
'post_title' => 'Cheese',
'post_content' => 'Method Man',
]);
$posts = Timber::get_posts($posts);
$template = '{% for post in posts | wp_list_filter("snoop")%}{{ post.content|striptags }}{% endfor %}';
$str = Timber::compile_string($template, [
'posts' => $posts,
]);
$this->assertEquals('Felicia Pearson', trim($str));
}
public function testIsArrayAssoc()
{
$arr = [14, 21, 'thing'];
$this->assertFalse(Timber\Helper::is_array_assoc($arr));
$assoc_array = [
'thing' => 'yeah',
'foo' => 'bar',
];
$this->assertTrue(Timber\Helper::is_array_assoc($assoc_array));
}
public function testTwigFilterFilter()
{
$template = "{% set sizes = [34, 36, 38, 40, 42] %}{{ sizes|filter(v => v > 38)|join(', ') }}";
$str = Timber::compile_string($template);
$this->assertEquals("40, 42", $str);
}
/**
* Test for when we're filtering something that's not an array.
*/
public function testArrayFilterWithBogusArray()
{
$this->expectException(Twig\Error\RuntimeError::class);
$template = '{% for post in posts | filter({slug:"snoop", post_content:"Idris Elba"}, "OR")%}{{ post.title }} {% endfor %}';
$str = Timber::compile_string($template, [
'posts' => 'foobar',
]);
$this->assertSame('', $str);
}
public function testConvertWPObject()
{
// Test WP_Post -> \Timber\Post
$post_id = $this->factory->post->create();
$wp_post = get_post($post_id);
$timber_post = \Timber\Helper::convert_wp_object($wp_post);
$this->assertTrue($timber_post instanceof \Timber\Post);
// Test WP_Term -> \Timber\Term
$term_id = $this->factory->term->create();
$wp_term = get_term($term_id);
$timber_term = \Timber\Helper::convert_wp_object($wp_term);
$this->assertTrue($timber_term instanceof \Timber\Term);
// Test WP_User -> \Timber\User
$user_id = $this->factory->user->create();
$wp_user = get_user_by('id', $user_id);
$timber_user = \Timber\Helper::convert_wp_object($wp_user);
$this->assertTrue($timber_user instanceof \Timber\User);
// Test strange input
$random_int = 2018;
$convert_int = \Timber\Helper::convert_wp_object($random_int);
$this->assertTrue($convert_int === $random_int);
$array = [];
$convert_array = \Timber\Helper::convert_wp_object($array);
$this->assertTrue(is_array($convert_array));
}
public function testConvertPostWithClassMap()
{
register_post_type('sport');
require_once('assets/Sport.php');
$this->register_post_classmap_temporarily([
'sport' => Sport::class,
]);
$sport_id = $this->factory->post->create([
'post_type' => 'sport',
'post_title' => 'Basketball Player',
]);
$wp_post = get_post($sport_id);
$sport_post = \Timber\Helper::convert_wp_object($wp_post);
$this->assertInstanceOf(Sport::class, $sport_post);
$this->assertEquals('ESPN', $sport_post->channel());
}
/**
* @expectedIncorrectUsage Accessing the thumbnail ID through {{ post._thumbnail_id }}
*/
public function testDoingItWrong()
{
$post_id = $this->factory->post->create();
$posts = Timber::get_posts();
update_post_meta($post_id, '_thumbnail_id', '707');
$post = Timber::get_post($post_id);
$thumbnail_id = $post->_thumbnail_id;
}
}