aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2015-08-13 21:39:51 +0200
committerVirtualTam <virtualtam@flibidi.net>2015-08-13 23:51:31 +0200
commitaedd62e2b84b4ea0d3c03f5c23ec594f4ebb1c17 (patch)
tree003ac85fc10d2b9b5051f299fda083a9ced69972
parent01e48f269df59e02798dad4a698c125d76b0ed70 (diff)
downloadShaarli-aedd62e2b84b4ea0d3c03f5c23ec594f4ebb1c17.tar.gz
Shaarli-aedd62e2b84b4ea0d3c03f5c23ec594f4ebb1c17.tar.zst
Shaarli-aedd62e2b84b4ea0d3c03f5c23ec594f4ebb1c17.zip
Cache: simplify cached content cleanup, improve tests
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
-rw-r--r--application/Cache.php20
-rw-r--r--tests/CacheTest.php18
2 files changed, 23 insertions, 15 deletions
diff --git a/application/Cache.php b/application/Cache.php
index 9c7e818f..5d050165 100644
--- a/application/Cache.php
+++ b/application/Cache.php
@@ -7,26 +7,18 @@
7 * Purges all cached pages 7 * Purges all cached pages
8 * 8 *
9 * @param string $pageCacheDir page cache directory 9 * @param string $pageCacheDir page cache directory
10 *
11 * @return mixed an error string if the directory is missing
10 */ 12 */
11function purgeCachedPages($pageCacheDir) 13function purgeCachedPages($pageCacheDir)
12{ 14{
13 if (! is_dir($pageCacheDir)) { 15 if (! is_dir($pageCacheDir)) {
14 return; 16 $error = 'Cannot purge '.$pageCacheDir.': no directory';
17 error_log($error);
18 return $error;
15 } 19 }
16 20
17 // TODO: check write access to the cache directory 21 array_map('unlink', glob($pageCacheDir.'/*.cache'));
18
19 $handler = opendir($pageCacheDir);
20 if ($handler == false) {
21 return;
22 }
23
24 while (($filename = readdir($handler)) !== false) {
25 if (endsWith($filename, '.cache')) {
26 unlink($pageCacheDir.'/'.$filename);
27 }
28 }
29 closedir($handler);
30} 22}
31 23
32/** 24/**
diff --git a/tests/CacheTest.php b/tests/CacheTest.php
index 4caf6552..aa5395b0 100644
--- a/tests/CacheTest.php
+++ b/tests/CacheTest.php
@@ -27,11 +27,14 @@ class CachedTest extends PHPUnit_Framework_TestCase
27 { 27 {
28 if (! is_dir(self::$testCacheDir)) { 28 if (! is_dir(self::$testCacheDir)) {
29 mkdir(self::$testCacheDir); 29 mkdir(self::$testCacheDir);
30 } else {
31 array_map('unlink', glob(self::$testCacheDir.'/*'));
30 } 32 }
31 33
32 foreach (self::$pages as $page) { 34 foreach (self::$pages as $page) {
33 file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page); 35 file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page);
34 } 36 }
37 file_put_contents(self::$testCacheDir.'/intru.der', 'ShouldNotBeThere');
35 } 38 }
36 39
37 /** 40 /**
@@ -42,7 +45,20 @@ class CachedTest extends PHPUnit_Framework_TestCase
42 purgeCachedPages(self::$testCacheDir); 45 purgeCachedPages(self::$testCacheDir);
43 foreach (self::$pages as $page) { 46 foreach (self::$pages as $page) {
44 $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache'); 47 $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache');
45 } 48 }
49
50 $this->assertFileExists(self::$testCacheDir.'/intru.der');
51 }
52
53 /**
54 * Purge cached pages - missing directory
55 */
56 public function testPurgeCachedPagesMissingDir()
57 {
58 $this->assertEquals(
59 'Cannot purge tests/dummycache_missing: no directory',
60 purgeCachedPages(self::$testCacheDir.'_missing')
61 );
46 } 62 }
47 63
48 /** 64 /**