Spaces:
Paused

File size: 5,358 Bytes
bf8c0ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

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

namespace Piwik\Tests\Framework\Mock;

use Piwik\Application\Kernel\GlobalSettingsProvider;
use Piwik\Config;
use Piwik\Plugin\Manager;
use Piwik\Tests\Framework\TestingEnvironmentVariables;

class TestConfig extends Config
{
    /**
     * @var bool
     */
    private $allowSave;

    /**
     * @var bool
     */
    private $doSetTestEnvironment;

    /**
     * @var TestingEnvironmentVariables
     */
    private $testingEnvironment;

    public function __construct(GlobalSettingsProvider $provider, TestingEnvironmentVariables $testingEnvironment, $allowSave = false, $doSetTestEnvironment = true)
    {
        parent::__construct($provider);

        $this->allowSave = $allowSave;
        $this->doSetTestEnvironment = $doSetTestEnvironment;
        $this->testingEnvironment = $testingEnvironment;

        $this->reload();

        $this->setFromTestEnvironment($testingEnvironment);
    }

    public function reload($pathLocal = null, $pathGlobal = null, $pathCommon = null)
    {
        parent::reload($pathLocal, $pathGlobal, $pathCommon);

        $this->setTestEnvironment();
    }

    public function forceSave()
    {
        if ($this->allowSave) {
            parent::forceSave();
        }

        if (!$this->doSetTestEnvironment) {
            return;
        }

        $environmentPlugins = $this->testingEnvironment->configOverride['PluginsInstalled']['PluginsInstalled'] ?? [];
        $installedPlugins = Manager::getInstance()->getInstalledPluginsName();

        $onlyEnvironment = array_diff($environmentPlugins, $installedPlugins);
        $onlyInstalled = array_diff($installedPlugins, $environmentPlugins);

        if ([] === $onlyEnvironment && [] === $onlyInstalled) {
            // environment matches list of installed plugins
            return;
        }

        $this->testingEnvironment->overrideConfig('PluginsInstalled', 'PluginsInstalled', $installedPlugins);
        $this->testingEnvironment->save();
    }

    public function setTestEnvironment()
    {
        if (!$this->allowSave) {
            $this->doNotWriteConfigInTests = true;
        }

        $chain = $this->settings->getIniFileChain();

        $databaseTestsSettings = $chain->get('database_tests'); // has to be __get otherwise when called from TestConfig, PHP will issue a NOTICE
        if (!empty($databaseTestsSettings)) {
            $chain->set('database', $databaseTestsSettings);
        }

        // Ensure local mods do not affect tests
        if (empty($pathGlobal)) {
            $general = $chain->getFrom($this->getLocalPath(), 'General');
            $instanceId = isset($general['instance_id']) ? $general['instance_id'] : null;

            $chain->set('Debug', $chain->getFrom($this->getGlobalPath(), 'Debug'));
            $chain->set('mail', $chain->getFrom($this->getGlobalPath(), 'mail'));

            $globalGeneral = $chain->getFrom($this->getGlobalPath(), 'General');
            if ($instanceId) {
                $globalGeneral['instance_id'] = $instanceId;
            }
            $chain->set('General', $globalGeneral);

            $chain->set('Segments', $chain->getFrom($this->getGlobalPath(), 'Segments'));
            $chain->set('Tracker', $chain->getFrom($this->getGlobalPath(), 'Tracker'));
            $chain->set('Deletelogs', $chain->getFrom($this->getGlobalPath(), 'Deletelogs'));
            $chain->set('Deletereports', $chain->getFrom($this->getGlobalPath(), 'Deletereports'));
            $chain->set('Development', $chain->getFrom($this->getGlobalPath(), 'Development'));
        }

        // for unit tests, we set that no plugin is installed. This will force
        // the test initialization to create the plugins tables, execute ALTER queries, etc.
        $chain->set('PluginsInstalled', array('PluginsInstalled' => array()));
    }

    private function setFromTestEnvironment(\Piwik\Tests\Framework\TestingEnvironmentVariables $testingEnvironment)
    {
        $chain = $this->settings->getIniFileChain();

        $general =& $chain->get('General');
        $log =& $chain->get('log');
        $database =& $chain->get('database');

        $log['log_writers'] = array('file');

        // TODO: replace this and below w/ configOverride use
        if ($testingEnvironment->tablesPrefix) {
            $database['tables_prefix'] = $testingEnvironment->tablesPrefix;
        }

        if ($testingEnvironment->dbName) {
            $database['dbname'] = $testingEnvironment->dbName;
        }

        if ($testingEnvironment->configOverride) {
            $cache =& $chain->getAll();
            $cache = $this->arrayMergeRecursiveDistinct($cache, $testingEnvironment->configOverride);
        }
    }

    private function arrayMergeRecursiveDistinct(array $array1, array $array2)
    {
        $result = $array1;

        foreach ($array2 as $key => $value) {
            if (is_array($value)) {
                $result[$key] = isset($result[$key]) && is_array($result[$key])
                    ? $this->arrayMergeRecursiveDistinct($result[$key], $value)
                    : $value;
            } else {
                $result[$key] = $value;
            }
        }

        return $result;
    }
}