Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F6082192
TemplateDataHooksTest.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
TemplateDataHooksTest.php
View Options
<?php
use
MediaWiki\Extension\TemplateData\Hooks
;
use
MediaWiki\User\UserIdentity
;
/**
* @group Database
* @covers \MediaWiki\Extension\TemplateData\Hooks
* @license GPL-2.0-or-later
*/
class
TemplateDataHooksTest
extends
MediaWikiIntegrationTestCase
{
private
Hooks
$hookHandler
;
/**
* @inheritDoc
*/
protected
function
setUp
():
void
{
parent
::
setUp
();
// TODO: Am I meant to mock the config?
$this
->
hookHandler
=
new
Hooks
(
$this
->
getServiceContainer
()->
getMainConfig
()
);
}
/**
* Test situations where Hooks::onSaveUserOptions will abort saving
*
* @param array $originalOptions
* @param array $modifiedOptions
* @param array $overrideConfig
*
* @covers \MediaWiki\Extension\TemplateData\Hooks::onSaveUserOptions
* @dataProvider provideOnSaveUserOptionsAbort
*/
public
function
testOnSaveUserOptionsAbort
(
$originalOptions
,
$modifiedOptions
,
$overrideConfig
=
[]
)
{
if
(
$overrideConfig
)
{
$this
->
overrideConfigValues
(
$overrideConfig
);
}
$user
=
$this
->
createMock
(
UserIdentity
::
class
);
$this
->
hookHandler
->
onSaveUserOptions
(
$user
,
$modifiedOptions
,
$originalOptions
);
// Assert that the array key 'templatedata-favorite-templates' is not set
$this
->
assertArrayNotHasKey
(
'templatedata-favorite-templates'
,
$modifiedOptions
);
}
/**
* Test situations where Hooks::onSaveUserOptions will modify the options
*
* @param array $originalOptions
* @param array $modifiedOptions
* @param array $expectedOptions
* @param array $overrideConfig
*
* @covers \MediaWiki\Extension\TemplateData\Hooks::onSaveUserOptions
* @dataProvider provideOnSaveUserOptions
*/
public
function
testOnSaveUserOptions
(
$originalOptions
,
$modifiedOptions
,
$expectedOptions
,
$overrideConfig
=
[]
)
{
if
(
$overrideConfig
)
{
$this
->
overrideConfigValues
(
$overrideConfig
);
}
$user
=
$this
->
createMock
(
UserIdentity
::
class
);
$this
->
hookHandler
->
onSaveUserOptions
(
$user
,
$modifiedOptions
,
$originalOptions
);
// Assert that the array key 'templatedata-favorite-templates' is set
$this
->
assertArrayHasKey
(
'templatedata-favorite-templates'
,
$modifiedOptions
);
// Assert that the options have been modified as expected
$this
->
assertSame
(
$expectedOptions
[
'templatedata-favorite-templates'
],
$modifiedOptions
[
'templatedata-favorite-templates'
]
);
}
/**
* Provide data for testOnSaveUserOptionsAbort
* Arrays are, in order:
* - original options
* - modified options
* - config overrides (optional)
*
* @return array
*/
public
static
function
provideOnSaveUserOptionsAbort
()
{
return
[
'more than TemplateDataMaxFavorites'
=>
[
[
'templatedata-favorite-templates'
=>
'[7,15,24]'
],
[
'templatedata-favorite-templates'
=>
'[7,15,24,1,2,3,4]'
],
[
'TemplateDataMaxFavorites'
=>
5
],
],
'invalid JSON'
=>
[
[
'templatedata-favorite-templates'
=>
'[7,15,24]'
],
[
'templatedata-favorite-templates'
=>
'fox'
],
],
];
}
/**
* Provide data for testOnSaveUserOptions
* Arrays are, in order:
* - original options
* - modified options
* - expected options after onSaveUserOptions has validated
* - config overrides (optional)
*
* @return array
*/
public
static
function
provideOnSaveUserOptions
()
{
return
[
'no change'
=>
[
[
'templatedata-favorite-templates'
=>
'[7,15,24]'
],
[
'templatedata-favorite-templates'
=>
'[7,15,24]'
],
[
'templatedata-favorite-templates'
=>
'[7,15,24]'
],
],
'added first favorite'
=>
[
[
'templatedata-favorite-templates'
=>
'[]'
],
[
'templatedata-favorite-templates'
=>
'[7]'
],
[
'templatedata-favorite-templates'
=>
'[7]'
],
],
'removed all favorites'
=>
[
[
'templatedata-favorite-templates'
=>
'[7,15,24]'
],
[
'templatedata-favorite-templates'
=>
'[]'
],
[
'templatedata-favorite-templates'
=>
'[]'
],
],
'removed duplicate'
=>
[
[
'templatedata-favorite-templates'
=>
'[7,15,24]'
],
[
'templatedata-favorite-templates'
=>
'[7,15,24,24,17]'
],
[
'templatedata-favorite-templates'
=>
'[7,15,24,17]'
],
],
'removed non-integers'
=>
[
[
'templatedata-favorite-templates'
=>
'[7,15,24]'
],
[
'templatedata-favorite-templates'
=>
'[7,15,24,"fox",8]'
],
[
'templatedata-favorite-templates'
=>
'[7,15,24,8]'
],
],
'removed integers less than 1'
=>
[
[
'templatedata-favorite-templates'
=>
'[7,15,24]'
],
[
'templatedata-favorite-templates'
=>
'[7,15,24,-1,8]'
],
[
'templatedata-favorite-templates'
=>
'[7,15,24,8]'
],
],
'combination'
=>
[
[
'templatedata-favorite-templates'
=>
'[7]'
],
[
'templatedata-favorite-templates'
=>
'[7,15,24,24,17,"fox",-1,8]'
],
[
'templatedata-favorite-templates'
=>
'[7,15,24,17,8]'
],
],
];
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Oct 5 2026, 11:58 (6 w, 1 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
71/7a/df09c4060114b516ba6dadeed6ec
Default Alt Text
TemplateDataHooksTest.php (4 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment