xref: /dokuwiki/_test/tests/inc/cache_use.test.php (revision 5f3da53e253bc878946671173feef853a48604b8)
116ca217dSMichael Hamann<?php
216ca217dSMichael Hamann
316ca217dSMichael Hamann/**
416ca217dSMichael Hamann * Class cache_use_test
516ca217dSMichael Hamann *
616ca217dSMichael Hamann * Tests if caching can actually be used
716ca217dSMichael Hamann */
816ca217dSMichael Hamannclass cache_use_test extends DokuWikiTest {
9*5f3da53eSChristopher Smith    /** @var cache_renderer $cache */
1016ca217dSMichael Hamann    private $cache;
1116ca217dSMichael Hamann
1216ca217dSMichael Hamann    function setUp() {
13b2356002SChristopher Smith        global $ID, $conf;
1416ca217dSMichael Hamann        parent::setUp();
1516ca217dSMichael Hamann
1616ca217dSMichael Hamann        $ID = 'cached';
1716ca217dSMichael Hamann        $file = wikiFN($ID);
18b2356002SChristopher Smith        $conf['cachetime'] = 0;  // ensure the value is not -1, which disables caching
1916ca217dSMichael Hamann
2016ca217dSMichael Hamann        saveWikiText($ID, 'Content', 'Created');
2116ca217dSMichael Hamann        // set the modification time a second in the past in order to ensure that the cache is newer than the page
2216ca217dSMichael Hamann        touch($file, time()-1);
2316ca217dSMichael Hamann
24b2356002SChristopher Smith        $this->cache = new cache_renderer($ID, $file, 'xhtml');
2516ca217dSMichael Hamann        $this->cache->storeCache('Test');
2616ca217dSMichael Hamann    }
2716ca217dSMichael Hamann
2816ca217dSMichael Hamann    function test_use() {
2916ca217dSMichael Hamann        $this->assertTrue($this->cache->useCache());
3016ca217dSMichael Hamann    }
3116ca217dSMichael Hamann
32b2356002SChristopher Smith    /**
33b2356002SChristopher Smith     * In all the following tests the cache should not be usable
34b2356002SChristopher Smith     * as such, they are meaningless if test_use didn't pass.
35b2356002SChristopher Smith     *
36b2356002SChristopher Smith     * @depends test_use
37b2356002SChristopher Smith     */
3816ca217dSMichael Hamann    function test_purge() {
39b2356002SChristopher Smith        /* @var Input $INPUT */
40b2356002SChristopher Smith        global $INPUT;
41b2356002SChristopher Smith        $INPUT->set('purge',1);
42b2356002SChristopher Smith
43b2356002SChristopher Smith        $this->assertFalse($this->cache->useCache());
44b2356002SChristopher Smith        $this->assertNotEmpty($this->cache->depends['purge']);
45b2356002SChristopher Smith    }
46b2356002SChristopher Smith
47b2356002SChristopher Smith    /**
48b2356002SChristopher Smith     * @depends test_use
49b2356002SChristopher Smith     */
50b2356002SChristopher Smith    function test_filedependency() {
51b2356002SChristopher Smith        // give the dependent src file the same mtime as the cache
52b2356002SChristopher Smith        touch($this->cache->file, filemtime($this->cache->cache));
53b2356002SChristopher Smith        $this->assertFalse($this->cache->useCache());
54b2356002SChristopher Smith    }
55b2356002SChristopher Smith
56b2356002SChristopher Smith    /**
57b2356002SChristopher Smith     * @depends test_use
58b2356002SChristopher Smith     */
59b2356002SChristopher Smith    function test_age() {
60b2356002SChristopher Smith        // need to age both our source file & the cache
61b2356002SChristopher Smith        $age = 10;
62b2356002SChristopher Smith        $time = time() - $age - 1;  // older than age
63b2356002SChristopher Smith
64b2356002SChristopher Smith        touch($this->cache->file, $time - 1);
65b2356002SChristopher Smith        touch($this->cache->cache, $time);
66b2356002SChristopher Smith
67b2356002SChristopher Smith        $this->assertFalse($this->cache->useCache(array('age' => $age)));
68b2356002SChristopher Smith    }
69b2356002SChristopher Smith
70b2356002SChristopher Smith    /**
71b2356002SChristopher Smith     * @depends test_use
72b2356002SChristopher Smith     */
73b2356002SChristopher Smith    function test_confnocaching() {
74b2356002SChristopher Smith        global $conf;
75b2356002SChristopher Smith        $conf['cachetime'] = -1;   // disables renderer caching
76b2356002SChristopher Smith
77b2356002SChristopher Smith        $this->assertFalse($this->cache->useCache());
78b2356002SChristopher Smith        $this->assertNotEmpty($this->cache->_nocache);
7916ca217dSMichael Hamann    }
8016ca217dSMichael Hamann}