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

UpdaterTest.php « Columns « Integration « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c9ca49d0c8fd65d9314e40c8f92634cbc42aa602 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?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\Plugins\Test\Columns;

use Piwik\Columns\Updater as ColumnsUpdater;
use Piwik\Common;
use Piwik\Db;
use Piwik\DbHelper;
use Piwik\Plugin\Dimension\ActionDimension;
use Piwik\Plugin\Dimension\ConversionDimension;
use Piwik\Plugin\Dimension\VisitDimension;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Updater;
use Piwik\Updater\Migration;

// NOTE: we can't use PHPUnit mock framework since we have to set columnName/columnType. reflection will set it, but
// for some reason, methods of base type don't see the set value.
class MockVisitDimension extends VisitDimension
{
    public function __construct($columnName, $columnType)
    {
        $this->columnName = $columnName;
        $this->columnType = $columnType;
    }
}

class MockActionDimension extends ActionDimension
{
    public function __construct($columnName, $columnType)
    {
        $this->columnName = $columnName;
        $this->columnType = $columnType;
    }
}

class MockConversionDimension extends ConversionDimension
{
    public function __construct($columnName, $columnType)
    {
        $this->columnName = $columnName;
        $this->columnType = $columnType;
    }
}

/**
 * @group Core
 */
class UpdaterTest extends IntegrationTestCase
{
    private $tableColumnsCache = array(); // for test performance

    /**
     * @var ColumnsUpdater
     */
    private $columnsUpdater;

    public function setUp(): void
    {
        parent::setUp();

        // recreate log_visit/log_link_visit_action/log_conversion tables w/o any dimensions
        $tablesToRecreate = array('log_visit', 'log_link_visit_action', 'log_conversion');
        foreach ($tablesToRecreate as $table) {
            Db::exec("DROP TABLE `" . Common::prefixTable($table) . "`");

            $tableCreateSql = DbHelper::getTableCreateSql($table);
            Db::exec($tableCreateSql);
        }

        $visitDimensions = array(
            $this->getMockVisitDimension("test_visit_col_1", "INTEGER(10) UNSIGNED NOT NULL"),
            $this->getMockVisitDimension("test_visit_col_2", "VARCHAR(32) NOT NULL")
        );

        $actionDimensions = array(
            $this->getMockActionDimension("test_action_col_1", "VARCHAR(32) NOT NULL"),
            $this->getMockActionDimension("test_action_col_2", "INTEGER(10) UNSIGNED DEFAULT NULL")
        );

        $conversionDimensions = array(
            $this->getMockConversionDimension("test_conv_col_1", "FLOAT DEFAULT NULL"),
            $this->getMockConversionDimension("test_conv_col_2", "VARCHAR(32) NOT NULL")
        );

        $this->columnsUpdater = new ColumnsUpdater($visitDimensions, $actionDimensions, $conversionDimensions);

        $this->tableColumnsCache = array();
    }


    /**
     * @param Migration\Db\Sql[] $queries
     * @return array
     */
    private function flattenQueries($queries)
    {
        $response = array();
        foreach ($queries as $query) {
            $response[$query->__toString()] = $query->getErrorCodesToIgnore();
        }
        return $response;
    }

    public function test_doUpdate_AddsDimensions_WhenDimensionsNotInTables()
    {
        $updater = $this->getMockUpdater();
        $this->columnsUpdater->doUpdate($updater);

        $this->assertDimensionsAddedToTables();
    }

    public function test_doUpdate_DoesNotError_WhenDimensionsAlreadyInTables()
    {
        $this->addDimensionsToTables();

        $updater = $this->getMockUpdater();
        $this->columnsUpdater->doUpdate($updater);

        $this->assertDimensionsAddedToTables();
    }

    public function test_getAllVersions_ReturnsFileVersionsOfAllDimensions()
    {
        $updater = $this->getMockUpdater();
        $actualVersions = $this->columnsUpdater->getAllVersions($updater);

        $expectedVersions = array(
            'log_visit.test_visit_col_1' => 'INTEGER(10) UNSIGNED NOT NULL',
            'log_visit.test_visit_col_2' => 'VARCHAR(32) NOT NULL',
            'log_link_visit_action.test_action_col_1' => 'VARCHAR(32) NOT NULL',
            'log_link_visit_action.test_action_col_2' => 'INTEGER(10) UNSIGNED DEFAULT NULL',
            'log_conversion.test_conv_col_1' => 'FLOAT DEFAULT NULL',
            'log_conversion.test_conv_col_2' => 'VARCHAR(32) NOT NULL'
        );
        $this->assertEquals($actualVersions, $expectedVersions);
    }

    /**
     * @dataProvider getCoreDimensionsForGetAllVersionsTest
     */
    public function test_getAllVersions_ReturnsNoVersions_ForCoreDimensions_ThatWereRefactored_AndHaveNoDbVersion($table, $columnName, $columnType)
    {
        $this->addDimensionsToTables();
        $this->addDimensionToTable($table, $columnName, $columnType);

        $updater = $this->getMockUpdater();
        $actualVersions = $this->columnsUpdater->getAllVersions($updater);

        $expectedVersions = array(
            'log_visit.test_visit_col_1' => 'INTEGER(10) UNSIGNED NOT NULL',
            'log_visit.test_visit_col_2' => 'VARCHAR(32) NOT NULL',
            'log_link_visit_action.test_action_col_1' => 'VARCHAR(32) NOT NULL',
            'log_link_visit_action.test_action_col_2' => 'INTEGER(10) UNSIGNED DEFAULT NULL',
            'log_conversion.test_conv_col_1' => 'FLOAT DEFAULT NULL',
            'log_conversion.test_conv_col_2' => 'VARCHAR(32) NOT NULL'
        );
        $this->assertEquals($actualVersions, $expectedVersions);
    }

    public function getCoreDimensionsForGetAllVersionsTest()
    {
        // only one test per table. otherwise test will be too slow (~2 mins for all).
        return array(
            array('log_visit', 'user_id', 'VARCHAR(200) NULL'),
            array('log_link_visit_action', 'idaction_event_category', 'INTEGER(10) UNSIGNED DEFAULT NULL'),
            array('log_conversion', 'revenue_tax', 'float default NULL')
        );
    }

    private function getMockVisitDimension($columnName, $columnType)
    {
        return new MockVisitDimension($columnName, $columnType);
    }

    private function getMockActionDimension($columnName, $columnType)
    {
        return new MockActionDimension($columnName, $columnType);
    }

    private function getMockConversionDimension($columnName, $columnType)
    {
        return new MockConversionDimension($columnName, $columnType);
    }

    private function getMockUpdater($hasNewVersion = true)
    {
        $result = $this->getMockBuilder("Piwik\\Updater")->onlyMethods(array('hasNewVersion'))->getMock();

        $result->expects($this->any())->method('hasNewVersion')->will($this->returnCallback(function () use ($hasNewVersion) {
            return $hasNewVersion;
        }));

        return $result;
    }

    private function assertDimensionsAddedToTables()
    {
        $this->assertTableHasColumn('log_visit', 'test_visit_col_1', 'int(10) unsigned', $allowNull = false);
        $this->assertTableHasColumn('log_visit', 'test_visit_col_2', 'varchar(32)', $allowNull = false);

        $this->assertTableHasColumn('log_link_visit_action', 'test_action_col_1', 'varchar(32)', $allowNull = false);
        $this->assertTableHasColumn('log_link_visit_action', 'test_action_col_2', 'int(10) unsigned', $allowNull = true);

        $this->assertTableHasColumn('log_conversion', 'test_conv_col_1', 'float', $allowNull = true);
        $this->assertTableHasColumn('log_conversion', 'test_conv_col_2', 'varchar(32)', $allowNull = false);
    }

    private function assertTableHasColumn($table, $columnName, $columnType, $allowNull)
    {
        $column = $this->getTableColumnInfo($table, $columnName);

        $this->assertNotNull($column, "Column '$columnName' does not exist in '$table'.");

        $this->assertEquals(strtolower($columnType), strtolower($column['Type']));
        if ($allowNull) {
            $this->assertEquals("yes", strtolower($column['Null']));
        } else {
            $this->assertEquals("no", strtolower($column['Null']));
        }
    }

    private function getTableColumns($table)
    {
        if (empty($this->tableColumnsCache[$table])) {
            $this->tableColumnsCache[$table] = Db::fetchAll("SHOW COLUMNS IN `" . Common::prefixTable($table) . "`");
        }
        return $this->tableColumnsCache[$table];
    }

    private function getTableColumnInfo($table, $columnName)
    {
        $columns = $this->getTableColumns($table);
        foreach ($columns as $row) {
            if ($row['Field'] == $columnName) {
                return $row;
            }
        }
        return null;
    }

    private function addDimensionsToTables()
    {
        $this->addDimensionToTable('log_visit', 'test_visit_col_1', "INTEGER UNSIGNED NOT NULL");
        $this->addDimensionToTable('log_visit', 'test_visit_col_2', "VARCHAR(32) NOT NULL");

        $this->addDimensionToTable('log_link_visit_action', 'test_action_col_1', "VARCHAR(32) NOT NULL");
        $this->addDimensionToTable('log_link_visit_action', 'test_action_col_2', "INTEGER(10) UNSIGNED DEFAULT NULL");

        $this->addDimensionToTable('log_conversion', 'test_conv_col_1', "FLOAT DEFAULT NULL");
        $this->addDimensionToTable('log_conversion', 'test_conv_col_2', "VARCHAR(32) NOT NULL");
    }

    private function addDimensionToTable($table, $column, $type)
    {
        Db::exec("ALTER TABLE `" . Common::prefixTable($table) . "` ADD COLUMN $column $type");
    }
}