6 * @copyright (c) 2008 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 define('IN_PHPBB', true);
12 define('PHP_EXT', 'php');
13 define('PHPBB_ROOT_PATH', '../phpBB/');
15 require_once 'test_framework/framework.php';
17 require_once '../phpBB/includes/constants.php';
18 require_once '../phpBB/includes/functions.php';
19 require_once '../phpBB/includes/template.php';
21 class phpbb_template_template_test
extends phpbb_test_case
24 private $template_path;
26 // Keep the contents of the cache for debugging?
27 const PRESERVE_CACHE
= false;
29 private function display($handle)
32 $this->assertTrue($this->template
->display($handle, false));
33 return self
::trim_template_result(ob_get_clean());
36 private static function trim_template_result($result)
38 return str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim($result)))));
41 private function setup_engine()
43 $this->template_path
= dirname(__FILE__
) . '/templates';
44 $this->template
= new template
;
45 $this->template
->set_custom_template($this->template_path
, 'tests');
48 protected function setUp()
50 // Test the engine can be used
51 $this->setup_engine();
53 if (!is_writable(dirname($this->template
->cachepath
)))
55 $this->markTestSkipped("Template cache directory is not writable.");
58 foreach (glob($this->template
->cachepath
. '*') as $file)
63 $GLOBALS['config'] = array(
64 'load_tplcompile' => true
69 * @todo put test data into templates/xyz.test
71 public static function template_data()
78 array(), // block vars
80 '', // Expected result
88 "pass\npass\n<!-- DUMMY var -->",
92 array('VARIABLE' => 'value'),
106 array('S_VALUE' => true),
113 array('S_VALUE' => true, 'S_OTHER_VALUE' => true),
120 array('S_VALUE' => false, 'S_OTHER_VALUE' => true),
135 array('loop' => array(array())),
142 array('loop' => array(array(), array()), 'loop.block' => array(array())),
144 "loop\nloop\nloop\nloop",
149 array('loop' => array(array(), array()), 'loop.block' => array(array()), 'block' => array(array(), array())),
151 "loop\nloop\nloop\nloop\n\nloop#0-block#0\nloop#0-block#1\nloop#1-block#0\nloop#1-block#1",
156 array('loop' => array(array('VARIABLE' => 'x'))),
158 "first\n0\n0\n1\nx\nset\nlast",
163 array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y'))),
165 "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast",
170 array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())),
172 "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast\n0\n\n1\nlast inner\ninner loop",
175 'loop_advanced.html',
177 array('loop' => array(array(), array(), array(), array(), array(), array(), array())),
179 "101234561\n101234561\n101234561\n1234561\n1\n101\n234\n10\n561\n561",
184 array('loop' => array(array(), array(), array(), array(), array(), array(), array())),
186 "xyz\nabc\n\n00\n11\n22\n33\n44\n55\n66\n\n144\n144",
193 trim(str_repeat("pass\n", 40)),
200 '<!-- echo "test"; -->',
204 array('VARIABLE' => 'value'),
212 array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())),
219 array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())),
221 "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast",
224 'loop_expressions.html',
226 array('loop' => array(array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array())),
228 "on\non\non\non\noff\noff\noff\noff\non\non\non\non\n\noff\noff\noff\non\non\non\noff\noff\noff\non\non\non",
235 "{ VARIABLE }\n{ VARIABLE }",
239 array('L_VARIABLE' => "Value'"),
246 array('LA_VARIABLE' => "Value'"),
249 "{ VARIABLE }\nValue'",
254 public function test_missing_file()
256 $filename = 'file_not_found.html';
258 $this->template
->set_filenames(array('test' => $filename));
259 $this->assertFileNotExists($this->template_path
. '/' . $filename, 'Testing missing file, file cannot exist');
261 $this->setExpectedTriggerError(E_USER_ERROR
, sprintf('template->_tpl_load_file(): File %s does not exist or is empty', realpath($this->template_path
) . '/' . $filename));
262 $this->display('test');
265 public function test_empty_file()
267 $this->setExpectedTriggerError(E_USER_ERROR
, sprintf("template->set_filenames: Empty filename specified for test"));
268 $this->template
->set_filenames(array('test' => ''));
271 public function test_invalid_handle()
273 $this->setExpectedTriggerError(E_USER_ERROR
, sprintf("template->_tpl_load(): No file specified for handle test"));
274 $this->display('test');
278 private function run_template($file, array $vars, array $block_vars, array $destroy, $expected, $cache_file)
280 $this->template
->set_filenames(array('test' => $file));
281 $this->template
->assign_vars($vars);
283 foreach ($block_vars as $block => $loops)
285 foreach ($loops as $_vars)
287 $this->template
->assign_block_vars($block, $_vars);
291 foreach ($destroy as $block)
293 $this->template
->destroy_block_vars($block);
296 $this->assertEquals($expected, $this->display('test'), "Testing $file");
297 $this->assertFileExists($cache_file);
300 if (self
::PRESERVE_CACHE
)
302 copy($cache_file, str_replace('ctpl_', 'tests_ctpl_', $cache_file));
307 * @dataProvider template_data
309 public function test_template($file, array $vars, array $block_vars, array $destroy, $expected)
311 $cache_file = $this->template
->cachepath
. str_replace('/', '.', $file) . '.' . PHP_EXT
;
313 $this->assertFileNotExists($cache_file);
315 $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file);
317 // Reset the engine state
318 $this->setup_engine();
320 $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file);
324 * @dataProvider template_data
326 public function test_assign_display($file, array $vars, array $block_vars, array $destroy, $expected)
328 $this->template
->set_filenames(array(
330 'container' => 'variable.html',
332 $this->template
->assign_vars($vars);
334 foreach ($block_vars as $block => $loops)
336 foreach ($loops as $_vars)
338 $this->template
->assign_block_vars($block, $_vars);
342 foreach ($destroy as $block)
344 $this->template
->destroy_block_vars($block);
347 $this->assertEquals($expected, self
::trim_template_result($this->template
->assign_display('test')), "Testing assign_display($file)");
349 $this->template
->assign_display('test', 'VARIABLE', false);
350 $this->assertEquals($expected, $this->display('container'), "Testing assign_display($file)");
353 public function test_php()
357 $config['tpl_allow_php'] = 1;
359 $cache_file = $this->template
->cachepath
. 'php.html.' . PHP_EXT
;
361 $this->assertFileNotExists($cache_file);
363 $this->run_template('php.html', array(), array(), array(), 'test', $cache_file);
365 unset($config['tpl_allow_php']);
368 public function test_includephp()
372 $config['tpl_allow_php'] = 1;
375 chdir(dirname(__FILE__
) . '/templates');
377 //$this->run_template('includephp.html', array(), array(), array(), 'testing included php', $cache_file);
379 $this->template
->set_filenames(array('test' => 'includephp.html'));
380 $this->assertEquals('testing included php', $this->display('test'), "Testing $file");
384 unset($config['tpl_allow_php']);
387 public static function alter_block_array_data()
392 array('VARIABLE' => 'before'),
409 'Test inserting before on top level block',
413 array('VARIABLE' => 'after'),
430 'Test inserting after on top level block',
434 array('VARIABLE' => 'pos #1'),
451 'Test inserting at 1 on top level block',
455 array('VARIABLE' => 'pos #1'),
471 'Test inserting at 1 on top level block',
475 array('VARIABLE' => 'before'),
480 middle - 0/3 - before
492 'Test inserting before on nested block',
496 array('VARIABLE' => 'after'),
513 'Test inserting after on nested block',
517 array('VARIABLE' => 'pos #1'),
523 middle - 1/3 - pos #1
534 'Test inserting at pos 1 on nested block',
538 array('VARIABLE' => 'before'),
546 middle - 0/4 - before
555 'Test inserting before on nested block (pos 1)',
559 array('VARIABLE' => 'before'),
571 middle - 0/3 - before
576 'Test inserting before on nested block (end)',
580 array('VARIABLE' => 'before'),
592 middle - 0/3 - before
597 'Test inserting before on nested block (end)',
619 * @dataProvider alter_block_array_data
621 public function test_alter_block_array($alter_block, array $vararray, $key, $mode, $expect, $description)
623 $this->template
->set_filenames(array('test' => 'loop_nested.html'));
626 $this->template
->assign_block_vars('outer', array());
627 $this->template
->assign_block_vars('outer.middle', array());
628 $this->template
->assign_block_vars('outer.middle', array());
629 $this->template
->assign_block_vars('outer', array());
630 $this->template
->assign_block_vars('outer.middle', array());
631 $this->template
->assign_block_vars('outer.middle', array());
632 $this->template
->assign_block_vars('outer.middle', array());
633 $this->template
->assign_block_vars('outer', array());
634 $this->template
->assign_block_vars('outer.middle', array());
635 $this->template
->assign_block_vars('outer.middle', array());
637 $this->assertEquals("outer - 0/3\nmiddle - 0/2\nmiddle - 1/2\nouter - 1/3\nmiddle - 0/3\nmiddle - 1/3\nmiddle - 2/3\nouter - 2/3\nmiddle - 0/2\nmiddle - 1/2", $this->display('test'), 'Ensuring template is built correctly before modification');
639 $this->template
->alter_block_array($alter_block, $vararray, $key, $mode);
640 $this->assertEquals($expect, $this->display('test'), $description);