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

OptionTest.php « Integration « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dfc279d7ba4ee343bbb0617b412ffe143181ed93 (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
201
202
203
204
205
206
207
208
209
210
211
<?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\Integration;

use Piwik\Common;
use Piwik\Db;
use Piwik\Option;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group Core
 */
class OptionTest extends IntegrationTestCase
{
    public function testGet()
    {
        // empty table, expect false (i.e., not found)
        $this->assertFalse(Option::get('anonymous_defaultReport'));

        // populate table, expect '1' (i.e., found)
        Db::query("INSERT INTO `" . Common::prefixTable('option') . "` VALUES ('anonymous_defaultReport', '1', false)");
        // force cache reload, so value is reloaded
        Option::clearCache();
        $this->assertSame('1', Option::get('anonymous_defaultReport'));

        // delete row (bypassing API), expect '1' (i.e., from cache)
        Db::query("DELETE FROM `" . Common::prefixTable('option') . "` WHERE option_name = ?", array('anonymous_defaultReport'));
        $this->assertSame('1', Option::get('anonymous_defaultReport'));

        // force cache reload, expect false (i.e., not found)
        Option::clearCache();
        $this->assertFalse(Option::get('anonymous_defaultReport'));
    }

    public function testGetOption()
    {
        // empty table, expect false (i.e., not found)
        $this->assertFalse(Option::get('anonymous_defaultReport'));

        // populate table, expect '1' (i.e., found)
        Db::query("INSERT INTO `" . Common::prefixTable('option') . "` VALUES ('anonymous_defaultReport', '1',true)");
        // force cache reload, so value is reloaded
        Option::clearCache();
        $this->assertSame('1', Option::get('anonymous_defaultReport'));

        // delete row (bypassing API), expect '1' (i.e., from cache)
        Db::query("DELETE FROM `" . Common::prefixTable('option') . "` WHERE option_name = ?", array('anonymous_defaultReport'));
        $this->assertSame('1', Option::get('anonymous_defaultReport'));

        // force cache reload, expect false (i.e., not found)
        Option::clearCache();
        $this->assertFalse(Option::get('anonymous_defaultReport'));
    }

    public function testSet()
    {
        // empty table, expect false (i.e., not found)
        $this->assertFalse(Option::get('anonymous_defaultReport'));

        // populate table, expect '1'
        Option::set('anonymous_defaultReport', '1', true);
        $this->assertSame('1', Option::get('anonymous_defaultReport'));
    }

    public function testSetOption()
    {
        // empty table, expect false (i.e., not found)
        $this->assertFalse(Option::get('anonymous_defaultReport'));

        // populate table, expect '1'
        Option::set('anonymous_defaultReport', '1', false);
        $this->assertSame('1', Option::get('anonymous_defaultReport'));
    }

    public function testDelete()
    {
        // empty table, expect false (i.e., not found)
        $this->assertFalse(Option::get('anonymous_defaultReport'));
        $this->assertFalse(Option::get('admin_defaultReport'));

        // populate table, expect '1'
        Option::set('anonymous_defaultReport', '1', true);
        Option::delete('_defaultReport');
        $this->assertSame('1', Option::get('anonymous_defaultReport'));

        // populate table, expect '2'
        Option::set('admin_defaultReport', '2', false);
        Option::delete('_defaultReport');
        $this->assertSame('2', Option::get('admin_defaultReport'));

        // delete with non-matching value, expect '1'
        Option::delete('anonymous_defaultReport', '2');
        $this->assertSame('1', Option::get('anonymous_defaultReport'));

        // delete with matching value, expect false
        Option::delete('anonymous_defaultReport', '1');
        $this->assertFalse(Option::get('anonymous_defaultReport'));

        // this shouldn't have been deleted, expect '2'
        $this->assertSame('2', Option::get('admin_defaultReport'));

        // deleted, expect false
        Option::delete('admin_defaultReport');
        $this->assertFalse(Option::get('admin_defaultReport'));
    }

    public function testDeleteLike()
    {
        // empty table, expect false (i.e., not found)
        $this->assertFalse(Option::get('anonymous_defaultReport'));
        $this->assertFalse(Option::get('admin_defaultReport'));
        $this->assertFalse(Option::get('visitor_defaultReport'));

        // insert guard - to test unescaped underscore
        Option::set('adefaultReport', '0', true);
        $this->assertTrue(Option::get('adefaultReport') === '0');

        // populate table, expect '1'
        Option::set('anonymous_defaultReport', '1', true);
        Option::deleteLike('\_defaultReport');
        $this->assertSame('1', Option::get('anonymous_defaultReport'));
        $this->assertSame('0', Option::get('adefaultReport'));

        // populate table, expect '2'
        Option::set('admin_defaultReport', '2', false);
        Option::deleteLike('\_defaultReport');
        $this->assertSame('2', Option::get('admin_defaultReport'));
        $this->assertSame('0', Option::get('adefaultReport'));

        // populate table, expect '3'
        Option::set('visitor_defaultReport', '3', false);
        Option::deleteLike('\_defaultReport');
        $this->assertSame('3', Option::get('visitor_defaultReport'));
        $this->assertSame('0', Option::get('adefaultReport'));

        // delete with non-matching value, expect '1'
        Option::deleteLike('%\_defaultReport', '4');
        $this->assertSame('1', Option::get('anonymous_defaultReport'));
        $this->assertSame('0', Option::get('adefaultReport'));

        // delete with matching pattern, expect false
        Option::deleteLike('%\_defaultReport', '1');
        $this->assertFalse(Option::get('anonymous_defaultReport'));
        $this->assertSame('0', Option::get('adefaultReport'));

        // this shouldn't have been deleted, expect '2' and '3'
        $this->assertSame('2', Option::get('admin_defaultReport'));
        $this->assertSame('3', Option::get('visitor_defaultReport'));
        $this->assertSame('0', Option::get('adefaultReport'));

        // deleted, expect false (except for the guard)
        Option::deleteLike('%\_defaultReport');
        $this->assertFalse(Option::get('admin_defaultReport'));
        $this->assertFalse(Option::get('visitor_defaultReport'));

        // unescaped backslash (single quotes)
        Option::deleteLike('%\_defaultReport');
        $this->assertSame('0', Option::get('adefaultReport'));

        // escaped backslash (single quotes)
        Option::deleteLike('%\\_defaultReport');
        $this->assertSame('0', Option::get('adefaultReport'));

        // unescaped backslash (double quotes)
        Option::deleteLike("%\_defaultReport");
        $this->assertSame('0', Option::get('adefaultReport'));

        // escaped backslash (double quotes)
        Option::deleteLike("%\\_defaultReport");
        $this->assertSame('0', Option::get('adefaultReport'));
    }

    public function testDeleteLike_underscoreNotWildcard()
    {
        // insert guard - to test unescaped underscore
        Option::set('adefaultReport', '1', true);

        Option::deleteLike("adefaul_Report"); // the underscore should not match a character
        $this->assertSame('1', Option::get('adefaultReport'));
    }

    public function testGetLike()
    {
        Option::set('adefaultReport', '1', true);
        Option::set('adefaultRepo', '1', true);
        Option::set('adefaultRepppppppport', '1', true);

        $values = Option::getLike("adefaultRepo%"); // the underscore should not match a character
        $this->assertSame(array(
            'adefaultRepo' => '1',
            'adefaultReport' => '1'
        ), $values);
    }

    public function testGetLike_underscoreNotWildcard()
    {
        // insert guard - to test unescaped underscore
        Option::set('adefaultReport', '1', true);

        $values = Option::getLike("adefaul_Report"); // the underscore should not match a character
        $this->assertSame(array(), $values);
        $values = Option::getLike("adefaul%Report");
        $this->assertSame(array('adefaultReport' => '1'), $values);
    }
}