mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 18:34:27 +02:00
3.1.21
This commit is contained in:
@@ -295,10 +295,10 @@ KEY `expire` (`expire`)
|
||||
public function normalizeString($in)
|
||||
{
|
||||
if (is_string($in)) {
|
||||
$in = str_replace("\r", '', $in);
|
||||
$in = str_replace("\t", ' ', $in);
|
||||
return str_replace(array("\r", "\t"), array('', ' '), $in);
|
||||
} else {
|
||||
return $in;
|
||||
}
|
||||
return $in;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -319,8 +319,10 @@ KEY `expire` (`expire`)
|
||||
$dir = isset($dir) ? $dir : $this->smarty->getTemplateDir(0);
|
||||
switch ($type) {
|
||||
case 'file':
|
||||
case 'filetest':
|
||||
case 'php':
|
||||
return $this->normalizePath($dir . $name, true);
|
||||
return $dir . $name;
|
||||
return $this->normalizePath($dir . $name);
|
||||
case 'mysqltest':
|
||||
case 'mysql':
|
||||
return sha1($type . ':' . $name);
|
||||
@@ -347,13 +349,12 @@ KEY `expire` (`expire`)
|
||||
$type = isset($type) ? $type : $tpl->source->type;
|
||||
$name = isset($name) ? $name : $tpl->source->name;
|
||||
switch ($type) {
|
||||
case 'php':
|
||||
case 'file':
|
||||
if ($tpl instanceof Smarty) {
|
||||
return sha1(realpath($this->normalizePath($this->smarty->getTemplateDir(0) . $name)));
|
||||
return sha1(getcwd() . $this->normalizePath($this->smarty->getTemplateDir(0) . $name));
|
||||
}
|
||||
return sha1(realpath($tpl->source->filepath));
|
||||
case 'php':
|
||||
return sha1($tpl->source->filepath);
|
||||
return sha1(getcwd() . $tpl->source->filepath);
|
||||
case 'mysqltest':
|
||||
case 'mysql':
|
||||
return sha1($type . ':' . $name);
|
||||
@@ -434,6 +435,12 @@ KEY `expire` (`expire`)
|
||||
$sp = $this->buildSourcePath($tpl, $name, $type, $dir);
|
||||
$uid = $this->buildUid($tpl, $sp, $name, $type);
|
||||
$_flag = '';
|
||||
if (isset($tpl->source) && $tpl->source->isConfig) {
|
||||
$_flag = '_' . ((int) $tpl->smarty->config_read_hidden + (int) $tpl->smarty->config_booleanize * 2
|
||||
+ (int) $tpl->smarty->config_overwrite * 4);
|
||||
} else {
|
||||
$_flag = '_' . ((int) $tpl->smarty->merge_compiled_includes + (int) $tpl->smarty->escape_html * 2);
|
||||
}
|
||||
$_filepath = $uid . $_flag;
|
||||
// if use_sub_dirs, break file into directories
|
||||
if ($sub) {
|
||||
@@ -485,7 +492,8 @@ KEY `expire` (`expire`)
|
||||
{
|
||||
$cacheType = isset($cacheType) ? $cacheType : $tpl->smarty->caching_type;
|
||||
switch ($cacheType) {
|
||||
case 'file':$sep = DS;
|
||||
case 'file':
|
||||
$sep = DS;
|
||||
$_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
|
||||
$_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
|
||||
$sp = $this->buildSourcePath($tpl, $name, $type, $dir);
|
||||
@@ -525,19 +533,6 @@ KEY `expire` (`expire`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefilter to remove \r from template source
|
||||
*
|
||||
* @param string $tpl_source
|
||||
* @param $template
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function remove_cr($tpl_source, $template)
|
||||
{
|
||||
return str_replace(array("\r\n", "\r"), "\n", $tpl_source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove Smarty object from cached resources
|
||||
*
|
||||
|
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* Smarty PHPunit tests for cache resource file
|
||||
*
|
||||
* @package PHPunit
|
||||
* @author Uwe Tews
|
||||
*/
|
||||
|
||||
include_once __DIR__ . '/../_shared/CacheResourceTestCommon.php';
|
||||
|
||||
/**
|
||||
* class for cache resource file tests
|
||||
*
|
||||
* @backupStaticAttributes enabled
|
||||
*/
|
||||
class CacheResourceCustomPDOTest extends CacheResourceTestCommon
|
||||
{
|
||||
|
||||
public function setUp($dir = null, $clear = true)
|
||||
{
|
||||
if (self::$config['cacheResource']['MysqlEnable'] != 'true') {
|
||||
$this->markTestSkipped('mysql tests are disabled');
|
||||
}
|
||||
if (self::$init) {
|
||||
$this->getConnection();
|
||||
}
|
||||
$this->setUpSmarty(__DIR__);
|
||||
parent::setUp();
|
||||
$this->smarty->setCachingType('pdo');
|
||||
$this->smarty->addPluginsDir(SMARTY_DIR . '../demo/plugins/');
|
||||
$this->assertTrue(false !== $this->smarty->loadPlugin('Smarty_CacheResource_Pdo'), 'loadPlugin() could not load PDO cache resource');
|
||||
$this->smarty->registerCacheResource('pdo', new Smarty_CacheResource_Pdo($this->getPDO(), 'output_cache'));
|
||||
}
|
||||
|
||||
public function testInit()
|
||||
{
|
||||
$this->cleanDirs();
|
||||
$this->initMysqlCache();
|
||||
}
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* Smarty PHPunit tests for cache resource file
|
||||
*
|
||||
* @package PHPunit
|
||||
* @author Uwe Tews
|
||||
*/
|
||||
|
||||
include_once __DIR__ . '/../_shared/CacheResourceTestCommon.php';
|
||||
|
||||
/**
|
||||
* class for cache resource file tests
|
||||
*
|
||||
* @backupStaticAttributes enabled
|
||||
*/
|
||||
class CacheResourceCustomPDOGzipTest extends CacheResourceTestCommon
|
||||
{
|
||||
|
||||
public function setUp($dir = null, $clear = true)
|
||||
{
|
||||
if (self::$config['cacheResource']['MysqlEnable'] != 'true') {
|
||||
$this->markTestSkipped('mysql tests are disabled');
|
||||
}
|
||||
if (self::$init) {
|
||||
$this->getConnection();
|
||||
}
|
||||
$this->setUpSmarty(__DIR__);
|
||||
parent::setUp();
|
||||
$this->smarty->setCachingType('pdo');
|
||||
$this->smarty->addPluginsDir(SMARTY_DIR . '../demo/plugins/');
|
||||
$this->assertTrue(false !== $this->smarty->loadPlugin('Smarty_CacheResource_Pdo_Gzip'), 'loadPlugin() could not load PDOGzip cache resource');
|
||||
$this->smarty->registerCacheResource('pdo', new Smarty_CacheResource_Pdo_Gzip($this->getPDO(), 'output_cache'));
|
||||
}
|
||||
|
||||
public function testInit()
|
||||
{
|
||||
$this->cleanDirs();
|
||||
$this->initMysqlCache();
|
||||
}
|
||||
}
|
@@ -447,7 +447,7 @@ class CacheResourceTestCommon extends PHPUnit_Smarty
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
*
|
||||
*/
|
||||
@@ -458,14 +458,13 @@ class CacheResourceTestCommon extends PHPUnit_Smarty
|
||||
$this->smarty->cache_lifetime = 1000;
|
||||
$this->smarty->assign('test', 6);
|
||||
$filepath = $this->buildSourcePath(null, 'cacheresource.tpl', 'file');
|
||||
touch($filepath);
|
||||
touch($filepath, $t = time());
|
||||
sleep(2);
|
||||
clearstatcache();
|
||||
$tpl = $this->smarty->createTemplate('cacheresource.tpl', $this->smarty);
|
||||
$this->assertEquals($t,$tpl->source->timestamp);
|
||||
$isCached = $tpl->isCached();
|
||||
$mustCompile = $tpl->mustCompile();
|
||||
$this->assertFalse($isCached, 'isCached() must be false after touch of source');
|
||||
$this->assertTrue($mustCompile, 'mustCompile() must be true after touch of source');
|
||||
$this->assertEquals('cache resource test:6 compiled:6 rendered:6', $this->smarty->fetch($tpl), 'recompile failed');
|
||||
$this->assertFalse($isCached, 'isCached() must be false after touch of source');
|
||||
$this->assertTrue($mustCompile, 'mustCompile() must be true after touch of source');
|
||||
|
@@ -332,18 +332,18 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty
|
||||
|
||||
/**
|
||||
* @expectedException SmartyCompilerException
|
||||
* @expectedExceptionMessage Syntax error in template ".\templates\025_parent.tpl"
|
||||
* @expectedExceptionMessage Syntax error in template "./templates/025_parent.tpl"
|
||||
* @expectedExceptionMessage tag {$smarty.block.child} used outside {block} tags
|
||||
* test {$this->smarty.block.child} outside {block]
|
||||
*/
|
||||
public function testSmartyBlockChildOutsideBlock_025()
|
||||
{
|
||||
$this->smarty->fetch('025_parent.tpl');
|
||||
}
|
||||
$this->smarty->fetch('025_parent.tpl');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException SmartyCompilerException
|
||||
* @expectedExceptionMessage Syntax error in template ".\templates\026_parent.tpl"
|
||||
* @expectedExceptionMessage Syntax error in template "./templates/026_parent.tpl"
|
||||
* @expectedExceptionMessage tag {$smarty.block.parent} used outside {block} tags
|
||||
* test {$this->smarty.block.parent} outside {block]
|
||||
*/
|
||||
@@ -354,7 +354,7 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty
|
||||
|
||||
/**
|
||||
* @expectedException SmartyCompilerException
|
||||
* @expectedExceptionMessage Syntax error in template ".\templates\027_parent.tpl"
|
||||
* @expectedExceptionMessage Syntax error in template "./templates/027_parent.tpl"
|
||||
* @expectedExceptionMessage illegal {$smarty.block.parent} in parent template
|
||||
* test {$this->smarty.block.parent} in parent template
|
||||
*/
|
||||
|
@@ -59,7 +59,7 @@ class CompileInsertTest extends PHPUnit_Smarty
|
||||
$this->assertEquals('param foo bar globalvar global', $this->smarty->fetch($tpl));
|
||||
}
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
* test insert plugin
|
||||
*/
|
||||
@@ -87,7 +87,25 @@ class CompileInsertTest extends PHPUnit_Smarty
|
||||
|
||||
/**
|
||||
*
|
||||
* @runInSeparateProcess
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
*
|
||||
*/
|
||||
public function testInsertPluginCaching1_2()
|
||||
{
|
||||
$this->smarty->addPluginsDir(__DIR__ . "/PHPunitplugins/");
|
||||
global $insertglobal;
|
||||
$insertglobal = 'changed global 2';
|
||||
$this->smarty->caching = 1;
|
||||
$tpl = $this->smarty->createTemplate('insertplugintest.tpl');
|
||||
$tpl->assign('foo', 'buh', true);
|
||||
// $this->assertTrue($tpl->isCached());
|
||||
$this->assertEquals('param foo buh globalvar changed global 2', $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
*
|
||||
*/
|
||||
@@ -102,6 +120,24 @@ class CompileInsertTest extends PHPUnit_Smarty
|
||||
$this->assertEquals('param foo bar globalvar changed global', $this->smarty->fetch('insertplugintest.tpl'));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
*
|
||||
*/
|
||||
public function testInsertPluginCaching1_4()
|
||||
{
|
||||
global $insertglobal;
|
||||
$this->smarty->addPluginsDir(__DIR__ . "/PHPunitplugins/");
|
||||
if (true) { //disabled
|
||||
$insertglobal = 'changed global 4';
|
||||
$this->smarty->caching = 1;
|
||||
$this->smarty->assign('foo', 'buh', true);
|
||||
$this->assertTrue($this->smarty->isCached('insertplugintest.tpl'));
|
||||
$this->assertEquals('param foo buh globalvar changed global 4', $this->smarty->fetch('insertplugintest.tpl'));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* test insert plugin caching 2
|
||||
*/
|
||||
@@ -118,7 +154,7 @@ class CompileInsertTest extends PHPUnit_Smarty
|
||||
|
||||
/**
|
||||
* test insert plugin caching 2
|
||||
* @runInSeparateProcess
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
*/
|
||||
public function testInsertPluginCaching2_2()
|
||||
|
@@ -51,6 +51,51 @@ class CompileFunctionTest extends PHPUnit_Smarty
|
||||
$this->assertEquals("default param default 1 2 1", $this->smarty->fetch('test_template_function_001.tpl'), $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
* @dataProvider functionProvider
|
||||
* test simple function call tag cached
|
||||
*/
|
||||
public function testSimpleFunctionCached_002($text)
|
||||
{
|
||||
$this->smarty->setCaching(1);
|
||||
$this->smarty->assign('param', 1);
|
||||
$this->smarty->assign('default', 2);
|
||||
$this->assertEquals("default param default 1 2 1", $this->smarty->fetch('test_template_function_002.tpl'), $text);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
* @dataProvider functionProvider
|
||||
* test simple function call tag cached no cache default variable
|
||||
*/
|
||||
public function testSimpleFunctionCachedNocacheDefault_002_1($text)
|
||||
{
|
||||
$this->smarty->setCaching(1);
|
||||
$this->smarty->setCompileId(1);
|
||||
$this->smarty->assign('param', 1);
|
||||
$this->smarty->assign('default', 2, true);
|
||||
$this->assertEquals("default param default 1 2 1", $this->smarty->fetch('test_template_function_002.tpl'), $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* test simple function call tag ached no cache default variable 2
|
||||
*
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
*/
|
||||
public function testSimpleFunctionCachedNocacheDefault_002_2()
|
||||
{
|
||||
$this->smarty->setCaching(1);
|
||||
$this->smarty->setCompileId(1);
|
||||
$this->smarty->assign('param', 4);
|
||||
$this->smarty->assign('default', 8, true);
|
||||
$this->assertEquals("default param default 1 8 4", $this->smarty->fetch('test_template_function_002.tpl'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
@@ -65,6 +110,23 @@ class CompileFunctionTest extends PHPUnit_Smarty
|
||||
$this->assertEquals("default 1", $this->smarty->fetch('test_template_function_003.tpl'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
* @dataProvider functionProvider
|
||||
* test simple function call tag plugin nocache
|
||||
*
|
||||
*/
|
||||
public function testSimpleFunctionCachedPluginNocache_003($text)
|
||||
{
|
||||
$this->smarty->setCaching(1);
|
||||
$this->smarty->setCompileId(1);
|
||||
$this->smarty->assign('param', 1);
|
||||
$this->smarty->assign('default', 2, true);
|
||||
$this->assertEquals("default 1", $this->smarty->fetch('test_template_function_003.tpl'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
@@ -154,6 +216,61 @@ class CompileFunctionTest extends PHPUnit_Smarty
|
||||
$this->assertContains('foo bar', $this->smarty->fetch($tpl), $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
* @dataProvider functionProviderInline
|
||||
* test external function definition nocache call
|
||||
*
|
||||
*/
|
||||
public function testExternalDefinedFunctionNocachedCall1($merge, $text)
|
||||
{
|
||||
$this->smarty->setMergeCompiledIncludes($merge);
|
||||
$cacheId = $merge ? 'merge' : null;
|
||||
$this->smarty->caching = 1;
|
||||
$this->smarty->cache_lifetime = 1000;
|
||||
$tpl = $this->smarty->createTemplate('test_template_function_nocache_call.tpl', $cacheId);
|
||||
$tpl->assign('foo', 'foo');
|
||||
$this->assertContains('foo foo', $this->smarty->fetch($tpl), $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
* @dataProvider functionProviderInline
|
||||
* test external function definition nocache call 2
|
||||
*
|
||||
*/
|
||||
public function testExternalDefinedFunctionNocachedCall2($merge, $text)
|
||||
{
|
||||
$this->smarty->setMergeCompiledIncludes($merge);
|
||||
$cacheId = $merge ? 'merge' : null;
|
||||
$this->smarty->caching = 1;
|
||||
$this->smarty->cache_lifetime = 1000;
|
||||
$tpl = $this->smarty->createTemplate('test_template_function_nocache_call.tpl', $cacheId);
|
||||
$this->assertTrue($this->smarty->isCached($tpl), $text);
|
||||
$tpl->assign('foo', 'bar');
|
||||
$this->assertContains('bar bar', $this->smarty->fetch($tpl), $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* test external function definition nocache call 3
|
||||
*
|
||||
* @run InSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
* @dataProvider functionProviderInline
|
||||
*/
|
||||
public function testExternalDefinedFunctionNocachedCall3($merge, $text)
|
||||
{
|
||||
$this->smarty->setMergeCompiledIncludes($merge);
|
||||
$cacheId = $merge ? 'merge' : null;
|
||||
$this->smarty->caching = 1;
|
||||
$this->smarty->cache_lifetime = 1000;
|
||||
$tpl = $this->smarty->createTemplate('test_template_function_nocache_call.tpl', $cacheId);
|
||||
$this->assertTrue($this->smarty->isCached($tpl), $text);
|
||||
$tpl->assign('foo', 'bar');
|
||||
$this->assertContains('bar bar', $this->smarty->fetch($tpl), $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function data provider inline
|
||||
|
@@ -37,6 +37,18 @@ class ConstantsTest extends PHPUnit_Smarty
|
||||
$this->assertEquals("hello world", $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testConstants2()
|
||||
{
|
||||
$tpl = $this->smarty->createTemplate('eval:{MYCONSTANTS}');
|
||||
$this->assertEquals("hello world", $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testConstants3()
|
||||
{
|
||||
$tpl = $this->smarty->createTemplate('eval:{$x=MYCONSTANTS}{$x}');
|
||||
$this->assertEquals("hello world", $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testConstants4()
|
||||
{
|
||||
$tpl = $this->smarty->createTemplate('eval:{TestConst::CONSTVAL}');
|
||||
@@ -48,4 +60,18 @@ class ConstantsTest extends PHPUnit_Smarty
|
||||
$tpl = $this->smarty->createTemplate('eval:{if TestConst::CONSTVAL == "okay"}yes{/if}');
|
||||
$this->assertEquals("yes", $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testConstants6()
|
||||
{
|
||||
$tpl = $this->smarty->createTemplate('eval:{$obj::CONSTVAL}');
|
||||
$tpl->assign('obj', new TestConst());
|
||||
$this->assertEquals("okay", $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testConstants7()
|
||||
{
|
||||
$tpl = $this->smarty->createTemplate('eval:{if $obj::CONSTVAL == "okay"}yes{/if}');
|
||||
$tpl->assign('obj', new TestConst());
|
||||
$this->assertEquals("yes", $this->smarty->fetch($tpl));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user