Fix tests so getName() doesn't return null

In a comment on I248f828f69d404d2bad8fe8c878e6204ca4b021e, Krinkle
says a module name should never be null for RL.

Change-Id: I1c03e3f47086a1cbe7175deb6aca1357735978dc
This commit is contained in:
Mark A. Hershberger 2022-07-13 14:08:43 -04:00 committed by MarkAHershberger
parent a42c2e1e71
commit 3a9744921d
5 changed files with 34 additions and 6 deletions

View file

@ -417,6 +417,7 @@ class FileModuleTest extends ResourceLoaderTestCase {
'scripts' => new FilePath( 'script.js', $filePath, 'rlfilepath' ),
'templates' => new FilePath( 'template.html', $filePath, 'rlfilepath' ),
] );
$testModule->setName( 'testModule' );
$expectedModule = new FileModule( [
'localBasePath' => $filePath,
'remoteBasePath' => 'rlfilepath',
@ -427,6 +428,7 @@ class FileModuleTest extends ResourceLoaderTestCase {
'scripts' => 'script.js',
'templates' => 'template.html',
] );
$expectedModule->setName( 'expectedModule' );
$context = $this->getResourceLoaderContext();
$this->assertEquals(

View file

@ -179,6 +179,7 @@ class ImageModuleTest extends ResourceLoaderTestCase {
],
],
] );
$testModule->setName( 'testModule' );
$expectedModule = new ImageModule( [
'localBasePath' => $filePath,
'remoteBasePath' => 'rlfilepath',
@ -193,6 +194,7 @@ class ImageModuleTest extends ResourceLoaderTestCase {
],
],
] );
$expectedModule->setName( 'testModule' );
$context = $this->getResourceLoaderContext();
$this->assertEquals(

View file

@ -26,10 +26,12 @@ class ModuleTest extends ResourceLoaderTestCase {
];
$module = new FileModule( $baseParams );
$module->setName( "" );
$version = json_encode( $module->getVersionHash( $context ) );
// Exactly the same
$module = new FileModule( $baseParams );
$module->setName( "" );
$this->assertEquals(
$version,
json_encode( $module->getVersionHash( $context ) ),
@ -40,6 +42,7 @@ class ModuleTest extends ResourceLoaderTestCase {
$module = new FileModule( [
'dependencies' => [ 'mediawiki', 'jquery' ],
] + $baseParams );
$module->setName( "" );
$this->assertEquals(
$version,
json_encode( $module->getVersionHash( $context ) ),
@ -50,6 +53,7 @@ class ModuleTest extends ResourceLoaderTestCase {
$module = new FileModule( [
'messages' => [ 'world', 'hello' ],
] + $baseParams );
$module->setName( "" );
$this->assertEquals(
$version,
json_encode( $module->getVersionHash( $context ) ),
@ -60,6 +64,7 @@ class ModuleTest extends ResourceLoaderTestCase {
$module = new FileModule( [
'scripts' => [ 'bar.js', 'foo.js' ],
] + $baseParams );
$module->setName( "" );
$this->assertNotEquals(
$version,
json_encode( $module->getVersionHash( $context ) ),
@ -68,6 +73,7 @@ class ModuleTest extends ResourceLoaderTestCase {
// Subclass
$module = new ResourceLoaderFileModuleTestingSubclass( $baseParams );
$module->setName( "" );
$this->assertNotEquals(
$version,
json_encode( $module->getVersionHash( $context ) ),
@ -80,6 +86,7 @@ class ModuleTest extends ResourceLoaderTestCase {
*/
public function testGetVersionHash_debug() {
$module = new ResourceLoaderTestModule( [ 'script' => 'foo();' ] );
$module->setName( "" );
$context = $this->getResourceLoaderContext( [ 'debug' => 'true' ] );
$this->assertSame( '', $module->getVersionHash( $context ) );
}
@ -92,6 +99,7 @@ class ModuleTest extends ResourceLoaderTestCase {
$module = new ResourceLoaderTestModule( [
'script' => 'foo();'
] );
$module->setName( "" );
$version = $module->getVersionHash( $context );
$this->assertSame( ResourceLoader::HASH_LENGTH, strlen( $version ), 'Hash length' );
}
@ -104,6 +112,7 @@ class ModuleTest extends ResourceLoaderTestCase {
$module = $this->getMockBuilder( Module::class )
->onlyMethods( [ 'getDefinitionSummary' ] )->getMock();
$module->method( 'getDefinitionSummary' )->willReturn( [ 'a' => 'summary' ] );
$module->setName( "" );
$this->expectException( LogicException::class );
$this->expectExceptionMessage( 'must call parent' );
@ -122,6 +131,7 @@ class ModuleTest extends ResourceLoaderTestCase {
] );
$context = $this->getResourceLoaderContext( [ 'debug' => 'true' ] );
$module->setConfig( $context->getResourceLoader()->getConfig() );
$module->setName( "" );
$this->assertEquals(
[
@ -229,6 +239,7 @@ class ModuleTest extends ResourceLoaderTestCase {
$module = new ResourceLoaderTestModule( [
'script' => $raw
] );
$module->setName( "" );
$this->assertEquals( $raw, $module->getScript( $context ), 'Raw script' );
$this->assertEquals(
$build,
@ -282,6 +293,7 @@ class ModuleTest extends ResourceLoaderTestCase {
$context = $this->getResourceLoaderContext();
$module = new ResourceLoaderTestModule();
$module->setName( "" );
$this->assertSame( [], $module->getHeaders( $context ), 'Default' );
$module = $this->getMockBuilder( ResourceLoaderTestModule::class )
@ -303,6 +315,7 @@ class ModuleTest extends ResourceLoaderTestCase {
'https://example.org/script.js' => [ 'as' => 'script' ],
'/example.png' => [ 'as' => 'image' ],
] );
$module->setName( "" );
$this->assertSame(
[
'Link: <https://example.org/script.js>;rel=preload;as=script,' .

View file

@ -672,27 +672,30 @@ END
protected function getFailFerryMock( $getter = 'getScript' ) {
$mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
->onlyMethods( [ $getter ] )
->onlyMethods( [ $getter, 'getName' ] )
->getMock();
$mock->method( $getter )->will( $this->throwException(
new Exception( 'Ferry not found' )
) );
$mock->method( 'getName' )->willReturn( __METHOD__ );
return $mock;
}
protected function getSimpleModuleMock( $script = '' ) {
$mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
->onlyMethods( [ 'getScript' ] )
->onlyMethods( [ 'getScript', 'getName' ] )
->getMock();
$mock->method( 'getScript' )->willReturn( $script );
$mock->method( 'getName' )->willReturn( __METHOD__ );
return $mock;
}
protected function getSimpleStyleModuleMock( $styles = '' ) {
$mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
->onlyMethods( [ 'getStyles' ] )
->onlyMethods( [ 'getStyles', 'getName' ] )
->getMock();
$mock->method( 'getStyles' )->willReturn( [ '' => $styles ] );
$mock->method( 'getName' )->willReturn( __METHOD__ );
return $mock;
}
@ -962,10 +965,11 @@ END
*/
public function testMakeModuleResponseExtraHeaders() {
$module = $this->getMockBuilder( ResourceLoaderTestModule::class )
->onlyMethods( [ 'getPreloadLinks' ] )->getMock();
->onlyMethods( [ 'getPreloadLinks', 'getName' ] )->getMock();
$module->method( 'getPreloadLinks' )->willReturn( [
'https://example.org/script.js' => [ 'as' => 'script' ],
] );
$module->method( 'getName' )->willReturn( __METHOD__ );
$rl = new EmptyResourceLoader();
$context = $this->getResourceLoaderContext(
@ -988,17 +992,19 @@ END
public function testMakeModuleResponseExtraHeadersMulti() {
$foo = $this->getMockBuilder( ResourceLoaderTestModule::class )
->onlyMethods( [ 'getPreloadLinks' ] )->getMock();
->onlyMethods( [ 'getPreloadLinks', 'getName' ] )->getMock();
$foo->method( 'getPreloadLinks' )->willReturn( [
'https://example.org/script.js' => [ 'as' => 'script' ],
] );
$foo->method( 'getName' )->willReturn( __METHOD__ );
$bar = $this->getMockBuilder( ResourceLoaderTestModule::class )
->onlyMethods( [ 'getPreloadLinks' ] )->getMock();
->onlyMethods( [ 'getPreloadLinks', 'getName' ] )->getMock();
$bar->method( 'getPreloadLinks' )->willReturn( [
'/example.png' => [ 'as' => 'image' ],
'/example.jpg' => [ 'as' => 'image' ],
] );
$bar->method( 'getName' )->willReturn( __METHOD__ );
$rl = new EmptyResourceLoader();
$context = $this->getResourceLoaderContext(

View file

@ -899,6 +899,7 @@ mw.loader.register([
] );
$module = new StartUpModule();
$module->setConfig( $rl1->getConfig() );
$module->setName( "" );
$version1 = $module->getVersionHash( $context1 );
$context2 = $this->getResourceLoaderContext();
@ -909,6 +910,7 @@ mw.loader.register([
] );
$module = new StartUpModule();
$module->setConfig( $rl2->getConfig() );
$module->setName( "" );
$version2 = $module->getVersionHash( $context2 );
$context3 = $this->getResourceLoaderContext();
@ -922,6 +924,7 @@ mw.loader.register([
] );
$module = new StartUpModule();
$module->setConfig( $rl3->getConfig() );
$module->setName( "" );
$version3 = $module->getVersionHash( $context3 );
// Module name *is* significant (T201686)
@ -952,6 +955,7 @@ mw.loader.register([
] );
$module = new StartUpModule();
$module->setConfig( $rl->getConfig() );
$module->setName( "" );
$version1 = $module->getVersionHash( $context );
$context = $this->getResourceLoaderContext();
@ -964,6 +968,7 @@ mw.loader.register([
] );
$module = new StartUpModule();
$module->setConfig( $rl->getConfig() );
$module->setName( "" );
$version2 = $module->getVersionHash( $context );
// Dependencies *are* significant (T201686)