Merge "Add DROP INDEX support to DatabaseSqlite::replaceVars method"
[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 * @covers Parser::setHook
24 */
25 public 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 * @covers Parser::setHook
40 */
41 public function testBadTagHooks( $tag ) {
42 global $wgParserConf, $wgContLang;
43 $parser = new Parser( $wgParserConf );
44
45 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
46 $parser->parse( "Foo<$tag>Bar</$tag>Baz", Title::newFromText( 'Test' ), ParserOptions::newFromUserAndLang( new User, $wgContLang ) );
47 $this->fail( 'Exception not thrown.' );
48 }
49
50 /**
51 * @dataProvider provideValidNames
52 * @covers Parser::setFunctionTagHook
53 */
54 public function testFunctionTagHooks( $tag ) {
55 global $wgParserConf, $wgContLang;
56 $parser = new Parser( $wgParserConf );
57
58 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), 0 );
59 $parserOutput = $parser->parse( "Foo<$tag>Bar</$tag>Baz", Title::newFromText( 'Test' ), ParserOptions::newFromUserAndLang( new User, $wgContLang ) );
60 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
61
62 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
63 }
64
65 /**
66 * @dataProvider provideBadNames
67 * @expectedException MWException
68 * @covers Parser::setFunctionTagHook
69 */
70 public function testBadFunctionTagHooks( $tag ) {
71 global $wgParserConf, $wgContLang;
72 $parser = new Parser( $wgParserConf );
73
74 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), SFH_OBJECT_ARGS );
75 $parser->parse( "Foo<$tag>Bar</$tag>Baz", Title::newFromText( 'Test' ), ParserOptions::newFromUserAndLang( new User, $wgContLang ) );
76 $this->fail( 'Exception not thrown.' );
77 }
78
79 function tagCallback( $text, $params, $parser ) {
80 return str_rot13( $text );
81 }
82
83 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
84 return str_rot13( $code );
85 }
86 }