Passed
Push — master ( ac79bd...625dce )
by Dispositif
13:46 queued 16s
created

WikiTextUtilTest::testExtractCommentedText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 1
b 1
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 : Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\Utils;
11
12
use PHPUnit\Framework\TestCase;
13
14
class WikiTextUtilTest extends TestCase
15
{
16
    public static function provideExternalLink(): array
17
    {
18
        return [
19 2
            ['[[fu]] [http://google.fr bla] [http://google.com blo]', '[[fu]] bla blo'],
20
            ['bla [http://google.fr]', 'bla'],
21 2
        ];
22 2
    }
23 2
24
    public static function provideWikilink()
25 2
    {
26
        return [
27
            [['fu_bar'], '[[fu bar]]'],
28
            [['fu', 'Fu'], '[[fu]]'],
29
            [['fu', 'bar'], '[[Bar|fu]]'],
30
            [['fu', '[[Bar]]'], '[[Bar|fu]]'], // Erreur "|lien auteur=[[Bla]]"
31
        ];
32
    }
33
34
    public static function provideWikify()
35 1
    {
36
        return [
37 1
            ['blabla<!-- fu -->', 'blabla'],
38 1
            ['{{lang|en|fubar}}', 'fubar'],
39 1
            ['{{langue|en|fubar}}', 'fubar'],
40
            ['[[wikilien]', 'wikilien'],
41 1
            ['[[wiki|wikilien]]', 'wikilien'],
42
            ['{{en}}', '{{en}}'],
43 1
            ['{{Lien|Jeffrey Robinson}}', 'Jeffrey Robinson'],
44
        ];
45 1
    }
46 1
47 1
    /**
48
     * @dataProvider provideExternalLink
49 1
     */
50
    public function testStripExternalLink($text, $expected)
51 1
    {
52
        $this::assertSame(
53 1
            $expected,
54
            WikiTextUtil::stripExternalLink($text)
55 1
        );
56 1
    }
57 1
58
    public function testExtractAllRefs()
59 1
    {
60
        $text = <<<EOF
61 1
bla <ref>toto.</ref> bla <ref name="tutu">Plop</ref>.
62
63 1
* [[bob]]
64
* https://test.com/page.html
65
*https://example.com/papa.
66 1
67 1
EOF;
68 1
        $expected = [
69
            0 => ['<ref>toto.</ref>', 'toto.'],
70 1
            1 => ['<ref name="tutu">Plop</ref>', 'Plop'],
71
            2 => [
72 1
                "* https://test.com/page.html\n",
73
                'https://test.com/page.html',
74 1
            ],
75
            3 => [
76 1
                "*https://example.com/papa.\n",
77 1
                'https://example.com/papa',
78 1
            ],
79
        ];
80
81 1
        $this::assertSame(
82 1
            $expected,
83 1
            WikiTextUtil::extractRefsAndListOfLinks($text)
84
        );
85 1
    }
86
87
    /**
88
     * @dataProvider provideWikilink
89
     */
90
    public function testWikilink($data, $expected)
91
    {
92
        $this::assertSame(
93 7
            $expected,
94
            WikiTextUtil::wikilink($data[0], $data[1] ?? null)
95 7
        );
96 7
    }
97 7
98
    public function testUpperfirst()
99 7
    {
100
        $this::assertSame(
101
            'Économie',
102
            WikiTextUtil::mb_ucfirst('économie')
103
        );
104
    }
105
106
    public function testLowerfirst()
107
    {
108
        $this::assertSame(
109
            'économie',
110
            WikiTextUtil::mb_lowerfirst('Économie')
111
        );
112
    }
113
114
    public function testGetWikilinkPages()
115
    {
116
        $text = 'bla [[fu|bar]] et [[back]] mais pas [[wikt:toto|bou]]';
117
118
        $this::assertSame(
119
            ['fu', 'back'],
120
            WikiTextUtil::getWikilinkPages($text)
121
        );
122
    }
123
124
    public function testRemoveHTMLcomments()
125
    {
126
        $text = 'blabla<!-- sdfqfqs 
127
<!-- blbal 
128
    --> ez';
129
        $this::assertSame(
130
            'blabla ez',
131
            WikiTextUtil::removeHTMLcomments($text)
132
        );
133
    }
134
135
    public function testIsCommented()
136
    {
137
        $text = 'blabla<!-- sdfqfqs 
138
        --> ez';
139
        $this::assertSame(
140
            true,
141
            WikiTextUtil::isCommented($text)
142
        );
143
144
        $this::assertSame(
145
            false,
146
            WikiTextUtil::isCommented('bla')
147
        );
148
    }
149
150
    /**
151
     * @dataProvider provideWikify
152
     */
153
    public function testUnWikify(string $text, string $expected)
154
    {
155
        $this::assertEquals(
156
            $expected,
157
            WikiTextUtil::unWikify($text)
158
        );
159
    }
160
161
    /**
162
     * @dataProvider provideContainsWikiTag
163
     */
164
    public function testContainsWikiTag(string $text, bool $expected)
165
    {
166
        $this::assertSame(
167
            $expected,
168
            WikiTextUtil::containsWikiTag($text)
169
        );
170
    }
171
172
    public static function provideContainsWikiTag(): array
173
    {
174
        return [
175
            ['http://bla', false],
176
            ['http://bla</ref>', true],
177
            ['http://bla<ref name="bla">', true],
178
            ['http://bla<nowiki>', true],
179
        ];
180
    }
181
182
    /**
183
     * @dataProvider provideExtractCommentedText
184
     */
185
    public function testExtractCommentedText(string $text, array $expected): void
186
    {
187
        $this::assertSame(
188
            $expected,
189
            WikiTextUtil::extractCommentedText($text)
190
        );
191
    }
192
193
    public static function provideExtractCommentedText(): array
194
    {
195
        return [
196
            ['bla <!-- fu --> bla <!-- bar --> bla', ['<!-- fu -->', '<!-- bar -->']],
197
            ['bla <!-- fu --> pof <!-- bar --> --> bla', ['<!-- fu -->', '<!-- bar -->']],
198
            ['bla <!-- fu <!-- bar --> --> bla', ['<!-- fu <!-- bar -->']],
199
        ];
200
    }
201
202
    public function testFilterSensitiveCommentsInText(): void
203
    {
204
        $text = 'bla <!-- 
205
        * https://skip.com 
206
        --> bla2 <!-- <ref>skip comment</ref> --> bla3<!-- keep --><!-- {{skip template}} -->';
207
        $expected = 'bla #FILTERED_COMMENT# bla2 #FILTERED_COMMENT# bla3<!-- keep -->#FILTERED_COMMENT#';
208
        $this::assertSame(
209
            $expected,
210
            WikiTextUtil::filterSensitiveCommentsInText($text)
211
        );
212
    }
213
}
214