| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\ResourceLoader; |
| 4 |
|
| 5 |
use MediaWiki\ResourceLoader\Context; |
| 6 |
use MediaWiki\ResourceLoader\UserOptionsModule; |
| 7 |
use MediaWiki\User\Options\StaticUserOptionsLookup; |
| 8 |
use MediaWiki\User\User; |
| 9 |
use MediaWikiIntegrationTestCase; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class UserOptionsModuleTest extends MediaWikiIntegrationTestCase { |
| 16 |
|
| 17 |
public function testGetScript() { |
| 18 |
$module = new UserOptionsModule(); |
| 19 |
$hooks = $this->createHookContainer(); |
| 20 |
$module->setHookContainer( $hooks ); |
| 21 |
$options = new StaticUserOptionsLookup( |
| 22 |
[ |
| 23 |
'Example1' => [], |
| 24 |
'Example2' => [ 'y' => '1', 'userjs-extra' => '1' ], |
| 25 |
], |
| 26 |
[ |
| 27 |
'x' => '1', |
| 28 |
'y' => '0', |
| 29 |
'foobar' => 'Rhabarberbarbara', |
| 30 |
'ipsum' => 'consectetur adipiscing elit', |
| 31 |
] |
| 32 |
); |
| 33 |
$this->setService( 'UserOptionsLookup', $options ); |
| 34 |
|
| 35 |
$script = $module->getScript( $this->makeContext() ); |
| 36 |
$this->assertStringContainsString( |
| 37 |
'"csrfToken":', |
| 38 |
$script, |
| 39 |
'always send csrfToken' |
| 40 |
); |
| 41 |
$this->assertStringNotContainsString( |
| 42 |
'Rhabarberbarbara', |
| 43 |
$script, |
| 44 |
'no default settings sent' |
| 45 |
); |
| 46 |
$this->assertStringNotContainsString( |
| 47 |
'mw.user.options.set', |
| 48 |
$script, |
| 49 |
'no in-page blob for anon default settings' |
| 50 |
); |
| 51 |
|
| 52 |
$script = $module->getScript( $this->makeContext( 'Example1' ) ); |
| 53 |
$this->assertStringNotContainsString( |
| 54 |
'mw.user.options.set', |
| 55 |
$script, |
| 56 |
'no in-page blob for logged-in default settings' |
| 57 |
); |
| 58 |
|
| 59 |
$script = $module->getScript( $this->makeContext( 'Example2' ) ); |
| 60 |
$this->assertStringContainsString( |
| 61 |
'mw.user.options.set', |
| 62 |
$script, |
| 63 |
'send blob for non-default settings' |
| 64 |
); |
| 65 |
$this->assertStringContainsString( |
| 66 |
'"y":"1"', |
| 67 |
$script, |
| 68 |
'send overridden value' |
| 69 |
); |
| 70 |
$this->assertStringContainsString( |
| 71 |
'"userjs-extra":"1"', |
| 72 |
$script, |
| 73 |
'send custom preference keys' |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
public function testResourceLoaderExcludeUserOptionsHook() { |
| 78 |
$module = new UserOptionsModule(); |
| 79 |
$hooks = $this->createHookContainer( [ |
| 80 |
'ResourceLoaderExcludeUserOptions' => static function ( |
| 81 |
array &$keysToExclude, |
| 82 |
Context $context |
| 83 |
): void { |
| 84 |
$keysToExclude[] = 'exclude-explicit'; |
| 85 |
$keysToExclude[] = 'exclude-default'; |
| 86 |
} |
| 87 |
] ); |
| 88 |
$module->setHookContainer( $hooks ); |
| 89 |
$options = new StaticUserOptionsLookup( |
| 90 |
[ |
| 91 |
'User' => [ 'include-explicit' => '1', 'exclude-explicit' => '1' ], |
| 92 |
], |
| 93 |
[ |
| 94 |
'exclude-default' => '1', |
| 95 |
] |
| 96 |
); |
| 97 |
$this->setService( 'UserOptionsLookup', $options ); |
| 98 |
|
| 99 |
$script = $module->getScript( $this->makeContext( 'User' ) ); |
| 100 |
$this->assertStringContainsString( |
| 101 |
'include-explicit', |
| 102 |
$script, |
| 103 |
'normal behavior' |
| 104 |
); |
| 105 |
$this->assertStringNotContainsString( |
| 106 |
'exclude-explicit', |
| 107 |
$script, |
| 108 |
'$keysToExclude filters' |
| 109 |
); |
| 110 |
|
| 111 |
$this->assertStringNotContainsString( |
| 112 |
'exclude-default', |
| 113 |
$script, |
| 114 |
'default excluded' |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
private function makeContext( ?string $name = null ) { |
| 119 |
$user = $this->createStub( User::class ); |
| 120 |
if ( $name ) { |
| 121 |
$user->method( 'isRegistered' )->willReturn( true ); |
| 122 |
$user->method( 'getName' )->willReturn( $name ); |
| 123 |
} |
| 124 |
$ctx = $this->createStub( Context::class ); |
| 125 |
$ctx->method( 'encodeJson' )->willReturnCallback( 'json_encode' ); |
| 126 |
$ctx->method( 'getUserObj' )->willReturn( $user ); |
| 127 |
return $ctx; |
| 128 |
} |
| 129 |
} |