|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\Tests\System; |
|
6
|
|
|
|
|
7
|
|
|
use WMDE\Fundraising\Frontend\Validation\TextPolicyValidator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @covers WMDE\Fundraising\Frontend\Validation\TextPolicyValidator |
|
11
|
|
|
* |
|
12
|
|
|
* @licence GNU GPL v2+ |
|
13
|
|
|
* @author Christoph Fischer <[email protected] |
|
14
|
|
|
*/ |
|
15
|
|
|
class TextPolicyValidatorTest extends \PHPUnit_Framework_TestCase { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @dataProvider urlTestProvider |
|
19
|
|
|
*/ |
|
20
|
|
|
public function testWhenGivenCommentHasURL_validatorReturnsFalse( $commentToTest ) { |
|
21
|
|
|
$this->skipIfNoInternet(); |
|
22
|
|
|
|
|
23
|
|
|
$textPolicyValidator = new TextPolicyValidator(); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertFalse( $textPolicyValidator->hasHarmlessContent( |
|
26
|
|
|
$commentToTest, |
|
27
|
|
|
TextPolicyValidator::CHECK_URLS | TextPolicyValidator::CHECK_URLS_DNS |
|
28
|
|
|
) ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private function skipIfNoInternet() { |
|
32
|
|
|
static $isConnected = null; |
|
33
|
|
|
|
|
34
|
|
|
if ( $isConnected === null ) { |
|
35
|
|
|
$isConnected = (bool)@fsockopen( 'www.google.com', 80, $num, $error, 1 ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ( !$isConnected ) { |
|
|
|
|
|
|
39
|
|
|
$this->markTestSkipped( 'No internet connection' ); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function urlTestProvider() { |
|
44
|
|
|
return [ |
|
45
|
|
|
[ 'www.example.com' ], |
|
46
|
|
|
[ 'http://www.example.com' ], |
|
47
|
|
|
[ 'https://www.example.com' ], |
|
48
|
|
|
[ 'example.com' ], |
|
49
|
|
|
[ 'example.com/test' ], |
|
50
|
|
|
[ 'example.com/teKAst/index.php' ], |
|
51
|
|
|
[ 'Ich mag Wikipedia. Aber meine Seite ist auch toll:example.com/teKAst/index.php' ], |
|
52
|
|
|
[ 'inwx.berlin' ], |
|
53
|
|
|
[ 'wwwwwww.website.com' ], |
|
54
|
|
|
[ 'TriebWerk-Grün.de' ], |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @dataProvider harmlessTestProvider |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testWhenGivenHarmlessComment_validatorReturnsTrue( $commentToTest ) { |
|
62
|
|
|
$this->skipIfNoInternet(); |
|
63
|
|
|
|
|
64
|
|
|
$textPolicyValidator = new TextPolicyValidator(); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertTrue( $textPolicyValidator->hasHarmlessContent( |
|
67
|
|
|
$commentToTest, |
|
68
|
|
|
TextPolicyValidator::CHECK_URLS | TextPolicyValidator::CHECK_URLS_DNS | TextPolicyValidator::CHECK_BADWORDS |
|
69
|
|
|
) ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function harmlessTestProvider() { |
|
73
|
|
|
return [ |
|
74
|
|
|
[ 'Wikipedia ist so super, meine Eltern sagen es ist eine toll Seite. Berlin ist auch Super.' ], |
|
75
|
|
|
[ 'Ich mag Wikipedia. Aber meine Seite ist auch toll. Googelt mal nach Bunsenbrenner!!!1' ], |
|
76
|
|
|
[ 'Bei Wikipedia kann man eine Menge zum Thema Hamster finden. Hamster fressen voll viel Zeug alter!' ], |
|
77
|
|
|
[ 'Manche Seiten haben keinen Inhalt, das finde ich sch...e' ], // this also tests the domain detection |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testHamlessContentWithDns() { |
|
82
|
|
|
$this->skipIfNoInternet(); |
|
83
|
|
|
|
|
84
|
|
|
if ( checkdnsrr( 'some-non-existing-domain-drfeszrfdaesr.sdferdyerdhgty', 'A' ) ) { |
|
85
|
|
|
// https://www.youtube.com/watch?v=HGBOeLdm-1s |
|
86
|
|
|
$this->markTestSkipped( 'Your DNS/ISP provider gives results for impossible host names.' ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$textPolicyValidator = new TextPolicyValidator(); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertTrue( $textPolicyValidator->hasHarmlessContent( |
|
92
|
|
|
'Ich mag Wikipedia.Wieso ? Weil ich es so toll finde!', |
|
93
|
|
|
TextPolicyValidator::CHECK_URLS | TextPolicyValidator::CHECK_URLS_DNS | TextPolicyValidator::CHECK_BADWORDS |
|
94
|
|
|
) ); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @dataProvider insultingTestProvider |
|
99
|
|
|
*/ |
|
100
|
|
|
public function testWhenGivenInsultingComment_validatorReturnsFalse( $commentToTest ) { |
|
101
|
|
|
$textPolicyValidator = $this->getPreFilledTextPolicyValidator(); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertFalse( $textPolicyValidator->hasHarmlessContent( |
|
104
|
|
|
$commentToTest, |
|
105
|
|
|
TextPolicyValidator::CHECK_BADWORDS |
|
106
|
|
|
) ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function insultingTestProvider() { |
|
110
|
|
|
return [ |
|
111
|
|
|
[ 'Alles Deppen!' ], |
|
112
|
|
|
[ 'Heil Hitler!' ], |
|
113
|
|
|
[ 'Duhamsterfresse!!!' ], |
|
114
|
|
|
[ 'Alles nur HAMSTERFRESSEN!!!!!!!!1111111111' ], |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @dataProvider whiteWordsInsultingTestProvider |
|
120
|
|
|
*/ |
|
121
|
|
|
public function testWhenGivenInsultingCommentAndWhiteWords_validatorReturnsFalse( $commentToTest ) { |
|
122
|
|
|
$textPolicyValidator = $this->getPreFilledTextPolicyValidator(); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertFalse( |
|
125
|
|
|
$textPolicyValidator->hasHarmlessContent( |
|
126
|
|
|
$commentToTest, |
|
127
|
|
|
TextPolicyValidator::CHECK_URLS |
|
128
|
|
|
| TextPolicyValidator::CHECK_URLS_DNS |
|
129
|
|
|
| TextPolicyValidator::CHECK_BADWORDS |
|
130
|
|
|
| TextPolicyValidator::IGNORE_WHITEWORDS |
|
131
|
|
|
) |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function whiteWordsInsultingTestProvider() { |
|
136
|
|
|
return [ |
|
137
|
|
|
[ 'Ich heisse Deppendorf ihr Deppen und das ist auch gut so!' ], |
|
138
|
|
|
[ 'Ihr Arschgeigen, ich wohne in Marsch und das ist auch gut so!' ], |
|
139
|
|
|
[ 'Bei Wikipedia gibts echt tolle Arschkrampen!' ], |
|
140
|
|
|
]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @dataProvider whiteWordsHarmlessTestProvider |
|
145
|
|
|
*/ |
|
146
|
|
|
public function testWhenGivenHarmlessCommentAndWhiteWords_validatorReturnsTrue( $commentToTest ) { |
|
147
|
|
|
$textPolicyValidator = $this->getPreFilledTextPolicyValidator(); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertTrue( |
|
150
|
|
|
$textPolicyValidator->hasHarmlessContent( |
|
151
|
|
|
$commentToTest, |
|
152
|
|
|
TextPolicyValidator::CHECK_URLS |
|
153
|
|
|
| TextPolicyValidator::CHECK_URLS_DNS |
|
154
|
|
|
| TextPolicyValidator::CHECK_BADWORDS |
|
155
|
|
|
| TextPolicyValidator::IGNORE_WHITEWORDS |
|
156
|
|
|
) |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function whiteWordsHarmlessTestProvider() { |
|
161
|
|
|
return [ |
|
162
|
|
|
[ 'Wikipedia ist so super, meine Eltern sagen es ist eine toll Seite. Berlin ist auch Super.' ], |
|
163
|
|
|
[ 'Ich heisse Deppendorf ihr und das ist auch gut so!' ], |
|
164
|
|
|
[ 'Bei Wikipedia gibts echt tolle Dinge!' ], |
|
165
|
|
|
[ 'Ick spend richtig Kohle, denn ick hab ne GmbH & Co.KG' ], |
|
166
|
|
|
]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @dataProvider insultingTestProviderWithRegexChars |
|
171
|
|
|
*/ |
|
172
|
|
|
public function testGivenBadWordMatchContainingRegexChars_validatorReturnsFalse( $commentToTest ) { |
|
173
|
|
|
$textPolicyValidator = $this->getPreFilledTextPolicyValidator(); |
|
174
|
|
|
|
|
175
|
|
|
$this->assertFalse( |
|
176
|
|
|
$textPolicyValidator->hasHarmlessContent( |
|
177
|
|
|
$commentToTest, |
|
178
|
|
|
TextPolicyValidator::CHECK_URLS |
|
179
|
|
|
| TextPolicyValidator::CHECK_URLS_DNS |
|
180
|
|
|
| TextPolicyValidator::CHECK_BADWORDS |
|
181
|
|
|
| TextPolicyValidator::IGNORE_WHITEWORDS |
|
182
|
|
|
) |
|
183
|
|
|
); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function insultingTestProviderWithRegexChars() { |
|
187
|
|
|
return [ |
|
188
|
|
|
[ 'Ich heisse Deppendorf (ihr Deppen und das ist auch gut so!' ], |
|
189
|
|
|
[ 'Ihr [Arschgeigen], ich wohne in //Marsch// und das ist auch gut so!' ], |
|
190
|
|
|
[ 'Bei #Wikipedia gibts echt tolle Arschkrampen!' ], |
|
191
|
|
|
]; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
private function getPreFilledTextPolicyValidator() { |
|
195
|
|
|
$textPolicyValidator = new TextPolicyValidator(); |
|
196
|
|
|
$textPolicyValidator->addBadWordsFromArray( |
|
197
|
|
|
[ |
|
198
|
|
|
'deppen', |
|
199
|
|
|
'hitler', |
|
200
|
|
|
'fresse', |
|
201
|
|
|
'arsch' |
|
202
|
|
|
] ); |
|
203
|
|
|
$textPolicyValidator->addWhiteWordsFromArray( |
|
204
|
|
|
[ |
|
205
|
|
|
'Deppendorf', |
|
206
|
|
|
'Marsch', |
|
207
|
|
|
'Co.KG', |
|
208
|
|
|
] ); |
|
209
|
|
|
return $textPolicyValidator; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
|
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.