}
$json = FormatJson::encode( (object)$messages );
+ // @codeCoverageIgnoreStart
if ( $json === false ) {
$this->logger->warning( 'Failed to encode message blob for {module} ({lang})', [
'module' => $module->getName(),
] );
$json = '{}';
}
+ // codeCoverageIgnoreEnd
return $json;
}
}
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.
$content = $this->getContentObj( $title );
$title = $content ? $content->getUltimateRedirectTarget() : null;
if ( !$title ) {
- return null;
+ return null; // Dead redirect
}
}
} 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 );
$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
<?php
+use MediaWiki\Session\SessionManager;
+
class FauxRequestTest extends PHPUnit_Framework_TestCase {
/**
* @covers FauxRequest::__construct
$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
*/
/**
* @covers Html::element
+ * @covers Html::rawElement
+ * @covers Html::openElement
+ * @covers Html::closeElement
*/
public function testElementBasics() {
$this->assertEquals(
/**
* @covers Html::namespaceSelector
+ * @covers Html::namespaceSelectorOptions
*/
public function testNamespaceSelector() {
$this->assertEquals(
$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" );
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();
$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
*/