Merge "objectcache: Complete code coverage for HashBagOStuff"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Sat, 1 Apr 2017 02:57:18 +0000 (02:57 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Sat, 1 Apr 2017 02:57:18 +0000 (02:57 +0000)
includes/cache/MessageBlobStore.php
includes/resourceloader/ResourceLoaderWikiModule.php
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/FauxRequestTest.php
tests/phpunit/includes/HtmlTest.php
tests/phpunit/includes/changes/RCCacheEntryFactoryTest.php
tests/phpunit/includes/resourceloader/MessageBlobStoreTest.php
tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php

index 5d48c03..14baeeb 100644 (file)
@@ -238,6 +238,7 @@ class MessageBlobStore implements LoggerAwareInterface {
                }
 
                $json = FormatJson::encode( (object)$messages );
+               // @codeCoverageIgnoreStart
                if ( $json === false ) {
                        $this->logger->warning( 'Failed to encode message blob for {module} ({lang})', [
                                'module' => $module->getName(),
@@ -245,6 +246,7 @@ class MessageBlobStore implements LoggerAwareInterface {
                        ] );
                        $json = '{}';
                }
+               // codeCoverageIgnoreEnd
                return $json;
        }
 }
index 92095f7..efa2165 100644 (file)
@@ -148,7 +148,7 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
        protected function getContent( $titleText ) {
                $title = Title::newFromText( $titleText );
                if ( !$title ) {
-                       return null;
+                       return null; // Bad title
                }
 
                // If the page is a redirect, follow the redirect.
@@ -156,7 +156,7 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                        $content = $this->getContentObj( $title );
                        $title = $content ? $content->getUltimateRedirectTarget() : null;
                        if ( !$title ) {
-                               return null;
+                               return null; // Dead redirect
                        }
                }
 
@@ -166,12 +166,12 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
                        $format = CONTENT_FORMAT_JAVASCRIPT;
                } else {
-                       return null;
+                       return null; // Bad content model
                }
 
                $content = $this->getContentObj( $title );
                if ( !$content ) {
-                       return null;
+                       return null; // No content found
                }
 
                return $content->serialize( $format );
index 419ff00..bb7267a 100644 (file)
@@ -1768,51 +1768,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $this->assertEmpty( $errors, implode( "\n", $errors ) );
        }
 
-       /**
-        * @param array $matcher
-        * @param string $actual
-        * @param bool $isHtml
-        *
-        * @return bool
-        */
-       private static function tagMatch( $matcher, $actual, $isHtml = true ) {
-               $dom = PHPUnit_Util_XML::load( $actual, $isHtml );
-               $tags = PHPUnit_Util_XML::findNodes( $dom, $matcher, $isHtml );
-               return count( $tags ) > 0 && $tags[0] instanceof DOMNode;
-       }
-
-       /**
-        * Note: we are overriding this method to remove the deprecated error
-        * @see https://phabricator.wikimedia.org/T71505
-        * @see https://github.com/sebastianbergmann/phpunit/issues/1292
-        * @deprecated
-        *
-        * @param array $matcher
-        * @param string $actual
-        * @param string $message
-        * @param bool $isHtml
-        */
-       public static function assertTag( $matcher, $actual, $message = '', $isHtml = true ) {
-               // trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
-
-               self::assertTrue( self::tagMatch( $matcher, $actual, $isHtml ), $message );
-       }
-
-       /**
-        * @see MediaWikiTestCase::assertTag
-        * @deprecated
-        *
-        * @param array $matcher
-        * @param string $actual
-        * @param string $message
-        * @param bool $isHtml
-        */
-       public static function assertNotTag( $matcher, $actual, $message = '', $isHtml = true ) {
-               // trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
-
-               self::assertFalse( self::tagMatch( $matcher, $actual, $isHtml ), $message );
-       }
-
        /**
         * Used as a marker to prevent wfResetOutputBuffers from breaking PHPUnit.
         * @return string
index e3713ab..9fe694d 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\Session\SessionManager;
+
 class FauxRequestTest extends PHPUnit_Framework_TestCase {
        /**
         * @covers FauxRequest::__construct
@@ -17,6 +19,17 @@ class FauxRequestTest extends PHPUnit_Framework_TestCase {
                $req = new FauxRequest( [], false, 'x' );
        }
 
+       /**
+        * @covers FauxRequest::__construct
+        */
+       public function testConstructWithSession() {
+               $session = SessionManager::singleton()->getEmptySession( new FauxRequest( [] ) );
+               $this->assertInstanceOf(
+                       FauxRequest::class,
+                       new FauxRequest( [], false, $session )
+               );
+       }
+
        /**
         * @covers FauxRequest::getText
         */
index b7249e5..115a8cf 100644 (file)
@@ -41,6 +41,9 @@ class HtmlTest extends MediaWikiTestCase {
 
        /**
         * @covers Html::element
+        * @covers Html::rawElement
+        * @covers Html::openElement
+        * @covers Html::closeElement
         */
        public function testElementBasics() {
                $this->assertEquals(
@@ -302,6 +305,7 @@ class HtmlTest extends MediaWikiTestCase {
 
        /**
         * @covers Html::namespaceSelector
+        * @covers Html::namespaceSelectorOptions
         */
        public function testNamespaceSelector() {
                $this->assertEquals(
index 4ff1eb8..97b4c08 100644 (file)
@@ -141,103 +141,69 @@ class RCCacheEntryFactoryTest extends MediaWikiLangTestCase {
                $this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
        }
 
+       private function assertValidHTML( $actual ) {
+               // Throws if invalid
+               $doc = PHPUnit_Util_XML::load( $actual, /* isHtml */ true );
+       }
+
        private function assertUserLinks( $user, $cacheEntry ) {
-               $this->assertTag(
-                       [
-                               'tag' => 'a',
-                               'attributes' => [
-                                       'class' => 'new mw-userlink'
-                               ],
-                               'content' => $user
-                       ],
+               $this->assertValidHTML( $cacheEntry->userlink );
+               $this->assertRegExp(
+                       '#^<a .*class="new mw-userlink".*><bdi>' . $user . '</bdi></a>#',
                        $cacheEntry->userlink,
                        'verify user link'
                );
 
-               $this->assertTag(
-                       [
-                               'tag' => 'span',
-                               'attributes' => [
-                                       'class' => 'mw-usertoollinks'
-                               ],
-                               'child' => [
-                                       'tag' => 'a',
-                                       'content' => 'talk',
-                               ]
-                       ],
+               $this->assertValidHTML( $cacheEntry->usertalklink );
+               $this->assertRegExp(
+                       '#^ <span class="mw-usertoollinks">\(.*<a .+>talk</a>.*\)</span>#',
                        $cacheEntry->usertalklink,
                        'verify user talk link'
                );
 
-               $this->assertTag(
-                       [
-                               'tag' => 'span',
-                               'attributes' => [
-                                       'class' => 'mw-usertoollinks'
-                               ],
-                               'child' => [
-                                       'tag' => 'a',
-                                       'content' => 'contribs',
-                               ]
-                       ],
+               $this->assertValidHTML( $cacheEntry->usertalklink );
+               $this->assertRegExp(
+                       '#^ <span class="mw-usertoollinks">\(.*<a .+>contribs</a>.*\)</span>$#',
                        $cacheEntry->usertalklink,
                        'verify user tool links'
                );
        }
 
        private function assertDeleteLogLink( $cacheEntry ) {
-               $this->assertTag(
-                       [
-                               'tag' => 'a',
-                               'attributes' => [
-                                       'href' => '/wiki/Special:Log/delete',
-                                       'title' => 'Special:Log/delete'
-                               ],
-                               'content' => 'Deletion log'
-                       ],
+               $this->assertEquals(
+                       '(<a href="/wiki/Special:Log/delete" title="Special:Log/delete">Deletion log</a>)',
                        $cacheEntry->link,
                        'verify deletion log link'
                );
+
+               $this->assertValidHTML( $cacheEntry->link );
        }
 
        private function assertRevDel( $cacheEntry ) {
-               $this->assertTag(
-                       [
-                               'tag' => 'span',
-                               'attributes' => [
-                                       'class' => 'history-deleted'
-                               ],
-                               'content' => '(username removed)'
-                       ],
+               $this->assertEquals(
+                       ' <span class="history-deleted">(username removed)</span>',
                        $cacheEntry->userlink,
                        'verify user link for change with deleted revision and user'
                );
+               $this->assertValidHTML( $cacheEntry->userlink );
        }
 
        private function assertTitleLink( $title, $cacheEntry ) {
-               $this->assertTag(
-                       [
-                               'tag' => 'a',
-                               'attributes' => [
-                                       'href' => '/wiki/' . $title,
-                                       'title' => $title
-                               ],
-                               'content' => $title
-                       ],
+               $this->assertEquals(
+                       '<a href="/wiki/' . $title . '" title="' . $title . '">' . $title . '</a>',
                        $cacheEntry->link,
                        'verify title link'
                );
+               $this->assertValidHTML( $cacheEntry->link );
        }
 
        private function assertQueryLink( $content, $params, $link ) {
-               $this->assertTag(
-                       [
-                               'tag' => 'a',
-                               'content' => $content
-                       ],
+               $this->assertRegExp(
+                       "#^<a .+>$content</a>$#",
                        $link,
-                       'assert query link element'
+                       'verify query link element'
                );
+               $this->assertValidHTML( $link );
 
                foreach ( $params as $key => $value ) {
                        $this->assertRegExp( '/' . $key . '=' . $value . '/', $link, "verify $key link params" );
index bf80845..29e1863 100644 (file)
@@ -44,6 +44,46 @@ class MessageBlobStoreTest extends PHPUnit_Framework_TestCase {
                return $module;
        }
 
+       /** @covers MessageBlobStore::setLogger */
+       public function testSetLogger() {
+               $blobStore = $this->makeBlobStore();
+               $this->assertSame( null, $blobStore->setLogger( new Psr\Log\NullLogger() ) );
+       }
+
+       /** @covers MessageBlobStore::getResourceLoader */
+       public function testGetResourceLoader() {
+               // Call protected method
+               $blobStore = TestingAccessWrapper::newFromObject( $this->makeBlobStore() );
+               $this->assertInstanceOf(
+                       ResourceLoader::class,
+                       $blobStore->getResourceLoader()
+               );
+       }
+
+       /** @covers MessageBlobStore::fetchMessage */
+       public function testFetchMessage() {
+               $module = $this->makeModule( [ 'mainpage' ] );
+               $rl = new ResourceLoader();
+               $rl->register( $module->getName(), $module );
+
+               $blobStore = $this->makeBlobStore( null, $rl );
+               $blob = $blobStore->getBlob( $module, 'en' );
+
+               $this->assertEquals( '{"mainpage":"Main Page"}', $blob, 'Generated blob' );
+       }
+
+       /** @covers MessageBlobStore::fetchMessage */
+       public function testFetchMessageFail() {
+               $module = $this->makeModule( [ 'i-dont-exist' ] );
+               $rl = new ResourceLoader();
+               $rl->register( $module->getName(), $module );
+
+               $blobStore = $this->makeBlobStore( null, $rl );
+               $blob = $blobStore->getBlob( $module, 'en' );
+
+               $this->assertEquals( '{"i-dont-exist":"\u29fci-dont-exist\u29fd"}', $blob, 'Generated blob' );
+       }
+
        public function testGetBlob() {
                $module = $this->makeModule( [ 'foo' ] );
                $rl = new ResourceLoader();
index 5cab8e2..2d0d958 100644 (file)
@@ -218,6 +218,113 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
        }
 
+       /**
+        * @covers ResourceLoaderWikiModule::preloadTitleInfo
+        */
+       public function testGetPreloadedBadTitle() {
+               // Mock values
+               $pages = [
+                       // Covers else branch for invalid page name
+                       '[x]' => [ 'type' => 'styles' ],
+               ];
+               $titleInfo = [];
+
+               // Set up objects
+               $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
+                       ->setMethods( [ 'getPages' ] ) ->getMock();
+               $module->method( 'getPages' )->willReturn( $pages );
+               $module::$returnFetchTitleInfo = $titleInfo;
+               $rl = new EmptyResourceLoader();
+               $rl->register( 'testmodule', $module );
+               $context = new ResourceLoaderContext( $rl, new FauxRequest() );
+
+               // Act
+               TestResourceLoaderWikiModule::preloadTitleInfo(
+                       $context,
+                       wfGetDB( DB_REPLICA ),
+                       [ 'testmodule' ]
+               );
+
+               // Assert
+               $module = TestingAccessWrapper::newFromObject( $module );
+               $this->assertEquals( $titleInfo, $module->getTitleInfo( $context ), 'Title info' );
+       }
+
+       /**
+        * @covers ResourceLoaderWikiModule::preloadTitleInfo
+        */
+       public function testGetPreloadedTitleInfoEmpty() {
+               $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
+               // Covers early return
+               $this->assertSame(
+                       null,
+                       ResourceLoaderWikiModule::preloadTitleInfo(
+                               $context,
+                               wfGetDB( DB_REPLICA ),
+                               []
+                       )
+               );
+       }
+
+       public static function provideGetContent() {
+               return [
+                       'Bad title' => [ null, '[x]' ],
+                       'Dead redirect' => [ null, [
+                               'text' => 'Dead redirect',
+                               'title' => 'Dead_redirect',
+                               'redirect' => 1,
+                       ] ],
+                       'Bad content model' => [ null, [
+                               'text' => 'MediaWiki:Wikitext',
+                               'ns' => NS_MEDIAWIKI,
+                               'title' => 'Wikitext',
+                       ] ],
+                       'No JS content found' => [ null, [
+                               'text' => 'MediaWiki:Script.js',
+                               'ns' => NS_MEDIAWIKI,
+                               'title' => 'Script.js',
+                       ] ],
+                       'No CSS content found' => [ null, [
+                               'text' => 'MediaWiki:Styles.css',
+                               'ns' => NS_MEDIAWIKI,
+                               'title' => 'Script.css',
+                       ] ],
+               ];
+       }
+
+       /**
+        * @covers ResourceLoaderWikiModule::getContent
+        * @dataProvider provideGetContent
+        */
+       public function testGetContent( $expected, $title ) {
+               $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader );
+               $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' )
+                       ->setMethods( [ 'getContentObj' ] ) ->getMock();
+               $module->expects( $this->any() )
+                       ->method( 'getContentObj' )->willReturn( null );
+
+               if ( is_array( $title ) ) {
+                       $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];
+                       $titleText = $title['text'];
+                       // Mock Title db access via LinkCache
+                       MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
+                               $title['id'],
+                               new TitleValue( $title['ns'], $title['title'] ),
+                               $title['len'],
+                               $title['redirect']
+                       );
+               } else {
+                       $titleText = $title;
+               }
+
+               $module = TestingAccessWrapper::newFromObject( $module );
+               $this->assertEquals(
+                       $expected,
+                       $module->getContent( $titleText )
+               );
+
+       }
+
        /**
         * @covers ResourceLoaderWikiModule::getContent
         */