forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-post-excerpt.php
359 lines (313 loc) · 11.7 KB
/
test-timber-post-excerpt.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
<?php
use Timber\PostExcerpt;
/**
* @group called-post-constructor
*/
class TestTimberPostExcerpt extends Timber_UnitTestCase
{
public function testDoubleEllipsis()
{
$post_id = $this->factory->post->create();
$post = Timber::get_post($post_id);
$post->post_excerpt = 'this is super dooper trooper long words';
$excerpt = new PostExcerpt($post, [
'words' => 3,
'force' => true,
]);
$this->assertSame(1, substr_count((string) $excerpt, '…'));
}
public function testReadMoreClassFilter()
{
$this->add_filter_temporarily('timber/post/excerpt/read_more_class', function ($class) {
return $class . ' and-foo';
});
$post_id = $this->factory->post->create([
'post_excerpt' => 'It turned out that just about anyone in authority — cops, judges, city leaders — was in on the game.',
]);
$post = Timber::get_post($post_id);
$text = new PostExcerpt($post, [
'words' => 10,
'always_add_read_more' => true,
]);
$this->assertStringContainsString('and-foo', (string) $text);
}
public function testReadMoreLinkFilter()
{
$post_id = $this->factory->post->create([
'post_excerpt' => 'Let this be the excerpt!',
]);
$post = Timber::get_post($post_id);
$excerpt = $post->excerpt([
'always_add_read_more' => true,
]);
$this->add_filter_temporarily('timber/post/excerpt/read_more_link', function ($link) {
return ' Foobar';
});
$this->assertEquals('Let this be the excerpt! Foobar', (string) $excerpt);
}
/**
* @expectedDeprecated timber/post/get_preview/read_more_link
*/
public function testReadMoreLinkFilterDeprecated()
{
$post_id = $this->factory->post->create([
'post_excerpt' => 'Let this be the excerpt!',
]);
$post = Timber::get_post($post_id);
$excerpt = $post->excerpt([
'always_add_read_more' => true,
]);
$this->add_filter_temporarily('timber/post/get_preview/read_more_link', function ($link) {
return ' Foobar';
});
$this->assertEquals('Let this be the excerpt! Foobar', (string) $excerpt);
}
public function testExcerptTags()
{
$post_id = $this->factory->post->create([
'post_excerpt' => 'It turned out that just about anyone in authority — cops, judges, city leaders — was in on the game.',
]);
$post = Timber::get_post($post_id);
$text = new PostExcerpt($post, [
'words' => 20,
'force' => false,
'read_more' => '',
'strip' => false,
]);
$this->assertStringNotContainsString('</p>', (string) $text);
}
public function testGetExcerpt()
{
global $wp_rewrite;
$struc = false;
$wp_rewrite->permalink_structure = $struc;
update_option('permalink_structure', $struc);
$post_id = $this->factory->post->create([
'post_content' => 'this is super dooper trooper long words',
]);
$post = Timber::get_post($post_id);
// no excerpt
$post->post_excerpt = '';
$str = Timber::compile_string('{{ post.excerpt({
words: 3,
always_add_read_more: true
}) }}', [
'post' => $post,
]);
$this->assertMatchesRegularExpression('/this is super… <a href="http:\/\/example.org\/\?p=\d+" class="read-more">Read More<\/a>/', $str);
// excerpt set, force is false, no read more
$post->post_excerpt = 'this is excerpt longer than three words';
$excerpt = new PostExcerpt($post, [
'words' => 3,
'force' => false,
'read_more' => '',
]);
$this->assertEquals((string) $excerpt, $post->post_excerpt);
// custom read more set
$post->post_excerpt = '';
$excerpt = new PostExcerpt($post, [
'words' => 3,
'force' => false,
'read_more' => 'Custom more',
'always_add_read_more' => true,
]);
$this->assertMatchesRegularExpression('/this is super… <a href="http:\/\/example.org\/\?p=\d+" class="read-more">Custom more<\/a>/', (string) $excerpt);
// content with <!--more--> tag, force false
$post->post_content = 'this is super dooper<!--more--> trooper long words';
$excerpt = new PostExcerpt($post, [
'words' => 2,
'force' => false,
'read_more' => '',
]);
$this->assertEquals('this is super dooper', (string) $excerpt);
}
public function testShortcodesInExcerptFromContent()
{
add_shortcode('mythang', function ($text) {
return 'mythangy';
});
$pid = $this->factory->post->create([
'post_content' => 'jared [mythang]',
'post_excerpt' => '',
]);
$post = Timber::get_post($pid);
$this->assertEquals('jared mythangy', $post->excerpt());
remove_shortcode('mythang');
}
public function testShortcodesInExcerptFromContentWithMoreTag()
{
add_shortcode('duck', function ($text) {
return 'Quack!';
});
$pid = $this->factory->post->create([
'post_content' => 'jared [duck] <!--more--> joojoo',
'post_excerpt' => '',
]);
$post = Timber::get_post($pid);
$this->assertEquals(
sprintf('jared Quack! <a href="%s" class="read-more">Read More</a>', $post->link()),
(string) $post->excerpt()
);
remove_shortcode('duck');
}
public function testExcerptWithSpaceInMoreTag()
{
$pid = $this->factory->post->create([
'post_content' => 'Lauren is a duck, but a great duck let me tell you why <!--more--> Lauren is not a duck',
'post_excerpt' => '',
]);
$post = Timber::get_post($pid);
$excerpt = new PostExcerpt($post, [
'words' => 3,
'force' => true,
]);
$this->assertEquals(
'Lauren is a… <a href="' . $post->link() . '" class="read-more">Read More</a>',
(string) $excerpt
);
}
public function testExcerptWithMoreTagAndForcedLength()
{
$pid = $this->factory->post->create([
'post_content' => 'Lauren is a duck<!-- more--> Lauren is not a duck',
'post_excerpt' => '',
]);
$post = Timber::get_post($pid);
$this->assertEquals(
sprintf('Lauren is a duck <a href="%s" class="read-more">Read More</a>', $post->link()),
(string) $post->excerpt()
);
}
public function testExcerptWithCustomMoreTag()
{
$pid = $this->factory->post->create([
'post_content' => 'Eric is a polar bear <!-- more But what is Elaina? --> Lauren is not a duck',
'post_excerpt' => '',
]);
$post = Timber::get_post($pid);
$this->assertEquals(
'Eric is a polar bear <a href="' . $post->link() . '" class="read-more">But what is Elaina?</a>',
(string) $post->excerpt()
);
}
public function testExcerptWithCustomEnd()
{
$pid = $this->factory->post->create([
'post_content' => 'Lauren is a duck, but a great duck let me tell you why Lauren is a duck',
'post_excerpt' => '',
]);
$post = Timber::get_post($pid);
$excerpt = new PostExcerpt($post, [
'words' => 3,
'force' => true,
'read_more' => 'Read More',
'strip' => true,
'end' => ' ???',
]);
$this->assertEquals(
'Lauren is a ??? <a href="' . $post->link() . '" class="read-more">Read More</a>',
(string) $excerpt
);
}
public function testExcerptWithCustomStripTags()
{
$pid = $this->factory->post->create([
'post_content' => '<span>Even in the <a href="">world</a> of make-believe there have to be rules. The parts have to be consistent and belong together</span>',
]);
$post = Timber::get_post($pid);
$post->post_excerpt = '';
$excerpt = new PostExcerpt($post, [
'words' => 6,
'force' => true,
'read_more' => 'Read More',
'always_add_read_more' => true,
'strip' => '<span>',
]);
$this->assertEquals(
'<span>Even in the world of make-believe</span>… <a href="' . $post->link() . '" class="read-more">Read More</a>',
(string) $excerpt
);
}
/**
* When the excerpt is not smaller than the content itself, there should not be a read more
* link.
*
* @ticket #1345
*/
public function testPostContentWithShorterLengthThanExpectedExcerpt()
{
$post_id = $this->factory->post->create([
'post_content' => 'Let this be the content, albeit a very short one!',
'post_excerpt' => '',
]);
$post = Timber::get_post($post_id);
$excerpt = $post->excerpt([
'always_add_read_more' => false,
]);
$this->assertEquals('Let this be the content, albeit a very short one!', (string) $excerpt);
}
/**
* When the excerpt is not smaller than the content itself, there should not be a read more
* link.
*
* @ticket #1345
*/
public function testPostContentWithShorterLengthThanExpectedExcerptUsingFilter()
{
$this->add_filter_temporarily(
'timber/post/excerpt/defaults',
function ($defaults) {
$defaults['always_add_read_more'] = false;
return $defaults;
}
);
$post_id = $this->factory->post->create([
'post_content' => 'Let this be the content, albeit a very short one!',
'post_excerpt' => '',
]);
$post = Timber::get_post($post_id);
$excerpt = $post->excerpt();
$this->assertEquals('Let this be the content, albeit a very short one!', (string) $excerpt);
}
/**
* When always_add_end is used, the end character should be added as well as the as a read
* more link, even when always_add_read_more is false.
*/
public function testAlwaysAddEndOption()
{
$post_id = $this->factory->post->create([
'post_content' => 'Let this be the content, albeit a very short one!',
'post_excerpt' => '',
]);
$post = Timber::get_post($post_id);
$excerpt = $post->excerpt([
'always_add_end' => true,
'always_add_read_more' => false,
]);
$this->assertEquals(sprintf(
'Let this be the content, albeit a very short one!…',
$post->link()
), (string) $excerpt);
}
/**
* When always_add_end is used, the end character should be added as well as the as a read
* more link, even when always_add_read_more is false.
*/
public function testAlwaysAddEndOptionUsingFilter()
{
$this->add_filter_temporarily(
'timber/post/excerpt/defaults',
function ($defaults) {
$defaults['always_add_end'] = true;
return $defaults;
}
);
$post_id = $this->factory->post->create([
'post_content' => 'Let this be the content, albeit a very short one!',
'post_excerpt' => '',
]);
$post = Timber::get_post($post_id);
$excerpt = $post->excerpt();
$this->assertEquals('Let this be the content, albeit a very short one!…', (string) $excerpt);
}
}