From d50c169612a46d41cde75d408716c547988b37c6 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Wed, 13 Nov 2019 11:19:56 +0100 Subject: [PATCH] Minor test updates for more complete test coverage The main motivation here is to cover the fallback code that was moved in I20c814d. At some point we might touch this code again. Bug: T238194 Change-Id: I0ab8a34b09790f42b10376eb3730c3b3c4ef53d2 --- tests/phpunit/unit/ApiQueryReferencesTest.php | 37 +++++++++++++- tests/phpunit/unit/CiteParserTagHooksTest.php | 50 +++++++++++++++++-- 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/unit/ApiQueryReferencesTest.php b/tests/phpunit/unit/ApiQueryReferencesTest.php index 9a4a713d9..006084cce 100644 --- a/tests/phpunit/unit/ApiQueryReferencesTest.php +++ b/tests/phpunit/unit/ApiQueryReferencesTest.php @@ -7,6 +7,7 @@ use ApiQuery; use ApiQueryReferences; use IContextSource; use Title; +use Wikimedia\AtEase\AtEase; use Wikimedia\Rdbms\IDatabase; use Wikimedia\TestingAccessWrapper; @@ -40,7 +41,21 @@ class ApiQueryReferencesTest extends \MediaWikiUnitTestCase { /** * @covers ::recursiveFetchRefsFromDB */ - public function testRecursiveFetchRefsFromDB() { + public function testRecursiveFetchRefsFromDB_fails() { + $api = $this->newApiQueryReferences(); + $title = $this->createMock( Title::class ); + + $dbr = $this->createMock( IDatabase::class ); + $dbr->method( 'selectField' ) + ->willReturn( false ); + + $this->assertFalse( $api->recursiveFetchRefsFromDB( $title, $dbr ) ); + } + + /** + * @covers ::recursiveFetchRefsFromDB + */ + public function testRecursiveFetchRefsFromDB_firstTry() { $api = $this->newApiQueryReferences(); $title = $this->createMock( Title::class ); @@ -51,6 +66,26 @@ class ApiQueryReferencesTest extends \MediaWikiUnitTestCase { $this->assertSame( [ 'refs' => [] ], $api->recursiveFetchRefsFromDB( $title, $dbr ) ); } + /** + * @covers ::recursiveFetchRefsFromDB + */ + public function testRecursiveFetchRefsFromDB_secondTry() { + $api = $this->newApiQueryReferences(); + $title = $this->createMock( Title::class ); + + $dbr = $this->createMock( IDatabase::class ); + $dbr->expects( $this->exactly( 2 ) ) + ->method( 'selectField' ) + ->willReturnOnConsecutiveCalls( '', gzencode( '{"refs":{}}' ) ); + + // Code relies on gzdecode() returning false, but that reports an error now + AtEase::suppressWarnings(); + $refs = $api->recursiveFetchRefsFromDB( $title, $dbr ); + AtEase::restoreWarnings(); + + $this->assertSame( [ 'refs' => [] ], $refs ); + } + /** * @return ApiQueryReferences */ diff --git a/tests/phpunit/unit/CiteParserTagHooksTest.php b/tests/phpunit/unit/CiteParserTagHooksTest.php index f761a13c3..43e75d27a 100644 --- a/tests/phpunit/unit/CiteParserTagHooksTest.php +++ b/tests/phpunit/unit/CiteParserTagHooksTest.php @@ -28,13 +28,31 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase { CiteParserTagHooks::initialize( $parser ); } + /** + * @covers ::ref + */ + public function testRef_fails() { + $cite = $this->createMock( Cite::class ); + $cite->method( 'ref' ) + ->willReturn( false ); + + $parser = $this->createMock( Parser::class ); + $parser->extCite = $cite; + + $frame = $this->createMock( PPFrame::class ); + + $html = CiteParserTagHooks::ref( null, [], $parser, $frame ); + $this->assertSame( '<ref></ref>', $html ); + } + /** * @covers ::ref */ public function testRef() { $cite = $this->createMock( Cite::class ); $cite->expects( $this->once() ) - ->method( 'ref' ); + ->method( 'ref' ) + ->willReturn( '' ); $parserOutput = $this->createMock( ParserOutput::class ); $parserOutput->expects( $this->once() ) @@ -47,7 +65,27 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase { ->willReturn( $parserOutput ); $parser->extCite = $cite; - CiteParserTagHooks::ref( null, [], $parser, $this->createMock( PPFrame::class ) ); + $frame = $this->createMock( PPFrame::class ); + + $html = CiteParserTagHooks::ref( null, [], $parser, $frame ); + $this->assertSame( '', $html ); + } + + /** + * @covers ::references + */ + public function testReferences_fails() { + $cite = $this->createMock( Cite::class ); + $cite->method( 'references' ) + ->willReturn( false ); + + $parser = $this->createMock( Parser::class ); + $parser->extCite = $cite; + + $frame = $this->createMock( PPFrame::class ); + + $html = CiteParserTagHooks::references( null, [], $parser, $frame ); + $this->assertSame( '<references/>', $html ); } /** @@ -56,12 +94,16 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase { public function testReferences() { $cite = $this->createMock( Cite::class ); $cite->expects( $this->once() ) - ->method( 'references' ); + ->method( 'references' ) + ->willReturn( '' ); $parser = $this->createMock( Parser::class ); $parser->extCite = $cite; - CiteParserTagHooks::references( null, [], $parser, $this->createMock( PPFrame::class ) ); + $frame = $this->createMock( PPFrame::class ); + + $html = CiteParserTagHooks::references( null, [], $parser, $frame ); + $this->assertSame( '', $html ); } }