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;
|
2024-06-08 21:31:42 +00:00
|
|
|
use MediaWiki\Parser\Parser;
|
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2019-11-12 10:45:13 +00:00
|
|
|
use ParserOptions;
|
|
|
|
use StripState;
|
2019-11-04 16:23:29 +00:00
|
|
|
|
|
|
|
/**
|
2023-12-15 11:12:16 +00:00
|
|
|
* @covers \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 {
|
|
|
|
|
|
|
|
public function testOnParserFirstCallInit() {
|
2023-07-27 09:39:46 +00:00
|
|
|
$parser = $this->createNoOpMock( Parser::class, [ 'setHook' ] );
|
2023-11-21 23:15:20 +00:00
|
|
|
$expectedTags = [ 'ref' => true, 'references' => true ];
|
2019-11-04 16:23:29 +00:00
|
|
|
$parser->expects( $this->exactly( 2 ) )
|
|
|
|
->method( 'setHook' )
|
2023-11-21 23:15:20 +00:00
|
|
|
->willReturnCallback( function ( $tag ) use ( &$expectedTags ) {
|
|
|
|
$this->assertArrayHasKey( $tag, $expectedTags );
|
|
|
|
unset( $expectedTags[$tag] );
|
|
|
|
} );
|
2019-11-04 16:23:29 +00:00
|
|
|
|
2020-12-28 15:40:47 +00:00
|
|
|
$citeParserHooks = new CiteParserHooks();
|
|
|
|
$citeParserHooks->onParserFirstCallInit( $parser );
|
2019-11-04 16:23:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 15:40:47 +00:00
|
|
|
public function testOnParserClearState() {
|
2024-04-12 17:49:44 +00:00
|
|
|
$parser = $this->createNoOpMock( Parser::class, [ '__isset' ] );
|
2020-12-28 15:40:47 +00:00
|
|
|
$parser->extCite = $this->createMock( Cite::class );
|
|
|
|
|
|
|
|
$citeParserHooks = new CiteParserHooks();
|
|
|
|
$citeParserHooks->onParserClearState( $parser );
|
|
|
|
|
2024-04-12 17:49:44 +00:00
|
|
|
$this->assertNull( $parser->extCite ?? null );
|
2020-12-28 15:40:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 10:45:13 +00:00
|
|
|
public function testOnParserCloned() {
|
2024-04-12 17:49:44 +00:00
|
|
|
$parser = $this->createNoOpMock( Parser::class, [ '__isset' ] );
|
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
|
|
|
|
2024-04-12 17:49:44 +00:00
|
|
|
$this->assertNull( $parser->extCite ?? null );
|
2019-11-12 10:45:13 +00:00
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
|
2023-12-15 11:43:49 +00:00
|
|
|
$parser = $this->createNoOpMock( Parser::class, [ '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
|
|
|
}
|
|
|
|
|
2019-11-04 16:23:29 +00:00
|
|
|
}
|