aboutsummaryrefslogtreecommitdiffstats
path: root/tests/template_test.php
blob: 1f9f805313ca469a5a0208b51e70cbaf20b7de4c (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
 * this file contains tests for the template engine
 *
 * @package test.util
 */

/** required, it is the file under test */
require_once('include/template_processor.php');
require_once('include/text.php');

class TemplateMockApp {
	public $theme_info=array();
}

if(!function_exists('current_theme')) {
function current_theme() {
	return 'clean';
}
}

if(!function_exists('x')) {
function x($s,$k = NULL) {
	return false;
}
}

if(!function_exists('get_app')) {
function get_app() {
	return new TemplateMockApp();
}
}

/**
 * TestCase for the template engine
 *
 * @author Alexander Kampmann
 * @package test.util
 */
class TemplateTest extends PHPUnit_Framework_TestCase {

	public function setUp() {
		global $t;
		$t=new Template;
	}

	public function testListToShort() {
		@list($first, $second)=array('first');

		$this->assertTrue(is_null($second));
	}

	public function testSimpleVariableString() {
		$tpl='Hello $name!';

		$text=replace_macros($tpl, array('$name'=>'Anna'));

		$this->assertEquals('Hello Anna!', $text);
	}

	public function testSimpleVariableInt() {
		$tpl='There are $num new messages!';

		$text=replace_macros($tpl, array('$num'=>172));

		$this->assertEquals('There are 172 new messages!', $text);
	}

	public function testConditionalElse() {
		$tpl='There{{ if $num!=1 }} are $num new messages{{ else }} is 1 new message{{ endif }}!';

		$text1=replace_macros($tpl, array('$num'=>1));
		$text22=replace_macros($tpl, array('$num'=>22));

		$this->assertEquals('There is 1 new message!', $text1);
		$this->assertEquals('There are 22 new messages!', $text22);
	}

	public function testConditionalNoElse() {
		$tpl='{{ if $num!=0 }}There are $num new messages!{{ endif }}';

		$text0=replace_macros($tpl, array('$num'=>0));
		$text22=replace_macros($tpl, array('$num'=>22));

		$this->assertEquals('', $text0);
		$this->assertEquals('There are 22 new messages!', $text22);
	}

	public function testConditionalFail() {
		$tpl='There {{ if $num!=1 }} are $num new messages{{ else }} is 1 new message{{ endif }}!';

		$text1=replace_macros($tpl, array());

		//$this->assertEquals('There is 1 new message!', $text1);
	}

	public function testSimpleFor() {
		$tpl='{{ for $messages as $message }} $message {{ endfor }}';

		$text=replace_macros($tpl, array('$messages'=>array('message 1', 'message 2')));

		$this->assertEquals(' message 1  message 2 ', $text);
	}

	public function testFor() {
		$tpl='{{ for $messages as $message }} from: $message.from to $message.to {{ endfor }}';

		$text=replace_macros($tpl, array('$messages'=>array(array('from'=>'Mike', 'to'=>'Alex'), array('from'=>'Alex', 'to'=>'Mike'))));

		$this->assertEquals(' from: Mike to Alex  from: Alex to Mike ', $text);
	}
	
	public function testKeyedFor() {
		$tpl='{{ for $messages as $from=>$to }} from: $from to $to {{ endfor }}';
	
		$text=replace_macros($tpl, array('$messages'=>array('Mike'=>'Alex', 'Sven'=>'Mike')));
	
		$this->assertEquals(' from: Mike to Alex  from: Sven to Mike ', $text);
	}

	public function testForEmpty() {
		$tpl='messages: {{for $messages as $message}} from: $message.from to $message.to  {{ endfor }}';

		$text=replace_macros($tpl, array('$messages'=>array()));

		$this->assertEquals('messages: ', $text);
	}

	public function testForWrongType() {
		$tpl='messages: {{for $messages as $message}} from: $message.from to $message.to  {{ endfor }}';

		$text=replace_macros($tpl, array('$messages'=>11));

		$this->assertEquals('messages: ', $text);
	}

	public function testForConditional() {
		$tpl='new messages: {{for $messages as $message}}{{ if $message.new }} $message.text{{endif}}{{ endfor }}';

		$text=replace_macros($tpl, array('$messages'=>array(
				array('new'=>true, 'text'=>'new message'),
				array('new'=>false, 'text'=>'old message'))));

		$this->assertEquals('new messages:  new message', $text);
	}
	
	public function testConditionalFor() {
		$tpl='{{ if $enabled }}new messages:{{for $messages as $message}} $message.text{{ endfor }}{{endif}}';
	
		$text=replace_macros($tpl, array('$enabled'=>true, 
				'$messages'=>array(
				array('new'=>true, 'text'=>'new message'),
				array('new'=>false, 'text'=>'old message'))));
	
		$this->assertEquals('new messages: new message old message', $text);
	}

	public function testFantasy() {
		$tpl='Fantasy: {{fantasy $messages}}';

		$text=replace_macros($tpl, array('$messages'=>'no no'));

		$this->assertEquals('Fantasy: {{fantasy no no}}', $text);
	}

	public function testInc() {
		$tpl='{{inc field_input.tpl with $field=$myvar}}{{ endinc }}';

		$text=replace_macros($tpl, array('$myvar'=>array('myfield', 'label', 'value', 'help')));

		$this->assertEquals("	\n"
				."	<div class='field input'>\n"
				."		<label for='id_myfield'>label</label>\n"
				."		<input name='myfield' id='id_myfield' value=\"value\">\n"
				."		<span class='field_help'>help</span>\n"
				."	</div>\n", $text);
	}

	public function testIncNoVar() {
		$tpl='{{inc field_input.tpl }}{{ endinc }}';

		$text=replace_macros($tpl, array('$field'=>array('myfield', 'label', 'value', 'help')));

		$this->assertEquals("	\n	<div class='field input'>\n		<label for='id_myfield'>label</label>\n"
				."		<input name='myfield' id='id_myfield' value=\"value\">\n"
				."		<span class='field_help'>help</span>\n"
				."	</div>\n", $text);
	}
	
	public function testDoubleUse() {
		$tpl='Hello $name! {{ if $enabled }} I love you! {{ endif }}';
	
		$text=replace_macros($tpl, array('$name'=>'Anna', '$enabled'=>false));
	
		$this->assertEquals('Hello Anna! ', $text);
		
		$tpl='Hey $name! {{ if $enabled }} I hate you! {{ endif }}';
		
		$text=replace_macros($tpl, array('$name'=>'Max', '$enabled'=>true));
		
		$this->assertEquals('Hey Max!  I hate you! ', $text);
	}
	
	public function testIncDouble() {
		$tpl='{{inc field_input.tpl with $field=$var1}}{{ endinc }}'
		.'{{inc field_input.tpl with $field=$var2}}{{ endinc }}';
	
		$text=replace_macros($tpl, array('$var1'=>array('myfield', 'label', 'value', 'help'), 
				'$var2'=>array('myfield2', 'label2', 'value2', 'help2')));
		
		$this->assertEquals("	\n"
				."	<div class='field input'>\n"
				."		<label for='id_myfield'>label</label>\n"
				."		<input name='myfield' id='id_myfield' value=\"value\">\n"
				."		<span class='field_help'>help</span>\n"
				."	</div>\n"
				."	\n"
				."	<div class='field input'>\n"
				."		<label for='id_myfield2'>label2</label>\n"
				."		<input name='myfield2' id='id_myfield2' value=\"value2\">\n"
				."		<span class='field_help'>help2</span>\n"
				."	</div>\n", $text);
	}
}