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

TestingEnvironmentVariables.php « Framework « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 596daa82f2ad92d72966f6d3174d0da85b8f31b3 (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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Framework;

use Piwik\Plugin\Manager as PluginManager;

/**
 * Sets the test environment.
 *
 * For testing purposes, we don't want this class to reference PIWIK_INCLUDE_PATH or other constants.
 */
class TestingEnvironmentVariables
{
    private $behaviorOverrideProperties = array();

    public function __construct()
    {
        $this->reload();
    }

    public function __get($key)
    {
        return isset($this->behaviorOverrideProperties[$key]) ? $this->behaviorOverrideProperties[$key] : null;
    }

    public function __set($key, $value)
    {
        $this->behaviorOverrideProperties[$key] = $value;
    }

    public function __isset($name)
    {
        return isset($this->behaviorOverrideProperties[$name]);
    }

    /**
     * Overrides a config entry.
     *
     * You can use this method either to set one specific config value `overrideConfig(group, name, value)`
     * or you can set a whole group of values `overrideConfig(group, valueObject)`.
     *
     * @param string $group  Eg 'General', 'log', or any other config group name
     * @param string|array $name  The name of the config within the group you want to overwrite. If you want to overwrite
     *                            the whole group just leave `$value` empty and instead provide an array of key/value pairs
     *                            here.
     * @param string|int|array|null $value  The value you want to set for the given config.
     * @throws \Exception if no name is set
     */
    public function overrideConfig($group, $name, $value = null)
    {
        if (empty($name) && !is_array($name)) {
            throw new \Exception('No name set that needs to be overwritten');
        }

        $config = $this->configOverride;

        if (empty($config)) {
            $config = array();
        }

        if (!isset($value) && is_array($name)) {
            $config[$group] = $name;
            $this->configOverride = $config;
            return;
        }

        if (!isset($config[$group])) {
            $config[$group] = array();
        }

        $config[$group][$name] = $value;
        $this->configOverride = $config;
    }

    public function save()
    {
        $includePath = __DIR__ . '/../../..';

        if(!file_exists($includePath . '/tmp')){
            mkdir($includePath . '/tmp');
        }

        $overridePath = $includePath . '/tmp/testingPathOverride.json';
        file_put_contents($overridePath, json_encode($this->behaviorOverrideProperties));
    }

    public function delete()
    {
        $this->behaviorOverrideProperties = array();
        $this->save();
    }

    public function getCoreAndSupportedPlugins()
    {
        $settings = new \Piwik\Application\Kernel\GlobalSettingsProvider();
        $pluginList = new \Piwik\Application\Kernel\PluginList($settings);
        $pluginManager = new PluginManager($pluginList);

        $disabledPlugins = $pluginList->getCorePluginsDisabledByDefault();
        $disabledPlugins[] = 'LoginHttpAuth';
        $disabledPlugins[] = 'LoginLdap';
        $disabledPlugins[] = 'ExampleVisualization';

        $disabledPlugins = array_diff($disabledPlugins, array(
            'DBStats', 'ExampleUI', 'ExampleCommand', 'ExampleSettingsPlugin'
        ));

        $plugins = array_filter($pluginManager->readPluginsDirectory(), function ($pluginName) use ($disabledPlugins, $pluginManager) {
            if (in_array($pluginName, $disabledPlugins)) {
                return false;
            }

            return $pluginManager->isPluginBundledWithCore($pluginName)
            || $pluginManager->isPluginOfficialAndNotBundledWithCore($pluginName);
        });

        sort($plugins);

        return $plugins;
    }

    public function reload()
    {
        $overridePath = __DIR__ . '/../../../tmp/testingPathOverride.json';
        if (file_exists($overridePath)) {
            $this->behaviorOverrideProperties = json_decode(file_get_contents($overridePath), true);
        }
    }
}