resourceloader: Improve ResourceLoaderWikiModule test coverage
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 25 Jul 2019 22:23:24 +0000 (23:23 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 25 Jul 2019 22:35:21 +0000 (23:35 +0100)
* Remove redundant getContent() cases that were all testing the same.
  The redirect logic should indeed be tested, but exists in getContentObj(),
  not getContent(). This test was also mocking getContentObj() thus not
  actually testing what the case claims to test.
  Fortunately, the right test already exists (testGetContentForRedirects),
  so this is redundant.

* Add actual coverage of successful outcomes for getContent (previously
  they were all error/null cases), with JS content, and with CSS content.

* Fix broken test case for "Bad content model". This was not working because
  it mocked out getContentObj, thus it wasn't actually testing "bad content model",
  but rather pointlessly duplicated the previous test case.
  Fix it by actually making it use a WikitextContent object, which makes it
  test the branch that handles incompatible content models.

Change-Id: I59af5318e536c730755352e9be8f995df1f56a86

includes/resourceloader/ResourceLoaderWikiModule.php
tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php

index b104073..a2501c4 100644 (file)
@@ -412,6 +412,7 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                return $titleInfo;
        }
 
                return $titleInfo;
        }
 
+       /** @return array */
        protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
                $titleInfo = [];
                $batch = new LinkBatch;
        protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
                $titleInfo = [];
                $batch = new LinkBatch;
index 5964915..089431e 100644 (file)
@@ -311,37 +311,41 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
 
        public static function provideGetContent() {
                yield 'Bad title' => [ null, '[x]' ];
 
        public static function provideGetContent() {
                yield 'Bad title' => [ null, '[x]' ];
-               yield 'Dead redirect' => [ null, [
-                       'text' => 'Dead redirect',
-                       'title' => 'Dead_redirect',
-                       'redirect' => 1,
-               ] ];
-               yield 'Bad content model' => [ null, [
-                       'text' => 'MediaWiki:Wikitext',
-                       'ns' => NS_MEDIAWIKI,
-                       'title' => 'Wikitext',
-               ] ];
+
                yield 'No JS content found' => [ null, [
                yield 'No JS content found' => [ null, [
-                       'text' => 'MediaWiki:Script.js',
+                       'text' => 'MediaWiki:Foo.js',
                        'ns' => NS_MEDIAWIKI,
                        'ns' => NS_MEDIAWIKI,
-                       'title' => 'Script.js',
+                       'title' => 'Foo.js',
                ] ];
                ] ];
-               yield 'No CSS content found' => [ null, [
-                       'text' => 'MediaWiki:Styles.css',
+
+               yield 'JS content' => [ 'code;', [
+                       'text' => 'MediaWiki:Foo.js',
                        'ns' => NS_MEDIAWIKI,
                        'ns' => NS_MEDIAWIKI,
-                       'title' => 'Script.css',
-               ] ];
+                       'title' => 'Foo.js',
+               ], new JavaScriptContent( 'code;' ) ];
+
+               yield 'CSS content' => [ 'code {}', [
+                       'text' => 'MediaWiki:Foo.css',
+                       'ns' => NS_MEDIAWIKI,
+                       'title' => 'Foo.css',
+               ], new CssContent( 'code {}' ) ];
+
+               yield 'Wikitext content' => [ null, [
+                       'text' => 'MediaWiki:Foo',
+                       'ns' => NS_MEDIAWIKI,
+                       'title' => 'Foo',
+               ], new WikitextContent( 'code;' ) ];
        }
 
        /**
         * @dataProvider provideGetContent
         */
        }
 
        /**
         * @dataProvider provideGetContent
         */
-       public function testGetContent( $expected, $title ) {
+       public function testGetContent( $expected, $title, Content $contentObj = null ) {
                $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader );
                $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
                        ->setMethods( [ 'getContentObj' ] )->getMock();
                $module->method( 'getContentObj' )
                $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader );
                $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
                        ->setMethods( [ 'getContentObj' ] )->getMock();
                $module->method( 'getContentObj' )
-                       ->willReturn( null );
+                       ->willReturn( $contentObj );
 
                if ( is_array( $title ) ) {
                        $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];
 
                if ( is_array( $title ) ) {
                        $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];