Warn if stateful ParserOutput transforms are used
[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 private function getParserOptions() {
47 global $wgContLang;
48 $popt = ParserOptions::newFromUserAndLang( new User, $wgContLang );
49 $popt->setWrapOutputClass( false );
50 return $popt;
51 }
52
53 /**
54 * @dataProvider provideValidNames
55 */
56 public function testTagHooks( $tag ) {
57 global $wgParserConf;
58 $parser = new Parser( $wgParserConf );
59
60 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
61 $parserOutput = $parser->parse(
62 "Foo<$tag>Bar</$tag>Baz",
63 Title::newFromText( 'Test' ),
64 $this->getParserOptions()
65 );
66 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
67
68 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
69 }
70
71 /**
72 * @dataProvider provideBadNames
73 * @expectedException MWException
74 */
75 public function testBadTagHooks( $tag ) {
76 global $wgParserConf;
77 $parser = new Parser( $wgParserConf );
78
79 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
80 $parser->parse(
81 "Foo<$tag>Bar</$tag>Baz",
82 Title::newFromText( 'Test' ),
83 $this->getParserOptions()
84 );
85 $this->fail( 'Exception not thrown.' );
86 }
87
88 /**
89 * @dataProvider provideValidNames
90 */
91 public function testFunctionTagHooks( $tag ) {
92 global $wgParserConf;
93 $parser = new Parser( $wgParserConf );
94
95 $parser->setFunctionTagHook( $tag, [ $this, 'functionTagCallback' ], 0 );
96 $parserOutput = $parser->parse(
97 "Foo<$tag>Bar</$tag>Baz",
98 Title::newFromText( 'Test' ),
99 $this->getParserOptions()
100 );
101 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
102
103 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
104 }
105
106 /**
107 * @dataProvider provideBadNames
108 * @expectedException MWException
109 */
110 public function testBadFunctionTagHooks( $tag ) {
111 global $wgParserConf;
112 $parser = new Parser( $wgParserConf );
113
114 $parser->setFunctionTagHook(
115 $tag,
116 [ $this, 'functionTagCallback' ],
117 Parser::SFH_OBJECT_ARGS
118 );
119 $parser->parse(
120 "Foo<$tag>Bar</$tag>Baz",
121 Title::newFromText( 'Test' ),
122 $this->getParserOptions()
123 );
124 $this->fail( 'Exception not thrown.' );
125 }
126
127 function tagCallback( $text, $params, $parser ) {
128 return str_rot13( $text );
129 }
130
131 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
132 return str_rot13( $code );
133 }
134 }