aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/feed/CacheTest.php
blob: c0a9f26f2dc933d7adf8644a8b580d797efe01de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
 * Cache tests
 */
namespace Shaarli\Feed;

// required to access $_SESSION array
session_start();

require_once 'application/feed/Cache.php';

/**
 * Unitary tests for cached pages
 */
class CacheTest extends \PHPUnit\Framework\TestCase
{
    // test cache directory
    protected static $testCacheDir = 'sandbox/dummycache';

    // dummy cached file names / content
    protected static $pages = array('a', 'toto', 'd7b59c');


    /**
     * Populate the cache with dummy files
     */
    public function setUp()
    {
        if (!is_dir(self::$testCacheDir)) {
            mkdir(self::$testCacheDir);
        } else {
            array_map('unlink', glob(self::$testCacheDir . '/*'));
        }

        foreach (self::$pages as $page) {
            file_put_contents(self::$testCacheDir . '/' . $page . '.cache', $page);
        }
        file_put_contents(self::$testCacheDir . '/intru.der', 'ShouldNotBeThere');
    }

    /**
     * Remove dummycache folder after each tests.
     */
    public function tearDown()
    {
        array_map('unlink', glob(self::$testCacheDir . '/*'));
        rmdir(self::$testCacheDir);
    }

    /**
     * Purge cached pages
     */
    public function testPurgeCachedPages()
    {
        purgeCachedPages(self::$testCacheDir);
        foreach (self::$pages as $page) {
            $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
        }

        $this->assertFileExists(self::$testCacheDir . '/intru.der');
    }

    /**
     * Purge cached pages - missing directory
     */
    public function testPurgeCachedPagesMissingDir()
    {
        $oldlog = ini_get('error_log');
        ini_set('error_log', '/dev/null');
        $this->assertEquals(
            'Cannot purge sandbox/dummycache_missing: no directory',
            purgeCachedPages(self::$testCacheDir . '_missing')
        );
        ini_set('error_log', $oldlog);
    }

    /**
     * Purge cached pages and session cache
     */
    public function testInvalidateCaches()
    {
        $this->assertArrayNotHasKey('tags', $_SESSION);
        $_SESSION['tags'] = array('goodbye', 'cruel', 'world');

        invalidateCaches(self::$testCacheDir);
        foreach (self::$pages as $page) {
            $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
        }

        $this->assertArrayNotHasKey('tags', $_SESSION);
    }
}