2015-07-09 22:14:39 +02:00
|
|
|
<?php
|
2020-07-07 10:15:56 +02:00
|
|
|
|
2015-07-09 22:14:39 +02:00
|
|
|
/**
|
|
|
|
* Cache tests
|
|
|
|
*/
|
2020-07-07 10:15:56 +02:00
|
|
|
|
2020-01-23 21:13:41 +01:00
|
|
|
namespace Shaarli\Render;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shaarli\Security\SessionManager;
|
2015-07-09 22:14:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unitary tests for cached pages
|
|
|
|
*/
|
2020-01-23 21:13:41 +01:00
|
|
|
class PageCacheManagerTest extends TestCase
|
2015-07-09 22:14:39 +02:00
|
|
|
{
|
|
|
|
// test cache directory
|
2015-11-24 02:52:22 +01:00
|
|
|
protected static $testCacheDir = 'sandbox/dummycache';
|
2015-07-09 22:14:39 +02:00
|
|
|
|
|
|
|
// dummy cached file names / content
|
|
|
|
protected static $pages = array('a', 'toto', 'd7b59c');
|
|
|
|
|
2020-01-23 21:13:41 +01:00
|
|
|
/** @var PageCacheManager */
|
|
|
|
protected $cacheManager;
|
|
|
|
|
|
|
|
/** @var SessionManager */
|
|
|
|
protected $sessionManager;
|
2015-07-09 22:14:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate the cache with dummy files
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
2020-05-17 14:16:32 +02:00
|
|
|
$this->cacheManager = new PageCacheManager(static::$testCacheDir, true);
|
2020-01-23 21:13:41 +01:00
|
|
|
|
2018-12-03 00:08:04 +01:00
|
|
|
if (!is_dir(self::$testCacheDir)) {
|
2015-07-09 22:14:39 +02:00
|
|
|
mkdir(self::$testCacheDir);
|
2015-08-13 21:39:51 +02:00
|
|
|
} else {
|
2018-12-03 00:08:04 +01:00
|
|
|
array_map('unlink', glob(self::$testCacheDir . '/*'));
|
2015-07-09 22:14:39 +02:00
|
|
|
}
|
2015-12-03 19:27:34 +01:00
|
|
|
|
2015-07-09 22:14:39 +02:00
|
|
|
foreach (self::$pages as $page) {
|
2018-12-03 00:08:04 +01:00
|
|
|
file_put_contents(self::$testCacheDir . '/' . $page . '.cache', $page);
|
2015-07-09 22:14:39 +02:00
|
|
|
}
|
2018-12-03 00:08:04 +01:00
|
|
|
file_put_contents(self::$testCacheDir . '/intru.der', 'ShouldNotBeThere');
|
2015-07-09 22:14:39 +02:00
|
|
|
}
|
|
|
|
|
2015-12-03 19:27:34 +01:00
|
|
|
/**
|
|
|
|
* Remove dummycache folder after each tests.
|
|
|
|
*/
|
|
|
|
public function tearDown()
|
|
|
|
{
|
2018-12-03 00:08:04 +01:00
|
|
|
array_map('unlink', glob(self::$testCacheDir . '/*'));
|
2015-12-03 19:27:34 +01:00
|
|
|
rmdir(self::$testCacheDir);
|
|
|
|
}
|
|
|
|
|
2015-07-09 22:14:39 +02:00
|
|
|
/**
|
|
|
|
* Purge cached pages
|
|
|
|
*/
|
|
|
|
public function testPurgeCachedPages()
|
|
|
|
{
|
2020-01-23 21:13:41 +01:00
|
|
|
$this->cacheManager->purgeCachedPages();
|
2015-07-09 22:14:39 +02:00
|
|
|
foreach (self::$pages as $page) {
|
2018-12-03 00:08:04 +01:00
|
|
|
$this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
|
2015-08-13 21:39:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:08:04 +01:00
|
|
|
$this->assertFileExists(self::$testCacheDir . '/intru.der');
|
2015-08-13 21:39:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purge cached pages - missing directory
|
|
|
|
*/
|
|
|
|
public function testPurgeCachedPagesMissingDir()
|
|
|
|
{
|
2020-05-17 14:16:32 +02:00
|
|
|
$this->cacheManager = new PageCacheManager(self::$testCacheDir . '_missing', true);
|
2020-01-23 21:13:41 +01:00
|
|
|
|
2016-07-23 14:16:07 +02:00
|
|
|
$oldlog = ini_get('error_log');
|
|
|
|
ini_set('error_log', '/dev/null');
|
2015-08-13 21:39:51 +02:00
|
|
|
$this->assertEquals(
|
2015-11-24 02:52:22 +01:00
|
|
|
'Cannot purge sandbox/dummycache_missing: no directory',
|
2020-01-23 21:13:41 +01:00
|
|
|
$this->cacheManager->purgeCachedPages()
|
2015-08-13 21:39:51 +02:00
|
|
|
);
|
2016-07-23 14:16:07 +02:00
|
|
|
ini_set('error_log', $oldlog);
|
2015-07-09 22:14:39 +02:00
|
|
|
}
|
|
|
|
}
|