Merge "Allow mobile to reduce image quality"
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / TagHooksTest.php
1 <?php
2
3 /**
4 * @group Parser
5 */
6 class TagHookTest extends MediaWikiTestCase {
7 public static function provideValidNames() {
8 return array(
9 array( 'foo' ),
10 array( 'foo-bar' ),
11 array( 'foo_bar' ),
12 array( 'FOO-BAR' ),
13 array( 'foo bar' )
14 );
15 }
16
17 public static function provideBadNames() {
18 return array( array( "foo<bar" ), array( "foo>bar" ), array( "foo\nbar" ), array( "foo\rbar" ) );
19 }
20
21 protected function setUp() {
22 parent::setUp();
23
24 $this->setMwGlobals( 'wgAlwaysUseTidy', false );
25 }
26
27 /**
28 * @dataProvider provideValidNames
29 * @covers Parser::setHook
30 */
31 public function testTagHooks( $tag ) {
32 global $wgParserConf, $wgContLang;
33 $parser = new Parser( $wgParserConf );
34
35 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
36 $parserOutput = $parser->parse(
37 "Foo<$tag>Bar</$tag>Baz",
38 Title::newFromText( 'Test' ),
39 ParserOptions::newFromUserAndLang( new User, $wgContLang )
40 );
41 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
42
43 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
44 }
45
46 /**
47 * @dataProvider provideBadNames
48 * @expectedException MWException
49 * @covers Parser::setHook
50 */
51 public function testBadTagHooks( $tag ) {
52 global $wgParserConf, $wgContLang;
53 $parser = new Parser( $wgParserConf );
54
55 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
56 $parser->parse(
57 "Foo<$tag>Bar</$tag>Baz",
58 Title::newFromText( 'Test' ),
59 ParserOptions::newFromUserAndLang( new User, $wgContLang )
60 );
61 $this->fail( 'Exception not thrown.' );
62 }
63
64 /**
65 * @dataProvider provideValidNames
66 * @covers Parser::setFunctionTagHook
67 */
68 public function testFunctionTagHooks( $tag ) {
69 global $wgParserConf, $wgContLang;
70 $parser = new Parser( $wgParserConf );
71
72 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), 0 );
73 $parserOutput = $parser->parse(
74 "Foo<$tag>Bar</$tag>Baz",
75 Title::newFromText( 'Test' ),
76 ParserOptions::newFromUserAndLang( new User, $wgContLang )
77 );
78 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
79
80 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
81 }
82
83 /**
84 * @dataProvider provideBadNames
85 * @expectedException MWException
86 * @covers Parser::setFunctionTagHook
87 */
88 public function testBadFunctionTagHooks( $tag ) {
89 global $wgParserConf, $wgContLang;
90 $parser = new Parser( $wgParserConf );
91
92 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), SFH_OBJECT_ARGS );
93 $parser->parse(
94 "Foo<$tag>Bar</$tag>Baz",
95 Title::newFromText( 'Test' ),
96 ParserOptions::newFromUserAndLang( new User, $wgContLang )
97 );
98 $this->fail( 'Exception not thrown.' );
99 }
100
101 function tagCallback( $text, $params, $parser ) {
102 return str_rot13( $text );
103 }
104
105 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
106 return str_rot13( $code );
107 }
108 }