* @group Output
*
* @todo factor tests in this class into providers and test methods
- *
*/
class OutputPageTest extends MediaWikiTestCase {
const SCREEN_MEDIA_QUERY = 'screen and (min-width: 982px)';
const SCREEN_ONLY_MEDIA_QUERY = 'only screen and (min-width: 982px)';
+ /**
+ * @covers OutputPage::addMeta
+ * @covers OutputPage::getMetaTags
+ * @covers OutputPage::getHeadLinksArray
+ */
+ public function testMetaTags() {
+ $outputPage = $this->newInstance();
+ $outputPage->addMeta( 'http:expires', '0' );
+ $outputPage->addMeta( 'keywords', 'first' );
+ $outputPage->addMeta( 'keywords', 'second' );
+
+ $expected = [
+ [ 'http:expires', '0' ],
+ [ 'keywords', 'first' ],
+ [ 'keywords', 'second' ],
+ ];
+ $this->assertSame( $expected, $outputPage->getMetaTags() );
+
+ $links = $outputPage->getHeadLinksArray();
+ $this->assertContains( '<meta http-equiv="expires" content="0"/>', $links );
+ $this->assertContains( '<meta name="keywords" content="first"/>', $links );
+ $this->assertContains( '<meta name="keywords" content="second"/>', $links );
+ $this->assertArrayNotHasKey( 'meta-robots', $links );
+ }
+
+ /**
+ * @covers OutputPage::setIndexPolicy
+ * @covers OutputPage::setFollowPolicy
+ * @covers OutputPage::getHeadLinksArray
+ */
+ public function testRobotsPolicies() {
+ $outputPage = $this->newInstance();
+ $outputPage->setIndexPolicy( 'noindex' );
+ $outputPage->setFollowPolicy( 'nofollow' );
+
+ $links = $outputPage->getHeadLinksArray();
+ $this->assertContains( '<meta name="robots" content="noindex,nofollow"/>', $links );
+ }
+
/**
* Tests a particular case of transformCssMedia, using the given input, globals,
* expected return, and message
] );
}
+ public static function provideTransformFilePath() {
+ $baseDir = dirname( __DIR__ ) . '/data/media';
+ return [
+ // File that matches basePath, and exists. Hash found and appended.
+ [ 'baseDir' => $baseDir, 'basePath' => '/w', '/w/test.jpg', '/w/test.jpg?edcf2' ],
+ // File that matches basePath, but not found on disk. Empty query.
+ [ 'baseDir' => $baseDir, 'basePath' => '/w', '/w/unknown.png', '/w/unknown.png?' ],
+ // File not matching basePath. Ignored.
+ [ 'baseDir' => $baseDir, 'basePath' => '/w', '/files/test.jpg' ],
+ // Empty string. Ignored.
+ [ 'baseDir' => $baseDir, 'basePath' => '/w', '', '' ],
+ // Similar path, but with domain component. Ignored.
+ [ 'baseDir' => $baseDir, 'basePath' => '/w', '//example.org/w/test.jpg' ],
+ [ 'baseDir' => $baseDir, 'basePath' => '/w', 'https://example.org/w/test.jpg' ],
+ // Unrelated path with domain component. Ignored.
+ [ 'baseDir' => $baseDir, 'basePath' => '/w', 'https://example.org/files/test.jpg' ],
+ [ 'baseDir' => $baseDir, 'basePath' => '/w', '//example.org/files/test.jpg' ],
+ // Unrelated path with domain, and empty base path (root mw install). Ignored.
+ [ 'baseDir' => $baseDir, 'basePath' => '', 'https://example.org/files/test.jpg' ],
+ [ 'baseDir' => $baseDir, 'basePath' => '', '//example.org/files/test.jpg' ], // T155310
+ ];
+ }
+
+ /**
+ * @dataProvider provideTransformFilePath
+ * @covers OutputPage::transformFilePath
+ * @covers OutputPage::transformResourcePath
+ */
+ public function testTransformResourcePath( $baseDir, $basePath, $path, $expected = null ) {
+ $this->setMwGlobals( 'IP', $baseDir );
+ $conf = new HashConfig( [ 'ResourceBasePath' => $basePath ] );
+
+ MediaWiki\suppressWarnings();
+ $actual = OutputPage::transformResourcePath( $conf, $path );
+ MediaWiki\restoreWarnings();
+
+ $this->assertEquals( $expected ?: $path, $actual );
+ }
+
public static function provideMakeResourceLoaderLink() {
// @codingStandardsIgnoreStart Generic.Files.LineLength
return [
/**
* @covers OutputPage::haveCacheVaryCookies
*/
- function testHaveCacheVaryCookies() {
+ public function testHaveCacheVaryCookies() {
$request = new FauxRequest();
$context = new RequestContext();
$context->setRequest( $request );
$request->setCookie( 'Token', '123' );
$this->assertTrue( $outputPage->haveCacheVaryCookies() );
}
+
+ /*
+ * @covers OutputPage::addCategoryLinks
+ * @covers OutputPage::getCategories
+ */
+ public function testGetCategories() {
+ $fakeResultWrapper = new FakeResultWrapper( [
+ (object) [
+ 'pp_value' => 1,
+ 'page_title' => 'Test'
+ ],
+ (object) [
+ 'page_title' => 'Test2'
+ ]
+ ] );
+ $outputPage = $this->getMockBuilder( 'OutputPage' )
+ ->setConstructorArgs( [ new RequestContext() ] )
+ ->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] )
+ ->getMock();
+ $outputPage->expects( $this->any() )
+ ->method( 'addCategoryLinksToLBAndGetResult' )
+ ->will( $this->returnValue( $fakeResultWrapper ) );
+
+ $outputPage->addCategoryLinks( [
+ 'Test' => 'Test',
+ 'Test2' => 'Test2',
+ ] );
+ $this->assertEquals( [ 0 => 'Test', '1' => 'Test2' ], $outputPage->getCategories() );
+ $this->assertEquals( [ 0 => 'Test2' ], $outputPage->getCategories( 'normal' ) );
+ $this->assertEquals( [ 0 => 'Test' ], $outputPage->getCategories( 'hidden' ) );
+ }
+
+ /**
+ * @return OutputPage
+ */
+ private function newInstance() {
+ $context = new RequestContext();
+
+ $context->setConfig( new HashConfig( [
+ 'AppleTouchIcon' => false,
+ 'DisableLangConversion' => true,
+ 'EnableAPI' => false,
+ 'EnableCanonicalServerLink' => false,
+ 'Favicon' => false,
+ 'Feed' => false,
+ 'LanguageCode' => false,
+ 'ReferrerPolicy' => false,
+ 'RightsPage' => false,
+ 'RightsUrl' => false,
+ 'UniversalEditButton' => false,
+ ] ) );
+
+ return new OutputPage( $context );
+ }
}
/**