Commit 2f0eff5f by Seldaek

+ Syntax: Added shortcut for {$dwoo.const.*} using '%', for example you can use

{%FOO} instead of {$dwoo.const.FOO} + Syntax: When using named parameters, typing a parameter name without any value is the same as typing param=true, for example {foo name="test" bar} and {foo name="test" bar=true} are equals, can be useful for very complex plugins with huge amounts of parameters. + Plugins: Added {loop} that combines {foreach} and {with}, see http://wiki.dwoo.org/index.php/Block:loop for details * API: DwooITemplate->clearCache now requires a Dwoo instance as its first arg, should not affect you unless you built a custom template class from scratch * {include} now uses the current resource if none is provided instead of using file as it did before * Dwoo uses include path instead of absolute includes * Fixed a regression in the handling of custom class plugins * Fixed a bug in DwooSecurityPolicy->getAllowedDirectories(), no security issue though * TestFest happened early for Dwoo, lots of new tests and more code covered git-svn-id: svn://dwoo.org/dwoo/trunk@29 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 92a1ced8
[2008-04-] 0.4.0
[2008-04-] 0.4.0 (Happy Getty)
! BC Break: $dwoo->output() and get() have been swapped internally, but it
doesn't change anything for you unless you called output(*, *, *, true)
directly to emulate get(). This was done to reduce some overhead
+ Plugins: Added {extends} and {block} to handle template inheritance, read
more about it at http://wiki.dwoo.org/index.php/TemplateInheritance
+ Plugins: Added {loop} that combines {foreach} and {with}, see
http://wiki.dwoo.org/index.php/Block:loop for details
+ Plugins: Added {do} that executes whatever you feed it whitout echoing the
result, used internally for extends but you can use it if required
+ Syntax: Added shortcut for {$dwoo.const.*} using '%', for example you can use
{%FOO} instead of {$dwoo.const.FOO}
+ Syntax: When using named parameters, typing a parameter name without any
value is the same as typing param=true, for example {foo name="test" bar} and
{foo name="test" bar=true} are equals, can be useful for very complex plugins
with huge amounts of parameters.
+ Syntax: Added support for {$foo+=5}, {$foo="a"}, {$foo++} and {$foo--}
+ Syntax: Added shortcut for $dwoo.*, you can now use {$.const.FOO} instead of
{$dwoo.const.FOO} for example, applies to all $dwoo.* vars
+ Syntax: Added shortcut for $dwoo.*, you can now use {$.foreach.foo} instead
of {$dwoo.foreach.foo} for example, applies to all $dwoo.* vars
+ API: Added getSource(), getUid() and getResourceIdentifier() to DwooITemplate
+ API: Added setSecurityPolicy() too DwooICompiler and modified the arguments
of its compile() method
......@@ -22,6 +30,15 @@
doing {foreach $foo}..{/foreach} will not however, that way you don't have
to do {if isset($foo)} before the foreach, but automated isset() calls don't
impact performance as much as they did before.
* API: DwooITemplate->clearCache now requires a Dwoo instance as its first arg,
should not affect you unless you built a custom template class from scratch
* {include} now uses the current resource if none is provided instead of using
file as it did before
* Dwoo uses include path instead of absolute includes
* Fixed a regression in the handling of custom class plugins
* Fixed a bug in DwooSecurityPolicy->getAllowedDirectories(), no security
issue though
* TestFest happened early for Dwoo, lots of new tests and more code covered
[2008-04-09] 0.3.4
! BC Break: DWOO_PATH constant changed to DWOO_DIRECTORY
......
<?php
include dirname(__FILE__).DIRECTORY_SEPARATOR . 'DwooInterfaces.php';
include dirname(__FILE__).DIRECTORY_SEPARATOR . 'DwooPlugin.php';
include dirname(__FILE__).DIRECTORY_SEPARATOR . 'DwooTemplateString.php';
include dirname(__FILE__).DIRECTORY_SEPARATOR . 'DwooTemplateFile.php';
include dirname(__FILE__).DIRECTORY_SEPARATOR . 'DwooData.php';
set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__));
// TODO move those in Dwoo/Interface, ...
include 'DwooInterfaces.php';
include 'DwooPlugin.php';
include 'DwooTemplateString.php';
include 'DwooTemplateFile.php';
include 'DwooData.php';
define('DWOO_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR);
if(defined('DWOO_CACHE_DIRECTORY') === false)
......@@ -760,32 +762,32 @@ class Dwoo
* @param int $olderThan minimum time (in seconds) required for a cached template to be cleared
* @return int the amount of templates cleared
*/
public function clearCache($olderThan=0)
{
$cacheDirs = new RecursiveDirectoryIterator($this->cacheDir);
$cache = new RecursiveIteratorIterator($cacheDirs);
$expired = time() - $olderThan;
$count = 0;
foreach($cache as $file)
{
if($cache->isDot() || $cache->isDir())
continue;
if($cache->getCTime() < $expired)
$count += unlink((string) $file) ? 1 : 0;
}
return $count;
}
/**
* [util function] fetches a template object of the given resource
*
* @param string $resourceName the resource name (i.e. file, string)
* @param string $resourceId the resource identifier (i.e. file path)
* @param int $cacheTime the cache time setting for this resource
* @param string $cacheId the unique cache identifier
* @param string $compileId the unique compiler identifier
* @return DwooITemplate
*/
public function clearCache($olderThan=-1)
{
$cacheDirs = new RecursiveDirectoryIterator($this->cacheDir);
$cache = new RecursiveIteratorIterator($cacheDirs);
$expired = time() - $olderThan;
$count = 0;
foreach($cache as $file)
{
if($cache->isDot() || $cache->isDir() || substr($file, -5) !== '.html')
continue;
if($cache->getCTime() < $expired)
$count += unlink((string) $file) ? 1 : 0;
}
return $count;
}
/**
* [util function] fetches a template object of the given resource
*
* @param string $resourceName the resource name (i.e. file, string)
* @param string $resourceId the resource identifier (i.e. file path)
* @param int $cacheTime the cache time setting for this resource
* @param string $cacheId the unique cache identifier
* @param string $compileId the unique compiler identifier
* @return DwooITemplate
*/
public function getTemplate($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null)
{
if(isset($this->resources[$resourceName]))
......@@ -1750,7 +1752,7 @@ class DwooSecurityPolicy
*/
public function getAllowedDirectories()
{
return $this->allowedPHPFunc;
return $this->allowedDirectories;
}
/**
......
......@@ -42,7 +42,8 @@ class DwooData implements DwooIDataProvider
/**
* clears a the entire data or only the given key
*
* @param string $name clears only that value if set or clears the entire data if left null
* @param array|string $name clears only one value if you give a name, multiple values if
* you give an array of names, or the entire data if left null
*/
public function clear($name = null)
{
......
......@@ -79,10 +79,11 @@ interface DwooITemplate
/**
* clears the cached template if it's older than the given time
*
* @param Dwoo $dwoo the dwoo instance that was used to cache that template
* @param int $olderThan minimum time (in seconds) required for the cache to be cleared
* @return bool true if the cache was not present or if it was deleted, false if it remains there
*/
public function clearCache($olderThan=0);
public function clearCache(Dwoo $dwoo, $olderThan = -1);
/**
* returns the compiled template file name
......
......@@ -217,11 +217,11 @@ class DwooTemplateString implements DwooITemplate
// file is not cacheable
if($cacheLength === 0)
{
{
return false;
}
// already checked, return cache file
if(isset(self::$cache['cached'][$this->cacheId]) === true)
if(isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile))
{
return $cachedFile;
}
......@@ -257,10 +257,11 @@ class DwooTemplateString implements DwooITemplate
/**
* clears the cached template if it's older than the given time
*
* @param Dwoo $dwoo the dwoo instance that was used to cache that template
* @param int $olderThan minimum time (in seconds) required for the cache to be cleared
* @return bool true if the cache was not present or if it was deleted, false if it remains there
*/
public function clearCache($olderThan=0)
public function clearCache(Dwoo $dwoo, $olderThan = -1)
{
$cachedFile = $dwoo->getCacheDir() . $this->cacheId.'.html';
......
......@@ -29,7 +29,7 @@ class DwooPlugin_else extends DwooBlockPlugin implements DwooICompilableBlock
{
$block =& $compiler->getCurrentBlock();
$out = '';
while($block['type'] !== 'if' && $block['type'] !== 'foreach' && $block['type'] !== 'for' && $block['type'] !== 'with')
while($block['type'] !== 'if' && $block['type'] !== 'foreach' && $block['type'] !== 'for' && $block['type'] !== 'with' && $block['type'] !== 'loop')
{
$out .= $compiler->removeTopBlock();
$block =& $compiler->getCurrentBlock();
......
<?php
/**
* TOCOM
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
*
* This file is released under the LGPL
* "GNU Lesser General Public License"
* More information can be found here:
* {@link http://www.gnu.org/copyleft/lesser.html}
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @link http://dwoo.org/
* @version 0.3.4
* @date 2008-04-09
* @package Dwoo
*/
class DwooPlugin_loop extends DwooBlockPlugin implements DwooICompilableBlock
{
public static $cnt=0;
public function init($from, $name='default')
{
}
public static function preProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='', $type)
{
$params = $compiler->getCompiledParams($params);
$tpl = $compiler->getTemplateSource(true);
// assigns params
$src = $params['from'];
$name = $params['name'];
// evaluates which global variables have to be computed
$varName = '$dwoo.loop.'.trim($name, '"\'').'.';
$shortVarName = '$.loop.'.trim($name, '"\'').'.';
$usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false;
$usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false;
$usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false;
$usesIndex = $usesFirst || strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false;
$usesIteration = $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false;
$usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false;
$usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false;
// gets foreach id
$cnt = self::$cnt++;
// builds pre processing output
$out = DwooCompiler::PHP_OPEN . "\n".'$_loop'.$cnt.'_data = '.$src.';';
// adds foreach properties
if($usesAny)
{
$out .= "\n".'$this->globals["loop"]['.$name.'] = array'."\n(";
if($usesIndex) $out .="\n\t".'"index" => 0,';
if($usesIteration) $out .="\n\t".'"iteration" => 1,';
if($usesFirst) $out .="\n\t".'"first" => null,';
if($usesLast) $out .="\n\t".'"last" => null,';
if($usesShow) $out .="\n\t".'"show" => $this->isArray($_loop'.$cnt.'_data, true, true),';
if($usesTotal) $out .="\n\t".'"total" => $this->isArray($_loop'.$cnt.'_data) ? count($_loop'.$cnt.'_data) : 0,';
$out.="\n);\n".'$_loop'.$cnt.'_glob =& $this->globals["loop"]['.$name.'];';
}
// checks if foreach must be looped
$out .= "\n".'if($this->isArray($_loop'.$cnt.'_data, true, true) === true)'."\n{";
// iterates over keys
$out .= "\n\t".'foreach($_loop'.$cnt.'_data as $this->scope["-loop-"])'."\n\t{";
// updates properties
if($usesFirst)
$out .= "\n\t\t".'$_loop'.$cnt.'_glob["first"] = (string) ($_loop'.$cnt.'_glob["index"] === 0);';
if($usesLast)
$out .= "\n\t\t".'$_loop'.$cnt.'_glob["last"] = (string) ($_loop'.$cnt.'_glob["iteration"] === $_loop'.$cnt.'_glob["total"]);';
$out .= "\n\t\t".'$_loop'.$cnt.'_scope = $this->setScope("-loop-");'."\n// -- loop start output\n".DwooCompiler::PHP_CLOSE;
// build post processing output and cache it
$postOut = DwooCompiler::PHP_OPEN . "\n".'// -- loop end output'."\n\t\t".'$this->forceScope($_loop'.$cnt.'_scope);';
// update properties
if($usesIndex)
$postOut.="\n\t\t".'$_loop'.$cnt.'_glob["index"]+=1;';
if($usesIteration)
$postOut.="\n\t\t".'$_loop'.$cnt.'_glob["iteration"]+=1;';
// end loop
$postOut .= "\n\t}\n}\n";
// get block params and save the post-processing output already
$currentBlock =& $compiler->getCurrentBlock();
$currentBlock['params']['postOutput'] = $postOut . DwooCompiler::PHP_CLOSE;
return $out;
}
public static function postProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='')
{
return $params['postOutput'];
}
}
?>
\ No newline at end of file
......@@ -31,14 +31,12 @@ class DwooPlugin_with extends DwooBlockPlugin implements DwooICompilableBlock
$rparams = $compiler->getRealParams($params);
$cparams = $compiler->getCompiledParams($params);
$c = $rparams['var'];
$compiler->setScope($c);
$compiler->setScope($rparams['var']);
$params =& $compiler->getCurrentBlock();
$params['params']['postOutput'] = DwooCompiler::PHP_OPEN."\n// -- end with output\n".'$this->forceScope($_with'.(self::$cnt).');'."\n}\n".DwooCompiler::PHP_CLOSE;
return DwooCompiler::PHP_OPEN.'if('.$cparams['var'].')'."\n{\n".'$_with'.(self::$cnt++).' = $this->setScope("'.$c.'");'."\n// -- start with output\n".DwooCompiler::PHP_CLOSE;
return DwooCompiler::PHP_OPEN.'if('.$cparams['var'].')'."\n{\n".'$_with'.(self::$cnt++).' = $this->setScope("'.$rparams['var'].'");'."\n// -- start with output\n".DwooCompiler::PHP_CLOSE;
}
public static function postProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='')
......
<?php
/**
* TOCOM
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
*
* This file is released under the LGPL
* "GNU Lesser General Public License"
* More information can be found here:
* {@link http://www.gnu.org/copyleft/lesser.html}
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @link http://dwoo.org/
* @version 0.3.4
* @date 2008-04-09
* @package Dwoo
*/
class DwooPlugin_dump extends DwooPlugin
{
public function process($var = '$')
{
if ($var === '$') {
$var = $this->dwoo->getData();
$out = '<div style="background:#aaa; padding:5px; margin:5px;">data';
} else {
$out = '<div style="background:#aaa; padding:5px; margin:5px;">dump';
}
if(!is_array($var)) {
return $this->exportVar('', $var);
}
$scope = $this->dwoo->getScope();
if ($var === $scope) {
$out .= ' (current scope): <div style="background:#ccc;">';
} else {
$out .= ':<div style="padding-left:20px;">';
}
$out .= $this->export($var, $scope);
return $out .'</div></div>';
}
protected function export($var, $scope)
{
$out = '';
foreach ($var as $i=>$v) {
if (is_array($v)) {
$out .= $i;
if($v===$scope) {
$out .= ' (current scope):<div style="background:#ccc;padding-left:20px;">'.$this->export($v, $scope).'</div>';
} else {
$out .= ':<div style="padding-left:20px;">'.$this->export($v, $scope).'</div>';
}
} else {
$out .= $this->exportVar($i.' = ', $v);
}
}
return $out;
}
protected function exportVar($i, $v)
{
if (is_string($v) || is_bool($v) || is_numeric($v)) {
return $i.var_export($v, true).'<br />';
} elseif (is_null($v)) {
return $i.'null<br />';
} elseif (is_object($v)) {
return $i.'object('.get_class($v).')<br />';
} elseif (is_resource($v)) {
return $i.'resource('.get_resource_type($v).')<br />';
} else {
return $i.var_export($v, true).'<br />';
}
}
}
<?php
/**
* TOCOM
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
*
* This file is released under the LGPL
* "GNU Lesser General Public License"
* More information can be found here:
* {@link http://www.gnu.org/copyleft/lesser.html}
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @link http://dwoo.org/
* @version 0.3.4
* @date 2008-04-09
* @package Dwoo
*/
class DwooPlugin_dump extends DwooPlugin
{
public function process($var = '$')
{
if ($var === '$') {
$var = $this->dwoo->getData();
$out = '<div style="background:#aaa; padding:5px; margin:5px;">data';
} else {
$out = '<div style="background:#aaa; padding:5px; margin:5px;">dump';
}
if(!is_array($var)) {
return $this->exportVar('', $var);
}
$scope = $this->dwoo->getScope();
if ($var === $scope) {
$out .= ' (current scope): <div style="background:#ccc;">';
} else {
$out .= ':<div style="padding-left:20px;">';
}
$out .= $this->export($var, $scope);
return $out .'</div></div>';
}
protected function export($var, $scope)
{
$out = '';
foreach ($var as $i=>$v) {
if (is_array($v) || (is_object($v) && $v instanceof Iterator)) {
$out .= $i.' ('.(is_array($v) ? 'array':'object:'.get_class($v)).')';
if($v===$scope) {
$out .= ' (current scope):<div style="background:#ccc;padding-left:20px;">'.$this->export($v, $scope).'</div>';
} else {
$out .= ':<div style="padding-left:20px;">'.$this->export($v, $scope).'</div>';
}
} else {
$out .= $this->exportVar($i.' = ', $v);
}
}
return $out;
}
protected function exportVar($i, $v)
{
if (is_string($v) || is_bool($v) || is_numeric($v)) {
return $i.htmlentities(var_export($v, true)).'<br />';
} elseif (is_null($v)) {
return $i.'null<br />';
} elseif (is_object($v)) {
return $i.'object('.get_class($v).')<br />';
} elseif (is_resource($v)) {
return $i.'resource('.get_resource_type($v).')<br />';
} else {
return $i.htmlentities(var_export($v, true)).'<br />';
}
}
}
?>
\ No newline at end of file
......@@ -30,8 +30,9 @@ function DwooPlugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id = n
$identifier = $m[2];
}
else
{
$resource = 'file';
{
// get the current template's resource
$resource = $dwoo->getCurrentTemplate()->getResourceName();
$identifier = $file;
}
......
......@@ -31,7 +31,52 @@ class BlockTests extends PHPUnit_Framework_TestCase
$fixCall->init('');
}
public function testIf()
public function testExtends()
{
$tpl = new DwooTemplateFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'extend1.html');
$tpl->forceCompilation();
$this->assertThat($this->dwoo->get($tpl, array(), $this->compiler), new DwooConstraintStringEquals("foo
child1
toplevelContent1
bar
toplevelContent2
baz"));
}
public function testNonExtendedBlocksFromParent()
{
$tpl = new DwooTemplateFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'toplevel.html');
$tpl->forceCompilation();
$this->assertThat($this->dwoo->get($tpl, array(), $this->compiler), new DwooConstraintStringEquals("foo
toplevelContent1
bar
toplevelContent2
baz"));
// fixes the init call not being called (which is normal)
$fixCall = new DwooPlugin_block($this->dwoo);
$fixCall->init('');
}
public function testExtendsMultiple()
{
$tpl = new DwooTemplateFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'extend2.html');
$tpl->forceCompilation();
$this->assertThat($this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler), new DwooConstraintStringEquals("foo
child1
toplevelContent1child2
bar
FOObartoplevelContent2
baz"));
}
public function testIf()
{
$tpl = new DwooTemplateString('{if "BAR"==reverse($foo|reverse|upper)}true{/if}');
$tpl->forceCompilation();
......@@ -224,6 +269,30 @@ class BlockTests extends PHPUnit_Framework_TestCase
$this->assertEquals('bar', $this->dwoo->get($tpl, array(), $this->compiler));
}
public function testLoop()
{
$tpl = new DwooTemplateString('{loop $foo}{$.loop.default.index}>{$0}/{$1}{/loop}');
$tpl->forceCompilation();
$this->assertEquals('0>a/b1>c/d', $this->dwoo->get($tpl, array('foo'=>array(array('a','b'), array('c','d'))) , $this->compiler));
// fixes the init call not being called (which is normal)
$fixCall = new DwooPlugin_loop($this->dwoo);
$fixCall->init('');
}
public function testLoopElse()
{
$tpl = new DwooTemplateString('{loop $foo}{$.loop.default.index}>{$0}/{$1}{else}MOO{/loop}');
$tpl->forceCompilation();
$this->assertEquals('MOO', $this->dwoo->get($tpl, array() , $this->compiler));
// fixes the init call not being called (which is normal)
$fixCall = new DwooPlugin_loop($this->dwoo);
$fixCall->init('');
}
public function testTextFormat()
{
$tpl = new DwooTemplateString('aa{textformat wrap=10}hello world is so unoriginal but hey.. {textformat wrap=4}a a a a a a{/textformat}it works.{/textformat}bb');
......
<?php
require_once dirname(dirname(__FILE__)).'/DwooCompiler.php';
class CallTests extends PHPUnit_Framework_TestCase
{
protected $compiler;
protected $dwoo;
public function __construct()
{
// extend this class and override this in your constructor to test a modded compiler
$this->compiler = new DwooCompiler();
$this->dwoo = new Dwoo();
}
public function testCustomFunctionPlugin()
{
$this->dwoo->addPlugin('test', 'plugin_custom_name');
$tpl = new DwooTemplateString('{test "xxx"}');
$tpl->forceCompilation();
$this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler));
$this->dwoo->removePlugin('test');
}
public function testHalfCustomClassPluginByClassMethodCallback()
{
$this->dwoo->addPlugin('test', array('plugin_half_custom', 'process'));
$tpl = new DwooTemplateString('{test "xxx"}');
$tpl->forceCompilation();
$this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler));
$this->dwoo->removePlugin('test');
}
public function testFullCustomClassPluginByClassMethodCallback()
{
$this->dwoo->addPlugin('test', array('plugin_full_custom', 'process'));
$tpl = new DwooTemplateString('{test "xxx"}');
$tpl->forceCompilation();
$this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler));
$this->dwoo->removePlugin('test');
}
public function testCustomClassPluginByClassname()
{
$this->dwoo->addPlugin('test', 'plugin_full_custom');
$tpl = new DwooTemplateString('{test "xxx"}');
$tpl->forceCompilation();
$this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler));
$this->dwoo->removePlugin('test');
}
public function testCustomObjectPluginByObjectMethodCallback()
{
$this->dwoo->addPlugin('test', array(new plugin_full_custom(), 'process'));
$tpl = new DwooTemplateString('{test "xxx"}');
$tpl->forceCompilation();
$this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler));
$this->dwoo->removePlugin('test');
}
public function testCustomBlockPluginByClassMethodCallback()
{
$this->dwoo->addPlugin('test', array('blockplugin_custom', 'process'));
$tpl = new DwooTemplateString('{test "xxx"}aaa{/test}');
$tpl->forceCompilation();
$this->assertEquals('xxxbaraaa', $this->dwoo->get($tpl, array(), $this->compiler));
$this->dwoo->removePlugin('test');
}
public function testCustomBlockPluginByClassname()
{
$this->dwoo->addPlugin('test', 'blockplugin_custom');
$tpl = new DwooTemplateString('{test "xxx"}aaa{/test}');
$tpl->forceCompilation();
$this->assertEquals('xxxbaraaa', $this->dwoo->get($tpl, array(), $this->compiler));
$this->dwoo->removePlugin('test');
}
/**
* @expectedException DwooException
*/
public function testCustomInvalidPlugin()
{
$this->dwoo->addPlugin('test', 'sdfmslkfmsle');
}
}
function plugin_custom_name(Dwoo $dwoo, $foo, $bar="bar")
{
return $foo.$bar;
}
class plugin_half_custom extends DwooPlugin
{
public function process($foo, $bar="bar")
{
return $foo.$bar;
}
}
class plugin_full_custom
{
public function process($foo, $bar="bar")
{
return $foo.$bar;
}
}
class blockplugin_custom extends DwooBlockPlugin
{
public function init($foo, $bar="bar")
{
$this->foo = $foo;
$this->bar = $bar;
}
public function process()
{
return $this->foo.$this->bar.$this->buffer;
}
}
?>
\ No newline at end of file
......@@ -175,6 +175,16 @@ class CompilerTests extends PHPUnit_Framework_TestCase
$this->assertEquals(TEST.' '.(Dwoo::FUNC_PLUGIN*Dwoo::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler));
}
public function testShortConstants()
{
if(!defined('TEST'))
define('TEST', 'Test');
$tpl = new DwooTemplateString('{%TEST} {$dwoo.const.Dwoo::FUNC_PLUGIN*%Dwoo::BLOCK_PLUGIN}');
$tpl->forceCompilation();
$this->assertEquals(TEST.' '.(Dwoo::FUNC_PLUGIN*Dwoo::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler));
}
public function testAltDelimiters()
{
$tpl = new DwooTemplateString('{"test"} <%"test"%> <%"foo{lol}\%>"%>');
......@@ -260,6 +270,16 @@ class CompilerTests extends PHPUnit_Framework_TestCase
$this->assertEquals("moo8", $this->dwoo->get($tpl, array('foo'=>0), $this->compiler));
}
public function testSetStringValToTrueWhenUsingNamedParams()
{
$this->dwoo->addPlugin('test', create_function('Dwoo $dwoo, $name, $bool=false', 'return $bool ? $name."!" : $name."?";'));
$tpl = new DwooTemplateString('{test name="Name"}{test name="Name" bool}');
$tpl->forceCompilation();
$this->assertEquals("Name?Name!", $this->dwoo->get($tpl, array(), $this->compiler));
$this->dwoo->removePlugin('test');
}
}
class MethodCallsHelper {
......
......@@ -19,6 +19,10 @@ class CoreTests extends PHPUnit_Framework_TestCase
$this->compiler = new DwooCompiler();
$this->dwoo = new Dwoo();
DwooLoader::rebuildClassPathCache(DWOO_DIRECTORY.'plugins', DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php');
// fixes the init call not being called (which is normal)
$fixCall = new DwooPlugin_topLevelBlock($this->dwoo);
$fixCall->init('');
}
public function testReadVar()
......@@ -51,6 +55,72 @@ class CoreTests extends PHPUnit_Framework_TestCase
$this->assertEquals('a', $this->dwoo->readParentVar(2, 'bar'));
}
public function testDwooOutput()
{
$tpl = new DwooTemplateString('a');
$tpl->forceCompilation();
ob_start();
$this->dwoo->output($tpl, array());
$output = ob_get_clean();
$this->assertEquals('a', $output);
}
/**
* @expectedException DwooException
*/
public function testDwooGetNonTemplate()
{
$this->dwoo->output(null, array());
}
/**
* @expectedException DwooException
*/
public function testDwooGetNonData()
{
$tpl = new DwooTemplateString('a');
$this->dwoo->get($tpl, null);
}
public function testGetSetSecurityPolicy()
{
$dwoo = new Dwoo();
$policy = new DwooSecurityPolicy();
$policy->setConstantHandling(DwooSecurityPolicy::CONST_ALLOW);
$dwoo->setSecurityPolicy($policy);
$this->assertEquals($policy, $dwoo->getSecurityPolicy());
$this->assertEquals($policy->getConstantHandling(), $dwoo->getSecurityPolicy()->getConstantHandling());
}
/**
* @expectedException DwooException
*/
public function testWrongResourceName()
{
$this->dwoo->getTemplate('sdmlb', 'fookm');
}
public function testIsCached()
{
$tpl = new DwooTemplateString('foo');
$this->assertEquals(false, $this->dwoo->isCached($tpl));
}
public function testClearCache()
{
$cacheDir = $this->dwoo->getCacheDir();
$this->dwoo->clearCache();
file_put_contents($cacheDir.DIRECTORY_SEPARATOR.'junk.html', 'test');
$this->assertEquals(1, $this->dwoo->clearCache());
}
public function testDwooGetFilename()
{
$this->assertEquals('44BAR', $this->dwoo->get(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'test.html', array('foo'=>44, 'bar'=>'BAR')));
}
public function testAssignVarInScope()
{
$tpl = new DwooTemplateString('{assign "Yay!" a.b->qux}{$a.b->qux}');
......@@ -126,7 +196,7 @@ class CoreTests extends PHPUnit_Framework_TestCase
$this->dwoo->addResource('file', 'DwooTemplateString', 'Fake');
$this->dwoo->removeResource('file');
$tpl = new DwooTemplateString('{include file="'.DWOO_DIRECTORY.'tests/resources/test.html" foo=3 bar=4}');
$tpl = new DwooTemplateString('{include file="file:'.DWOO_DIRECTORY.'tests/resources/test.html" foo=3 bar=4}');
$tpl->forceCompilation();
$this->assertEquals("34", $this->dwoo->get($tpl, array()));
}
......@@ -147,6 +217,78 @@ class CoreTests extends PHPUnit_Framework_TestCase
$this->assertEquals(null, $tpl->getCompiler());
$this->assertEquals(false, DwooTemplateString::templateFactory($this->dwoo, 'foo', 5));
}
public function testCachedTemplateAndClearCache()
{
$tpl = new DwooTemplateString('foo{$foo}', 10, 'cachetest');
$tpl->forceCompilation();
$this->assertEquals("foo1", $this->dwoo->get($tpl, array('foo'=>1)));
$this->assertEquals(true, $this->dwoo->isCached($tpl));
$this->assertEquals("foo1", $this->dwoo->get($tpl, array('foo'=>1)));
$this->assertEquals(1, $this->dwoo->clearCache(-11));
$this->assertEquals(false, $this->dwoo->isCached($tpl));
}
public function testCachedTemplateAndOutput()
{
$tpl = new DwooTemplateString('foo{$foo}', 10, 'cachetest');
$tpl->forceCompilation();
ob_start();
$this->dwoo->output($tpl, array('foo'=>1));
$cap = ob_get_clean();
$this->assertEquals("foo1", $cap);
$this->assertEquals(true, $this->dwoo->isCached($tpl));
ob_start();
$this->dwoo->output($tpl, array('foo'=>1));
$cap = ob_get_clean();
$this->assertEquals("foo1", $cap);
$this->assertEquals(1, $this->dwoo->clearCache(-11));
}
public function testCachedTemplateWithDwooCache()
{
$dwoo = new Dwoo();
$dwoo->setCacheTime(5);
$tpl = new DwooTemplateString('foo{$foo}bar', null, 'cachetest2');
$tpl->forceCompilation();
$this->assertEquals("foo1bar", $dwoo->get($tpl, array('foo'=>1)));
$this->assertEquals(true, $dwoo->isCached($tpl));
$this->assertEquals("foo1bar", $dwoo->get($tpl, array('foo'=>1)));
$this->assertEquals(1, $dwoo->clearCache(-11));
$this->assertEquals(false, $dwoo->isCached($tpl));
}
public function testClearCacheOnTemplateClass()
{
$dwoo = new Dwoo();
$dwoo->setCacheTime(5);
$tpl = new DwooTemplateString('foo{$foo}bar', null, 'cachetest2');
$tpl->forceCompilation();
$this->assertEquals("foo1bar", $dwoo->get($tpl, array('foo'=>1)));
$this->assertEquals(true, $dwoo->isCached($tpl));
$this->assertEquals("foo1bar", $dwoo->get($tpl, array('foo'=>1)));
$this->assertEquals(false, $tpl->clearCache($dwoo, 1));
$this->assertEquals(true, $tpl->clearCache($dwoo, -1));
$this->assertEquals(false, $dwoo->isCached($tpl));
}
public function testTemplateGetSet()
{
$dwoo = new Dwoo();
$dwoo->setCacheTime(5);
$tpl = new DwooTemplateString('foo');
$tpl2 = new DwooTemplateFile('./resources/test.html');
$this->assertEquals(false, $tpl->getResourceIdentifier());
$this->assertEquals('string', $tpl->getResourceName());
$this->assertEquals('file', $tpl2->getResourceName());
$this->assertEquals(hash('md4','foo'), $tpl->getUid());
}
}
?>
\ No newline at end of file
......@@ -11,17 +11,15 @@ class DataTests extends PHPUnit_Framework_TestCase
{
// extend this class and override this in your constructor to test a modded compiler
$this->compiler = new DwooCompiler();
$this->dwoo = new Dwoo();
$this->dwoo = new Dwoo();
$this->tpl = new DwooTemplateString('{$var}{$var2}{$var3}{$var4}');
$this->tpl->forceCompilation();
}
public function testDwooData()
public function testSetMergeAndClear()
{
// test simple assign
$tpl = new DwooTemplateString('{$var}{$var2}{$var3}{$var4}');
$tpl->forceCompilation();
$data = new DwooData();
$data->setData(array('foo'));
$this->assertEquals(array('foo'), $data->getData());
......@@ -31,15 +29,49 @@ class DataTests extends PHPUnit_Framework_TestCase
$data->clear();
$this->assertEquals(array(), $data->getData());
}
public function testAssign()
{
$data = new DwooData();
$data->assign('var', '1');
$data->assign(array('var2'=>'1', 'var3'=>1));
$ref = 0;
$data->assignByRef('var4', $ref);
$ref = 1;
$this->assertEquals('1111', $this->dwoo->get($tpl, $data, $this->compiler));
$this->assertEquals('1111', $this->dwoo->get($this->tpl, $data, $this->compiler));
}
public function testClear()
{
$data = new DwooData();
$data->assign(array('var2'=>'1', 'var3'=>1, 'var4'=>5));
$data->clear(array('var2', 'var4'));
$this->assertEquals(array('var3'=>1), $data->getData());
$data->assign('foo', 'moo');
$data->clear('var3');
$this->assertEquals(array('foo'=>'moo'), $data->getData());
}
public function testAppend()
{
$data = new DwooData();
$data->assign('var', 'val');
$data->append('var', 'moo');
$this->assertEquals(array('var'=>array('val','moo')), $data->getData());
$data->assign('var', 'val');
$data->append(array('var'=>'moo', 'var2'=>'bar'));
$this->assertEquals(array('var'=>array('val','moo'), 'var2'=>array('bar')), $data->getData());
}
}
?>
\ No newline at end of file
......@@ -6,7 +6,7 @@ if(!ini_get('date.timezone'))
define('DWOO_CACHE_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'cache');
define('DWOO_COMPILE_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'compiled');
require dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'Dwoo.php';
class DwooTests {
public static function suite() {
PHPUnit_Util_Filter::addDirectoryToWhitelist(DWOO_DIRECTORY.'plugins/builtin');
......@@ -32,5 +32,27 @@ class DwooTests {
return $suite;
}
}
// Evaluates two strings and ignores differences in line endings (\r\n == \n == \r)
class DwooConstraintStringEquals extends PHPUnit_Framework_Constraint
{
protected $target;
public function __construct($target)
{
$this->target = preg_replace('#(\r\n|\r)#', "\n", $target);
}
public function evaluate($other)
{
$this->other = preg_replace('#(\r\n|\r)#', "\n", $other);
return $this->target == $this->other;
}
public function toString()
{
return 'equals "'.$this->target.'" / "'.$this->other.'"';
}
}
?>
\ No newline at end of file
......@@ -134,6 +134,14 @@ class FuncTests extends PHPUnit_Framework_TestCase
$this->assertEquals("moo", $this->dwoo->get($tpl, array('foo'=>"moo"), $this->compiler));
}
public function testDoEmpty()
{
$tpl = new DwooTemplateString('{do}');
$tpl->forceCompilation();
$this->assertEquals("", $this->dwoo->get($tpl, array('foo'=>"moo"), $this->compiler));
}
public function testEscape()
{
......@@ -196,32 +204,6 @@ class FuncTests extends PHPUnit_Framework_TestCase
$this->assertEquals("moo+baz", $this->dwoo->get($tpl, array('foo'=>'{$test}', 'test'=>'moo'), $this->compiler));
}
public function testExtends()
{
$tpl = new DwooTemplateFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'extend1.html');
$tpl->forceCompilation();
$this->assertEquals("foo
child1
toplevelContent1
bar
toplevelContent2
baz", $this->dwoo->get($tpl, array(), $this->compiler));
}
public function testExtendsMultiple()
{
$tpl = new DwooTemplateFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'extend2.html');
$tpl->forceCompilation();
$this->assertEquals("foo
child1
toplevelContent1child2
bar
FOObartoplevelContent2
baz", $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
}
public function testFetch()
{
$tpl = new DwooTemplateString('{fetch file="'.DWOO_DIRECTORY.'tests/resources/test.html"}');
......@@ -232,7 +214,7 @@ baz", $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
public function testInclude()
{
$tpl = new DwooTemplateString('{include file=\''.DWOO_DIRECTORY.'tests/resources/test.html\' foo=$a bar=$b}');
$tpl = new DwooTemplateString('{include file=\'file:'.DWOO_DIRECTORY.'tests/resources/test.html\' foo=$a bar=$b}');
$tpl->forceCompilation();
$this->assertEquals("AB", $this->dwoo->get($tpl, array('a'=>'A', 'b'=>'B')));
......
......@@ -68,7 +68,30 @@ class SecurityTests extends PHPUnit_Framework_TestCase
$this->assertEquals("fooOK", $this->dwoo->get($tpl, array(), $this->compiler));
}
public function testAllowDirectoryGetSet()
{
$old = $this->policy->getAllowedDirectories();
$this->policy->allowDirectory(array('./resources'));
$this->policy->allowDirectory('./temp');
$this->assertEquals(array_merge($old, array(realpath('./resources')=>true, realpath('./temp')=>true)), $this->policy->getAllowedDirectories());
$this->policy->disallowDirectory(array('./resources'));
$this->policy->disallowDirectory('./temp');
$this->assertEquals($old, $this->policy->getAllowedDirectories());
}
public function testAllowPhpGetSet()
{
$old = $this->policy->getAllowedPhpFunctions();
$this->policy->allowPhpFunction(array('a','b'));
$this->policy->allowPhpFunction('c');
$this->assertEquals(array_merge($old, array('a'=>true, 'b'=>true, 'c'=>true)), $this->policy->getAllowedPhpFunctions());
$this->policy->disallowPhpFunction(array('a', 'b'));
$this->policy->disallowPhpFunction('c');
$this->assertEquals($old, $this->policy->getAllowedPhpFunctions());
}
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment