2019-11-04 16:23:29 +00:00
|
|
|
<?php
|
|
|
|
|
2019-11-12 10:45:13 +00:00
|
|
|
namespace Cite\Tests\Unit;
|
2019-11-04 16:23:29 +00:00
|
|
|
|
2019-11-19 14:12:11 +00:00
|
|
|
use Cite\Cite;
|
|
|
|
use Cite\Hooks\CiteParserHooks;
|
2019-11-04 16:23:29 +00:00
|
|
|
use Parser;
|
2019-11-12 10:45:13 +00:00
|
|
|
use ParserOptions;
|
2019-11-12 11:52:55 +00:00
|
|
|
use ParserOutput;
|
2019-11-12 10:45:13 +00:00
|
|
|
use StripState;
|
2019-11-04 16:23:29 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-19 14:12:11 +00:00
|
|
|
* @coversDefaultClass \Cite\Hooks\CiteParserHooks
|
2019-11-19 10:31:08 +00:00
|
|
|
*
|
|
|
|
* @license GPL-2.0-or-later
|
2019-11-04 16:23:29 +00:00
|
|
|
*/
|
|
|
|
class CiteParserHooksTest extends \MediaWikiUnitTestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::onParserFirstCallInit
|
|
|
|
*/
|
|
|
|
public function testOnParserFirstCallInit() {
|
2023-07-27 09:39:46 +00:00
|
|
|
$parser = $this->createNoOpMock( Parser::class, [ 'setHook' ] );
|
2019-11-04 16:23:29 +00:00
|
|
|
$parser->expects( $this->exactly( 2 ) )
|
|
|
|
->method( 'setHook' )
|
|
|
|
->withConsecutive(
|
|
|
|
[ 'ref', $this->isType( 'callable' ) ],
|
|
|
|
[ 'references', $this->isType( 'callable' ) ]
|
|
|
|
);
|
|
|
|
|
2020-12-28 15:40:47 +00:00
|
|
|
$citeParserHooks = new CiteParserHooks();
|
|
|
|
$citeParserHooks->onParserFirstCallInit( $parser );
|
2019-11-04 16:23:29 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 10:45:13 +00:00
|
|
|
/**
|
2020-12-28 15:40:47 +00:00
|
|
|
* @covers ::onParserClearState
|
|
|
|
*/
|
|
|
|
public function testOnParserClearState() {
|
2021-08-12 12:24:52 +00:00
|
|
|
$parser = $this->createParser();
|
2020-12-28 15:40:47 +00:00
|
|
|
$parser->extCite = $this->createMock( Cite::class );
|
|
|
|
|
|
|
|
$citeParserHooks = new CiteParserHooks();
|
|
|
|
$citeParserHooks->onParserClearState( $parser );
|
|
|
|
|
|
|
|
$this->assertFalse( isset( $parser->extCite ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::onParserCloned
|
2019-11-12 10:45:13 +00:00
|
|
|
*/
|
|
|
|
public function testOnParserCloned() {
|
2021-08-12 12:24:52 +00:00
|
|
|
$parser = $this->createParser();
|
2019-12-11 10:01:05 +00:00
|
|
|
$parser->extCite = $this->createMock( Cite::class );
|
2019-11-12 10:45:13 +00:00
|
|
|
|
2020-12-28 15:40:47 +00:00
|
|
|
$citeParserHooks = new CiteParserHooks();
|
|
|
|
$citeParserHooks->onParserCloned( $parser );
|
2019-11-12 10:45:13 +00:00
|
|
|
|
2019-12-11 10:01:05 +00:00
|
|
|
$this->assertFalse( isset( $parser->extCite ) );
|
2019-11-12 10:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::onParserAfterParse
|
|
|
|
*/
|
2019-11-12 11:52:55 +00:00
|
|
|
public function testAfterParseHooks() {
|
2019-11-12 10:45:13 +00:00
|
|
|
$cite = $this->createMock( Cite::class );
|
2019-12-04 15:49:56 +00:00
|
|
|
$cite->expects( $this->once() )
|
2019-11-12 10:45:13 +00:00
|
|
|
->method( 'checkRefsNoReferences' );
|
|
|
|
|
2019-11-28 10:15:19 +00:00
|
|
|
$parserOptions = $this->createMock( ParserOptions::class );
|
|
|
|
$parserOptions->method( 'getIsSectionPreview' )
|
|
|
|
->willReturn( false );
|
|
|
|
|
2021-08-12 12:24:52 +00:00
|
|
|
$parser = $this->createParser( [ 'getOptions', 'getOutput' ] );
|
2019-11-12 10:45:13 +00:00
|
|
|
$parser->method( 'getOptions' )
|
2019-11-28 10:15:19 +00:00
|
|
|
->willReturn( $parserOptions );
|
2019-11-12 11:52:55 +00:00
|
|
|
$parser->method( 'getOutput' )
|
|
|
|
->willReturn( $this->createMock( ParserOutput::class ) );
|
2019-11-12 10:45:13 +00:00
|
|
|
$parser->extCite = $cite;
|
|
|
|
|
2019-11-28 10:15:19 +00:00
|
|
|
$text = '';
|
2020-12-28 15:40:47 +00:00
|
|
|
$citeParserHooks = new CiteParserHooks();
|
|
|
|
$citeParserHooks->onParserAfterParse( $parser, $text, $this->createMock( StripState::class ) );
|
2019-11-12 10:45:13 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 12:24:52 +00:00
|
|
|
/**
|
|
|
|
* @param array $configurableMethods
|
|
|
|
* @return Parser
|
|
|
|
*/
|
|
|
|
private function createParser( $configurableMethods = [] ) {
|
|
|
|
return $this->getMockBuilder( Parser::class )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->onlyMethods( $configurableMethods )
|
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:23:29 +00:00
|
|
|
}
|