| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Preferences\IntvalFilter; |
| 22 |
use MediaWiki\Preferences\MultiUsernameFilter; |
| 23 |
use MediaWiki\Preferences\TimezoneFilter; |
| 24 |
use MediaWiki\User\CentralId\CentralIdLookup; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class FiltersTest extends \MediaWikiUnitTestCase { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function testIntvalFilter() { |
| 35 |
$filter = new IntvalFilter(); |
| 36 |
self::assertSame( 0, $filter->filterFromForm( '0' ) ); |
| 37 |
self::assertSame( 3, $filter->filterFromForm( '3' ) ); |
| 38 |
self::assertSame( '123', $filter->filterForForm( '123' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function testTimezoneFilter( $input, $expected ) { |
| 49 |
$filter = new TimezoneFilter(); |
| 50 |
$result = $filter->filterFromForm( $input ); |
| 51 |
self::assertEquals( $expected, $result ); |
| 52 |
} |
| 53 |
|
| 54 |
public static function provideTimezoneFilter() { |
| 55 |
return [ |
| 56 |
[ 'ZoneInfo', 'Offset|0' ], |
| 57 |
[ 'ZoneInfo|bogus', 'Offset|0' ], |
| 58 |
[ 'System', 'System|0' ], |
| 59 |
[ 'System|120', 'System|0' ], |
| 60 |
[ '2:30', 'Offset|150' ], |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function testMultiUsernameFilterFrom( $input, $expected ) { |
| 72 |
$filter = $this->makeMultiUsernameFilter(); |
| 73 |
$result = $filter->filterFromForm( $input ); |
| 74 |
self::assertSame( $expected, $result ); |
| 75 |
} |
| 76 |
|
| 77 |
public static function provideMultiUsernameFilterFrom() { |
| 78 |
return [ |
| 79 |
[ '', null ], |
| 80 |
[ "\n\n\n", null ], |
| 81 |
[ 'Foo', '1' ], |
| 82 |
[ "\n\n\nFoo\nBar\n", "1\n2" ], |
| 83 |
[ "Baz\nInvalid\nFoo", "3\n1" ], |
| 84 |
[ "Invalid", null ], |
| 85 |
[ "Invalid\n\n\nInvalid\n", null ], |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function testMultiUsernameFilterFor( $input, $expected ) { |
| 97 |
$filter = $this->makeMultiUsernameFilter(); |
| 98 |
$result = $filter->filterForForm( $input ); |
| 99 |
self::assertSame( $expected, $result ); |
| 100 |
} |
| 101 |
|
| 102 |
public static function provideMultiUsernameFilterFor() { |
| 103 |
return [ |
| 104 |
[ '', '' ], |
| 105 |
[ "\n", '' ], |
| 106 |
[ '1', 'Foo' ], |
| 107 |
[ "\n1\n\n2\377\n", "Foo\nBar" ], |
| 108 |
[ "666\n667", '' ], |
| 109 |
]; |
| 110 |
} |
| 111 |
|
| 112 |
private function makeMultiUsernameFilter() { |
| 113 |
$userMapping = [ |
| 114 |
'Foo' => 1, |
| 115 |
'Bar' => 2, |
| 116 |
'Baz' => 3, |
| 117 |
]; |
| 118 |
$flipped = array_flip( $userMapping ); |
| 119 |
$idLookup = $this->getMockBuilder( CentralIdLookup::class ) |
| 120 |
->disableOriginalConstructor() |
| 121 |
->onlyMethods( [ 'centralIdsFromNames', 'namesFromCentralIds' ] ) |
| 122 |
->getMockForAbstractClass(); |
| 123 |
|
| 124 |
$idLookup->method( 'centralIdsFromNames' ) |
| 125 |
->will( self::returnCallback( static function ( $names ) use ( $userMapping ) { |
| 126 |
$ids = []; |
| 127 |
foreach ( $names as $name ) { |
| 128 |
$ids[] = $userMapping[$name] ?? null; |
| 129 |
} |
| 130 |
return array_filter( $ids, 'is_numeric' ); |
| 131 |
} ) ); |
| 132 |
$idLookup->method( 'namesFromCentralIds' ) |
| 133 |
->will( self::returnCallback( static function ( $ids ) use ( $flipped ) { |
| 134 |
$names = []; |
| 135 |
foreach ( $ids as $id ) { |
| 136 |
$names[] = $flipped[$id] ?? null; |
| 137 |
} |
| 138 |
return array_filter( $names, 'is_string' ); |
| 139 |
} ) ); |
| 140 |
|
| 141 |
return new MultiUsernameFilter( $idLookup ); |
| 142 |
} |
| 143 |
} |