Debian Codegraph @

extensions/AbuseFilter/tests/phpunit/unit/GlobalNameUtilsTest.php

raw symbols SCIP
1 <?php
2
3 /**
4 * Generic tests for utility functions in AbuseFilter that do NOT require DB access
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 *
23 * @license GPL-2.0-or-later
24 */
25
26 use MediaWiki\Extension\AbuseFilter\GlobalNameUtils;
27
28 /**
29 * @group Test
30 * @group AbuseFilter
31 * @group AbuseFilterGeneric
32 * @covers \MediaWiki\Extension\AbuseFilter\GlobalNameUtils
33 */
34 class GlobalNameUtilsTest extends MediaWikiUnitTestCase {
35
36 /**
37 * @dataProvider provideBuildGlobalName
38 */
39 public function testBuildGlobalName( int $id, bool $global, string $expected ) {
40 $name = GlobalNameUtils::buildGlobalName( $id, $global );
41 $filterDef = GlobalNameUtils::splitGlobalName( $name );
42 $this->assertSame( $expected, $name );
43 $this->assertSame( [ $id, $global ], $filterDef );
44 }
45
46 public static function provideBuildGlobalName(): array {
47 return [
48 [ 1, false, '1' ],
49 [ 2, true, 'global-2' ],
50 ];
51 }
52
53 /**
54 * @param string $name The name of a filter
55 * @param array|null $expected If array, the expected result like [ id, isGlobal ].
56 * If null it means that we're expecting an exception.
57 * @dataProvider provideGlobalNames
58 */
59 public function testSplitGlobalName( $name, $expected ) {
60 if ( $expected !== null ) {
61 $actual = GlobalNameUtils::splitGlobalName( $name );
62 $this->assertSame( $expected, $actual );
63 } else {
64 $this->expectException( InvalidArgumentException::class );
65 GlobalNameUtils::splitGlobalName( $name );
66 }
67 }
68
69 public static function provideGlobalNames() {
70 return [
71 [ '15', [ 15, false ] ],
72 [ 15, [ 15, false ] ],
73 [ 'global-1', [ 1, true ] ],
74 [ 'new', null ],
75 [ false, null ],
76 [ 'global-15-global', null ],
77 [ 0, [ 0, false ] ],
78 [ 'global-', null ],
79 [ 'global-lol', null ],
80 [ 'global-17.2', null ],
81 [ '17,2', null ],
82 ];
83 }
84 }