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
|
<?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;
use Piwik\Common;
use Piwik\Config;
use Piwik\Db;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* @group Core
*/
class DbTest extends IntegrationTestCase
{
private $dbReaderConfigBackup;
public function setUp()
{
parent::setUp();
$this->dbReaderConfigBackup = Config::getInstance()->database_reader;
}
public function tearDown()
{
Db::destroyDatabaseObject();
Config::getInstance()->database_reader = $this->dbReaderConfigBackup;
parent::tearDown();
}
public function test_getColumnNamesFromTable()
{
$this->assertColumnNames('access', array('idaccess', 'login', 'idsite', 'access'));
$this->assertColumnNames('option', array('option_name', 'option_value', 'autoload'));
}
public function test_getDb()
{
$db = Db::get();
$this->assertNotEmpty($db);
$this->assertTrue($db instanceof Db\AdapterInterface);
}
public function test_hasReaderDatabaseObject_byDefaultNotInUse()
{
$this->assertFalse(Db::hasReaderDatabaseObject());
}
public function test_hasReaderConfigured_byDefaultNotConfigured()
{
$this->assertFalse(Db::hasReaderConfigured());
}
public function test_getReader_whenNotConfigured_StillReturnsRegularDbConnection()
{
$this->assertFalse(Db::hasReaderConfigured());// ensure no reader is configured
$db = Db::getReader();
$this->assertNotEmpty($db);
$this->assertTrue($db instanceof Db\AdapterInterface);
}
public function test_withReader()
{
Config::getInstance()->database_reader = Config::getInstance()->database;
$this->assertFalse(Db::hasReaderDatabaseObject());
$this->assertTrue(Db::hasReaderConfigured());
$db = Db::getReader();
$this->assertNotEmpty($db);
$this->assertTrue($db instanceof Db\AdapterInterface);
$this->assertTrue(Db::hasReaderDatabaseObject());
Db::destroyDatabaseObject();
$this->assertFalse(Db::hasReaderDatabaseObject());
}
public function test_withReader_createsDifferentConnectionForDb()
{
Config::getInstance()->database_reader = Config::getInstance()->database;
$db = Db::getReader();
$this->assertNotSame($db->getConnection(), Db::get()->getConnection());
}
public function test_withoutReader_usesSameDbConnection()
{
$this->assertFalse(Db::hasReaderConfigured());
$this->assertFalse(Db::hasReaderDatabaseObject());
$db = Db::getReader();
$this->assertSame($db->getConnection(), Db::get()->getConnection());
}
private function assertColumnNames($tableName, $expectedColumnNames)
{
$colmuns = Db::getColumnNamesFromTable(Common::prefixTable($tableName));
$this->assertEquals($expectedColumnNames, $colmuns);
}
/**
* @dataProvider getIsOptimizeInnoDBTestData
*/
public function test_isOptimizeInnoDBSupported_ReturnsCorrectResult($version, $expectedResult)
{
$result = Db::isOptimizeInnoDBSupported($version);
$this->assertEquals($expectedResult, $result);
}
/**
* @dataProvider getDbAdapter
*/
public function test_SqlMode_IsSet_PDO($adapter, $expectedClass)
{
Db::destroyDatabaseObject();
Config::getInstance()->database['adapter'] = $adapter;
$db = Db::get();
// make sure test is useful and setting adapter works
$this->assertInstanceOf($expectedClass, $db);
$result = $db->fetchOne('SELECT @@SESSION.sql_mode');
$expected = 'NO_AUTO_VALUE_ON_ZERO';
$this->assertSame($expected, $result);
}
/**
* @expectedException \Exception
* @expectedExceptionMessagelock name has to be 64 characters or less
*/
public function test_getDbLock_shouldThrowAnException_IfDbLockNameIsTooLong()
{
Db::getDbLock(str_pad('test', 65, '1'));
}
public function test_getDbLock_shouldGetLock()
{
$db = Db::get();
$this->assertTrue(Db::getDbLock('MyLock'));
// same session still has lock
$this->assertTrue(Db::getDbLock('MyLock'));
Db::setDatabaseObject(null);
// different session, should not be able to acquire lock
$this->assertFalse(Db::getDbLock('MyLock', 1));
// different session cannot release lock
$this->assertFalse(Db::releaseDbLock('MyLock'));
Db::destroyDatabaseObject();
// release lock again by using previous session
Db::setDatabaseObject($db);
$this->assertTrue(Db::releaseDbLock('MyLock'));
Db::destroyDatabaseObject();
}
/**
* @dataProvider getDbAdapter
*/
public function test_getRowCount($adapter, $expectedClass)
{
Db::destroyDatabaseObject();
Config::getInstance()->database['adapter'] = $adapter;
$db = Db::get();
// make sure test is useful and setting adapter works
$this->assertInstanceOf($expectedClass, $db);
$result = $db->query('select 21');
$this->assertEquals(1, $db->rowCount($result));
}
public function getDbAdapter()
{
return array(
array('Mysqli', 'Piwik\Db\Adapter\Mysqli'),
array('PDO\MYSQL', 'Piwik\Db\Adapter\Pdo\Mysql')
);
}
public function getIsOptimizeInnoDBTestData()
{
return array(
array("10.0.17-MariaDB-1~trusty", false),
array("10.1.1-MariaDB-1~trusty", true),
array("10.2.0-MariaDB-1~trusty", true),
array("10.6.19-0ubuntu0.14.04.1", false),
// for sanity. maybe not ours.
array("", false),
array(0, false),
array(false, false),
array("slkdf(@*#lkesjfMariaDB", false),
array("slkdfjq3rujlkv", false),
);
}
}
|