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

DbTest.php « Tracker « Integration « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f4a63d735203859cfe84c8365612ff6561740128 (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
/**
 * 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\Tracker;

use Piwik\Common;
use Piwik\Config;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Timer;
use Piwik\Tracker;

/**
 * Tracker DB test
 *
 * @group Core
 * @group TrackerDbTest
 */
class DbTest extends IntegrationTestCase
{
    private $tableName;
    public function setUp(): void
    {
        parent::setUp();
        $this->tableName = Common::prefixTable('option');
    }

    public function test_innodb_lock_wait_timeout()
    {
        $idSite1 = Fixture::createWebsite('2020-01-01 02:02:02');
        $idSite2 = Fixture::createWebsite('2020-01-01 02:02:02');

        // we expect it does not take more than 3s for the lock exceeded time to happen
        // usually be like 30-50 seconds
        $tracker = Config::getInstance()->Tracker;
        $tracker['innodb_lock_wait_timeout'] = 3;
        Config::getInstance()->Tracker = $tracker;

        $db1 = Tracker\Db::connectPiwikTrackerDb();
        $db2 = Tracker\Db::connectPiwikTrackerDb();

        $timer = new Timer();

        $db1->beginTransaction();
        $db2->beginTransaction();

        $site = Common::prefixTable('site');
        $db1->query("UPDATE $site SET name = ? WHERE idsite = ?", ['foo', $idSite1]);
        $db2->query("UPDATE $site SET name = ? WHERE idsite = ?", ['foo', $idSite2]);

        try {
            $db1->query("UPDATE $site SET name = ? WHERE idsite = ?", ['bar', $idSite2]);
        } catch (\Exception $e) {
            $this->assertStringContainsString('Lock wait timeout exceeded; try restarting transaction', $e->getMessage());
            if ($this->isMysqli()) { // code is not included in error message on Mysqli
                $this->assertEquals(1205, $e->getCode());
            } else {
                $this->assertStringContainsString(' 1205 ', $e->getMessage()); // mysql error code
                $this->assertTrue($db1->isErrNo($e, 1205));
            }
            $this->assertTrue($e instanceof Tracker\Db\DbException);
        }

        $ms = $timer->getTimeMs();

        $this->assertGreaterThan(3000, $ms);
        $this->assertLessThan(5000, $ms);

    }
    public function test_rowCount_whenUpdating_returnsAllMatchedRowsNotOnlyUpdatedRows()
    {
        $db = Tracker::getDatabase();
        // insert one record
        $db->query("INSERT INTO `" . Common::prefixTable('option') . "` VALUES ('rowid', '1', false)");

        // We will now UPDATE this table and check rowCount() value
        $sqlUpdate = "UPDATE `" . Common::prefixTable('option') . "` SET option_value = 2";

        // when no record was updated, return 0
        $result = $db->query($sqlUpdate . " WHERE option_name = 'NOT FOUND'");
        $this->assertSame(0, $db->rowCount($result));

        // when one record was found and updated, returns 1
        $result = $db->query($sqlUpdate . " WHERE option_name = 'rowid'");
        $this->assertSame(1, $db->rowCount($result));

        // when one record was found but NOT actually updated (as values have not changed), we make sure to return 1
        // testing for MYSQLI_CLIENT_FOUND_ROWS and MYSQL_ATTR_FOUND_ROWS
        $result = $db->query($sqlUpdate . " WHERE option_name = 'rowid'");
        $this->assertSame(1, $db->rowCount($result));
    }

    public function test_rowCount_whenInserting()
    {
        $db = Tracker::getDatabase();
        // insert one record
        $result = $this->insertRowId();

        $this->assertSame(1, $db->rowCount($result));
    }

    public function test_fetchOne_notExistingTable()
    {
        $this->expectException(\Piwik\Tracker\Db\DbException::class);
        $this->expectExceptionMessage('doesn\'t exist');

        $db = Tracker::getDatabase();
        $this->insertRowId(3);
        $val = $db->fetchOne('SELECT option_value FROM foobarbaz where option_value = "rowid"');
        $this->assertEquals('3', $val);
    }

    public function test_query_error_whenInsertingDuplicateRow()
    {
        $this->expectException(\Piwik\Tracker\Db\DbException::class);
        $this->expectExceptionMessage('Duplicate entry');

        $this->insertRowId();
        $this->insertRowId();
    }

    public function test_fetchOne()
    {
        $db = Tracker::getDatabase();
        $this->insertRowId(3);
        $val = $db->fetchOne('SELECT option_value FROM `' . $this->tableName . '` where option_name = "rowid"');
        $this->assertEquals('3', $val);
    }

    public function test_fetchOne_noMatch()
    {
        $db = Tracker::getDatabase();
        $val = $db->fetchOne('SELECT option_value from `' . $this->tableName . '` where option_name = "foobar"');
        $this->assertFalse($val);
    }

    public function test_fetchRow()
    {
        $db = Tracker::getDatabase();
        $this->insertRowId(3);
        $val = $db->fetchRow('SELECT option_value from `' . $this->tableName . '` where option_name = "rowid"');
        $this->assertEquals(array(
            'option_value' => '3'
        ), $val);
    }

    public function test_fetchRow_noMatch()
    {
        $db = Tracker::getDatabase();
        $val = $db->fetchRow('SELECT option_value from `' . $this->tableName . '` where option_name = "foobar"');
        $this->assertFalse($val);
    }

    public function test_fetch()
    {
        $db = Tracker::getDatabase();
        $this->insertRowId(3);
        $val = $db->fetch('SELECT option_value from `' . $this->tableName . '` where option_name = "rowid"');
        $this->assertEquals(array(
            'option_value' => '3'
        ), $val);
    }

    public function test_fetch_noMatch()
    {
        $db = Tracker::getDatabase();
        $val = $db->fetch('SELECT option_value from `' . $this->tableName . '` where option_name = "foobar"');
        $this->assertFalse($val);
    }

    public function test_fetchAll()
    {
        $db = Tracker::getDatabase();
        $this->insertRowId(3);
        $val = $db->fetchAll('SELECT option_value from `' . $this->tableName . '` where option_name = "rowid"');
        $this->assertEquals(array(
            array(
                'option_value' => '3'
            )
        ), $val);
    }

    public function test_fetchAll_noMatch()
    {
        $db = Tracker::getDatabase();
        $val = $db->fetchAll('SELECT option_value from `' . $this->tableName . '` where option_name = "foobar"');
        $this->assertEquals(array(), $val);
    }

    private function insertRowId($value = '1')
    {
        $db = Tracker::getDatabase();
        return $db->query("INSERT INTO `" . $this->tableName . "` VALUES ('rowid', '$value', false)");
    }
}