piwik /tests/PHPUnit/Integration/Core/OptionTest.php

Language PHP Lines 193
MD5 Hash c5d0c47092583a390044214c2d8db3f2 Estimated Cost $2,332 (why?)
Repository https://github.com/CodeYellowBV/piwik.git View Raw File View Project SPDX
  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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
use Piwik\Common;
use Piwik\Db;
use Piwik\Option;

require_once "Option.php";

/**
 * Class Core_OptionTest
 *
 * @group Core
 */
class Core_OptionTest extends DatabaseTestCase
{
    /**
     * @group Core
     */
    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)");
        $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'));
    }

    /**
     * @group Core
     */
    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)");
        $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'));
    }

    /**
     * @group Core
     */
    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'));
    }

    /**
     * @group Core
     */
    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'));
    }

    /**
     * @group Core
     */
    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'));
    }

    /**
     * @group Core
     */
    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'));
    }
}
Back to Top