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

SystemSettingTest.php « Plugin « Settings « Integration « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f0d55e5fbab8697e3bbb2a85d256fb3ae8f7d0c8 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
 * Piwik - 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\Integration\Settings\Plugin;

use Piwik\Config;
use Piwik\Db;
use Piwik\Settings\FieldConfig;
use Piwik\Settings\Plugin\SystemSetting;
use Piwik\Tests\Integration\Settings\IntegrationTestCase;

/**
 * @group PluginSettings
 * @group Settings
 * @group SystemSetting
 */
class SystemSettingTest extends IntegrationTestCase
{

    public function tearDown(): void
    {
        Config::getInstance()->MyPluginName = array();
        parent::tearDown();
    }

    public function test_constructor_shouldNotEstablishADatabaseConnection()
    {
        $this->assertNotDbConnectionCreated();

        $this->buildSetting('name', FieldConfig::TYPE_INT, 'MyPlugin');

        $this->assertNotDbConnectionCreated();
    }

    public function test_setSettingValue_shouldThrowException_IfAUserIsTryingToSetASettingWhichNeedsSuperUserPermission()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('CoreAdminHome_PluginSettingChangeNotAllowed');

        $this->setUser();
        $setting = $this->buildSetting('mysystem');

        $setting->setValue(2);
    }

    public function test_setSettingValue_shouldThrowException_IfAnonymousIsTryingToSetASettingWhichNeedsSuperUserPermission()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('CoreAdminHome_PluginSettingChangeNotAllowed');

        $this->setAnonymousUser();
        $setting = $this->buildSetting('mysystem');

        $setting->setValue(2);
    }

    public function test_setSettingValue_shouldSucceed_IfSuperUserTriesToSaveASettingWhichRequiresSuperUserPermission()
    {
        $this->setSuperUser();

        $setting = $this->buildSetting('mysystem');
        $setting->setValue(2);

        $this->assertSettingHasValue($setting, 2);
    }

    public function test_getSettingValue_shouldBeReadableBySuperUser()
    {
        $this->setSuperUser();
        $setting = $this->buildSetting('myusersetting');
        $this->assertEquals('', $setting->getValue());
    }

    public function test_getSettingValue_shouldReturnValue_IfReadbleByCurrentUserIsAllowed()
    {
        $this->setUser();
        $setting = $this->buildSetting('myusersetting');

        $this->assertEquals('', $setting->getValue());
    }

    public function test_getSettingValue_fromConfig_IfOneIsConfiguredInsteadOfTheValueFromDatabase()
    {
        $this->setSuperUser();
        $setting = $this->buildSetting('myusersetting');
        $setting->setValue('test');
        $this->assertEquals('test', $setting->getValue());

        Config::getInstance()->MyPluginName = array('myusersetting' => 'mynewvalue');
        $value = $setting->getValue();
        $this->assertEquals('mynewvalue', $value);
    }

    public function test_getSettingValue_fromConfig_ShouldConvertToTheSpecifiedType()
    {
        $this->setSuperUser();
        $setting = $this->buildSetting('myusersetting', FieldConfig::TYPE_BOOL);

        Config::getInstance()->MyPluginName = array('myusersetting' => '1');

        $this->assertTrue($setting->getValue());
    }

    public function test_getSettingValue_fromConfig_isCaseSensitive()
    {
        $this->setSuperUser();
        $setting = $this->buildSetting('myUsersetting');

        Config::getInstance()->MyPluginName = array('myusersetting' => '1');

        $this->assertSame('', $setting->getValue());

        Config::getInstance()->MyPluginName = array('myUsersetting' => '1');

        $this->assertSame('1', $setting->getValue());
    }

    public function test_getSettingsValue_fromConfig_ShouldSetObjectToNotWritableAsSoonAsAValueIsConfigured()
    {
        $this->setSuperUser();
        $setting = $this->buildSetting('myusersetting');

        $this->assertTrue($setting->isWritableByCurrentUser());

        Config::getInstance()->MyPluginName = array('myusersetting' => '0');
        $this->assertFalse($setting->isWritableByCurrentUser());
    }

    public function test_setIsWritableByCurrentUser()
    {
        $this->setSuperUser();
        $setting = $this->buildSetting('myusersetting');

        $this->assertTrue($setting->isWritableByCurrentUser());

        $setting->setIsWritableByCurrentUser(false);
        $this->assertFalse($setting->isWritableByCurrentUser());

        $setting->setIsWritableByCurrentUser(true);
        $this->assertTrue($setting->isWritableByCurrentUser());
    }

    public function test_setSettingsValue_shouldNotBePossible_AsSoonAsAConfigValueIsConfigured()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('CoreAdminHome_PluginSettingChangeNotAllowed');

        $this->setSuperUser();
        $setting = $this->buildSetting('myusersetting');

        Config::getInstance()->MyPluginName = array('myusersetting' => '0');
        $setting->setValue('test');
    }

    public function test_save_shouldSaveDifferentValuesForDifferentPluginsAndFields()
    {
        $plugin1 = $this->buildSetting('field1', null, $login = 'plugin1');
        $plugin1->setValue('value1');
        $plugin1->save();

        $plugin2 = $this->buildSetting('field1', null, $login = 'plugin2');
        $this->assertSame('value1', $plugin1->getValue());
        $this->assertSame('', $plugin2->getValue());
        $plugin2->setValue('value2');
        $plugin2->save();

        $plugin3 = $this->buildSetting('field1', null, $login = 'plugin3');
        $this->assertSame('value1', $plugin1->getValue());
        $this->assertSame('value2', $plugin2->getValue());
        $this->assertSame('', $plugin3->getValue());

        $plugin1Field2 = $this->buildSetting('field2', null, $login = 'plugin1');
        $this->assertSame('', $plugin1Field2->getValue());
        $plugin1Field2->setValue('value1Field2');
        $plugin1Field2->save();

        $this->assertSame('value1', $plugin1->getValue());
        $this->assertSame('value1Field2', $plugin1Field2->getValue());
    }

    private function buildSetting($name, $type = null, $plugin = null)
    {
        if (!isset($type)) {
            $type = FieldConfig::TYPE_STRING;
        }
        if (!isset($plugin)) {
            $plugin = 'MyPluginName';
        }

        $systemSetting = new SystemSetting($name, $default = '', $type, $plugin);

        return $systemSetting;
    }

}