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