diff options
-rw-r--r-- | tests/template/template.php | 141 | ||||
-rw-r--r-- | tests/template/templates/_dummy_include.php | 3 | ||||
-rw-r--r-- | tests/template/templates/expressions.html | 3 | ||||
-rw-r--r-- | tests/template/templates/if.html | 2 | ||||
-rw-r--r-- | tests/template/templates/includephp.html | 1 | ||||
-rw-r--r-- | tests/template/templates/loop_vars.html | 5 | ||||
-rw-r--r-- | tests/template/templates/php.html | 1 |
7 files changed, 147 insertions, 9 deletions
diff --git a/tests/template/template.php b/tests/template/template.php index 7c1bcbf75a..f3337c6842 100644 --- a/tests/template/template.php +++ b/tests/template/template.php @@ -25,12 +25,17 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase { ob_start(); $this->assertTrue($this->template->display($handle, false)); - $contents = str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim(ob_get_contents()))))); + $contents = self::trim_template_result(ob_get_contents()); ob_end_clean(); return $contents; } + private static function trim_template_result($result) + { + return str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim($result))))); + } + private function setup_engine() { $this->template = new template; @@ -68,6 +73,7 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase '', // File array(), // vars array(), // block vars + array(), // destroy '', // Expected result ), */ @@ -75,84 +81,132 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase 'variable.html', array('VARIABLE' => 'value'), array(), + array(), 'value', ), array( 'if.html', array(), array(), + array(), '0', ), array( 'if.html', array('S_VALUE' => true), array(), + array(), '1', ), array( + 'if.html', + array('S_VALUE' => true, 'S_OTHER_VALUE' => true), + array(), + array(), + '1', + ), + array( + 'if.html', + array('S_VALUE' => false, 'S_OTHER_VALUE' => true), + array(), + array(), + '2', + ), + array( 'loop.html', array(), array(), + array(), "noloop\nnoloop", ), array( 'loop.html', array(), array('loop' => array(array())), + array(), "loop\nloop", ), array( 'loop.html', array(), array('loop' => array(array(), array())), + array(), "loop\nloop\nloop\nloop", ), array( 'loop_vars.html', array(), array('loop' => array(array('VARIABLE' => 'x'))), - "first\n0\n0\n1\nx\nlast", + array(), + "first\n0\n0\n1\nx\nset\nlast", ), array( 'loop_vars.html', array(), array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y'))), - "first\n0\n0\n2\nx\n1\n1\n2\ny\nlast", + array(), + "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast", ), array( 'loop_vars.html', array(), array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), - "first\n0\n0\n2\nx\n1\n1\n2\ny\nlast\n0\n1", + array(), + "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast\n0\n\n1\nlast inner\ninner loop", ), array( 'loop_advanced.html', array(), array('loop' => array(array(), array(), array(), array(), array(), array(), array())), + array(), "101234561\n101234561\n101234561\n1234561\n1\n101\n234\n10\n561\n561", ), array( 'define.html', array(), array(), + array(), "xyz\nabc", ), array( 'expressions.html', array(), array(), - trim(str_repeat("pass\n", 38)), + array(), + trim(str_repeat("pass\n", 40)), + ), + array( + 'php.html', + array(), + array(), + array(), + '<!-- echo "test"; -->', ), array( 'include.html', array('VARIABLE' => 'value'), array(), + array(), 'value', ), + array( + 'loop_vars.html', + array(), + array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), + array('loop'), + '', + ), + array( + 'loop_vars.html', + array(), + array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), + array('loop.inner'), + "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast", + ), ); } - private function run_template($file, array $vars, array $block_vars, $expected, $cache_file) + private function run_template($file, array $vars, array $block_vars, array $destroy, $expected, $cache_file) { $this->template->set_filenames(array('test' => $file)); $this->template->assign_vars($vars); @@ -165,6 +219,11 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase } } + foreach ($destroy as $block) + { + $this->template->destroy_block_vars($block); + } + $this->assertEquals($expected, $this->display('test'), "Testing $file"); $this->assertFileExists($cache_file); } @@ -172,18 +231,82 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase /** * @dataProvider template_data */ - public function test_template($file, array $vars, array $block_vars, $expected) + public function test_template($file, array $vars, array $block_vars, array $destroy, $expected) { $cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.' . PHP_EXT; $this->assertFileNotExists($cache_file); - $this->run_template($file, $vars, $block_vars, $expected, $cache_file); + $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); // Reset the engine state $this->setup_engine(); - $this->run_template($file, $vars, $block_vars, $expected, $cache_file); + $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); + } + + /** + * @dataProvider template_data + */ + public function test_assign_display($file, array $vars, array $block_vars, array $destroy, $expected) + { + $this->template->set_filenames(array( + 'test' => $file, + 'container' => 'variable.html', + )); + $this->template->assign_vars($vars); + + foreach ($block_vars as $block => $loops) + { + foreach ($loops as $_vars) + { + $this->template->assign_block_vars($block, $_vars); + } + } + + foreach ($destroy as $block) + { + $this->template->destroy_block_vars($block); + } + + $this->assertEquals($expected, self::trim_template_result($this->template->assign_display('test')), "Testing assign_display($file)"); + + $this->template->assign_display('test', 'VARIABLE', false); + $this->assertEquals($expected, $this->display('container'), "Testing assign_display($file)"); + } + + public function test_php() + { + global $config; + + $config['tpl_allow_php'] = 1; + + $cache_file = $this->template->cachepath . 'php.html.' . PHP_EXT; + + $this->assertFileNotExists($cache_file); + + $this->run_template('php.html', array(), array(), array(), 'test', $cache_file); + + unset($config['tpl_allow_php']); + } + + public function test_includephp() + { + global $config; + + $config['tpl_allow_php'] = 1; + + $cwd = getcwd(); + chdir(dirname(__FILE__) . '/templates'); + + //$this->run_template('includephp.html', array(), array(), array(), 'testing included php', $cache_file); + + $this->template->set_filenames(array('test' => 'includephp.html')); + $this->assertEquals('testing included php', $this->display('test'), "Testing $file"); + + chdir($cwd); + + unset($config['tpl_allow_php']); } } ?>
\ No newline at end of file diff --git a/tests/template/templates/_dummy_include.php b/tests/template/templates/_dummy_include.php new file mode 100644 index 0000000000..1de5dddf59 --- /dev/null +++ b/tests/template/templates/_dummy_include.php @@ -0,0 +1,3 @@ +<?php + +echo "testing included php"; diff --git a/tests/template/templates/expressions.html b/tests/template/templates/expressions.html index c70dc9418c..f0b38cc2ec 100644 --- a/tests/template/templates/expressions.html +++ b/tests/template/templates/expressions.html @@ -8,6 +8,7 @@ <!-- IF 32 is odd -->fail<!-- ELSE -->pass<!-- ENDIF --> +<!-- IF 32 is div by 16 -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF 24 == 24 -->pass<!-- ELSE -->fail<!-- ENDIF --> @@ -83,3 +84,5 @@ <!-- IF 6 % 4 == 2 -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF 24 mod 12 == 0 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF not (43 > 53) -->pass<!-- ELSE -->fail<!-- ENDIF --> diff --git a/tests/template/templates/if.html b/tests/template/templates/if.html index d8343c4391..3ed7a0c2e4 100644 --- a/tests/template/templates/if.html +++ b/tests/template/templates/if.html @@ -1,5 +1,7 @@ <!-- IF S_VALUE --> 1 +<!-- ELSEIF S_OTHER_VALUE --> +2 <!-- ELSE --> 0 <!-- ENDIF --> diff --git a/tests/template/templates/includephp.html b/tests/template/templates/includephp.html new file mode 100644 index 0000000000..42c78b9377 --- /dev/null +++ b/tests/template/templates/includephp.html @@ -0,0 +1 @@ +<!-- INCLUDEPHP _dummy_include.php --> diff --git a/tests/template/templates/loop_vars.html b/tests/template/templates/loop_vars.html index 6bbd4a2710..1ecf49bdf2 100644 --- a/tests/template/templates/loop_vars.html +++ b/tests/template/templates/loop_vars.html @@ -9,10 +9,15 @@ {loop.VARIABLE} +<!-- IF loop.VARIABLE -->set<!-- ENDIF --> + <!-- IF loop.S_LAST_ROW -->last<!-- ENDIF --> <!-- BEGIN inner --> {inner.S_ROW_NUM} +<!-- IF inner.S_LAST_ROW and inner.S_ROW_NUM and inner.S_NUM_ROWS -->last inner<!-- ENDIF --> + <!-- END inner --> <!-- END loop --> +<!-- IF .loop.inner -->inner loop<!-- ENDIF --> diff --git a/tests/template/templates/php.html b/tests/template/templates/php.html new file mode 100644 index 0000000000..07a260cdb3 --- /dev/null +++ b/tests/template/templates/php.html @@ -0,0 +1 @@ +<!-- PHP -->echo "test";<!-- ENDPHP --> |