Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F5957046
FilterImporterTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
FilterImporterTest.php
View Options
<?php
namespace
MediaWiki\Extension\AbuseFilter\Tests\Unit
;
use
Generator
;
use
MediaWiki\Config\ServiceOptions
;
use
MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesRegistry
;
use
MediaWiki\Extension\AbuseFilter\Filter\Filter
;
use
MediaWiki\Extension\AbuseFilter\Filter\Flags
;
use
MediaWiki\Extension\AbuseFilter\Filter\LastEditInfo
;
use
MediaWiki\Extension\AbuseFilter\Filter\MutableFilter
;
use
MediaWiki\Extension\AbuseFilter\Filter\Specs
;
use
MediaWiki\Extension\AbuseFilter\FilterImporter
;
use
MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner
;
use
MediaWiki\Extension\AbuseFilter\InvalidImportDataException
;
use
MediaWikiUnitTestCase
;
/**
* @group Test
* @group AbuseFilter
* @covers \MediaWiki\Extension\AbuseFilter\FilterImporter
*/
class
FilterImporterTest
extends
MediaWikiUnitTestCase
{
private
const
GOOD_FILTER_DATA
=
[
'rules'
=>
'foobar'
,
'name'
=>
'foobar'
,
'comments'
=>
'foobar'
,
'group'
=>
'foobar'
,
'actions'
=>
[],
'enabled'
=>
true
,
'deleted'
=>
false
,
'privacyLevel'
=>
true
,
'global'
=>
false
];
/**
* @param string[]|null $groups
* @param bool|null $isCentral
* @param string[]|null $actions
* @return FilterImporter
*/
private
function
getImporter
(
?
array
$groups
=
null
,
?
bool
$isCentral
=
null
,
?
array
$actions
=
null
):
FilterImporter
{
$actions
=
array_fill_keys
(
$actions
??
[
'warn'
,
'disallow'
,
'block'
],
true
);
$registry
=
new
ConsequencesRegistry
(
$this
->
createMock
(
AbuseFilterHookRunner
::
class
),
$actions
);
return
new
FilterImporter
(
new
ServiceOptions
(
FilterImporter
::
CONSTRUCTOR_OPTIONS
,
[
'AbuseFilterValidGroups'
=>
$groups
??
[
'default'
],
'AbuseFilterIsCentral'
=>
$isCentral
??
false
,
]
),
$registry
);
}
public
function
testEncodeData
()
{
$importer
=
$this
->
getImporter
();
$filter
=
MutableFilter
::
newDefault
();
$actions
=
[
'disallow'
=>
[]
];
$this
->
assertIsString
(
$importer
->
encodeData
(
$filter
,
$actions
)
);
}
/**
* @param mixed $data
* @dataProvider provideInvalidData
*/
public
function
testDecodeData_invalid
(
$data
)
{
$importer
=
$this
->
getImporter
();
$this
->
expectException
(
InvalidImportDataException
::
class
);
$importer
->
decodeData
(
$data
);
}
public
static
function
provideInvalidData
()
{
$cases
=
[
'non-object'
=>
'foo'
,
'bad top-level keys'
=>
(
object
)[
'foo'
=>
1
],
'non-object data'
=>
(
object
)[
'data'
=>
null
],
'wrong actions type'
=>
(
object
)[
'data'
=>
(
object
)[],
'actions'
=>
true
],
'wrong data keys'
=>
(
object
)[
'data'
=>
(
object
)[
'foobar'
=>
42
],
'actions'
=>
[]
],
'wrong action name'
=>
(
object
)[
'data'
=>
(
object
)
self
::
GOOD_FILTER_DATA
,
'actions'
=>
[
'unknown'
=>
[]
]
],
'non-array action params'
=>
(
object
)[
'data'
=>
(
object
)
self
::
GOOD_FILTER_DATA
,
'actions'
=>
[
'block'
=>
true
]
],
];
foreach
(
$cases
as
$name
=>
$case
)
{
yield
$name
=>
[
json_encode
(
$case
)
];
}
}
/**
* @param Filter $origFilter
* @param array $origActions
* @param Filter $expectedFilter
* @param array $configOptions
* @dataProvider provideRoundTrip
*/
public
function
testRoundTrip
(
Filter
$origFilter
,
array
$origActions
,
Filter
$expectedFilter
,
array
$configOptions
)
{
$importer
=
$this
->
getImporter
(
$configOptions
[
'groups'
]
??
null
,
$configOptions
[
'central'
]
??
null
,
$configOptions
[
'actions'
]
??
null
);
$actualFilter
=
$importer
->
decodeData
(
$importer
->
encodeData
(
$origFilter
,
$origActions
)
);
$this
->
assertEquals
(
$expectedFilter
,
$actualFilter
);
}
/**
* @return Generator
*/
public
static
function
provideRoundTrip
():
Generator
{
$actions
=
[
'block'
=>
[],
'warn'
=>
[]
];
$filter
=
new
MutableFilter
(
new
Specs
(
'rules'
,
'comments'
,
'name'
,
array_keys
(
$actions
),
'some-group'
),
new
Flags
(
true
,
false
,
true
,
true
),
$actions
,
new
LastEditInfo
(
0
,
''
,
''
)
);
yield
'normal'
=>
[
$filter
,
$actions
,
$filter
,
[
'central'
=>
true
,
'groups'
=>
[
'default'
,
'some-group'
]
]
];
$expFilter
=
clone
$filter
;
$expFilter
->
setGroup
(
'default'
);
yield
'group not in use'
=>
[
$filter
,
$actions
,
$expFilter
,
[
'central'
=>
true
,
'groups'
=>
[
'default'
]
]
];
$expFilter
=
clone
$filter
;
$expFilter
->
setGlobal
(
false
);
yield
'cannot create as global'
=>
[
$filter
,
$actions
,
$expFilter
,
[
'groups'
=>
[
'default'
,
'some-group'
]
]
];
}
public
function
testConstruct
()
{
$this
->
assertInstanceOf
(
FilterImporter
::
class
,
new
FilterImporter
(
$this
->
createMock
(
ServiceOptions
::
class
),
$this
->
createMock
(
ConsequencesRegistry
::
class
)
)
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Oct 3, 04:50 (3 w, 1 h ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
95/79/cdbeb8d0d135351f7fcb9dbc8266
Default Alt Text
FilterImporterTest.php (4 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment