Completed
Push — master ( d6728d...9d5144 )
by Mikael
02:29
created

CTextFilterTest::testUppercase()   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
        $text = <<<EOD
60
{{{
61
{
62
    "title": "JSON title"
63
}
64
}}}
65
<h1 class=''>My title</h1>
66
EOD;
67
        $exp = "JSON title";
68
        $res = $filter->parse($text, ["titlefromh1", "jsonfrontmatter"]);
69
        $title = $res->frontmatter["title"];
70
        $this->assertEquals($exp, $title, "Title missmatch");
71
72
        $exp = "JSON title";
73
        $res = $filter->parse($text, ["jsonfrontmatter", "titlefromh1"]);
74
        $title = $res->frontmatter["title"];
75
        $this->assertEquals($exp, $title, "Title missmatch");
76
77
        $text = <<<EOD
78
{{{
79
{
80
    "title": "JSON title"
81
}
82
}}}
83
My title
84
=================================
85
86
This is the index page.
87
EOD;
88
        $exp = "JSON title";
89
        $res = $filter->parse($text, ["jsonfrontmatter", "markdown", "titlefromh1"]);
90
        $title = $res->frontmatter["title"];
91
        $this->assertEquals($exp, $title, "Title missmatch");
92
93
        $text = <<<EOD
94
{{{
95
{
96
    "title-no": "JSON title"
97
}
98
}}}
99
My title
100
=================================
101
102
This is the index page.
103
EOD;
104
        $exp = "My title";
105
        $res = $filter->parse($text, ["jsonfrontmatter", "markdown", "titlefromh1"]);
106
        $title = $res->frontmatter["title"];
107
        $this->assertEquals($exp, $title, "Title missmatch");
108
109
    }
110
111
112
113
     /**
114
      * Test.
115
      *
116
      * @expectedException /Mos/TextFilter/Exception
117
      *
118
      * @return void
119
      */
120
    public function testJsonFrontMatterException()
121
    {
122
        $filter = new CTextFilter();
123
124
        $text = <<<EOD
125
{{{
126
127
}}}
128
EOD;
129
        $filter->parse($text, ["jsonfrontmatter"]);
130
    }
131
132
133
134
     /**
135
      * Test.
136
      *
137
      * @return void
138
      */
139
    public function testJsonFrontMatter()
140
    {
141
        $filter = new CTextFilter();
142
143
        $text = "";
144
        $res = $filter->parse($text, ["jsonfrontmatter"]);
145
        $this->assertNull($res->frontmatter, "Frontmatter should be null");
146
        $this->assertEmpty($res->text, "Text should be empty");
147
148
        $text = <<<EOD
149
{{{
150
}}}
151
152
EOD;
153
        $res = $filter->parse($text, ["jsonfrontmatter"]);
154
        $this->assertEmpty($res->frontmatter, "Frontmatter should be empty");
155
        $this->assertEmpty($res->text, "Text should be empty");
156
157
        $txt = "TEXT";
158
        $text = <<<EOD
159
{{{
160
{
161
    "key": "value"
162
}
163
}}}
164
$txt
165
EOD;
166
        $res = $filter->parse($text, ["jsonfrontmatter"]);
167
        $this->assertEquals(
168
            $res->frontmatter,
169
            [
170
                "key" => "value"
171
            ],
172
            "Frontmatter should be empty"
173
        );
174
        $this->assertEquals($txt, $res->text, "Text missmatch");
175
    }
176
177
178
179
    /**
180
     * Test.
181
     *
182
     * @expectedException /Mos/TextFilter/Exception
183
     *
184
     * @return void
185
     */
186
    public function testYamlFrontMatterException()
187
    {
188
        if (!function_exists("yaml_parse")) {
189
            return;
190
        }
191
192
        $filter = new CTextFilter();
193
194
        $text = <<<EOD
195
---
196
197
---
198
EOD;
199
        $filter->parse($text, ["yamlfrontmatter"]);
200
    }
201
202
203
204
    /**
205
     * Test.
206
     *
207
     * @return void
208
     */
209
    public function testYamlFrontMatter()
210
    {
211
        if (!function_exists("yaml_parse")) {
212
            return;
213
        }
214
215
        $filter = new CTextFilter();
216
217
        $text = "";
218
        $res = $filter->parse($text, ["yamlfrontmatter"]);
219
        $this->assertNull($res->frontmatter, "Frontmatter should be null");
220
        $this->assertEmpty($res->text, "Text should be empty");
221
222
        $text = <<<EOD
223
---
224
---
225
226
EOD;
227
        $res = $filter->parse($text, ["yamlfrontmatter"]);
228
        $this->assertEmpty($res->frontmatter, "Frontmatter should be empty");
229
        $this->assertEmpty($res->text, "Text should be empty");
230
231
        $txt = "TEXT";
232
        $text = <<<EOD
233
---
234
key: value
235
---
236
$txt
237
EOD;
238
        $res = $filter->parse($text, ["yamlfrontmatter"]);
239
        $this->assertEquals(
240
            $res->frontmatter,
241
            [
242
                "key" => "value"
243
            ],
244
            "Frontmatter not matching"
245
        );
246
        $this->assertEquals($txt, $res->text, "Text missmatch");
247
248
        $text = <<<EOD
249
---
250
key1: value1
251
key2: This is a long sentence.
252
---
253
My Article
254
=================================
255
256
This is an example on writing text and adding a YAML frontmatter.
257
258
Subheading
259
---------------------------------
260
261
More text.
262
263
EOD;
264
        $res = $filter->parse($text, ["yamlfrontmatter", "markdown"]);
265
        $this->assertEquals(
266
            $res->frontmatter,
267
            [
268
                "key1" => "value1",
269
                "key2" => "This is a long sentence."
270
            ],
271
            "Frontmatter not matching"
272
        );
273
274
        $text = <<<EOD
275
My Article
276
=================================
277
278
This is an example on writing text and adding a YAML frontmatter.
279
280
Subheading
281
---------------------------------
282
283
More text.
284
285
EOD;
286
        $res = $filter->parse($text, ["yamlfrontmatter", "markdown"]);
287
        $this->assertEmpty($res->frontmatter, "Frontmatter should be empty");
288
    }
289
290
291
292
    /**
293
     * Test.
294
     *
295
     * @return void
296
     */
297
    public function testGetFilters()
298
    {
299
        $filter = new CTextFilter();
300
301
        $filters = $filter->getFilters();
302
        $res = array_diff($this->standardFilters, $filters);
303
        $this->assertTrue(empty($res), "Missmatch standard filters.");
304
    }
305
306
307
308
309
    /**
310
     * Test.
311
     *
312
     * @return void
313
     */
314
    public function testHasFilter()
315
    {
316
        $filter = new CTextFilter();
317
318
        $res = $filter->hasFilter("markdown");
319
        $this->assertTrue($res, "Missmatch has filters.");
320
    }
321
322
323
324
325
    /**
326
     * Test.
327
     *
328
     * @expectedException /Mos/TextFilter/Exception
329
     *
330
     * @return void
331
     */
332
    public function testHasFilterException()
333
    {
334
        $filter = new CTextFilter();
335
336
        $filter->hasFilter("NOT EXISTING");
337
    }
338
339
340
341
342
    /**
343
     * Test.
344
     *
345
     * @return void
346
     */
347
    public function testPurifier()
348
    {
349
        $filter = new CTextFilter();
350
351
        $text = "Header\n=========";
352
        $exp  = "<h1>Header</h1>\n";
353
        $res = $filter->parse($text, ["markdown", "purify"]);
354
        $this->assertEquals($exp, $res->text, "Purify failed");
355
    }
356
357
358
359
    /**
360
     * Test.
361
     *
362
     * @return void
363
     */
364 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...
365
    {
366
        $filter = new CTextFilter();
367
368
        $html = "Header\n=========";
369
        $exp  = "<h1>Header</h1>\n";
370
        $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...
371
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
372
    }
373
374
375
376
    /**
377
     * Test.
378
     *
379
     * @return void
380
     */
381 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...
382
    {
383
        $filter = new CTextFilter();
384
385
        $html = "Header[b]text[/b]\n=========";
386
        $exp  = "<h1>Header<strong>text</strong></h1>\n";
387
        $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...
388
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
389
    }
390
391
392
393
    /**
394
     * Test.
395
     *
396
     * @return void
397
     */
398 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...
399
    {
400
        $filter = new CTextFilter();
401
402
        $html = "Header[b]text[/b]\n=========";
403
        $exp  = "<h1>Header<strong>text</strong></h1>\n";
404
        $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...
405
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
406
    }
407
408
409
410
    /**
411
     * Test.
412
     *
413
     * @return void
414
     */
415 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...
416
    {
417
        $filter = new CTextFilter();
418
419
        $html = "Header\n=========";
420
        $exp  = "<h1>Header</h1>\n";
421
        $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...
422
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
423
    }
424
425
426
427
    /**
428
     * Test.
429
     *
430
     * @return void
431
     */
432 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...
433
    {
434
        $filter = new CTextFilter();
435
436
        $html = "Header\n=========";
437
        $exp  = "<h1>Header</h1>\n";
438
        $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...
439
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
440
    }
441
442
443
444
    /**
445
     * Test.
446
     *
447
     * @return void
448
     */
449 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...
450
    {
451
        $filter = new CTextFilter();
452
453
        $html = "[b]text[/b]";
454
        $exp  = "<strong>text</strong>";
455
        $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...
456
        $this->assertEquals($exp, $res, "BBCode [b] failed: '$res'");
457
    }
458
459
460
461
    /**
462
     * Test.
463
     *
464
     * @return void
465
     */
466 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...
467
    {
468
        $filter = new CTextFilter();
469
470
        $html = "http://example.com/humans.txt";
471
        $exp  = "<a href='$html'>$html</a>";
472
        $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...
473
        $this->assertEquals($exp, $res, "clickable failed: '$res'");
474
    }
475
476
477
478
    /**
479
     * Test.
480
     *
481
     * @return void
482
     */
483 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...
484
    {
485
        $filter = new CTextFilter();
486
487
        $html = "hej\nhej";
488
        $exp  = "hej<br />\nhej";
489
        $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...
490
        $this->assertEquals($exp, $res, "nl2br failed: '$res'");
491
    }
492
493
494
495
    /**
496
     * Test.
497
     *
498
     * @return void
499
     */
500
    public function testShortCodeFigure()
501
    {
502
        $filter = new CTextFilter();
503
504
        $src = "/img/me.png";
505
        $caption = "This is me.";
506
        
507
        $html = <<<EOD
508
[FIGURE src=$src caption="$caption"]
509
EOD;
510
511
        $exp  = <<<EOD
512
<figure class='figure'>
513
<a href='$src'><img src='$src' alt='$caption'/></a>
514
<figcaption markdown=1>$caption</figcaption>
515
</figure>
516
EOD;
517
        $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...
518
        $this->assertEquals($exp, $res, "shortcode failed: '$res'");
519
    }
520
521
522
523
    /**
524
     * Test.
525
     *
526
     * @expectedException Exception
527
     *
528
     * @return void
529
     */
530
    public function testDoItException()
531
    {
532
        $filter = new CTextFilter();
533
        $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...
534
    }
535
}
536