aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/lib/classes/htmlpurifier/HtmlpurifierInjectorClassifylinksTest.php
blob: b8b055810411df2d625a8e60843c01247fc22d51 (plain)
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
<?php
/**
 * htmlpurifier_injector_classifylinks_test.php - Unit tests for the HTMLPurifier_Injector_ClassifyLinks class.
 **
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * @category    Stud.IP
 * @copyright   (c) 2014 Stud.IP e.V.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @since       File available since Release 3.0
 * @author      Robert Costa <rcosta@uos.de>
 */
require_once 'tests/unit/fakeserver.php';

require_once 'lib/classes/htmlpurifier/HTMLPurifier_Injector_ClassifyLinks.php';

/**
 * Test case for HTMLPurifier_Injector_ClassifyLinks.
 */
class HTMLPurifier_Injector_ClassifyLinksTest extends \Codeception\Test\Unit
{
    public function setUp(): void
    {
        $config = new Config([
            'CONVERT_IDNA_URL' => false,
        ]);
        Config::set($config);
    }

    /**
     * @dataProvider dataProvider
     */
    public function testClassifyLinks($uri, $domains, $in, $out)
    {
        # create purifier
        $config = \HTMLPurifier_Config::createDefault();
        $config->set('AutoFormat.Custom', ['ClassifyLinks']);
        $config->set('Cache.DefinitionImpl', null);
        $purifier = new \HTMLPurifier($config);

        # run test
        fakeServer($uri, $domains);
        $this->assertEquals($out, $purifier->purify($in));
    }

    /**
     * Data provider for testClassifyLinks.
     */
    public function dataProvider()
    {
        // domains of faked Stud.IP server
        $domains = [
            'org' => 'example.org/studip',
            'home' => 'example.org/~home',
            'net' => 'example.net/studip',
        ];

        // return absolute URL for a path on one of the faked domains
        $url = function ($domainKey, $path) use (&$domains) {
            return 'http://' . $domains[$domainKey] . '/' . $path;
        };
        $a = function ($url, $className, $content) {
            $href = ' href="' . $url . '"';
            $class = empty($className) ? '' : ' class="' . $className . '"';
            return '<a' . $href . $class . '>' . $content . '</a>';
        };

        // class names for internal and external links
        $in = 'link-intern';
        $ex = 'link-extern';

        // return test data
        return [
            [
                $url('org', 'index.php'),
                $domains,
                $a('http://www.google.de', '', 'Google'),
                $a('http://www.google.de', $ex, 'Google')
            ],
            [
                $url('org', 'index.php'),
                $domains,
                $a($url('org', 'index.php'), '', 'Main Page'),
                $a($url('org', 'index.php'), $in, 'Main Page')
            ],
            [
                $url('org', 'index.php'),
                $domains,
                $a($url('home', 'index.php'), '', 'Main Page'),
                $a($url('home', 'index.php'), $in, 'Main Page')
            ],
            [
                $url('org', 'index.php'),
                $domains,
                $a($url('net', 'index.php'), '', 'Main Page'),
                $a($url('net', 'index.php'), $in, 'Main Page')
            ],
        ];
    }

    public function tearDown(): void
    {
        Config::set(null);
    }
}