| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cite\Tests\Unit; |
| 4 |
|
| 5 |
use Cite\Cite; |
| 6 |
use Cite\Hooks\CiteParserHooks; |
| 7 |
use MediaWiki\Config\Config; |
| 8 |
use MediaWiki\Parser\Parser; |
| 9 |
use MediaWiki\Parser\ParserOptions; |
| 10 |
use MediaWiki\Parser\ParserOutput; |
| 11 |
use MediaWiki\Parser\StripState; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class CiteParserHooksTest extends \MediaWikiUnitTestCase { |
| 18 |
|
| 19 |
private function newCiteParserHooks() { |
| 20 |
return new CiteParserHooks( |
| 21 |
$this->createNoOpMock( Config::class ) |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
public function testOnParserFirstCallInit() { |
| 26 |
$parser = $this->createNoOpMock( Parser::class, [ 'setHook' ] ); |
| 27 |
$expectedTags = [ 'ref' => true, 'references' => true ]; |
| 28 |
$parser->expects( $this->exactly( 2 ) ) |
| 29 |
->method( 'setHook' ) |
| 30 |
->willReturnCallback( function ( $tag ) use ( &$expectedTags ) { |
| 31 |
$this->assertArrayHasKey( $tag, $expectedTags ); |
| 32 |
unset( $expectedTags[$tag] ); |
| 33 |
} ); |
| 34 |
|
| 35 |
$citeParserHooks = $this->newCiteParserHooks(); |
| 36 |
$citeParserHooks->onParserFirstCallInit( $parser ); |
| 37 |
} |
| 38 |
|
| 39 |
public function testOnParserClearState() { |
| 40 |
$parser = $this->createNoOpMock( Parser::class, [ '__isset' ] ); |
| 41 |
$parser->extCite = $this->createMock( Cite::class ); |
| 42 |
|
| 43 |
$citeParserHooks = $this->newCiteParserHooks(); |
| 44 |
$citeParserHooks->onParserClearState( $parser ); |
| 45 |
|
| 46 |
$this->assertNull( $parser->extCite ?? null ); |
| 47 |
} |
| 48 |
|
| 49 |
public function testOnParserCloned() { |
| 50 |
$parser = $this->createNoOpMock( Parser::class, [ '__isset' ] ); |
| 51 |
$parser->extCite = $this->createMock( Cite::class ); |
| 52 |
|
| 53 |
$citeParserHooks = $this->newCiteParserHooks(); |
| 54 |
$citeParserHooks->onParserCloned( $parser ); |
| 55 |
|
| 56 |
$this->assertNull( $parser->extCite ?? null ); |
| 57 |
} |
| 58 |
|
| 59 |
public function testAfterParseHooks() { |
| 60 |
$cite = $this->createMock( Cite::class ); |
| 61 |
$cite->expects( $this->once() ) |
| 62 |
->method( 'checkRefsNoReferences' ); |
| 63 |
|
| 64 |
$parserOptions = $this->createMock( ParserOptions::class ); |
| 65 |
$parserOptions->method( 'getIsSectionPreview' ) |
| 66 |
->willReturn( false ); |
| 67 |
|
| 68 |
$parser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getOutput' ] ); |
| 69 |
$parser->method( 'getOptions' ) |
| 70 |
->willReturn( $parserOptions ); |
| 71 |
$parser->method( 'getOutput' ) |
| 72 |
->willReturn( $this->createMock( ParserOutput::class ) ); |
| 73 |
$parser->extCite = $cite; |
| 74 |
|
| 75 |
$text = ''; |
| 76 |
$citeParserHooks = $this->newCiteParserHooks(); |
| 77 |
$citeParserHooks->onParserAfterParse( $parser, $text, $this->createMock( StripState::class ) ); |
| 78 |
} |
| 79 |
|
| 80 |
} |