mediawiki-extensions-Cite/tests/phpunit/unit/CiteParserHooksTest.php
Thiemo Kreuz a7c4e14f42 Remove obsolete ParserBeforeTidy hook handler
I was able to track this code down to I093d85d from 2012, which was done
right after the ParserAfterParse hook was introduced. I believe the
redundant code path was left to keep the Cite extension compatible with
old MediaWiki versions that did not had this hook yet.

I also noticed this code path is most probably entirely redundant with
the current version of MediaWiki. The *only* thing this code does is
blocking the ParserBeforeTidy hook from doing the same thing a second
time if the ParserAfterParse hook was called before. But it does *not*
block any other compination, e.g. if the two hooks are called the other
way around, or the same hook twice.

In core, it looks like it is impossible for the ParserBeforeTidy hook
being fired without the ParserAfterParse hook being fired before. If this
is true, this is in fact dead code.

Change-Id: Iacf8b600c7abdeaf89c22c2fc31e646f57245e47
2019-12-04 16:56:43 +01:00

90 lines
2.1 KiB
PHP

<?php
namespace Cite\Tests\Unit;
use Cite\Cite;
use Cite\Hooks\CiteParserHooks;
use Parser;
use ParserOptions;
use ParserOutput;
use StripState;
/**
* @coversDefaultClass \Cite\Hooks\CiteParserHooks
*
* @license GPL-2.0-or-later
*/
class CiteParserHooksTest extends \MediaWikiUnitTestCase {
/**
* @covers ::onParserFirstCallInit
*/
public function testOnParserFirstCallInit() {
$parser = $this->createMock( Parser::class );
$parser->expects( $this->exactly( 2 ) )
->method( 'setHook' )
->withConsecutive(
[ 'ref', $this->isType( 'callable' ) ],
[ 'references', $this->isType( 'callable' ) ]
);
CiteParserHooks::onParserFirstCallInit( $parser );
$this->assertInstanceOf( Cite::class, $parser->extCite );
}
/**
* @covers ::onParserClearState
*/
public function testOnParserClearState() {
$cite = $this->createMock( Cite::class );
$cite->expects( $this->once() )
->method( 'clearState' );
$parser = $this->createMock( Parser::class );
$parser->extCite = $cite;
CiteParserHooks::onParserClearState( $parser );
}
/**
* @covers ::onParserCloned
*/
public function testOnParserCloned() {
$cite = $this->createMock( Cite::class );
$cite->expects( $this->once() )
->method( 'clearState' );
$parser = $this->createMock( Parser::class );
$parser->extCite = $cite;
CiteParserHooks::onParserCloned( $parser );
$this->assertNotSame( $cite, $parser->extCite );
}
/**
* @covers ::onParserAfterParse
*/
public function testAfterParseHooks() {
$cite = $this->createMock( Cite::class );
$cite->expects( $this->once() )
->method( 'checkRefsNoReferences' );
$parserOptions = $this->createMock( ParserOptions::class );
$parserOptions->method( 'getIsSectionPreview' )
->willReturn( false );
$parser = $this->createMock( Parser::class );
$parser->method( 'getOptions' )
->willReturn( $parserOptions );
$parser->method( 'getOutput' )
->willReturn( $this->createMock( ParserOutput::class ) );
$parser->extCite = $cite;
$text = '';
CiteParserHooks::onParserAfterParse( $parser, $text, $this->createMock( StripState::class ) );
}
}