Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IniFileChainCacheTest.php « Config « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9aecf438a59f65c640f78f1c93b18a4bb4f27506 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Unit\Config;

use Piwik\Config;
use Piwik\Config\Cache;
use Piwik\Config\IniFileChain;

class TestIniFileChain extends IniFileChain
{
    public $addHostInfo = '';

    public function __construct(array $defaultSettingsFiles = [], $userSettingsFile = null, $addhostInfo = '')
    {
        $this->addHostInfo = $addhostInfo;
        parent::__construct($defaultSettingsFiles, $userSettingsFile);
    }

    protected function mergeFileSettings()
    {
        $settings = parent::mergeFileSettings();

        if (!empty($this->addHostInfo)) {
            $settings['General'] = ['trusted_hosts' => [$this->addHostInfo]];
        }

        return $settings;
    }
}

/**
 * @group Core
 */
class IniFileChainCacheTest extends IniFileChainTest
{
    /**
     * @var Cache
     */
    private $cache;

    private $testHost = 'mytest.matomo.org';

    public function setUp(): void
    {
        $GLOBALS['ENABLE_CONFIG_PHP_CACHE'] = true;
        $_SERVER['HTTP_HOST'] = $this->testHost;
        $this->cache = new Cache();
        parent::setUp();
        $this->setTrustedHosts();
    }

    private function setTrustedHosts()
    {
        Config::setSetting('General', 'trusted_hosts', [$this->testHost, 'foonot.exists']);
    }

    public function tearDown(): void
    {
        $this->cache->doDelete(IniFileChain::CONFIG_CACHE_KEY);
        unset($GLOBALS['ENABLE_CONFIG_PHP_CACHE']);
        unset($_SERVER['HTTP_HOST']);
        parent::tearDown();
    }

    /**
     * @dataProvider getMergingTestData
     */
    public function testReloadShouldNotPopulateCacheWhenNoTrustedHostIsConfigured($testDescription, $defaultSettingFiles, $userSettingsFile, $expected)
    {
        $value = $this->cache->doFetch(IniFileChain::CONFIG_CACHE_KEY);
        $this->assertEquals(false, $value);

        // reading the chain should populate the cache
        $fileChain = new TestIniFileChain($defaultSettingFiles, $userSettingsFile);
        $this->assertEquals($expected, $fileChain->getAll(), "'$testDescription' failed");

        $value = $this->cache->doFetch(IniFileChain::CONFIG_CACHE_KEY);
        $this->assertEquals(false, $value);
    }

    /**
     * @dataProvider getMergingTestData
     */
    public function testReloadShouldNotPopulateCacheWhenTrustedHostIsNotValid($testDescription, $defaultSettingFiles, $userSettingsFile, $expected)
    {
        $value = $this->cache->doFetch(IniFileChain::CONFIG_CACHE_KEY);
        $this->assertEquals(false, $value);

        // reading the chain should populate the cache
        $fileChain = new TestIniFileChain($defaultSettingFiles, $userSettingsFile, 'foo.bar.com');
        $expected['General'] = ['trusted_hosts' => ['foo.bar.com']];
        $this->assertEquals($expected, $fileChain->getAll(), "'$testDescription' failed");

        $value = $this->cache->doFetch(IniFileChain::CONFIG_CACHE_KEY);
        $this->assertEquals(false, $value);
    }

    /**
     * @dataProvider getMergingTestData
     */
    public function testReloadShoulPopulateCacheWhenTrustedHostIsValid($testDescription, $defaultSettingFiles, $userSettingsFile, $expected)
    {
        $value = $this->cache->doFetch(IniFileChain::CONFIG_CACHE_KEY);
        $this->assertEquals(false, $value);

        // reading the chain should populate the cache
        $fileChain = new TestIniFileChain($defaultSettingFiles, $userSettingsFile, $this->testHost);
        $expected['General'] = ['trusted_hosts' => [$this->testHost]];
        $this->assertEquals($expected, $fileChain->getAll(), "'$testDescription' failed");

        $value = $this->cache->doFetch(IniFileChain::CONFIG_CACHE_KEY);
        $settingsChain = $value['settingsChain'];
        unset($value['settingsChain']);
        $this->assertEquals(['mergedSettings' => $expected], $value);

        foreach ($defaultSettingFiles as $defaultSettingFile) {
            self::assertTrue(array_key_exists($defaultSettingFile, $settingsChain));
        }

        $this->assertNotEmpty(array_keys($settingsChain));
    }

    /**
     * @dataProvider getMergingTestData
     */
    public function testReloadCanReadFromCache($testDescription, $defaultSettingFiles, $userSettingsFile, $expected)
    {
        $value = $this->cache->doFetch(IniFileChain::CONFIG_CACHE_KEY);
        $this->assertEquals(false, $value);

        $userSettingsFileCopy = dirname($userSettingsFile) . '/copy.' . basename($userSettingsFile);
        copy($userSettingsFile, $userSettingsFileCopy);

        // reading the chain should populate the cache
        $fileChain = new TestIniFileChain($defaultSettingFiles, $userSettingsFileCopy, $this->testHost);
        $expected['General'] = ['trusted_hosts' => [$this->testHost]];
        $this->assertEquals($expected, $fileChain->getAll(), "'$testDescription' failed");

        // ensure it can be read only from cache
        $testChain = new TestIniFileChain(['foo'], $userSettingsFileCopy);
        $this->assertEquals($expected, $testChain->getAll(), "'$testDescription' failed");
        unlink($userSettingsFileCopy);
    }

    /**
     * @dataProvider getMergingTestData
     */
    public function testPopulateCacheDeleteCache($testDescription, $defaultSettingFiles, $userSettingsFile, $expected)
    {
        $this->testReloadShoulPopulateCacheWhenTrustedHostIsValid($testDescription, $defaultSettingFiles, $userSettingsFile, $expected);

        $value = $this->cache->doFetch(IniFileChain::CONFIG_CACHE_KEY);
        $this->assertNotEmpty($value);

        // dumping the cache should delete it

        $fileChain = new TestIniFileChain($defaultSettingFiles, $userSettingsFile);
        $fileChain->deleteConfigCache();

        $value = $this->cache->doFetch(IniFileChain::CONFIG_CACHE_KEY);
        $this->assertEquals(false, $value);
    }
}