|
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
|
|
|
/** |
|
17
|
|
|
* @dataProvider provideExternalLink |
|
18
|
|
|
*/ |
|
19
|
2 |
|
public function testStripExternalLink($text, $expected) |
|
20
|
|
|
{ |
|
21
|
2 |
|
$this::assertSame( |
|
22
|
2 |
|
$expected, |
|
23
|
2 |
|
WikiTextUtil::stripExternalLink($text) |
|
24
|
|
|
); |
|
25
|
2 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function provideExternalLink(): array |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
|
|
['[[fu]] [http://google.fr bla] [http://google.com blo]', '[[fu]] bla blo'], |
|
31
|
|
|
['bla [http://google.fr]', 'bla'], |
|
32
|
|
|
]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
public function testExtractAllRefs() |
|
36
|
|
|
{ |
|
37
|
1 |
|
$text = <<<EOF |
|
38
|
1 |
|
bla <ref>toto.</ref> bla <ref name="tutu">Plop</ref>. |
|
39
|
1 |
|
|
|
40
|
|
|
* [[bob]] |
|
41
|
1 |
|
* https://test.com/page.html |
|
42
|
|
|
*https://example.com/papa. |
|
43
|
1 |
|
|
|
44
|
|
|
EOF; |
|
45
|
1 |
|
$expected = [ |
|
46
|
1 |
|
0 => ['<ref>toto.</ref>', 'toto.'], |
|
47
|
1 |
|
1 => ['<ref name="tutu">Plop</ref>', 'Plop'], |
|
48
|
|
|
2 => [ |
|
49
|
1 |
|
"* https://test.com/page.html\n", |
|
50
|
|
|
'https://test.com/page.html', |
|
51
|
1 |
|
], |
|
52
|
|
|
3 => [ |
|
53
|
1 |
|
"*https://example.com/papa.\n", |
|
54
|
|
|
'https://example.com/papa', |
|
55
|
1 |
|
], |
|
56
|
1 |
|
]; |
|
57
|
1 |
|
|
|
58
|
|
|
$this::assertSame( |
|
59
|
1 |
|
$expected, |
|
60
|
|
|
WikiTextUtil::extractRefsAndListOfLinks($text) |
|
61
|
1 |
|
); |
|
62
|
|
|
} |
|
63
|
1 |
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @dataProvider provideWikilink |
|
66
|
1 |
|
*/ |
|
67
|
1 |
|
public function testWikilink($data, $expected) |
|
68
|
1 |
|
{ |
|
69
|
|
|
$this::assertSame( |
|
70
|
1 |
|
$expected, |
|
71
|
|
|
WikiTextUtil::wikilink($data[0], $data[1] ?? null) |
|
72
|
1 |
|
); |
|
73
|
|
|
} |
|
74
|
1 |
|
|
|
75
|
|
|
public function provideWikilink() |
|
76
|
1 |
|
{ |
|
77
|
1 |
|
return [ |
|
78
|
1 |
|
[['fu_bar'], '[[fu bar]]'], |
|
79
|
|
|
[['fu', 'Fu'], '[[fu]]'], |
|
80
|
|
|
[['fu', 'bar'], '[[Bar|fu]]'], |
|
81
|
1 |
|
[['fu', '[[Bar]]'], '[[Bar|fu]]'], // Erreur "|lien auteur=[[Bla]]" |
|
82
|
1 |
|
]; |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public function testUpperfirst() |
|
86
|
|
|
{ |
|
87
|
|
|
$this::assertSame( |
|
88
|
|
|
'Économie', |
|
89
|
|
|
WikiTextUtil::mb_ucfirst('économie') |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
7 |
|
public function testLowerfirst() |
|
94
|
|
|
{ |
|
95
|
7 |
|
$this::assertSame( |
|
96
|
7 |
|
'économie', |
|
97
|
7 |
|
WikiTextUtil::mb_lowerfirst('Économie') |
|
98
|
|
|
); |
|
99
|
7 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testGetWikilinkPages() |
|
102
|
|
|
{ |
|
103
|
|
|
$text = 'bla [[fu|bar]] et [[back]] mais pas [[wikt:toto|bou]]'; |
|
104
|
|
|
|
|
105
|
|
|
$this::assertSame( |
|
106
|
|
|
['fu', 'back'], |
|
107
|
|
|
WikiTextUtil::getWikilinkPages($text) |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testRemoveHTMLcomments() |
|
112
|
|
|
{ |
|
113
|
|
|
$text = 'blabla<!-- sdfqfqs |
|
114
|
|
|
<!-- blbal |
|
115
|
|
|
--> ez'; |
|
116
|
|
|
$this::assertSame( |
|
117
|
|
|
'blabla ez', |
|
118
|
|
|
WikiTextUtil::removeHTMLcomments($text) |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testIsCommented() |
|
123
|
|
|
{ |
|
124
|
|
|
$text = 'blabla<!-- sdfqfqs |
|
125
|
|
|
--> ez'; |
|
126
|
|
|
$this::assertSame( |
|
127
|
|
|
true, |
|
128
|
|
|
WikiTextUtil::isCommented($text) |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
$this::assertSame( |
|
132
|
|
|
false, |
|
133
|
|
|
WikiTextUtil::isCommented('bla') |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @dataProvider provideWikify |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $text |
|
141
|
|
|
* @param string $expected |
|
142
|
|
|
*/ |
|
143
|
|
|
public function testUnWikify(string $text, string $expected) |
|
144
|
|
|
{ |
|
145
|
|
|
$this::assertEquals( |
|
146
|
|
|
$expected, |
|
147
|
|
|
WikiTextUtil::unWikify($text) |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function provideWikify() |
|
152
|
|
|
{ |
|
153
|
|
|
return [ |
|
154
|
|
|
['blabla<!-- fu -->', 'blabla'], |
|
155
|
|
|
['{{lang|en|fubar}}', 'fubar'], |
|
156
|
|
|
['{{langue|en|fubar}}', 'fubar'], |
|
157
|
|
|
['[[wikilien]', 'wikilien'], |
|
158
|
|
|
['[[wiki|wikilien]]', 'wikilien'], |
|
159
|
|
|
['{{en}}', '{{en}}'], |
|
160
|
|
|
['{{Lien|Jeffrey Robinson}}', 'Jeffrey Robinson'], |
|
161
|
|
|
]; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|