Files
smarty/tests/UnitTests/CacheResourceTests/File/CacheResourceFileTest.php
Simon Wisselink 39b69f0142 Feature/php8 support (#629)
Adds support for PHP8.0, dropping support for PHP7.0 and below.

Backwards incompatible changes:
- Dropped support for php asp tags in templates (removed from php since php7.0)
- Dropped deprecated API calls that where only accessible through SmartyBC
- Dropped support for {php} and {include_php} tags and embedded PHP in templates. Embedded PHP will now be passed through as is.
- Removed all PHP_VERSION_ID and compare_version checks and conditional code blocks that are now no longer required
- Dropped deprecated SMARTY_RESOURCE_CHAR_SET and SMARTY_RESOURCE_DATE_FORMAT constants
- Dropped deprecated Smarty::muteExpectedErrors and Smarty::unmuteExpectedErrors API methods
- Dropped deprecated $smarty->getVariable() method. Use $smarty->getTemplateVars() instead.
- $smarty->registerResource() no longer accepts an array of callback functions

See the changelog for more details.

Switched CI from Travis to Github CI.
2021-10-13 12:15:17 +02:00

384 lines
16 KiB
PHP

<?php
/**
* Smarty PHPunit tests for cache resource file
*
* @package PHPunit
* @author Uwe Tews
*/
include_once dirname(__FILE__) . '/../_shared/CacheResourceTestCommon.php';
/**
* class for cache resource file tests
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class CacheResourceFileTest extends CacheResourceTestCommon
{
public function setUp(): void
{
$this->setUpSmarty(dirname(__FILE__));
parent::setUp();
$this->smarty->setCachingType('filetest');
}
public function testInit()
{
$this->cleanDirs();
}
/**
* test getCachedFilepath with configuration['useSubDirs'] enabled
*/
public function testGetCachedFilepathSubDirs()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(true);
$tpl = $this->smarty->createTemplate('helloworld.tpl');
$this->assertEquals($this->buildCachedPath($tpl, true, null, null, 'helloworld.tpl', $type = 'file', $this->smarty->getTemplateDir(0), 'file')
, $tpl->cached->filepath);
}
/**
* test getCachedFilepath with cache_id
*/
public function testGetCachedFilepathCacheId()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(true);
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar');
$this->assertEquals($this->buildCachedPath($tpl, true, 'foo|bar', null, 'helloworld.tpl', $type = 'file', $this->smarty->getTemplateDir(0), 'file')
, $tpl->cached->filepath);
}
/**
* test getCachedFilepath with compile_id
*/
public function testGetCachedFilepathCompileId()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(true);
$tpl = $this->smarty->createTemplate('helloworld.tpl', null, 'blar');
$this->assertEquals($this->buildCachedPath($tpl, true, null, 'blar', 'helloworld.tpl', $type = 'file', $this->smarty->getTemplateDir(0), 'file')
, $tpl->cached->filepath);
}
/**
* test getCachedFilepath with cache_id and compile_id
*/
public function testGetCachedFilepathCacheIdCompileId()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(true);
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
$this->assertEquals($this->buildCachedPath($tpl, true, 'foo|bar', 'blar', 'helloworld.tpl', $type = 'file', $this->smarty->getTemplateDir(0), 'file')
, $tpl->cached->filepath);
}
/**
* test cache->clear_all with cache_id and compile_id
*/
public function testClearCacheAllCacheIdCompileId()
{
$this->cleanCacheDir();
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(true);
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertEquals(1, $this->smarty->clearAllCache());
}
/**
* test cache->clear with cache_id and compile_id
*/
public function testClearCacheCacheIdCompileId()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->cleanCacheDir();
$this->smarty->setUseSubDirs(false);
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl);
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar2', 'blar');
$this->writeCachedContent($tpl2);
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl3);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
$this->assertEquals(2, $this->smarty->clearCache(null, 'foo|bar'));
$this->assertFalse(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertFalse(file_exists($tpl3->cached->filepath));
}
public function testClearCacheCacheIdCompileId2()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(false);
$this->cleanCacheDir();
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl);
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar2', 'blar');
$this->writeCachedContent($tpl2);
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl3);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
$this->assertEquals(2, $this->smarty->clearCache('helloworld.tpl'));
$this->assertFalse(file_exists($tpl->cached->filepath));
$this->assertFalse(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
}
public function testClearCacheCacheIdCompileId2Sub()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(true);
$this->cleanCacheDir();
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl);
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar2', 'blar');
$this->writeCachedContent($tpl2);
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl3);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
$this->assertEquals(2, $this->smarty->clearCache('helloworld.tpl'));
$this->assertFalse(file_exists($tpl->cached->filepath));
$this->assertFalse(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
}
public function testClearCacheCacheIdCompileId3()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->cleanCacheDir();
$this->smarty->setUseSubDirs(false);
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl);
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
$this->writeCachedContent($tpl2);
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl3);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
$this->assertEquals(1, $this->smarty->clearCache('helloworld.tpl', null, 'blar2'));
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertFalse(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
}
public function testClearCacheCacheIdCompileId3Sub()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->cleanCacheDir();
$this->smarty->setUseSubDirs(true);
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl);
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
$this->writeCachedContent($tpl2);
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl3);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
$this->assertEquals(1, $this->smarty->clearCache('helloworld.tpl', null, 'blar2'));
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertFalse(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
}
public function testClearCacheCacheIdCompileId4()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(false);
$this->cleanCacheDir();
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl);
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
$this->writeCachedContent($tpl2);
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl3);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
$this->assertEquals(1, $this->smarty->clearCache('helloworld.tpl', null, 'blar2'));
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertFalse(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
}
public function testClearCacheCacheIdCompileId4Sub()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(true);
$this->cleanCacheDir();
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl);
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
$this->writeCachedContent($tpl2);
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl3);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
$this->assertEquals(1, $this->smarty->clearCache('helloworld.tpl', null, 'blar2'));
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertFalse(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
}
public function testClearCacheCacheIdCompileId5()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(false);
$this->cleanCacheDir();
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl);
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
$this->writeCachedContent($tpl2);
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl3);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
$this->assertEquals(2, $this->smarty->clearCache(null, null, 'blar'));
$this->assertFalse(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertFalse(file_exists($tpl3->cached->filepath));
}
public function testClearCacheCacheIdCompileId5Sub()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(true);
$this->cleanCacheDir();
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl);
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
$this->writeCachedContent($tpl2);
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
$this->writeCachedContent($tpl3);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
$this->assertEquals(2, $this->smarty->clearCache(null, null, 'blar'));
$this->assertFalse(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertFalse(file_exists($tpl3->cached->filepath));
}
public function testClearCacheCacheFile()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(false);
$this->cleanCacheDir();
$tpl = $this->smarty->createTemplate('helloworld.tpl');
$this->writeCachedContent($tpl);
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', null, 'bar');
$this->writeCachedContent($tpl2);
$tpl3 = $this->smarty->createTemplate('helloworld.tpl', 'buh|blar');
$this->writeCachedContent($tpl3);
$tpl4 = $this->smarty->createTemplate('helloworld2.tpl');
$this->writeCachedContent($tpl4);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
$this->assertTrue(file_exists($tpl4->cached->filepath));
$this->assertEquals(3, $this->smarty->clearCache('helloworld.tpl'));
$this->assertFalse(file_exists($tpl->cached->filepath));
$this->assertFalse(file_exists($tpl2->cached->filepath));
$this->assertFalse(file_exists($tpl3->cached->filepath));
$this->assertTrue(file_exists($tpl4->cached->filepath));
}
public function testClearCacheCacheFileSub()
{
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setUseSubDirs(true);
$this->cleanCacheDir();
$tpl = $this->smarty->createTemplate('helloworld.tpl');
$this->writeCachedContent($tpl);
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', null, 'bar');
$this->writeCachedContent($tpl2);
$tpl3 = $this->smarty->createTemplate('helloworld.tpl', 'buh|blar');
$this->writeCachedContent($tpl3);
$tpl4 = $this->smarty->createTemplate('helloworld2.tpl');
$this->writeCachedContent($tpl4);
$this->assertTrue(file_exists($tpl->cached->filepath));
$this->assertTrue(file_exists($tpl2->cached->filepath));
$this->assertTrue(file_exists($tpl3->cached->filepath));
$this->assertTrue(file_exists($tpl4->cached->filepath));
$this->assertEquals(3, $this->smarty->clearCache('helloworld.tpl'));
$this->assertFalse(file_exists($tpl->cached->filepath));
$this->assertFalse(file_exists($tpl2->cached->filepath));
$this->assertFalse(file_exists($tpl3->cached->filepath));
$this->assertTrue(file_exists($tpl4->cached->filepath));
}
/**
* test cache->clear_all method
*/
public function testClearCacheAll()
{
$this->cleanCacheDir();
file_put_contents($this->smarty->getCacheDir() . 'dummy.php', 'test');
$this->assertEquals(1, $this->smarty->clearAllCache());
}
/**
* test cache->clear_all method not expired
*/
public function testClearCacheAllNotExpired()
{
$this->cleanCacheDir();
file_put_contents($this->smarty->getCacheDir() . 'dummy.php', 'test');
touch($this->smarty->getCacheDir() . 'dummy.php', time() - 1000);
clearstatcache();
$this->assertEquals(0, $this->smarty->clearAllCache(2000));
}
/**
* test cache->clear_all method expired
*/
public function testClearCacheAllExpired()
{
$this->cleanCacheDir();
file_put_contents($this->smarty->getCacheDir() . 'dummy.php', 'test');
touch($this->smarty->getCacheDir() . 'dummy.php', time() - 1000);
clearstatcache();
$this->assertEquals(1, $this->smarty->clearAllCache(500));
}
private function writeCachedContent($tpl)
{
$tpl->writeCachedContent("echo 'hello world';\n");
}
}