+ $this->assertSame( [], $op->getCategories() );
+
+ return $op;
+ }
+
+ private function doCategoryAsserts( $op, $expectedNormal, $expectedHidden ) {
+ $this->assertSame( array_merge( $expectedHidden, $expectedNormal ), $op->getCategories() );
+ $this->assertSame( $expectedNormal, $op->getCategories( 'normal' ) );
+ $this->assertSame( $expectedHidden, $op->getCategories( 'hidden' ) );
+ }
+
+ private function doCategoryLinkAsserts( $op, $expectedNormal, $expectedHidden ) {
+ $catLinks = $op->getCategoryLinks();
+ $this->assertSame( (bool)$expectedNormal + (bool)$expectedHidden, count( $catLinks ) );
+ if ( $expectedNormal ) {
+ $this->assertSame( count( $expectedNormal ), count( $catLinks['normal'] ) );
+ }
+ if ( $expectedHidden ) {
+ $this->assertSame( count( $expectedHidden ), count( $catLinks['hidden'] ) );
+ }
+
+ foreach ( $expectedNormal as $i => $name ) {
+ $this->assertContains( $name, $catLinks['normal'][$i] );
+ }
+ foreach ( $expectedHidden as $i => $name ) {
+ $this->assertContains( $name, $catLinks['hidden'][$i] );
+ }
+ }
+
+ public function provideGetCategories() {
+ return [
+ 'No categories' => [ [], [], null, [], [] ],
+ 'Simple test' => [
+ [ 'Test1' => 'Some sortkey', 'Test2' => 'A different sortkey' ],
+ [ 'Test1' => (object)[ 'pp_value' => 1, 'page_title' => 'Test1' ],
+ 'Test2' => (object)[ 'page_title' => 'Test2' ] ],
+ null,
+ [ 'Test2' ],
+ [ 'Test1' ],
+ ],
+ 'Invalid title' => [
+ [ '[' => '[', 'Test' => 'Test' ],
+ [ 'Test' => (object)[ 'page_title' => 'Test' ] ],
+ null,
+ [ 'Test' ],
+ [],
+ ],
+ 'Variant link' => [
+ [ 'Test' => 'Test', 'Estay' => 'Estay' ],
+ [ 'Test' => (object)[ 'page_title' => 'Test' ] ],
+ function ( &$link, &$title ) {
+ if ( $link === 'Estay' ) {
+ $link = 'Test';
+ $title = Title::makeTitleSafe( NS_CATEGORY, $link );
+ }
+ },
+ // For adding one by one, the variant gets added as well as the original category,
+ // but if you add them all together the second time gets skipped.
+ [ 'onebyone' => [ 'Test', 'Test' ], 'default' => [ 'Test' ] ],
+ [],
+ ],
+ ];
+ }
+
+ /**
+ * @covers OutputPage::getCategories
+ */
+ public function testGetCategoriesInvalid() {
+ $this->setExpectedException( InvalidArgumentException::class,
+ 'Invalid category type given: hiddne' );
+
+ $op = $this->newInstance();
+ $op->getCategories( 'hiddne' );
+ }
+
+ // @todo Should we test addCategoryLinksToLBAndGetResult? If so, how? Insert some test rows in
+ // the DB?
+
+ /**
+ * @covers OutputPage::setIndicators
+ * @covers OutputPage::getIndicators
+ * @covers OutputPage::addParserOutputMetadata
+ */
+ public function testIndicators() {
+ $op = $this->newInstance();
+ $this->assertSame( [], $op->getIndicators() );
+
+ $op->setIndicators( [] );
+ $this->assertSame( [], $op->getIndicators() );
+
+ // Test sorting alphabetically
+ $op->setIndicators( [ 'b' => 'x', 'a' => 'y' ] );
+ $this->assertSame( [ 'a' => 'y', 'b' => 'x' ], $op->getIndicators() );
+
+ // Test overwriting existing keys
+ $op->setIndicators( [ 'c' => 'z', 'a' => 'w' ] );
+ $this->assertSame( [ 'a' => 'w', 'b' => 'x', 'c' => 'z' ], $op->getIndicators() );
+
+ // Test with ParserOutput
+ $stubPO = $this->createParserOutputStub( 'getIndicators', [ 'c' => 'u', 'd' => 'v' ] );
+ $op->addParserOutputMetadata( $stubPO );
+ $this->assertSame( [ 'a' => 'w', 'b' => 'x', 'c' => 'u', 'd' => 'v' ],
+ $op->getIndicators() );
+ }
+
+ /**
+ * @covers OutputPage::addHelpLink
+ * @covers OutputPage::getIndicators
+ */
+ public function testAddHelpLink() {
+ $op = $this->newInstance();
+
+ $op->addHelpLink( 'Manual:PHP unit testing' );
+ $indicators = $op->getIndicators();
+ $this->assertSame( [ 'mw-helplink' ], array_keys( $indicators ) );
+ $this->assertContains( 'Manual:PHP_unit_testing', $indicators['mw-helplink'] );
+
+ $op->addHelpLink( 'https://phpunit.de', true );
+ $indicators = $op->getIndicators();
+ $this->assertSame( [ 'mw-helplink' ], array_keys( $indicators ) );
+ $this->assertContains( 'https://phpunit.de', $indicators['mw-helplink'] );
+ $this->assertNotContains( 'mediawiki', $indicators['mw-helplink'] );
+ $this->assertNotContains( 'Manual:PHP', $indicators['mw-helplink'] );
+ }
+
+ /**
+ * @covers OutputPage::prependHTML
+ * @covers OutputPage::addHTML
+ * @covers OutputPage::addElement
+ * @covers OutputPage::clearHTML
+ * @covers OutputPage::getHTML
+ */
+ public function testBodyHTML() {
+ $op = $this->newInstance();
+ $this->assertSame( '', $op->getHTML() );
+
+ $op->addHTML( 'a' );
+ $this->assertSame( 'a', $op->getHTML() );
+
+ $op->addHTML( 'b' );
+ $this->assertSame( 'ab', $op->getHTML() );
+
+ $op->prependHTML( 'c' );
+ $this->assertSame( 'cab', $op->getHTML() );
+
+ $op->addElement( 'p', [ 'id' => 'foo' ], 'd' );
+ $this->assertSame( 'cab<p id="foo">d</p>', $op->getHTML() );
+
+ $op->clearHTML();
+ $this->assertSame( '', $op->getHTML() );
+ }
+
+ /**
+ * @dataProvider provideRevisionId
+ * @covers OutputPage::setRevisionId
+ * @covers OutputPage::getRevisionId
+ */
+ public function testRevisionId( $newVal, $expected ) {
+ $op = $this->newInstance();
+
+ $this->assertNull( $op->setRevisionId( $newVal ) );
+ $this->assertSame( $expected, $op->getRevisionId() );
+ $this->assertSame( $expected, $op->setRevisionId( null ) );
+ $this->assertNull( $op->getRevisionId() );
+ }
+
+ public function provideRevisionId() {
+ return [
+ [ null, null ],
+ [ 7, 7 ],
+ [ -1, -1 ],
+ [ 3.2, 3 ],
+ [ '0', 0 ],
+ [ '32% finished', 32 ],
+ [ false, 0 ],
+ ];
+ }
+
+ /**
+ * @covers OutputPage::setRevisionTimestamp
+ * @covers OutputPage::getRevisionTimestamp
+ */
+ public function testRevisionTimestamp() {
+ $op = $this->newInstance();
+ $this->assertNull( $op->getRevisionTimestamp() );
+
+ $this->assertNull( $op->setRevisionTimestamp( 'abc' ) );
+ $this->assertSame( 'abc', $op->getRevisionTimestamp() );
+ $this->assertSame( 'abc', $op->setRevisionTimestamp( null ) );
+ $this->assertNull( $op->getRevisionTimestamp() );
+ }
+
+ /**
+ * @covers OutputPage::setFileVersion
+ * @covers OutputPage::getFileVersion
+ */
+ public function testFileVersion() {
+ $op = $this->newInstance();
+ $this->assertNull( $op->getFileVersion() );
+
+ $stubFile = $this->createMock( File::class );
+ $stubFile->method( 'exists' )->willReturn( true );
+ $stubFile->method( 'getTimestamp' )->willReturn( '12211221123321' );
+ $stubFile->method( 'getSha1' )->willReturn( 'bf3ffa7047dc080f5855377a4f83cd18887e3b05' );
+
+ $op->setFileVersion( $stubFile );
+
+ $this->assertEquals(
+ [ 'time' => '12211221123321', 'sha1' => 'bf3ffa7047dc080f5855377a4f83cd18887e3b05' ],
+ $op->getFileVersion()
+ );
+
+ $stubMissingFile = $this->createMock( File::class );
+ $stubMissingFile->method( 'exists' )->willReturn( false );
+
+ $op->setFileVersion( $stubMissingFile );
+ $this->assertNull( $op->getFileVersion() );
+
+ $op->setFileVersion( $stubFile );
+ $this->assertNotNull( $op->getFileVersion() );
+
+ $op->setFileVersion( null );
+ $this->assertNull( $op->getFileVersion() );
+ }
+
+ private function createParserOutputStub( $method = '', $retVal = [] ) {
+ $pOut = $this->getMock( ParserOutput::class );
+ if ( $method !== '' ) {
+ $pOut->method( $method )->willReturn( $retVal );
+ }
+
+ $arrayReturningMethods = [
+ 'getCategories',
+ 'getFileSearchOptions',
+ 'getHeadItems',
+ 'getIndicators',
+ 'getLanguageLinks',
+ 'getOutputHooks',
+ 'getTemplateIds',
+ ];
+
+ foreach ( $arrayReturningMethods as $method ) {
+ $pOut->method( $method )->willReturn( [] );
+ }
+
+ return $pOut;
+ }
+
+ /**
+ * @covers OutputPage::getTemplateIds
+ * @covers OutputPage::addParserOutputMetadata
+ */
+ public function testTemplateIds() {
+ $op = $this->newInstance();
+ $this->assertSame( [], $op->getTemplateIds() );
+
+ // Test with no template id's
+ $stubPOEmpty = $this->createParserOutputStub();
+ $op->addParserOutputMetadata( $stubPOEmpty );
+ $this->assertSame( [], $op->getTemplateIds() );
+
+ // Test with some arbitrary template id's
+ $ids = [
+ NS_MAIN => [ 'A' => 3, 'B' => 17 ],
+ NS_TALK => [ 'C' => 31 ],
+ NS_MEDIA => [ 'D' => -1 ],
+ ];
+
+ $stubPO1 = $this->createParserOutputStub( 'getTemplateIds', $ids );
+
+ $op->addParserOutputMetadata( $stubPO1 );
+ $this->assertSame( $ids, $op->getTemplateIds() );
+
+ // Test merging with a second set of id's
+ $stubPO2 = $this->createParserOutputStub( 'getTemplateIds', [
+ NS_MAIN => [ 'E' => 1234 ],
+ NS_PROJECT => [ 'F' => 5678 ],