| 1 |
lars |
1 |
<?php
|
|
|
2 |
require_once 'HTML/Template/IT.php';
|
|
|
3 |
require_once 'PHPUnit/Framework/TestCase.php';
|
|
|
4 |
|
|
|
5 |
class ITTest extends PHPUnit_Framework_TestCase
|
|
|
6 |
{
|
|
|
7 |
/**
|
|
|
8 |
* An HTML_Template_IT object
|
|
|
9 |
* @var object
|
|
|
10 |
*/
|
|
|
11 |
var $tpl;
|
|
|
12 |
|
|
|
13 |
function setUp()
|
|
|
14 |
{
|
|
|
15 |
$this->tpl = new HTML_Template_IT(dirname(__FILE__) . '/templates');
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
function tearDown()
|
|
|
19 |
{
|
|
|
20 |
unset($this->tpl);
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
function _stripWhitespace($str)
|
|
|
24 |
{
|
|
|
25 |
return preg_replace('/\\s+/', '', $str);
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
function _methodExists($name)
|
|
|
29 |
{
|
|
|
30 |
if (in_array(strtolower($name), get_class_methods($this->tpl))) {
|
|
|
31 |
return true;
|
|
|
32 |
}
|
|
|
33 |
$this->assertTrue(false, 'method '. $name . ' not implemented in ' . get_class($this->tpl));
|
|
|
34 |
return false;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Tests a setTemplate method
|
|
|
39 |
*
|
|
|
40 |
*/
|
|
|
41 |
function testSetTemplate()
|
|
|
42 |
{
|
|
|
43 |
$result = $this->tpl->setTemplate('A template', false, false);
|
|
|
44 |
if (PEAR::isError($result)) {
|
|
|
45 |
$this->assertTrue(false, 'Error setting template: '. $result->getMessage());
|
|
|
46 |
}
|
|
|
47 |
$this->assertEquals('A template', $this->tpl->get());
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Tests a loadTemplatefile method
|
|
|
52 |
*
|
|
|
53 |
*/
|
|
|
54 |
function testLoadTemplatefile()
|
|
|
55 |
{
|
|
|
56 |
$result = $this->tpl->loadTemplatefile('loadtemplatefile.html', false, false);
|
|
|
57 |
if (PEAR::isError($result)) {
|
|
|
58 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
59 |
}
|
|
|
60 |
$this->assertEquals('A template', trim($this->tpl->get()));
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Tests a setVariable method
|
|
|
65 |
*
|
|
|
66 |
*/
|
|
|
67 |
function testSetVariable()
|
|
|
68 |
{
|
|
|
69 |
$result = $this->tpl->setTemplate('{placeholder1} {placeholder2} {placeholder3}', true, true);
|
|
|
70 |
if (PEAR::isError($result)) {
|
|
|
71 |
$this->assertTrue(false, 'Error setting template: '. $result->getMessage());
|
|
|
72 |
}
|
|
|
73 |
// "scalar" call
|
|
|
74 |
$this->tpl->setVariable('placeholder1', 'var1');
|
|
|
75 |
// array call
|
|
|
76 |
$this->tpl->setVariable(array(
|
|
|
77 |
'placeholder2' => 'var2',
|
|
|
78 |
'placeholder3' => 'var3'
|
|
|
79 |
));
|
|
|
80 |
$this->assertEquals('var1 var2 var3', $this->tpl->get());
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Tests the <!-- INCLUDE --> functionality
|
|
|
85 |
*
|
|
|
86 |
*/
|
|
|
87 |
function testInclude()
|
|
|
88 |
{
|
|
|
89 |
$result = $this->tpl->loadTemplateFile('include.html', false, false);
|
|
|
90 |
if (PEAR::isError($result)) {
|
|
|
91 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
92 |
}
|
|
|
93 |
$this->assertEquals('Master file; Included file', trim($this->tpl->get()));
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
*
|
|
|
98 |
*/
|
|
|
99 |
function testCurrentBlock()
|
|
|
100 |
{
|
|
|
101 |
$result = $this->tpl->loadTemplateFile('blockiteration.html', true, true);
|
|
|
102 |
if (PEAR::isError($result)) {
|
|
|
103 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
104 |
}
|
|
|
105 |
$this->tpl->setVariable('outer', 'a');
|
|
|
106 |
$this->tpl->setCurrentBlock('inner_block');
|
|
|
107 |
for ($i = 0; $i < 5; $i++) {
|
|
|
108 |
$this->tpl->setVariable('inner', $i + 1);
|
|
|
109 |
$this->tpl->parseCurrentBlock();
|
|
|
110 |
} // for
|
|
|
111 |
$this->assertEquals('a|1|2|3|4|5#', $this->_stripWhitespace($this->tpl->get()));
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
*
|
|
|
116 |
*/
|
|
|
117 |
function testRemovePlaceholders()
|
|
|
118 |
{
|
|
|
119 |
$result = $this->tpl->setTemplate('{placeholder1},{placeholder2},{placeholder3}', true, true);
|
|
|
120 |
if (PEAR::isError($result)) {
|
|
|
121 |
$this->assertTrue(false, 'Error setting template: '. $result->getMessage());
|
|
|
122 |
}
|
|
|
123 |
// we do not set {placeholder3}
|
|
|
124 |
$this->tpl->setVariable(array(
|
|
|
125 |
'placeholder1' => 'var1',
|
|
|
126 |
'placeholder2' => 'var2'
|
|
|
127 |
));
|
|
|
128 |
$this->assertEquals('var1,var2,', $this->tpl->get());
|
|
|
129 |
|
|
|
130 |
// Now, we should really add a switch for keeping {stuff} in
|
|
|
131 |
// data supplied to setVariable() safe. Until then, removing it should
|
|
|
132 |
// be expected behaviour
|
|
|
133 |
$result = $this->tpl->setTemplate('{placeholder1},{placeholder2},{placeholder3}', true, true);
|
|
|
134 |
$this->tpl->setOption('preserve_input', false);
|
|
|
135 |
if (PEAR::isError($result)) {
|
|
|
136 |
$this->assertTrue(false, 'Error setting template: '. $result->getMessage());
|
|
|
137 |
}
|
|
|
138 |
$this->tpl->setVariable(array(
|
|
|
139 |
'placeholder1' => 'var1',
|
|
|
140 |
'placeholder2' => 'var2',
|
|
|
141 |
'placeholder3' => 'var3{stuff}'
|
|
|
142 |
));
|
|
|
143 |
$this->assertEquals('var1,var2,var3', $this->tpl->get());
|
|
|
144 |
|
|
|
145 |
$result = $this->tpl->setTemplate('{placeholder1},{placeholder2},{placeholder3}', true, true);
|
|
|
146 |
$this->tpl->setOption('preserve_input', true);
|
|
|
147 |
if (PEAR::isError($result)) {
|
|
|
148 |
$this->assertTrue(false, 'Error setting template: '. $result->getMessage());
|
|
|
149 |
}
|
|
|
150 |
$this->tpl->setVariable(array(
|
|
|
151 |
'placeholder1' => 'var1',
|
|
|
152 |
'placeholder2' => 'var2',
|
|
|
153 |
'placeholder3' => 'var3{stuff}'
|
|
|
154 |
));
|
|
|
155 |
$this->assertEquals('var1,var2,var3{stuff}', $this->tpl->get());
|
|
|
156 |
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
*
|
|
|
161 |
*/
|
|
|
162 |
function testTouchBlock()
|
|
|
163 |
{
|
|
|
164 |
$result = $this->tpl->loadTemplateFile('blockiteration.html', false, true);
|
|
|
165 |
if (PEAR::isError($result)) {
|
|
|
166 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
167 |
}
|
|
|
168 |
$this->tpl->setVariable('outer', 'data');
|
|
|
169 |
// inner_block should be preserved in output, even if empty
|
|
|
170 |
$this->tpl->touchBlock('inner_block');
|
|
|
171 |
$this->assertEquals('data|{inner}#', $this->_stripWhitespace($this->tpl->get()));
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
// Not available in stock class
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
*
|
|
|
178 |
*/
|
|
|
179 |
/*
|
|
|
180 |
function testHideBlock()
|
|
|
181 |
{
|
|
|
182 |
if (!$this->_methodExists('hideBlock')) {
|
|
|
183 |
return;
|
|
|
184 |
}
|
|
|
185 |
$result = $this->tpl->loadTemplateFile('blockiteration.html', false, true);
|
|
|
186 |
if (PEAR::isError($result)) {
|
|
|
187 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
188 |
}
|
|
|
189 |
$this->tpl->setVariable(array(
|
|
|
190 |
'outer' => 'data',
|
|
|
191 |
'inner' => 'stuff'
|
|
|
192 |
));
|
|
|
193 |
// inner_block is not empty, but should be removed nonetheless
|
|
|
194 |
$this->tpl->hideBlock('inner_block');
|
|
|
195 |
$this->assertEquals('data#', $this->_stripWhitespace($this->tpl->get()));
|
|
|
196 |
}
|
|
|
197 |
*/
|
|
|
198 |
/**
|
|
|
199 |
*
|
|
|
200 |
*/
|
|
|
201 |
/*
|
|
|
202 |
function testSetGlobalVariable()
|
|
|
203 |
{
|
|
|
204 |
if (!$this->_methodExists('setGlobalVariable')) {
|
|
|
205 |
return;
|
|
|
206 |
}
|
|
|
207 |
$result = $this->tpl->loadTemplateFile('globals.html', false, true);
|
|
|
208 |
if (PEAR::isError($result)) {
|
|
|
209 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
210 |
}
|
|
|
211 |
$this->tpl->setGlobalVariable('glob', 'glob');
|
|
|
212 |
// {var2} is not, block_two should be removed
|
|
|
213 |
$this->tpl->setVariable(array(
|
|
|
214 |
'var1' => 'one',
|
|
|
215 |
'var3' => 'three'
|
|
|
216 |
));
|
|
|
217 |
for ($i = 0; $i < 3; $i++) {
|
|
|
218 |
$this->tpl->setVariable('var4', $i + 1);
|
|
|
219 |
$this->tpl->parse('block_four');
|
|
|
220 |
} // for
|
|
|
221 |
$this->assertEquals('glob:one#glob:three|glob:1|glob:2|glob:3#', $this->_stripWhitespace($this->tpl->get()));
|
|
|
222 |
}
|
|
|
223 |
*/
|
|
|
224 |
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Test for bug #9501. preg_replace treat $<NUM> and \<NUM> as
|
|
|
228 |
* backreferences. IT escapes them.
|
|
|
229 |
*
|
|
|
230 |
*/
|
|
|
231 |
function testBug9501()
|
|
|
232 |
{
|
|
|
233 |
$this->tpl->setTemplate("Test: {VALUE}");
|
|
|
234 |
$this->tpl->clearCache = true;
|
|
|
235 |
|
|
|
236 |
$this->tpl->setVariable("VALUE", '$12.34');
|
|
|
237 |
$this->assertEquals('Test: $12.34', $this->tpl->get());
|
|
|
238 |
|
|
|
239 |
$this->tpl->setVariable("VALUE", '$1256.34');
|
|
|
240 |
$this->assertEquals('Test: $1256.34', $this->tpl->get());
|
|
|
241 |
|
|
|
242 |
$this->tpl->setVariable("VALUE", '^1.34');
|
|
|
243 |
$this->assertEquals('Test: ^1.34', $this->tpl->get());
|
|
|
244 |
|
|
|
245 |
$this->tpl->setVariable("VALUE", '$1.34');
|
|
|
246 |
$this->assertEquals('Test: $1.34', $this->tpl->get());
|
|
|
247 |
|
|
|
248 |
$this->tpl->setVariable("VALUE", '\$12.34');
|
|
|
249 |
$this->assertEquals('Test: \$12.34', $this->tpl->get());
|
|
|
250 |
|
|
|
251 |
$this->tpl->setVariable("VALUE", "\$12.34");
|
|
|
252 |
$this->assertEquals('Test: $12.34', $this->tpl->get());
|
|
|
253 |
|
|
|
254 |
$this->tpl->setVariable("VALUE", "\$12.34");
|
|
|
255 |
$this->assertEquals('Test: $12.34', $this->tpl->get());
|
|
|
256 |
|
|
|
257 |
// $12 is not parsed as a variable as it starts with a number
|
|
|
258 |
$this->tpl->setVariable("VALUE", "$12.34");
|
|
|
259 |
$this->assertEquals('Test: $12.34', $this->tpl->get());
|
|
|
260 |
|
|
|
261 |
$this->tpl->setVariable("VALUE", "\\$12.34");
|
|
|
262 |
$this->assertEquals('Test: \$12.34', $this->tpl->get());
|
|
|
263 |
|
|
|
264 |
// taken from the bugreport
|
|
|
265 |
$word = 'Cost is $456.98';
|
|
|
266 |
$this->tpl->setVariable("VALUE", $word);
|
|
|
267 |
$this->assertEquals('Test: Cost is $456.98', $this->tpl->get());
|
|
|
268 |
|
|
|
269 |
$word = "Cost is \$" . '183.22';
|
|
|
270 |
$this->tpl->setVariable("VALUE", $word);
|
|
|
271 |
$this->assertEquals('Test: Cost is $183.22', $this->tpl->get());
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
function testBug9783 ()
|
|
|
275 |
{
|
|
|
276 |
$this->tpl->setTemplate("<!-- BEGIN entry -->{DATA} <!-- END entry -->", true, true);
|
|
|
277 |
$data = array ('{Bakken}', 'Soria', 'Joye');
|
|
|
278 |
foreach ($data as $name) {
|
|
|
279 |
$this->tpl->setCurrentBlock('entry');
|
|
|
280 |
$this->tpl->setVariable('DATA', $name);
|
|
|
281 |
$this->tpl->parseCurrentBlock();
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
$this->assertEquals('{Bakken} Soria Joye', trim($this->tpl->get()));
|
|
|
285 |
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
function testBug9853 ()
|
|
|
289 |
{
|
|
|
290 |
$this->tpl->loadTemplatefile("bug_9853_01.tpl", true, true);
|
|
|
291 |
|
|
|
292 |
$this->tpl->setVariable("VAR" , "Ok !");
|
|
|
293 |
$this->tpl->parse("foo1");
|
|
|
294 |
|
|
|
295 |
$this->tpl->setVariable("VAR" , "Ok !");
|
|
|
296 |
$this->tpl->parse("foo2");
|
|
|
297 |
|
|
|
298 |
$this->tpl->setVariable("VAR." , "Ok !");
|
|
|
299 |
$this->tpl->setVariable("VAR2" , "Okay");
|
|
|
300 |
$this->tpl->parse("bar");
|
|
|
301 |
|
|
|
302 |
$this->tpl->parse();
|
|
|
303 |
$output01 = $this->tpl->get();
|
|
|
304 |
|
|
|
305 |
$this->tpl->loadTemplatefile("bug_9853_02.tpl", true, true);
|
|
|
306 |
|
|
|
307 |
$this->tpl->setVariable("VAR" , "Ok !");
|
|
|
308 |
$this->tpl->parse("foo.");
|
|
|
309 |
|
|
|
310 |
$this->tpl->setVariable("VAR" , "Ok !");
|
|
|
311 |
$this->tpl->parse("foo2");
|
|
|
312 |
|
|
|
313 |
$this->tpl->setVariable("VAR." , "Ok !");
|
|
|
314 |
$this->tpl->setVariable("VAR2" , "Okay");
|
|
|
315 |
$this->tpl->parse("bar");
|
|
|
316 |
|
|
|
317 |
$this->tpl->parse();
|
|
|
318 |
$output02 = $this->tpl->get();
|
|
|
319 |
|
|
|
320 |
$this->assertEquals($output01, $output02);
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
/**
|
|
|
325 |
* Tests iterations over two blocks
|
|
|
326 |
*
|
|
|
327 |
*/
|
|
|
328 |
function testBlockIteration()
|
|
|
329 |
{
|
|
|
330 |
$data = array(
|
|
|
331 |
'a',
|
|
|
332 |
array('b', array('1', '2', '3', '4')),
|
|
|
333 |
'c',
|
|
|
334 |
array('d', array('5', '6', '7'))
|
|
|
335 |
);
|
|
|
336 |
|
|
|
337 |
$result = $this->tpl->loadTemplateFile('blockiteration.html', true, true);
|
|
|
338 |
if (PEAR::isError($result)) {
|
|
|
339 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
340 |
}
|
|
|
341 |
foreach ($data as $value) {
|
|
|
342 |
if (is_array($value)) {
|
|
|
343 |
$this->tpl->setVariable('outer', $value[0]);
|
|
|
344 |
foreach ($value[1] as $v) {
|
|
|
345 |
$this->tpl->setVariable('inner', $v);
|
|
|
346 |
$this->tpl->parse('inner_block');
|
|
|
347 |
}
|
|
|
348 |
} else {
|
|
|
349 |
$this->tpl->setVariable('outer', $value);
|
|
|
350 |
}
|
|
|
351 |
$this->tpl->parse('outer_block');
|
|
|
352 |
}
|
|
|
353 |
$this->assertEquals('a#b|1|2|3|4#c#d|5|6|7#', $this->_stripWhitespace($this->tpl->get()));
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
/**
|
|
|
357 |
*
|
|
|
358 |
*
|
|
|
359 |
*/
|
|
|
360 |
function testTouchBlockIteration()
|
|
|
361 |
{
|
|
|
362 |
$data = array('a','b','c','d','e');
|
|
|
363 |
$result = $this->tpl->loadTemplateFile('blockiteration.html', true, true);
|
|
|
364 |
if (PEAR::isError($result)) {
|
|
|
365 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
366 |
}
|
|
|
367 |
for ($i = 0; $i < count($data); $i++) {
|
|
|
368 |
$this->tpl->setVariable('outer', $data[$i]);
|
|
|
369 |
// the inner_block is empty and should be removed
|
|
|
370 |
if (0 == $i % 2) {
|
|
|
371 |
$this->tpl->touchBlock('inner_block');
|
|
|
372 |
}
|
|
|
373 |
$this->tpl->parse('outer_block');
|
|
|
374 |
}
|
|
|
375 |
$this->assertEquals('a|#b#c|#d#e|#', $this->_stripWhitespace($this->tpl->get()));
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
public function testShouldSetOptionsCorrectly() {
|
|
|
379 |
$result = $this->tpl->setOption('removeEmptyBlocks', false);
|
|
|
380 |
|
|
|
381 |
$this->assertFalse(PEAR::isError($result));
|
|
|
382 |
|
|
|
383 |
$this->assertFalse($this->tpl->removeEmptyBlocks);
|
|
|
384 |
|
|
|
385 |
$result = $this->tpl->setOption('removeEmptyBlocks', true);
|
|
|
386 |
|
|
|
387 |
$this->assertFalse(PEAR::isError($result));
|
|
|
388 |
|
|
|
389 |
$this->assertTrue($this->tpl->removeEmptyBlocks);
|
|
|
390 |
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
|
|
|
394 |
public function testPlaceholderReplacementScope() {
|
|
|
395 |
$result = $this->tpl->loadTemplateFile('placeholderreplacementscope.html', true, true);
|
|
|
396 |
|
|
|
397 |
if (PEAR::isError($result)) {
|
|
|
398 |
$this->fail('Error loading template file: ' . $result->getMessage());
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
|
|
|
402 |
$this->tpl->setCurrentBlock('foo');
|
|
|
403 |
$this->tpl->setVariable('var1','test');
|
|
|
404 |
$this->tpl->parseCurrentBlock();
|
|
|
405 |
$this->tpl->setCurrentBlock('bar');
|
|
|
406 |
$this->tpl->setVariable('var1','not');
|
|
|
407 |
$this->tpl->setVariable('var2','good');
|
|
|
408 |
$this->tpl->parseCurrentBlock();
|
|
|
409 |
|
|
|
410 |
$actual = $this->_stripWhitespace($this->tpl->get());
|
|
|
411 |
$this->assertEquals('testgood', $actual);
|
|
|
412 |
}
|
|
|
413 |
|
|
|
414 |
}
|
|
|
415 |
|
|
|
416 |
?>
|