1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-06 06:38:00 +02:00

Cache handler test added. Cache handler getLastError() method added.

This commit is contained in:
Cameron
2020-12-16 09:45:11 -08:00
parent 8e5bc7d829
commit 1c0cfbe0ff
6 changed files with 235 additions and 2 deletions

View File

@@ -31,6 +31,7 @@ class ecache {
public $CachenqMD5;
public $UserCacheActive; // Checkable flag - TRUE if user cache enabled
public $SystemCacheActive; // Checkable flag - TRUE if system cache enabled
private $lastError;
const CACHE_PREFIX = '<?php exit;';
@@ -146,6 +147,12 @@ class ecache {
else
{
$ret = file_get_contents($cache_file);
if($ret === false)
{
$this->lastError = "Couldn't read ".$cache_file;
}
if (substr($ret,0,strlen(self::CACHE_PREFIX)) == self::CACHE_PREFIX)
{
$ret = substr($ret, strlen(self::CACHE_PREFIX));
@@ -154,18 +161,28 @@ class ecache {
{
$ret = substr($ret, 5); // Handle the history for now
}
return $ret;
}
}
else
{
// e107::getDebug()->log("Couldn't find cache file: ".json_encode($cache_file));
$this->lastError = "Cache file not found: ".$cache_file;
return false;
}
}
return false;
}
/**
* Return the last error encountered during cache processing.
* @return mixed
*/
public function getLastError()
{
return $this->lastError;
}
/**
* @return string
* @param string $CacheTag
@@ -225,7 +242,7 @@ class ecache {
{
if(isset($this) && $this instanceof ecache)
{
return $this->set($CacheTag, $Data, $ForceCache, $bRaw, true);
$this->set($CacheTag, $Data, $ForceCache, $bRaw, true);
}
else
{

View File

@@ -0,0 +1 @@
<?php exit;<!-- tablestyle: style=default id=wm --><div>Get Started</div>

View File

@@ -0,0 +1,5 @@
<?php exit;array (
'most_members_online' => 4,
'most_guests_online' => 4,
'most_online_datestamp' => 1534279911,
)

View File

@@ -0,0 +1,3 @@
{
"status": "not needed"
}

View File

@@ -0,0 +1,18 @@
array (
'_FIELD_TYPES' =>
array (
'online_timestamp' => 'int',
'online_flag' => 'int',
'online_user_id' => 'escape',
'online_ip' => 'escape',
'online_location' => 'escape',
'online_pagecount' => 'int',
'online_active' => 'int',
'online_agent' => 'escape',
'online_language' => 'escape',
),
'_NOTNULL' =>
array (
'online_location' => '',
),
)

View File

@@ -0,0 +1,189 @@
<?php
class ecacheTest extends \Codeception\Test\Unit
{
/** @var ecache */
protected $cache;
protected function _before()
{
try
{
$this->cache = $this->make('ecache');
}
catch(Exception $e)
{
$this->assertTrue(false, $e->getMessage());
}
$file = codecept_data_dir('ecache/content/S_Config_test.cache.php_');
$dest = e_CACHE_CONTENT."S_Config_test.cache.php";
if(!file_exists($dest) && !copy($file,$dest))
{
$this->assertTrue(false, "Couldn't copy cache file from ".$file);
}
$file = codecept_data_dir('ecache/content/S_Update_core.cache.php_');
$dest = e_CACHE_CONTENT."S_Update_core.cache.php";
if(!file_exists($dest) && !copy($file, $dest))
{
$this->assertTrue(false, "Couldn't copy cache file from ".$file);
}
$file = codecept_data_dir('ecache/db/online.php_');
$dest = e_CACHE_DB."online.php";
if(!file_exists($dest) && !copy($file, $dest))
{
$this->assertTrue(false, "Couldn't copy cache file from ".$file);
}
$file = codecept_data_dir('ecache/content/C_wmessage_0800fc577294c34e0b28ad2839435945.cache.php_');
$dest = e_CACHE_CONTENT."C_wmessage_0800fc577294c34e0b28ad2839435945.cache.php";
if(!file_exists($dest) && !copy($file, $dest))
{
$this->assertTrue(false, "Couldn't copy cache file from ".$file);
}
}
public function testRetrieve()
{
$tests = array(
0 => array(
'name' => 'Config_test',
'system' => true,
'expected' => "array (
'most_members_online' => 4,
'most_guests_online' => 4,
'most_online_datestamp' => 1534279911,
)",
),
1 => array(
'name' => 'Update_core',
'system' => true,
'expected' => '{
"status": "not needed"
}',
),
2 => array(
'name' => 'wmessage',
'system' => false,
'expected' => '<!--tablestyle:style=defaultid=wm--><div>GetStarted</div>',
),
);
$this->cache->setMD5('hash'); // set a consistent hash value: ie. 0800fc577294c34e0b28ad2839435945
$clean = ["\t", "\n", " "];
foreach($tests as $var)
{
$result = $this->cache->retrieve($var['name'], false, true, $var['system']);
$result = str_replace($clean, '', $result);
$expected = str_replace($clean, '', $var['expected']);
$this->assertSame($expected, $result);
}
$errorStatus = $this->cache->getLastError();
$this->assertEmpty($errorStatus, "An error occurred during cache: ".$errorStatus);
}
/*
public function testClear_sys()
{
}
public function testSet_sys()
{
}
public function testClear()
{
}
public function testDelete()
{
}
public function test__construct()
{
}
public function testClearAll()
{
}
public function testGetMD5()
{
}
public function testSetMD5()
{
}
public function testRetrieve_sys()
{
}*/
public function testSetAndRetrieve()
{
$tests = array(
0 => array(
'data' => 'This is my cached data',
'braw' => false,
'system' => true
),
1 => array(
'data' => 'This is my cached data 1',
'braw' => false,
'system' => false
),
2 => array(
'data' => 'This is my cached data 2',
'braw' => true,
'system' => false
),
3 => array(
'data' => 'This is my cached data 3',
'braw' => true,
'system' => true
),
);
foreach($tests as $index => $var)
{
$tag = "custom_".$index;
$this->cache->set($tag, $var['data'], true, $var['braw'], $var['system']);
$result = $this->cache->retrieve($tag, false, true, $var['system']);
$this->assertSame($var['data'],$result);
}
}
/*
public function testCache_fname()
{
}
*/
}