aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/TemplateParserTest.php
blob: ad2deb5f8f8ca64eec168eab2d0be7a5aef17fb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

/**
 * @group Templates
 * @covers TemplateParser
 */
class TemplateParserTest extends MediaWikiTestCase {

	protected $templateDir;

	protected function setUp() : void {
		parent::setUp();

		$this->setMwGlobals( [
			'wgSecretKey' => 'foo',
		] );

		$this->templateDir = dirname( __DIR__ ) . '/data/templates/';
	}

	/**
	 * @dataProvider provideProcessTemplate
	 */
	public function testProcessTemplate( $name, $args, $result, $exception = false ) {
		if ( $exception ) {
			$this->expectException( $exception );
		}
		$tp = new TemplateParser( $this->templateDir );
		$this->assertEquals( $result, $tp->processTemplate( $name, $args ) );
	}

	public static function provideProcessTemplate() {
		return [
			[
				'foobar',
				[],
				"hello world!\n"
			],
			[
				'foobar_args',
				[
					'planet' => 'world',
				],
				"hello world!\n",
			],
			[
				'../foobar',
				[],
				false,
				'UnexpectedValueException'
			],
			[
				"\000../foobar",
				[],
				false,
				'UnexpectedValueException'
			],
			[
				'/',
				[],
				false,
				'UnexpectedValueException'
			],
			[
				// Allegedly this can strip ext in windows.
				'baz<',
				[],
				false,
				'UnexpectedValueException'
			],
			[
				'\\foo',
				[],
				false,
				'UnexpectedValueException'
			],
			[
				'C:\bar',
				[],
				false,
				'UnexpectedValueException'
			],
			[
				"foo\000bar",
				[],
				false,
				'UnexpectedValueException'
			],
			[
				'nonexistenttemplate',
				[],
				false,
				'RuntimeException',
			],
			[
				'has_partial',
				[
					'planet' => 'world',
				],
				"Partial hello world!\n in here\n",
			],
			[
				'bad_partial',
				[],
				false,
				'Exception',
			],
			[
				'parentvars',
				[
					'foo' => 'f',
					'bar' => [
						[ 'baz' => 'x' ],
						[ 'baz' => 'y' ]
					]
				],
				"f\n\n\tf x\n\n\tf y\n\n"
			]
		];
	}

	public function testEnableRecursivePartials() {
		$tp = new TemplateParser( $this->templateDir );
		$data = [ 'r' => [ 'r' => [ 'r' => [] ] ] ];

		$tp->enableRecursivePartials( true );
		$this->assertEquals( 'rrr', $tp->processTemplate( 'recurse', $data ) );

		$tp->enableRecursivePartials( false );
		$this->expectException( Exception::class );
		$tp->processTemplate( 'recurse', $data );
	}

}