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