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