Merge "Allow editing transcluded sections via the API"
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserMethodsTest.php
1 <?php
2
3 class ParserMethodsTest extends MediaWikiLangTestCase {
4
5 public static function providePreSaveTransform() {
6 return array(
7 array( 'hello this is ~~~',
8 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
9 ),
10 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
11 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
12 ),
13 );
14 }
15
16 /**
17 * @dataProvider providePreSaveTransform
18 * @covers Parser::preSaveTransform
19 */
20 public function testPreSaveTransform( $text, $expected ) {
21 global $wgParser;
22
23 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
24 $user = new User();
25 $user->setName( "127.0.0.1" );
26 $popts = ParserOptions::newFromUser( $user );
27 $text = $wgParser->preSaveTransform( $text, $title, $user, $popts );
28
29 $this->assertEquals( $expected, $text );
30 }
31
32 /**
33 * @expectedException MWException
34 * @expectedExceptionMessage Parser state cleared while parsing. Did you call Parser::parse recursively?
35 * @covers Parser::lock
36 */
37 public function testRecursiveParse() {
38 global $wgParser;
39 $title = Title::newFromText( 'foo' );
40 $po = new ParserOptions;
41 $wgParser->setHook( 'recursivecallparser', array( $this, 'helperParserFunc' ) );
42 $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
43 }
44
45 public function helperParserFunc( $input, $args, $parser) {
46 $title = Title::newFromText( 'foo' );
47 $po = new ParserOptions;
48 $parser->parse( $input, $title, $po );
49 return 'bar';
50 }
51
52 /**
53 * @covers Parser::callParserFunction
54 */
55 public function testCallParserFunction() {
56 global $wgParser;
57
58 // Normal parses test passing PPNodes. Test passing an array.
59 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
60 $wgParser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML );
61 $frame = $wgParser->getPreprocessor()->newFrame();
62 $ret = $wgParser->callParserFunction( $frame, '#tag',
63 array( 'pre', 'foo', 'style' => 'margin-left: 1.6em' )
64 );
65 $ret['text'] = $wgParser->mStripState->unstripBoth( $ret['text'] );
66 $this->assertSame( array(
67 'found' => true,
68 'text' => '<pre style="margin-left: 1.6em">foo</pre>',
69 ), $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
70 }
71
72 /**
73 * @covers Parser::parse
74 * @covers ParserOutput::getSections
75 */
76 public function testGetSections() {
77 global $wgParser;
78
79 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
80 $out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
81 $this->assertSame( array(
82 array(
83 'toclevel' => 1,
84 'level' => '2',
85 'line' => 'foo',
86 'number' => '1',
87 'index' => '1',
88 'fromtitle' => $title->getPrefixedDBkey(),
89 'byteoffset' => 0,
90 'anchor' => 'foo',
91 ),
92 array(
93 'toclevel' => 1,
94 'level' => '2',
95 'line' => 'bar',
96 'number' => '2',
97 'index' => '',
98 'fromtitle' => false,
99 'byteoffset' => null,
100 'anchor' => 'bar',
101 ),
102 array(
103 'toclevel' => 1,
104 'level' => '2',
105 'line' => 'baz',
106 'number' => '3',
107 'index' => '2',
108 'fromtitle' => $title->getPrefixedDBkey(),
109 'byteoffset' => 21,
110 'anchor' => 'baz',
111 ),
112 ), $out->getSections(), 'getSections() with proper value when <h2> is used' );
113 }
114 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
115 // replaceSection(), getPreloadText()
116 }