Passed
Push — master ( d644e9...e142f8 )
by Dispositif
15:55
created

WikiTextUtilTest::provideConcatenatedRefFixture()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 provideConcatenatedRefFixture
49 1
     */
50
    public function testFixConcatenatedRefs($text, $expected)
51 1
    {
52
        $this::assertSame($expected, WikiTextUtil::fixConcatenatedRefsSyntax($text));
53 1
    }
54
55 1
    public static function provideConcatenatedRefFixture(): array
56 1
    {
57 1
        return [
58
            ['<ref>fu</ref><ref name="1">bar</ref>', '<ref>fu</ref>{{,}}<ref name="1">bar</ref>'],
59 1
            ['<ref>fu</ref>  <ref name="1">bar</ref>', '<ref>fu</ref>{{,}}<ref name="1">bar</ref>'],
60
            ['<ref>fu</ref>{{,}}<ref name="1">bar</ref>', '<ref>fu</ref>{{,}}<ref name="1">bar</ref>'],
61 1
            ['<ref name="A" /> <ref name="B">', '<ref name="A" />{{,}}<ref name="B">'],
62
            ['<ref name=A /><ref name="B">', '<ref name=A />{{,}}<ref name="B">'],
63 1
        ];
64
    }
65
66 1
    /**
67 1
     * @dataProvider provideExternalLink
68 1
     */
69
    public function testStripExternalLink($text, $expected)
70 1
    {
71
        $this::assertSame(
72 1
            $expected,
73
            WikiTextUtil::stripExternalLink($text)
74 1
        );
75
    }
76 1
77 1
    public function testExtractAllRefs()
78 1
    {
79
        $text = <<<EOF
80
bla <ref>toto.</ref> bla <ref name="tutu">Plop</ref>.
81 1
82 1
* [[bob]]
83 1
* https://test.com/page.html
84
*https://example.com/papa.
85 1
86
EOF;
87
        $expected = [
88
            0 => ['<ref>toto.</ref>', 'toto.'],
89
            1 => ['<ref name="tutu">Plop</ref>', 'Plop'],
90
            2 => [
91
                "* https://test.com/page.html\n",
92
                'https://test.com/page.html',
93 7
            ],
94
            3 => [
95 7
                "*https://example.com/papa.\n",
96 7
                'https://example.com/papa',
97 7
            ],
98
        ];
99 7
100
        $this::assertSame(
101
            $expected,
102
            WikiTextUtil::extractRefsAndListOfLinks($text)
103
        );
104
    }
105
106
    /**
107
     * @dataProvider provideWikilink
108
     */
109
    public function testWikilink($data, $expected)
110
    {
111
        $this::assertSame(
112
            $expected,
113
            WikiTextUtil::wikilink($data[0], $data[1] ?? null)
114
        );
115
    }
116
117
    public function testUpperfirst()
118
    {
119
        $this::assertSame(
120
            'Économie',
121
            WikiTextUtil::mb_ucfirst('économie')
122
        );
123
    }
124
125
    public function testLowerfirst()
126
    {
127
        $this::assertSame(
128
            'économie',
129
            WikiTextUtil::mb_lowerfirst('Économie')
130
        );
131
    }
132
133
    public function testGetWikilinkPages()
134
    {
135
        $text = 'bla [[fu|bar]] et [[back]] mais pas [[wikt:toto|bou]]';
136
137
        $this::assertSame(
138
            ['fu', 'back'],
139
            WikiTextUtil::getWikilinkPages($text)
140
        );
141
    }
142
143
    public function testRemoveHTMLcomments()
144
    {
145
        $text = 'blabla<!-- sdfqfqs 
146
<!-- blbal 
147
    --> ez';
148
        $this::assertSame(
149
            'blabla ez',
150
            WikiTextUtil::removeHTMLcomments($text)
151
        );
152
    }
153
154
    public function testIsCommented()
155
    {
156
        $text = 'blabla<!-- sdfqfqs 
157
        --> ez';
158
        $this::assertSame(
159
            true,
160
            WikiTextUtil::isCommented($text)
161
        );
162
163
        $this::assertSame(
164
            false,
165
            WikiTextUtil::isCommented('bla')
166
        );
167
    }
168
169
    /**
170
     * @dataProvider provideWikify
171
     */
172
    public function testUnWikify(string $text, string $expected)
173
    {
174
        $this::assertEquals(
175
            $expected,
176
            WikiTextUtil::unWikify($text)
177
        );
178
    }
179
}
180