Completed
Push — master ( 617ba6...7f5c80 )
by Mikael
07:52
created

CTextFilterTest::testMarkdownArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
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
         'purify',
22
         'titlefromh1',
23
     ];
24
25
26
27
     /**
28
      * Test.
29
      *
30
      * @return void
31
      */
32
    public function testTitleFromFirstH1()
33
    {
34
        $filter = new CTextFilter();
35
36
        $text = "";
37
        $res = $filter->parse($text, ["titlefromh1"]);
38
        $title = $res->frontmatter["title"];
39
        $this->assertNull($title, "Title should be null");
40
41
        $text = "<h1>My title</h1>";
42
        $exp = "My title";
43
        $res = $filter->parse($text, ["titlefromh1"]);
44
        $title = $res->frontmatter["title"];
45
        $this->assertEquals($exp, $title, "Title missmatch");
46
47
        $text = "<h1><a href=''>My title</a></h1>";
48
        $exp = "My title";
49
        $res = $filter->parse($text, ["titlefromh1"]);
50
        $title = $res->frontmatter["title"];
51
        $this->assertEquals($exp, $title, "Title missmatch");
52
53
        $text = "<h1 class=''>My title</h1>";
54
        $exp = "My title";
55
        $res = $filter->parse($text, ["titlefromh1"]);
56
        $title = $res->frontmatter["title"];
57
        $this->assertEquals($exp, $title, "Title missmatch");
58
    }
59
60
61
62
     /**
63
      * Test.
64
      *
65
      * @expectedException /Mos/TextFilter/Exception
66
      *
67
      * @return void
68
      */
69
    public function testJsonFrontMatterException()
70
    {
71
        $filter = new CTextFilter();
72
73
        $text = <<<EOD
74
{{{
75
76
}}}
77
EOD;
78
        $filter->parse($text, ["jsonfrontmatter"]);
79
    }
80
81
82
83
     /**
84
      * Test.
85
      *
86
      * @return void
87
      */
88
    public function testJsonFrontMatter()
89
    {
90
        $filter = new CTextFilter();
91
92
        $text = "";
93
        $res = $filter->parse($text, ["jsonfrontmatter"]);
94
        $this->assertNull($res->frontmatter, "Frontmatter should be null");
95
        $this->assertEmpty($res->text, "Text should be empty");
96
97
        $text = <<<EOD
98
{{{
99
}}}
100
101
EOD;
102
        $res = $filter->parse($text, ["jsonfrontmatter"]);
103
        $this->assertEmpty($res->frontmatter, "Frontmatter should be empty");
104
        $this->assertEmpty($res->text, "Text should be empty");
105
106
        $txt = "TEXT";
107
        $text = <<<EOD
108
{{{
109
{
110
    "key": "value"
111
}
112
}}}
113
$txt
114
EOD;
115
        $res = $filter->parse($text, ["jsonfrontmatter"]);
116
        $this->assertEquals(
117
            $res->frontmatter,
118
            [
119
                "key" => "value"
120
            ],
121
            "Frontmatter should be empty"
122
        );
123
        $this->assertEquals($txt, $res->text, "Text missmatch");
124
    }
125
126
127
128
    /**
129
     * Test.
130
     *
131
     * @expectedException /Mos/TextFilter/Exception
132
     *
133
     * @return void
134
     */
135
    public function testYamlFrontMatterException()
136
    {
137
        if (!function_exists("yaml_parse")) {
138
            return;
139
        }
140
141
        $filter = new CTextFilter();
142
143
        $text = <<<EOD
144
---
145
146
---
147
EOD;
148
        $filter->parse($text, ["yamlfrontmatter"]);
149
    }
150
151
152
153
    /**
154
     * Test.
155
     *
156
     * @return void
157
     */
158
    public function testYamlFrontMatter()
159
    {
160
        if (!function_exists("yaml_parse")) {
161
            return;
162
        }
163
164
        $filter = new CTextFilter();
165
166
        $text = "";
167
        $res = $filter->parse($text, ["yamlfrontmatter"]);
168
        $this->assertNull($res->frontmatter, "Frontmatter should be null");
169
        $this->assertEmpty($res->text, "Text should be empty");
170
171
        $text = <<<EOD
172
---
173
---
174
175
EOD;
176
        $res = $filter->parse($text, ["yamlfrontmatter"]);
177
        $this->assertEmpty($res->frontmatter, "Frontmatter should be empty");
178
        $this->assertEmpty($res->text, "Text should be empty");
179
180
        $txt = "TEXT";
181
        $text = <<<EOD
182
---
183
key: value
184
---
185
$txt
186
EOD;
187
        $res = $filter->parse($text, ["yamlfrontmatter"]);
188
        $this->assertEquals(
189
            $res->frontmatter,
190
            [
191
                "key" => "value"
192
            ],
193
            "Frontmatter not matching"
194
        );
195
        $this->assertEquals($txt, $res->text, "Text missmatch");
196
197
        $text = <<<EOD
198
---
199
key1: value1
200
key2: This is a long sentence.
201
---
202
My Article
203
=================================
204
205
This is an example on writing text and adding a YAML frontmatter.
206
207
EOD;
208
        $res = $filter->parse($text, ["yamlfrontmatter", "markdown"]);
209
        //var_dump($res);
210
        $this->assertEquals(
211
            $res->frontmatter,
212
            [
213
                "key1" => "value1",
214
                "key2" => "This is a long sentence."
215
            ],
216
            "Frontmatter not matching"
217
        );
218
        //$this->assertEquals($txt, $res->text, "Text missmatch");
219
    }
220
221
222
223
    /**
224
     * Test.
225
     *
226
     * @return void
227
     */
228
    public function testGetFilters()
229
    {
230
        $filter = new CTextFilter();
231
232
        $filters = $filter->getFilters();
233
        $res = array_diff($this->standardFilters, $filters);
234
        $this->assertTrue(empty($res), "Missmatch standard filters.");
235
    }
236
237
238
239
240
    /**
241
     * Test.
242
     *
243
     * @return void
244
     */
245
    public function testHasFilter()
246
    {
247
        $filter = new CTextFilter();
248
249
        $res = $filter->hasFilter("markdown");
250
        $this->assertTrue($res, "Missmatch has filters.");
251
    }
252
253
254
255
256
    /**
257
     * Test.
258
     *
259
     * @expectedException /Mos/TextFilter/Exception
260
     *
261
     * @return void
262
     */
263
    public function testHasFilterException()
264
    {
265
        $filter = new CTextFilter();
266
267
        $filter->hasFilter("NOT EXISTING");
268
    }
269
270
271
272
273
    /**
274
     * Test.
275
     *
276
     * @return void
277
     */
278
    public function testPurifier()
279
    {
280
        $filter = new CTextFilter();
281
282
        $text = "Header\n=========";
283
        $exp  = "<h1>Header</h1>\n";
284
        $res = $filter->parse($text, ["markdown", "purify"]);
285
        $this->assertEquals($exp, $res->text, "Purify failed");
286
    }
287
288
289
290
    /**
291
     * Test.
292
     *
293
     * @return void
294
     */
295 View Code Duplication
    public function testMarkdown()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
296
    {
297
        $filter = new CTextFilter();
298
299
        $html = "Header\n=========";
300
        $exp  = "<h1>Header</h1>\n";
301
        $res = $filter->doFilter($html, "markdown");
0 ignored issues
show
Deprecated Code introduced by
The method Mos\TextFilter\CTextFilter::doFilter() has been deprecated with message: deprecated since version 1.2 in favour of parse().

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
302
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
303
    }
304
305
306
307
    /**
308
     * Test.
309
     *
310
     * @return void
311
     */
312 View Code Duplication
    public function testMarkdownAndBBCode()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
313
    {
314
        $filter = new CTextFilter();
315
316
        $html = "Header[b]text[/b]\n=========";
317
        $exp  = "<h1>Header<strong>text</strong></h1>\n";
318
        $res = $filter->doFilter($html, "markdown, bbcode");
0 ignored issues
show
Deprecated Code introduced by
The method Mos\TextFilter\CTextFilter::doFilter() has been deprecated with message: deprecated since version 1.2 in favour of parse().

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
319
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
320
    }
321
322
323
324
    /**
325
     * Test.
326
     *
327
     * @return void
328
     */
329 View Code Duplication
    public function testMarkdownAndBBCodeAsArray()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
330
    {
331
        $filter = new CTextFilter();
332
333
        $html = "Header[b]text[/b]\n=========";
334
        $exp  = "<h1>Header<strong>text</strong></h1>\n";
335
        $res = $filter->doFilter($html, ["markdown", "bbcode"]);
0 ignored issues
show
Deprecated Code introduced by
The method Mos\TextFilter\CTextFilter::doFilter() has been deprecated with message: deprecated since version 1.2 in favour of parse().

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
336
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
337
    }
338
339
340
341
    /**
342
     * Test.
343
     *
344
     * @return void
345
     */
346 View Code Duplication
    public function testMarkdownArray()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
347
    {
348
        $filter = new CTextFilter();
349
350
        $html = "Header\n=========";
351
        $exp  = "<h1>Header</h1>\n";
352
        $res = $filter->doFilter($html, ["markdown"]);
0 ignored issues
show
Deprecated Code introduced by
The method Mos\TextFilter\CTextFilter::doFilter() has been deprecated with message: deprecated since version 1.2 in favour of parse().

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
353
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
354
    }
355
356
357
358
    /**
359
     * Test.
360
     *
361
     * @return void
362
     */
363 View Code Duplication
    public function testUppercase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
364
    {
365
        $filter = new CTextFilter();
366
367
        $html = "Header\n=========";
368
        $exp  = "<h1>Header</h1>\n";
369
        $res = $filter->doFilter($html, "MARKDOWN");
0 ignored issues
show
Deprecated Code introduced by
The method Mos\TextFilter\CTextFilter::doFilter() has been deprecated with message: deprecated since version 1.2 in favour of parse().

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
370
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
371
    }
372
373
374
375
    /**
376
     * Test.
377
     *
378
     * @return void
379
     */
380 View Code Duplication
    public function testBBCode()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
381
    {
382
        $filter = new CTextFilter();
383
384
        $html = "[b]text[/b]";
385
        $exp  = "<strong>text</strong>";
386
        $res = $filter->doFilter($html, "bbcode");
0 ignored issues
show
Deprecated Code introduced by
The method Mos\TextFilter\CTextFilter::doFilter() has been deprecated with message: deprecated since version 1.2 in favour of parse().

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
387
        $this->assertEquals($exp, $res, "BBCode [b] failed: '$res'");
388
    }
389
390
391
392
    /**
393
     * Test.
394
     *
395
     * @return void
396
     */
397 View Code Duplication
    public function testClickable()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
398
    {
399
        $filter = new CTextFilter();
400
401
        $html = "http://example.com/humans.txt";
402
        $exp  = "<a href='$html'>$html</a>";
403
        $res = $filter->doFilter($html, "clickable");
0 ignored issues
show
Deprecated Code introduced by
The method Mos\TextFilter\CTextFilter::doFilter() has been deprecated with message: deprecated since version 1.2 in favour of parse().

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
404
        $this->assertEquals($exp, $res, "clickable failed: '$res'");
405
    }
406
407
408
409
    /**
410
     * Test.
411
     *
412
     * @return void
413
     */
414 View Code Duplication
    public function testNl2Br()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
415
    {
416
        $filter = new CTextFilter();
417
418
        $html = "hej\nhej";
419
        $exp  = "hej<br />\nhej";
420
        $res = $filter->doFilter($html, "nl2br");
0 ignored issues
show
Deprecated Code introduced by
The method Mos\TextFilter\CTextFilter::doFilter() has been deprecated with message: deprecated since version 1.2 in favour of parse().

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
421
        $this->assertEquals($exp, $res, "nl2br failed: '$res'");
422
    }
423
424
425
426
    /**
427
     * Test.
428
     *
429
     * @return void
430
     */
431
    public function testShortCodeFigure()
432
    {
433
        $filter = new CTextFilter();
434
435
        $src = "/img/me.png";
436
        $caption = "This is me.";
437
        
438
        $html = <<<EOD
439
[FIGURE src=$src caption="$caption"]
440
EOD;
441
442
        $exp  = <<<EOD
443
<figure class='figure'>
444
<a href='$src'><img src='$src' alt='$caption'/></a>
445
<figcaption markdown=1>$caption</figcaption>
446
</figure>
447
EOD;
448
        $res = $filter->doFilter($html, "shortcode");
0 ignored issues
show
Deprecated Code introduced by
The method Mos\TextFilter\CTextFilter::doFilter() has been deprecated with message: deprecated since version 1.2 in favour of parse().

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
449
        $this->assertEquals($exp, $res, "shortcode failed: '$res'");
450
    }
451
452
453
454
    /**
455
     * Test.
456
     *
457
     * @expectedException Exception
458
     *
459
     * @return void
460
     */
461
    public function testDoItException()
462
    {
463
        $filter = new CTextFilter();
464
        $filter->doFilter("void", "no-such-filter");
0 ignored issues
show
Deprecated Code introduced by
The method Mos\TextFilter\CTextFilter::doFilter() has been deprecated with message: deprecated since version 1.2 in favour of parse().

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
465
    }
466
}
467