1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mos\TextFilter; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A testclass |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class CTextFilterTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Supported filters. |
13
|
|
|
*/ |
14
|
|
|
private $standardFilters = [ |
15
|
|
|
'yamlfrontmatter', |
16
|
|
|
'bbcode', |
17
|
|
|
'clickable', |
18
|
|
|
'markdown', |
19
|
|
|
'nl2br', |
20
|
|
|
'shortcode', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test. |
27
|
|
|
* |
28
|
|
|
* @expectedException /Mos/TextFilter/Exception |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function testJsonFrontMatterException() |
33
|
|
|
{ |
34
|
|
|
$filter = new CTextFilter(); |
35
|
|
|
|
36
|
|
|
$text = <<<EOD |
37
|
|
|
{{{ |
38
|
|
|
|
39
|
|
|
}}} |
40
|
|
|
EOD; |
41
|
|
|
$filter->parse($text, ["jsonfrontmatter"]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test. |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function testJsonFrontMatter() |
52
|
|
|
{ |
53
|
|
|
$filter = new CTextFilter(); |
54
|
|
|
|
55
|
|
|
$text = ""; |
56
|
|
|
$res = $filter->parse($text, ["jsonfrontmatter"]); |
57
|
|
|
$this->assertNull($res->frontmatter, "Frontmatter should be null"); |
58
|
|
|
$this->assertEmpty($res->text, "Text should be empty"); |
59
|
|
|
|
60
|
|
|
$text = <<<EOD |
61
|
|
|
{{{ |
62
|
|
|
}}} |
63
|
|
|
|
64
|
|
|
EOD; |
65
|
|
|
$res = $filter->parse($text, ["jsonfrontmatter"]); |
66
|
|
|
$this->assertEmpty($res->frontmatter, "Frontmatter should be empty"); |
67
|
|
|
$this->assertEmpty($res->text, "Text should be empty"); |
68
|
|
|
|
69
|
|
|
$txt = "TEXT"; |
70
|
|
|
$text = <<<EOD |
71
|
|
|
{{{ |
72
|
|
|
{ |
73
|
|
|
"key": "value" |
74
|
|
|
} |
75
|
|
|
}}} |
76
|
|
|
$txt |
77
|
|
|
EOD; |
78
|
|
|
$res = $filter->parse($text, ["jsonfrontmatter"]); |
79
|
|
|
$this->assertEquals( |
80
|
|
|
$res->frontmatter, |
81
|
|
|
[ |
82
|
|
|
"key" => "value" |
83
|
|
|
], |
84
|
|
|
"Frontmatter should be empty" |
85
|
|
|
); |
86
|
|
|
$this->assertEquals($txt, $res->text, "Text missmatch"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test. |
93
|
|
|
* |
94
|
|
|
* @expectedException /Mos/TextFilter/Exception |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function testYamlFrontMatterException() |
99
|
|
|
{ |
100
|
|
|
$filter = new CTextFilter(); |
101
|
|
|
|
102
|
|
|
$text = <<<EOD |
103
|
|
|
--- |
104
|
|
|
|
105
|
|
|
--- |
106
|
|
|
EOD; |
107
|
|
|
$filter->parse($text, ["yamlfrontmatter"]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Test. |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function testYamlFrontMatter() |
118
|
|
|
{ |
119
|
|
|
$filter = new CTextFilter(); |
120
|
|
|
|
121
|
|
|
$text = ""; |
122
|
|
|
$res = $filter->parse($text, ["yamlfrontmatter"]); |
123
|
|
|
$this->assertNull($res->frontmatter, "Frontmatter should be null"); |
124
|
|
|
$this->assertEmpty($res->text, "Text should be empty"); |
125
|
|
|
|
126
|
|
|
$text = <<<EOD |
127
|
|
|
--- |
128
|
|
|
--- |
129
|
|
|
|
130
|
|
|
EOD; |
131
|
|
|
$res = $filter->parse($text, ["yamlfrontmatter"]); |
132
|
|
|
$this->assertEmpty($res->frontmatter, "Frontmatter should be empty"); |
133
|
|
|
$this->assertEmpty($res->text, "Text should be empty"); |
134
|
|
|
|
135
|
|
|
$txt = "TEXT"; |
136
|
|
|
$text = <<<EOD |
137
|
|
|
--- |
138
|
|
|
key: value |
139
|
|
|
--- |
140
|
|
|
$txt |
141
|
|
|
EOD; |
142
|
|
|
$res = $filter->parse($text, ["yamlfrontmatter"]); |
143
|
|
|
$this->assertEquals( |
144
|
|
|
$res->frontmatter, |
145
|
|
|
[ |
146
|
|
|
"key" => "value" |
147
|
|
|
], |
148
|
|
|
"Frontmatter not matching" |
149
|
|
|
); |
150
|
|
|
$this->assertEquals($txt, $res->text, "Text missmatch"); |
151
|
|
|
|
152
|
|
|
$text = <<<EOD |
153
|
|
|
--- |
154
|
|
|
key1: value1 |
155
|
|
|
key2: This is a long sentence. |
156
|
|
|
--- |
157
|
|
|
My Article |
158
|
|
|
================================= |
159
|
|
|
|
160
|
|
|
This is an example on writing text and adding a YAML frontmatter. |
161
|
|
|
|
162
|
|
|
EOD; |
163
|
|
|
$res = $filter->parse($text, ["yamlfrontmatter", "markdown"]); |
164
|
|
|
//var_dump($res); |
165
|
|
|
$this->assertEquals( |
166
|
|
|
$res->frontmatter, |
167
|
|
|
[ |
168
|
|
|
"key1" => "value1", |
169
|
|
|
"key2" => "This is a long sentence." |
170
|
|
|
], |
171
|
|
|
"Frontmatter not matching" |
172
|
|
|
); |
173
|
|
|
//$this->assertEquals($txt, $res->text, "Text missmatch"); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Test. |
180
|
|
|
* |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
|
|
public function testGetFilters() |
184
|
|
|
{ |
185
|
|
|
$filter = new CTextFilter(); |
186
|
|
|
|
187
|
|
|
$filters = $filter->getFilters(); |
188
|
|
|
$res = array_diff($this->standardFilters, $filters); |
189
|
|
|
$this->assertTrue(empty($res), "Missmatch standard filters."); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
|
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Test. |
197
|
|
|
* |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
public function testHasFilter() |
201
|
|
|
{ |
202
|
|
|
$filter = new CTextFilter(); |
203
|
|
|
|
204
|
|
|
$res = $filter->hasFilter("markdown"); |
205
|
|
|
$this->assertTrue($res, "Missmatch has filters."); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Test. |
213
|
|
|
* |
214
|
|
|
* @expectedException /Mos/TextFilter/Exception |
215
|
|
|
* |
216
|
|
|
* @return void |
217
|
|
|
*/ |
218
|
|
|
public function testHasFilterException() |
219
|
|
|
{ |
220
|
|
|
$filter = new CTextFilter(); |
221
|
|
|
|
222
|
|
|
$filter->hasFilter("NOT EXISTING"); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Test. |
230
|
|
|
* |
231
|
|
|
* @return void |
232
|
|
|
*/ |
233
|
|
|
public function testPurifier() |
234
|
|
|
{ |
235
|
|
|
$filter = new CTextFilter(); |
236
|
|
|
|
237
|
|
|
$text = "Header\n========="; |
238
|
|
|
$exp = "<h1>Header</h1>\n"; |
239
|
|
|
$res = $filter->parse($text, ["markdown", "purify"]); |
240
|
|
|
$this->assertEquals($exp, $res->text, "Purify failed"); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Test. |
247
|
|
|
* |
248
|
|
|
* @return void |
249
|
|
|
*/ |
250
|
|
View Code Duplication |
public function testMarkdown() |
|
|
|
|
251
|
|
|
{ |
252
|
|
|
$filter = new CTextFilter(); |
253
|
|
|
|
254
|
|
|
$html = "Header\n========="; |
255
|
|
|
$exp = "<h1>Header</h1>\n"; |
256
|
|
|
$res = $filter->doFilter($html, "markdown"); |
|
|
|
|
257
|
|
|
$this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'"); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Test. |
264
|
|
|
* |
265
|
|
|
* @return void |
266
|
|
|
*/ |
267
|
|
View Code Duplication |
public function testMarkdownAndBBCode() |
|
|
|
|
268
|
|
|
{ |
269
|
|
|
$filter = new CTextFilter(); |
270
|
|
|
|
271
|
|
|
$html = "Header[b]text[/b]\n========="; |
272
|
|
|
$exp = "<h1>Header<strong>text</strong></h1>\n"; |
273
|
|
|
$res = $filter->doFilter($html, "markdown, bbcode"); |
|
|
|
|
274
|
|
|
$this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'"); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Test. |
281
|
|
|
* |
282
|
|
|
* @return void |
283
|
|
|
*/ |
284
|
|
View Code Duplication |
public function testMarkdownAndBBCodeAsArray() |
|
|
|
|
285
|
|
|
{ |
286
|
|
|
$filter = new CTextFilter(); |
287
|
|
|
|
288
|
|
|
$html = "Header[b]text[/b]\n========="; |
289
|
|
|
$exp = "<h1>Header<strong>text</strong></h1>\n"; |
290
|
|
|
$res = $filter->doFilter($html, ["markdown", "bbcode"]); |
|
|
|
|
291
|
|
|
$this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'"); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Test. |
298
|
|
|
* |
299
|
|
|
* @return void |
300
|
|
|
*/ |
301
|
|
View Code Duplication |
public function testMarkdownArray() |
|
|
|
|
302
|
|
|
{ |
303
|
|
|
$filter = new CTextFilter(); |
304
|
|
|
|
305
|
|
|
$html = "Header\n========="; |
306
|
|
|
$exp = "<h1>Header</h1>\n"; |
307
|
|
|
$res = $filter->doFilter($html, ["markdown"]); |
|
|
|
|
308
|
|
|
$this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'"); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
|
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Test. |
315
|
|
|
* |
316
|
|
|
* @return void |
317
|
|
|
*/ |
318
|
|
View Code Duplication |
public function testUppercase() |
|
|
|
|
319
|
|
|
{ |
320
|
|
|
$filter = new CTextFilter(); |
321
|
|
|
|
322
|
|
|
$html = "Header\n========="; |
323
|
|
|
$exp = "<h1>Header</h1>\n"; |
324
|
|
|
$res = $filter->doFilter($html, "MARKDOWN"); |
|
|
|
|
325
|
|
|
$this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'"); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Test. |
332
|
|
|
* |
333
|
|
|
* @return void |
334
|
|
|
*/ |
335
|
|
View Code Duplication |
public function testBBCode() |
|
|
|
|
336
|
|
|
{ |
337
|
|
|
$filter = new CTextFilter(); |
338
|
|
|
|
339
|
|
|
$html = "[b]text[/b]"; |
340
|
|
|
$exp = "<strong>text</strong>"; |
341
|
|
|
$res = $filter->doFilter($html, "bbcode"); |
|
|
|
|
342
|
|
|
$this->assertEquals($exp, $res, "BBCode [b] failed: '$res'"); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
|
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Test. |
349
|
|
|
* |
350
|
|
|
* @return void |
351
|
|
|
*/ |
352
|
|
View Code Duplication |
public function testClickable() |
|
|
|
|
353
|
|
|
{ |
354
|
|
|
$filter = new CTextFilter(); |
355
|
|
|
|
356
|
|
|
$html = "http://example.com/humans.txt"; |
357
|
|
|
$exp = "<a href='$html'>$html</a>"; |
358
|
|
|
$res = $filter->doFilter($html, "clickable"); |
|
|
|
|
359
|
|
|
$this->assertEquals($exp, $res, "clickable failed: '$res'"); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
|
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Test. |
366
|
|
|
* |
367
|
|
|
* @return void |
368
|
|
|
*/ |
369
|
|
View Code Duplication |
public function testNl2Br() |
|
|
|
|
370
|
|
|
{ |
371
|
|
|
$filter = new CTextFilter(); |
372
|
|
|
|
373
|
|
|
$html = "hej\nhej"; |
374
|
|
|
$exp = "hej<br />\nhej"; |
375
|
|
|
$res = $filter->doFilter($html, "nl2br"); |
|
|
|
|
376
|
|
|
$this->assertEquals($exp, $res, "nl2br failed: '$res'"); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
|
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Test. |
383
|
|
|
* |
384
|
|
|
* @return void |
385
|
|
|
*/ |
386
|
|
|
public function testShortCodeFigure() |
387
|
|
|
{ |
388
|
|
|
$filter = new CTextFilter(); |
389
|
|
|
|
390
|
|
|
$src = "/img/me.png"; |
391
|
|
|
$caption = "This is me."; |
392
|
|
|
|
393
|
|
|
$html = <<<EOD |
394
|
|
|
[FIGURE src=$src caption="$caption"] |
395
|
|
|
EOD; |
396
|
|
|
|
397
|
|
|
$exp = <<<EOD |
398
|
|
|
<figure class='figure'> |
399
|
|
|
<a href='$src'><img src='$src' alt='$caption'/></a> |
400
|
|
|
<figcaption markdown=1>$caption</figcaption> |
401
|
|
|
</figure> |
402
|
|
|
EOD; |
403
|
|
|
$res = $filter->doFilter($html, "shortcode"); |
|
|
|
|
404
|
|
|
$this->assertEquals($exp, $res, "shortcode failed: '$res'"); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
|
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Test. |
411
|
|
|
* |
412
|
|
|
* @expectedException Exception |
413
|
|
|
* |
414
|
|
|
* @return void |
415
|
|
|
*/ |
416
|
|
|
public function testDoItException() |
417
|
|
|
{ |
418
|
|
|
$filter = new CTextFilter(); |
419
|
|
|
$filter->doFilter("void", "no-such-filter"); |
|
|
|
|
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.