From 24559cd1af456535fb7cdcc5fbb63091a08f9830 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Thu, 13 Dec 2018 22:38:03 +0100 Subject: [PATCH] Add basic tests for ULS' ResourceLoader modules This is a starting point to increase the test coverage in this code base. Change-Id: I215713e928e9b55615afd17ccf7a7082ef559e01 --- ...ResourceLoaderULSJsonMessageModuleTest.php | 35 ++++++++++++ tests/phpunit/ResourceLoaderULSModuleTest.php | 29 ++++++++++ tests/phpunit/ULSJsonMessageLoaderTest.php | 54 +++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 tests/phpunit/ResourceLoaderULSJsonMessageModuleTest.php create mode 100644 tests/phpunit/ResourceLoaderULSModuleTest.php create mode 100644 tests/phpunit/ULSJsonMessageLoaderTest.php diff --git a/tests/phpunit/ResourceLoaderULSJsonMessageModuleTest.php b/tests/phpunit/ResourceLoaderULSJsonMessageModuleTest.php new file mode 100644 index 00000000..739d3908 --- /dev/null +++ b/tests/phpunit/ResourceLoaderULSJsonMessageModuleTest.php @@ -0,0 +1,35 @@ +createMock( \ResourceLoaderContext::class ); + $context->method( 'getLanguage' ) + ->willReturn( 'en' ); + + $this->assertContainsOnly( 'string', $instance->getDependencies(), 'dependencies' ); + $this->assertContainsOnly( 'string', $instance->getTargets(), 'targets' ); + + $summary = $instance->getDefinitionSummary( $context ); + $lastElement = end( $summary ); + $this->assertArrayHasKey( 'fileHashes', $lastElement ); + $this->assertContainsOnly( 'string', $lastElement['fileHashes'] ); + + $script = $instance->getScript( $context ); + $this->assertStringStartsWith( 'mw.uls.loadLocalization("en",{"', $script ); + } + +} diff --git a/tests/phpunit/ResourceLoaderULSModuleTest.php b/tests/phpunit/ResourceLoaderULSModuleTest.php new file mode 100644 index 00000000..5036f621 --- /dev/null +++ b/tests/phpunit/ResourceLoaderULSModuleTest.php @@ -0,0 +1,29 @@ +createMock( \ResourceLoaderContext::class ); + $context->method( 'getLanguage' ) + ->willReturn( 'en' ); + + $script = $instance->getScript( $context ); + $this->assertStringStartsWith( 'mw.config.set({"wgULSLanguages":{"', $script ); + + $this->assertTrue( $instance->enableModuleContentVersion() ); + } + +} diff --git a/tests/phpunit/ULSJsonMessageLoaderTest.php b/tests/phpunit/ULSJsonMessageLoaderTest.php new file mode 100644 index 00000000..817f2500 --- /dev/null +++ b/tests/phpunit/ULSJsonMessageLoaderTest.php @@ -0,0 +1,54 @@ +setExpectedException( \MWException::class ); + $instance->getFilenames( null ); + } + + public function testGetMessagesWithBadInput() { + $instance = new ULSJsonMessageLoader(); + + $this->setExpectedException( \MWException::class ); + $instance->getMessages( null ); + } + + public function testWithInvalidLanguageCode() { + $instance = new ULSJsonMessageLoader(); + $languageCode = '0'; + + $filenames = $instance->getFilenames( $languageCode ); + $this->assertSame( [], $filenames ); + + $messages = $instance->getMessages( $languageCode ); + $this->assertSame( [], $messages ); + } + + public function testWithValidLanguageCode() { + $instance = new ULSJsonMessageLoader(); + $languageCode = 'en'; + + $filenames = $instance->getFilenames( $languageCode ); + $this->assertContainsOnly( 'string', $filenames ); + + $messages = $instance->getMessages( $languageCode ); + unset( $messages['@metadata'] ); + $this->assertContainsOnly( 'string', array_keys( $messages ) ); + $this->assertContainsOnly( 'string', $messages ); + } + +}