Merge "Remove some ancient upgrade information from release notes"
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / TagHooksTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group Parser
6 *
7 * @covers Parser
8 * @covers StripState
9 *
10 * @covers Preprocessor_DOM
11 * @covers PPDStack
12 * @covers PPDStackElement
13 * @covers PPDPart
14 * @covers PPFrame_DOM
15 * @covers PPTemplateFrame_DOM
16 * @covers PPCustomFrame_DOM
17 * @covers PPNode_DOM
18 *
19 * @covers Preprocessor_Hash
20 * @covers PPDStack_Hash
21 * @covers PPDStackElement_Hash
22 * @covers PPDPart_Hash
23 * @covers PPFrame_Hash
24 * @covers PPTemplateFrame_Hash
25 * @covers PPCustomFrame_Hash
26 * @covers PPNode_Hash_Tree
27 * @covers PPNode_Hash_Text
28 * @covers PPNode_Hash_Array
29 * @covers PPNode_Hash_Attr
30 */
31 class TagHookTest extends MediaWikiTestCase {
32 public static function provideValidNames() {
33 return [
34 [ 'foo' ],
35 [ 'foo-bar' ],
36 [ 'foo_bar' ],
37 [ 'FOO-BAR' ],
38 [ 'foo bar' ]
39 ];
40 }
41
42 public static function provideBadNames() {
43 return [ [ "foo<bar" ], [ "foo>bar" ], [ "foo\nbar" ], [ "foo\rbar" ] ];
44 }
45
46 /**
47 * @dataProvider provideValidNames
48 */
49 public function testTagHooks( $tag ) {
50 global $wgParserConf, $wgContLang;
51 $parser = new Parser( $wgParserConf );
52
53 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
54 $parserOutput = $parser->parse(
55 "Foo<$tag>Bar</$tag>Baz",
56 Title::newFromText( 'Test' ),
57 ParserOptions::newFromUserAndLang( new User, $wgContLang )
58 );
59 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
60
61 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
62 }
63
64 /**
65 * @dataProvider provideBadNames
66 * @expectedException MWException
67 */
68 public function testBadTagHooks( $tag ) {
69 global $wgParserConf, $wgContLang;
70 $parser = new Parser( $wgParserConf );
71
72 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
73 $parser->parse(
74 "Foo<$tag>Bar</$tag>Baz",
75 Title::newFromText( 'Test' ),
76 ParserOptions::newFromUserAndLang( new User, $wgContLang )
77 );
78 $this->fail( 'Exception not thrown.' );
79 }
80
81 /**
82 * @dataProvider provideValidNames
83 */
84 public function testFunctionTagHooks( $tag ) {
85 global $wgParserConf, $wgContLang;
86 $parser = new Parser( $wgParserConf );
87
88 $parser->setFunctionTagHook( $tag, [ $this, 'functionTagCallback' ], 0 );
89 $parserOutput = $parser->parse(
90 "Foo<$tag>Bar</$tag>Baz",
91 Title::newFromText( 'Test' ),
92 ParserOptions::newFromUserAndLang( new User, $wgContLang )
93 );
94 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
95
96 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
97 }
98
99 /**
100 * @dataProvider provideBadNames
101 * @expectedException MWException
102 */
103 public function testBadFunctionTagHooks( $tag ) {
104 global $wgParserConf, $wgContLang;
105 $parser = new Parser( $wgParserConf );
106
107 $parser->setFunctionTagHook(
108 $tag,
109 [ $this, 'functionTagCallback' ],
110 Parser::SFH_OBJECT_ARGS
111 );
112 $parser->parse(
113 "Foo<$tag>Bar</$tag>Baz",
114 Title::newFromText( 'Test' ),
115 ParserOptions::newFromUserAndLang( new User, $wgContLang )
116 );
117 $this->fail( 'Exception not thrown.' );
118 }
119
120 function tagCallback( $text, $params, $parser ) {
121 return str_rot13( $text );
122 }
123
124 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
125 return str_rot13( $code );
126 }
127 }