blob: 523f197cde5868bb7d62d51ff101fd02b974d019 (
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
|
<?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\Db;
use Piwik\DbHelper;
use Piwik\Option;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Version;
class DbHelperTest extends IntegrationTestCase
{
public function setUp(): void
{
parent::setUp();
DbHelper::dropDatabase('newdb; create database anotherdb;');
DbHelper::dropDatabase('testdb');
}
public function test_getInstallVersion_isCurrentVersion()
{
$this->assertSame(Version::VERSION, DbHelper::getInstallVersion());
}
public function test_recordInstallVersion_setsCurrentVersion()
{
Option::delete(Db\Schema\Mysql::OPTION_NAME_MATOMO_INSTALL_VERSION);
$this->assertNull(DbHelper::getInstallVersion());
DbHelper::recordInstallVersion();
$this->assertSame(Version::VERSION, DbHelper::getInstallVersion());
}
public function test_recordInstallVersion_doesNotOverwritePreviouslySetVersion()
{
$this->setInstallVersion('2.1.0');
DbHelper::recordInstallVersion();
DbHelper::recordInstallVersion();
DbHelper::recordInstallVersion();
$this->assertSame('2.1.0', DbHelper::getInstallVersion());
}
public function test_wasMatomoInstalledBeforeVersion_sameVersion()
{
$this->setInstallVersion('2.1.0');
$this->assertFalse(DbHelper::wasMatomoInstalledBeforeVersion('2.1.0'));
}
public function test_wasMatomoInstalledBeforeVersion_whenUsedNewerVersion()
{
$this->setInstallVersion('2.1.0');
$this->assertFalse(DbHelper::wasMatomoInstalledBeforeVersion('2.0.0'));
}
public function test_wasMatomoInstalledBeforeVersion_whenWasInstalledBeforeThatVersion()
{
$this->setInstallVersion('2.1.0');
$this->assertTrue(DbHelper::wasMatomoInstalledBeforeVersion('2.2.0'));
}
private function setInstallVersion($version)
{
Option::set(Db\Schema\Mysql::OPTION_NAME_MATOMO_INSTALL_VERSION, $version);
}
public function test_createDatabase_escapesInputProperly()
{
$dbName = 'newdb`; create database anotherdb;`';
DbHelper::createDatabase($dbName);
$this->assertDbExists($dbName);
$this->assertDbNotExists('anotherdb');
}
public function test_dropDatabase_escapesInputProperly()
{
DbHelper::createDatabase("testdb");
$this->assertDbExists('testdb');
DbHelper::dropDatabase('testdb`; create database anotherdb;`');
$this->assertDbExists('testdb');
$this->assertDbNotExists('anotherdb');
}
private function assertDbExists($dbName)
{
$dbs = Db::fetchAll("SHOW DATABASES");
$dbs = array_column($dbs, 'Database');
self::assertTrue(in_array($this->cleanName($dbName), $dbs));
}
private function assertDbNotExists($dbName)
{
$dbs = Db::fetchAll("SHOW DATABASES");
$dbs = array_column($dbs, 'Database');
self::assertTrue(!in_array($this->cleanName($dbName), $dbs));
}
private function cleanName($dbName)
{
return str_replace('`', '', $dbName);
}
}
|