| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Tests\Unit; |
| 4 |
|
| 5 |
use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesRegistry; |
| 6 |
use MediaWiki\Extension\AbuseFilter\Filter\Filter; |
| 7 |
use MediaWiki\Extension\AbuseFilter\Filter\Flags; |
| 8 |
use MediaWiki\Extension\AbuseFilter\Filter\LastEditInfo; |
| 9 |
use MediaWiki\Extension\AbuseFilter\Filter\Specs; |
| 10 |
use MediaWiki\Extension\AbuseFilter\FilterCompare; |
| 11 |
use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner; |
| 12 |
use MediaWikiUnitTestCase; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class FilterCompareTest extends MediaWikiUnitTestCase { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function testCompareVersions( |
| 27 |
Filter $firstVersion, |
| 28 |
Filter $secondVersion, |
| 29 |
array $expected |
| 30 |
) { |
| 31 |
$allActions = [ |
| 32 |
'throttle', 'warn', 'disallow', 'blockautopromote', 'block', 'rangeblock', 'degroup', 'tag' |
| 33 |
]; |
| 34 |
$allActions = array_fill_keys( $allActions, true ); |
| 35 |
$registry = new ConsequencesRegistry( $this->createMock( AbuseFilterHookRunner::class ), $allActions ); |
| 36 |
$compare = new FilterCompare( $registry ); |
| 37 |
|
| 38 |
$this->assertSame( $expected, $compare->compareVersions( $firstVersion, $secondVersion ) ); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static function provideVersions() { |
| 46 |
$baseSpecs = [ |
| 47 |
'actions' => [], |
| 48 |
'user' => 1, |
| 49 |
'user_text' => 'Foo', |
| 50 |
'timestamp' => '20181016155634', |
| 51 |
'id' => 42 |
| 52 |
]; |
| 53 |
$makeFilter = static function ( $specs ) use ( $baseSpecs ) { |
| 54 |
$specs += $baseSpecs; |
| 55 |
return new Filter( |
| 56 |
new Specs( |
| 57 |
$specs['rules'], |
| 58 |
$specs['comments'], |
| 59 |
$specs['name'], |
| 60 |
array_keys( $specs['actions'] ), |
| 61 |
$specs['group'] |
| 62 |
), |
| 63 |
new Flags( |
| 64 |
$specs['enabled'], |
| 65 |
$specs['deleted'], |
| 66 |
$specs['hidden'], |
| 67 |
$specs['global'] |
| 68 |
), |
| 69 |
$specs['actions'], |
| 70 |
new LastEditInfo( |
| 71 |
$specs['user'], |
| 72 |
$specs['user_text'], |
| 73 |
$specs['timestamp'] |
| 74 |
), |
| 75 |
$specs['id'] |
| 76 |
); |
| 77 |
}; |
| 78 |
|
| 79 |
return [ |
| 80 |
[ |
| 81 |
$makeFilter( [ |
| 82 |
'name' => 'Comments', |
| 83 |
'rules' => '/*Pattern*/', |
| 84 |
'comments' => 'Comments', |
| 85 |
'deleted' => 0, |
| 86 |
'enabled' => 1, |
| 87 |
'hidden' => 0, |
| 88 |
'global' => 0, |
| 89 |
'group' => 'default', |
| 90 |
'actions' => [ 'disallow' => [] ] |
| 91 |
] ), |
| 92 |
$makeFilter( [ |
| 93 |
'name' => 'OtherComments', |
| 94 |
'rules' => '/*Other pattern*/', |
| 95 |
'comments' => 'Other comments', |
| 96 |
'deleted' => 1, |
| 97 |
'enabled' => 0, |
| 98 |
'hidden' => 1, |
| 99 |
'global' => 1, |
| 100 |
'group' => 'flow', |
| 101 |
'actions' => [ 'disallow' => [] ] |
| 102 |
] ), |
| 103 |
[ |
| 104 |
'af_public_comments', |
| 105 |
'af_pattern', |
| 106 |
'af_comments', |
| 107 |
'af_deleted', |
| 108 |
'af_enabled', |
| 109 |
'af_hidden', |
| 110 |
'af_global', |
| 111 |
'af_group', |
| 112 |
] |
| 113 |
], |
| 114 |
[ |
| 115 |
$makeFilter( [ |
| 116 |
'name' => 'Comments', |
| 117 |
'rules' => '/*Pattern*/', |
| 118 |
'comments' => 'Comments', |
| 119 |
'deleted' => 0, |
| 120 |
'enabled' => 1, |
| 121 |
'hidden' => 0, |
| 122 |
'global' => 0, |
| 123 |
'group' => 'default', |
| 124 |
'actions' => [ 'disallow' => [] ] |
| 125 |
] ), |
| 126 |
$makeFilter( [ |
| 127 |
'name' => 'Comments', |
| 128 |
'rules' => '/*Pattern*/', |
| 129 |
'comments' => 'Comments', |
| 130 |
'deleted' => 0, |
| 131 |
'enabled' => 1, |
| 132 |
'hidden' => 0, |
| 133 |
'global' => 0, |
| 134 |
'group' => 'default', |
| 135 |
'actions' => [ 'disallow' => [] ] |
| 136 |
] ), |
| 137 |
[] |
| 138 |
], |
| 139 |
[ |
| 140 |
$makeFilter( [ |
| 141 |
'name' => 'Comments', |
| 142 |
'rules' => '/*Pattern*/', |
| 143 |
'comments' => 'Comments', |
| 144 |
'deleted' => 0, |
| 145 |
'enabled' => 1, |
| 146 |
'hidden' => 0, |
| 147 |
'global' => 0, |
| 148 |
'group' => 'default', |
| 149 |
'actions' => [ 'disallow' => [] ] |
| 150 |
] ), |
| 151 |
$makeFilter( [ |
| 152 |
'name' => 'Comments', |
| 153 |
'rules' => '/*Pattern*/', |
| 154 |
'comments' => 'Comments', |
| 155 |
'deleted' => 0, |
| 156 |
'enabled' => 1, |
| 157 |
'hidden' => 0, |
| 158 |
'global' => 0, |
| 159 |
'group' => 'default', |
| 160 |
'actions' => [ 'degroup' => [] ] |
| 161 |
] ), |
| 162 |
[ 'actions' ] |
| 163 |
], |
| 164 |
[ |
| 165 |
$makeFilter( [ |
| 166 |
'name' => 'Comments', |
| 167 |
'rules' => '/*Pattern*/', |
| 168 |
'comments' => 'Comments', |
| 169 |
'deleted' => 0, |
| 170 |
'enabled' => 1, |
| 171 |
'hidden' => 0, |
| 172 |
'global' => 0, |
| 173 |
'group' => 'default', |
| 174 |
'actions' => [ 'disallow' => [] ] |
| 175 |
] ), |
| 176 |
$makeFilter( [ |
| 177 |
'name' => 'OtherComments', |
| 178 |
'rules' => '/*Other pattern*/', |
| 179 |
'comments' => 'Other comments', |
| 180 |
'deleted' => 1, |
| 181 |
'enabled' => 0, |
| 182 |
'hidden' => 1, |
| 183 |
'global' => 1, |
| 184 |
'group' => 'flow', |
| 185 |
'actions' => [ 'blockautopromote' => [] ] |
| 186 |
] ), |
| 187 |
[ |
| 188 |
'af_public_comments', |
| 189 |
'af_pattern', |
| 190 |
'af_comments', |
| 191 |
'af_deleted', |
| 192 |
'af_enabled', |
| 193 |
'af_hidden', |
| 194 |
'af_global', |
| 195 |
'af_group', |
| 196 |
'actions' |
| 197 |
] |
| 198 |
], |
| 199 |
[ |
| 200 |
$makeFilter( [ |
| 201 |
'name' => 'Comments', |
| 202 |
'rules' => '/*Pattern*/', |
| 203 |
'comments' => 'Comments', |
| 204 |
'deleted' => 0, |
| 205 |
'enabled' => 1, |
| 206 |
'hidden' => 0, |
| 207 |
'global' => 0, |
| 208 |
'group' => 'default', |
| 209 |
'actions' => [ 'disallow' => [] ] |
| 210 |
] ), |
| 211 |
$makeFilter( [ |
| 212 |
'name' => 'Comments', |
| 213 |
'rules' => '/*Pattern*/', |
| 214 |
'comments' => 'Comments', |
| 215 |
'deleted' => 0, |
| 216 |
'enabled' => 1, |
| 217 |
'hidden' => 0, |
| 218 |
'global' => 0, |
| 219 |
'group' => 'default', |
| 220 |
'actions' => [ 'warn' => [ 'abusefilter-warning' ] ] |
| 221 |
] ), |
| 222 |
[ 'actions' ] |
| 223 |
], |
| 224 |
[ |
| 225 |
$makeFilter( [ |
| 226 |
'name' => 'Comments', |
| 227 |
'rules' => '/*Pattern*/', |
| 228 |
'comments' => 'Comments', |
| 229 |
'deleted' => 0, |
| 230 |
'enabled' => 1, |
| 231 |
'hidden' => 0, |
| 232 |
'global' => 0, |
| 233 |
'group' => 'default', |
| 234 |
'actions' => [ 'warn' => [ 'abusefilter-warning' ] ] |
| 235 |
] ), |
| 236 |
$makeFilter( [ |
| 237 |
'name' => 'Comments', |
| 238 |
'rules' => '/*Pattern*/', |
| 239 |
'comments' => 'Comments', |
| 240 |
'deleted' => 0, |
| 241 |
'enabled' => 1, |
| 242 |
'hidden' => 0, |
| 243 |
'global' => 0, |
| 244 |
'group' => 'default', |
| 245 |
'actions' => [ 'disallow' => [] ] |
| 246 |
] ), |
| 247 |
[ 'actions' ] |
| 248 |
], |
| 249 |
[ |
| 250 |
$makeFilter( [ |
| 251 |
'name' => 'Comments', |
| 252 |
'rules' => '/*Pattern*/', |
| 253 |
'comments' => 'Comments', |
| 254 |
'deleted' => 0, |
| 255 |
'enabled' => 1, |
| 256 |
'hidden' => 0, |
| 257 |
'global' => 0, |
| 258 |
'group' => 'default', |
| 259 |
'actions' => [ 'warn' => [ 'abusefilter-warning' ] ] |
| 260 |
] ), |
| 261 |
$makeFilter( [ |
| 262 |
'name' => 'Comments', |
| 263 |
'rules' => '/*Pattern*/', |
| 264 |
'comments' => 'Comments', |
| 265 |
'deleted' => 0, |
| 266 |
'enabled' => 1, |
| 267 |
'hidden' => 0, |
| 268 |
'global' => 0, |
| 269 |
'group' => 'default', |
| 270 |
'actions' => [ |
| 271 |
'warn' => [ 'abusefilter-my-best-warning' ], |
| 272 |
'degroup' => [] |
| 273 |
] |
| 274 |
] ), |
| 275 |
[ 'actions' ] |
| 276 |
], |
| 277 |
[ |
| 278 |
$makeFilter( [ |
| 279 |
'name' => 'Comments', |
| 280 |
'rules' => '/*Pattern*/', |
| 281 |
'comments' => 'Comments', |
| 282 |
'deleted' => 0, |
| 283 |
'enabled' => 1, |
| 284 |
'hidden' => 0, |
| 285 |
'global' => 0, |
| 286 |
'group' => 'default', |
| 287 |
'actions' => [ 'warn' => [ 'abusefilter-warning' ] ] |
| 288 |
] ), |
| 289 |
$makeFilter( [ |
| 290 |
'name' => 'Comments', |
| 291 |
'rules' => '/*Other Pattern*/', |
| 292 |
'comments' => 'Comments', |
| 293 |
'deleted' => 0, |
| 294 |
'enabled' => 1, |
| 295 |
'hidden' => 1, |
| 296 |
'global' => 0, |
| 297 |
'group' => 'flow', |
| 298 |
'actions' => [ 'warn' => [ 'abusefilter-my-best-warning' ] ] |
| 299 |
] ), |
| 300 |
[ |
| 301 |
'af_pattern', |
| 302 |
'af_hidden', |
| 303 |
'af_group', |
| 304 |
'actions' |
| 305 |
] |
| 306 |
], |
| 307 |
[ |
| 308 |
$makeFilter( [ |
| 309 |
'name' => 'Comments', |
| 310 |
'rules' => '/*Pattern*/', |
| 311 |
'comments' => 'Comments', |
| 312 |
'deleted' => 0, |
| 313 |
'enabled' => 1, |
| 314 |
'hidden' => 0, |
| 315 |
'global' => 0, |
| 316 |
'group' => 'default', |
| 317 |
'actions' => [ 'warn' => [ 'abusefilter-beautiful-warning' ] ] |
| 318 |
] ), |
| 319 |
$makeFilter( [ |
| 320 |
'name' => 'Comments', |
| 321 |
'rules' => '/*Pattern*/', |
| 322 |
'comments' => 'Comments', |
| 323 |
'deleted' => 0, |
| 324 |
'enabled' => 1, |
| 325 |
'hidden' => 0, |
| 326 |
'global' => 0, |
| 327 |
'group' => 'flow', |
| 328 |
'actions' => [ 'warn' => [ 'abusefilter-my-best-warning' ] ] |
| 329 |
] ), |
| 330 |
[ |
| 331 |
'af_group', |
| 332 |
'actions' |
| 333 |
] |
| 334 |
], |
| 335 |
]; |
| 336 |
} |
| 337 |
|
| 338 |
public function testConstruct() { |
| 339 |
$this->assertInstanceOf( |
| 340 |
FilterCompare::class, |
| 341 |
new FilterCompare( $this->createMock( ConsequencesRegistry::class ) ) |
| 342 |
); |
| 343 |
} |
| 344 |
} |