Completed
Push — master ( 9d5144...4f67af )
by Mikael
03:33
created

CTextFilterTest::testSyntaxHighlightGeshiMethod()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
rs 8.8571
cc 1
eloc 20
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
         'geshi',
20
         'nl2br',
21
         'shortcode',
22
         'purify',
23
         'titlefromh1',
24
     ];
25
26
27
28
     /**
29
      * Test.
30
      *
31
      * @return void
32
      */
33
    public function testSyntaxHighlightGeshiMethod()
34
    {
35
        $filter = new CTextFilter();
36
37
        $text = "";
38
        $exp  = '<pre class="text geshi">&nbsp;</pre>';
39
        $res = $filter->parse($text, ["geshi"]);
40
        $this->assertEquals($exp, $res->text, "Geshi did not match");
41
42
        $text = <<<'EOD'
43
$php = "hi";
44
EOD;
45
        $exp  = '<pre class="text geshi">$php = &quot;hi&quot;;</pre>';
46
        $res = $filter->parse($text, ["geshi"]);
47
        $this->assertEquals($exp, $res->text, "Geshi did not match");
48
49
        $text = <<<'EOD'
50
$php = "hi";
51
EOD;
52
53
        // @codingStandardsIgnoreStart
54
        $exp = <<<'EOD'
55
<pre class="php geshi"><span class="re0">$php</span> <span class="sy0">=</span> <span class="st0">&quot;hi&quot;</span><span class="sy0">;</span></pre>
56
EOD;
57
        // @codingStandardsIgnoreEnd
58
        $res = $filter->syntaxHighlightGeSHi($text, "php");
59
        $this->assertEquals($exp, $res, "Geshi did not match");
60
    }
61
62
63
64
    /**
65
     * Test.
66
     *
67
     * @return void
68
     */
69
    public function testSyntaxHighlightGeshiShortCode()
70
    {
71
        $filter = new CTextFilter();
72
73
        $text = <<<'EOD'
74
```text
75
```
76
77
EOD;
78
        $exp = <<<'EOD'
79
<pre class="text geshi">&nbsp;</pre>
80
EOD;
81
        $res = $filter->parse($text, ["shortcode"]);
82
        $this->assertEquals($exp, $res->text, "Geshi did not match");
83
84
        $text = <<<'EOD'
85
```
86
```
87
88
EOD;
89
        $exp = <<<'EOD'
90
<pre class="text geshi">&nbsp;</pre>
91
EOD;
92
        $res = $filter->parse($text, ["shortcode"]);
93
        $this->assertEquals($exp, $res->text, "Geshi did not match");
94
95
        $text = <<<'EOD'
96
```text
97
$php = "hi";
98
```
99
100
EOD;
101
        $exp = <<<'EOD'
102
<pre class="text geshi">$php = &quot;hi&quot;;
103
&nbsp;</pre>
104
EOD;
105
        $res = $filter->parse($text, ["shortcode"]);
106
        $this->assertEquals($exp, $res->text, "Geshi did not match");
107
108
        $text = <<<'EOD'
109
```php
110
$php = "hi";
111
```
112
113
EOD;
114
        // @codingStandardsIgnoreStart
115
        $exp = <<<'EOD'
116
<pre class="php geshi"><span class="re0">$php</span> <span class="sy0">=</span> <span class="st0">&quot;hi&quot;</span><span class="sy0">;</span>
117
&nbsp;</pre>
118
EOD;
119
        // @codingStandardsIgnoreEnd
120
        $res = $filter->parse($text, ["shortcode"]);
121
        $this->assertEquals($exp, $res->text, "Geshi did not match");
122
123
        $text = <<<'EOD'
0 ignored issues
show
Unused Code introduced by
$text is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
124
```php
125
$php = "hi";
126
```
127
128
EOD;
129
    }
130
131
132
133
     /**
134
      * Test.
135
      *
136
      * @return void
137
      */
138
    public function testTitleFromFirstH1()
139
    {
140
        $filter = new CTextFilter();
141
142
        $text = "";
143
        $res = $filter->parse($text, ["titlefromh1"]);
144
        $title = $res->frontmatter["title"];
145
        $this->assertNull($title, "Title should be null");
146
147
        $text = "<h1>My title</h1>";
148
        $exp = "My title";
149
        $res = $filter->parse($text, ["titlefromh1"]);
150
        $title = $res->frontmatter["title"];
151
        $this->assertEquals($exp, $title, "Title missmatch");
152
153
        $text = "<h1><a href=''>My title</a></h1>";
154
        $exp = "My title";
155
        $res = $filter->parse($text, ["titlefromh1"]);
156
        $title = $res->frontmatter["title"];
157
        $this->assertEquals($exp, $title, "Title missmatch");
158
159
        $text = "<h1 class=''>My title</h1>";
160
        $exp = "My title";
161
        $res = $filter->parse($text, ["titlefromh1"]);
162
        $title = $res->frontmatter["title"];
163
        $this->assertEquals($exp, $title, "Title missmatch");
164
165
        $text = <<<EOD
166
{{{
167
{
168
    "title": "JSON title"
169
}
170
}}}
171
<h1 class=''>My title</h1>
172
EOD;
173
        $exp = "JSON title";
174
        $res = $filter->parse($text, ["titlefromh1", "jsonfrontmatter"]);
175
        $title = $res->frontmatter["title"];
176
        $this->assertEquals($exp, $title, "Title missmatch");
177
178
        $exp = "JSON title";
179
        $res = $filter->parse($text, ["jsonfrontmatter", "titlefromh1"]);
180
        $title = $res->frontmatter["title"];
181
        $this->assertEquals($exp, $title, "Title missmatch");
182
183
        $text = <<<EOD
184
{{{
185
{
186
    "title": "JSON title"
187
}
188
}}}
189
My title
190
=================================
191
192
This is the index page.
193
EOD;
194
        $exp = "JSON title";
195
        $res = $filter->parse($text, ["jsonfrontmatter", "markdown", "titlefromh1"]);
196
        $title = $res->frontmatter["title"];
197
        $this->assertEquals($exp, $title, "Title missmatch");
198
199
        $text = <<<EOD
200
{{{
201
{
202
    "title-no": "JSON title"
203
}
204
}}}
205
My title
206
=================================
207
208
This is the index page.
209
EOD;
210
        $exp = "My title";
211
        $res = $filter->parse($text, ["jsonfrontmatter", "markdown", "titlefromh1"]);
212
        $title = $res->frontmatter["title"];
213
        $this->assertEquals($exp, $title, "Title missmatch");
214
215
    }
216
217
218
219
     /**
220
      * Test.
221
      *
222
      * @expectedException /Mos/TextFilter/Exception
223
      *
224
      * @return void
225
      */
226
    public function testJsonFrontMatterException()
227
    {
228
        $filter = new CTextFilter();
229
230
        $text = <<<EOD
231
{{{
232
233
}}}
234
EOD;
235
        $filter->parse($text, ["jsonfrontmatter"]);
236
    }
237
238
239
240
     /**
241
      * Test.
242
      *
243
      * @return void
244
      */
245
    public function testJsonFrontMatter()
246
    {
247
        $filter = new CTextFilter();
248
249
        $text = "";
250
        $res = $filter->parse($text, ["jsonfrontmatter"]);
251
        $this->assertNull($res->frontmatter, "Frontmatter should be null");
252
        $this->assertEmpty($res->text, "Text should be empty");
253
254
        $text = <<<EOD
255
{{{
256
}}}
257
258
EOD;
259
        $res = $filter->parse($text, ["jsonfrontmatter"]);
260
        $this->assertEmpty($res->frontmatter, "Frontmatter should be empty");
261
        $this->assertEmpty($res->text, "Text should be empty");
262
263
        $txt = "TEXT";
264
        $text = <<<EOD
265
{{{
266
{
267
    "key": "value"
268
}
269
}}}
270
$txt
271
EOD;
272
        $res = $filter->parse($text, ["jsonfrontmatter"]);
273
        $this->assertEquals(
274
            $res->frontmatter,
275
            [
276
                "key" => "value"
277
            ],
278
            "Frontmatter should be empty"
279
        );
280
        $this->assertEquals($txt, $res->text, "Text missmatch");
281
    }
282
283
284
285
    /**
286
     * Test.
287
     *
288
     * @expectedException /Mos/TextFilter/Exception
289
     *
290
     * @return void
291
     */
292
    public function testYamlFrontMatterException()
293
    {
294
        if (!function_exists("yaml_parse")) {
295
            return;
296
        }
297
298
        $filter = new CTextFilter();
299
300
        $text = <<<EOD
301
---
302
303
---
304
EOD;
305
        $filter->parse($text, ["yamlfrontmatter"]);
306
    }
307
308
309
310
    /**
311
     * Test.
312
     *
313
     * @return void
314
     */
315
    public function testYamlFrontMatter()
316
    {
317
        if (!function_exists("yaml_parse")) {
318
            return;
319
        }
320
321
        $filter = new CTextFilter();
322
323
        $text = "";
324
        $res = $filter->parse($text, ["yamlfrontmatter"]);
325
        $this->assertNull($res->frontmatter, "Frontmatter should be null");
326
        $this->assertEmpty($res->text, "Text should be empty");
327
328
        $text = <<<EOD
329
---
330
---
331
332
EOD;
333
        $res = $filter->parse($text, ["yamlfrontmatter"]);
334
        $this->assertEmpty($res->frontmatter, "Frontmatter should be empty");
335
        $this->assertEmpty($res->text, "Text should be empty");
336
337
        $txt = "TEXT";
338
        $text = <<<EOD
339
---
340
key: value
341
---
342
$txt
343
EOD;
344
        $res = $filter->parse($text, ["yamlfrontmatter"]);
345
        $this->assertEquals(
346
            $res->frontmatter,
347
            [
348
                "key" => "value"
349
            ],
350
            "Frontmatter not matching"
351
        );
352
        $this->assertEquals($txt, $res->text, "Text missmatch");
353
354
        $text = <<<EOD
355
---
356
key1: value1
357
key2: This is a long sentence.
358
---
359
My Article
360
=================================
361
362
This is an example on writing text and adding a YAML frontmatter.
363
364
Subheading
365
---------------------------------
366
367
More text.
368
369
EOD;
370
        $res = $filter->parse($text, ["yamlfrontmatter", "markdown"]);
371
        $this->assertEquals(
372
            $res->frontmatter,
373
            [
374
                "key1" => "value1",
375
                "key2" => "This is a long sentence."
376
            ],
377
            "Frontmatter not matching"
378
        );
379
380
        $text = <<<EOD
381
My Article
382
=================================
383
384
This is an example on writing text and adding a YAML frontmatter.
385
386
Subheading
387
---------------------------------
388
389
More text.
390
391
EOD;
392
        $res = $filter->parse($text, ["yamlfrontmatter", "markdown"]);
393
        $this->assertEmpty($res->frontmatter, "Frontmatter should be empty");
394
    }
395
396
397
398
    /**
399
     * Test.
400
     *
401
     * @return void
402
     */
403
    public function testGetFilters()
404
    {
405
        $filter = new CTextFilter();
406
407
        $filters = $filter->getFilters();
408
        $res = array_diff($this->standardFilters, $filters);
409
        $this->assertTrue(empty($res), "Missmatch standard filters.");
410
    }
411
412
413
414
415
    /**
416
     * Test.
417
     *
418
     * @return void
419
     */
420
    public function testHasFilter()
421
    {
422
        $filter = new CTextFilter();
423
424
        $res = $filter->hasFilter("markdown");
425
        $this->assertTrue($res, "Missmatch has filters.");
426
    }
427
428
429
430
431
    /**
432
     * Test.
433
     *
434
     * @expectedException /Mos/TextFilter/Exception
435
     *
436
     * @return void
437
     */
438
    public function testHasFilterException()
439
    {
440
        $filter = new CTextFilter();
441
442
        $filter->hasFilter("NOT EXISTING");
443
    }
444
445
446
447
448
    /**
449
     * Test.
450
     *
451
     * @return void
452
     */
453
    public function testPurifier()
454
    {
455
        $filter = new CTextFilter();
456
457
        $text = "Header\n=========";
458
        $exp  = "<h1>Header</h1>\n";
459
        $res = $filter->parse($text, ["markdown", "purify"]);
460
        $this->assertEquals($exp, $res->text, "Purify failed");
461
    }
462
463
464
465
    /**
466
     * Test.
467
     *
468
     * @return void
469
     */
470 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...
471
    {
472
        $filter = new CTextFilter();
473
474
        $html = "Header\n=========";
475
        $exp  = "<h1>Header</h1>\n";
476
        $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...
477
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
478
    }
479
480
481
482
    /**
483
     * Test.
484
     *
485
     * @return void
486
     */
487 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...
488
    {
489
        $filter = new CTextFilter();
490
491
        $html = "Header[b]text[/b]\n=========";
492
        $exp  = "<h1>Header<strong>text</strong></h1>\n";
493
        $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...
494
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
495
    }
496
497
498
499
    /**
500
     * Test.
501
     *
502
     * @return void
503
     */
504 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...
505
    {
506
        $filter = new CTextFilter();
507
508
        $html = "Header[b]text[/b]\n=========";
509
        $exp  = "<h1>Header<strong>text</strong></h1>\n";
510
        $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...
511
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
512
    }
513
514
515
516
    /**
517
     * Test.
518
     *
519
     * @return void
520
     */
521 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...
522
    {
523
        $filter = new CTextFilter();
524
525
        $html = "Header\n=========";
526
        $exp  = "<h1>Header</h1>\n";
527
        $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...
528
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
529
    }
530
531
532
533
    /**
534
     * Test.
535
     *
536
     * @return void
537
     */
538 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...
539
    {
540
        $filter = new CTextFilter();
541
542
        $html = "Header\n=========";
543
        $exp  = "<h1>Header</h1>\n";
544
        $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...
545
        $this->assertEquals($exp, $res, "Markdown <h1> failed: '$res'");
546
    }
547
548
549
550
    /**
551
     * Test.
552
     *
553
     * @return void
554
     */
555 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...
556
    {
557
        $filter = new CTextFilter();
558
559
        $html = "[b]text[/b]";
560
        $exp  = "<strong>text</strong>";
561
        $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...
562
        $this->assertEquals($exp, $res, "BBCode [b] failed: '$res'");
563
    }
564
565
566
567
    /**
568
     * Test.
569
     *
570
     * @return void
571
     */
572 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...
573
    {
574
        $filter = new CTextFilter();
575
576
        $html = "http://example.com/humans.txt";
577
        $exp  = "<a href='$html'>$html</a>";
578
        $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...
579
        $this->assertEquals($exp, $res, "clickable failed: '$res'");
580
    }
581
582
583
584
    /**
585
     * Test.
586
     *
587
     * @return void
588
     */
589 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...
590
    {
591
        $filter = new CTextFilter();
592
593
        $html = "hej\nhej";
594
        $exp  = "hej<br />\nhej";
595
        $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...
596
        $this->assertEquals($exp, $res, "nl2br failed: '$res'");
597
    }
598
599
600
601
    /**
602
     * Test.
603
     *
604
     * @return void
605
     */
606
    public function testShortCodeFigure()
607
    {
608
        $filter = new CTextFilter();
609
610
        $src = "/img/me.png";
611
        $caption = "This is me.";
612
        
613
        $html = <<<EOD
614
[FIGURE src=$src caption="$caption"]
615
EOD;
616
617
        $exp  = <<<EOD
618
<figure class='figure'>
619
<a href='$src'><img src='$src' alt='$caption'/></a>
620
<figcaption markdown=1>$caption</figcaption>
621
</figure>
622
EOD;
623
        $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...
624
        $this->assertEquals($exp, $res, "shortcode failed: '$res'");
625
    }
626
627
628
629
    /**
630
     * Test.
631
     *
632
     * @expectedException Exception
633
     *
634
     * @return void
635
     */
636
    public function testDoItException()
637
    {
638
        $filter = new CTextFilter();
639
        $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...
640
    }
641
}
642