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