Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / parser / TagHooksTest.php
blob2a4a4ecbd69b48b91309c3e3063deec1231519fa
1 <?php
3 /**
4 * @group Database
5 * @group Parser
6 */
7 class TagHookTest extends MediaWikiTestCase {
8 public static function provideValidNames() {
9 return [
10 [ 'foo' ],
11 [ 'foo-bar' ],
12 [ 'foo_bar' ],
13 [ 'FOO-BAR' ],
14 [ 'foo bar' ]
18 public static function provideBadNames() {
19 return [ [ "foo<bar" ], [ "foo>bar" ], [ "foo\nbar" ], [ "foo\rbar" ] ];
22 /**
23 * @dataProvider provideValidNames
24 * @covers Parser::setHook
26 public function testTagHooks( $tag ) {
27 global $wgParserConf, $wgContLang;
28 $parser = new Parser( $wgParserConf );
30 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
31 $parserOutput = $parser->parse(
32 "Foo<$tag>Bar</$tag>Baz",
33 Title::newFromText( 'Test' ),
34 ParserOptions::newFromUserAndLang( new User, $wgContLang )
36 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
38 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
41 /**
42 * @dataProvider provideBadNames
43 * @expectedException MWException
44 * @covers Parser::setHook
46 public function testBadTagHooks( $tag ) {
47 global $wgParserConf, $wgContLang;
48 $parser = new Parser( $wgParserConf );
50 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
51 $parser->parse(
52 "Foo<$tag>Bar</$tag>Baz",
53 Title::newFromText( 'Test' ),
54 ParserOptions::newFromUserAndLang( new User, $wgContLang )
56 $this->fail( 'Exception not thrown.' );
59 /**
60 * @dataProvider provideValidNames
61 * @covers Parser::setFunctionTagHook
63 public function testFunctionTagHooks( $tag ) {
64 global $wgParserConf, $wgContLang;
65 $parser = new Parser( $wgParserConf );
67 $parser->setFunctionTagHook( $tag, [ $this, 'functionTagCallback' ], 0 );
68 $parserOutput = $parser->parse(
69 "Foo<$tag>Bar</$tag>Baz",
70 Title::newFromText( 'Test' ),
71 ParserOptions::newFromUserAndLang( new User, $wgContLang )
73 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
75 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
78 /**
79 * @dataProvider provideBadNames
80 * @expectedException MWException
81 * @covers Parser::setFunctionTagHook
83 public function testBadFunctionTagHooks( $tag ) {
84 global $wgParserConf, $wgContLang;
85 $parser = new Parser( $wgParserConf );
87 $parser->setFunctionTagHook(
88 $tag,
89 [ $this, 'functionTagCallback' ],
90 Parser::SFH_OBJECT_ARGS
92 $parser->parse(
93 "Foo<$tag>Bar</$tag>Baz",
94 Title::newFromText( 'Test' ),
95 ParserOptions::newFromUserAndLang( new User, $wgContLang )
97 $this->fail( 'Exception not thrown.' );
100 function tagCallback( $text, $params, $parser ) {
101 return str_rot13( $text );
104 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
105 return str_rot13( $code );