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

ModelTest.php « tests « CustomVariables « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bfca81c777c003f20bb43b45b6ef79fc3c253a95 (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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CustomVariables\tests;
use Piwik\Db;
use Piwik\Plugins\CustomVariables\Model;

/**
 * @group CustomVariables
 * @group ModelTest
 * @group Database
 */
class ModelTest extends \DatabaseTestCase
{
    /**
     * @expectedException \Exception
     */
    public function test_construct_shouldFailInCaseOfEmptyScope()
    {
        new Model(null);
    }

    /**
     * @expectedException \Exception
     */
    public function test_construct_shouldFailInCaseOfInvalidScope()
    {
        new Model('inValId');
    }

    public function testGetAllScopes()
    {
        $this->assertEquals(array('log_link_visit_action', 'log_visit', 'log_conversion'), Model::getScopes());
    }

    public function test_Install_Uninstall()
    {
        $this->assertEquals(5, $this->getPageScope()->getCurrentNumCustomVars());
        $this->assertEquals(5, $this->getVisitScope()->getCurrentNumCustomVars());
        $this->assertEquals(5, $this->getConversionScope()->getCurrentNumCustomVars());

        Model::uninstall();

        $this->assertEquals(0, $this->getPageScope()->getCurrentNumCustomVars());
        $this->assertEquals(0, $this->getVisitScope()->getCurrentNumCustomVars());
        $this->assertEquals(0, $this->getConversionScope()->getCurrentNumCustomVars());

        $this->getPageScope()->addCustomVariable();
        $this->getPageScope()->addCustomVariable();
        $this->getVisitScope()->addCustomVariable();

        $this->assertEquals(2, $this->getPageScope()->getCurrentNumCustomVars());
        $this->assertEquals(1, $this->getVisitScope()->getCurrentNumCustomVars());
        $this->assertEquals(0, $this->getConversionScope()->getCurrentNumCustomVars());

        Model::install();

        $this->assertEquals(5, $this->getPageScope()->getCurrentNumCustomVars());
        $this->assertEquals(5, $this->getVisitScope()->getCurrentNumCustomVars());
        $this->assertEquals(5, $this->getConversionScope()->getCurrentNumCustomVars());
    }

    public function testGetCustomVariableIndexFromFieldName()
    {
        $this->assertSame(0, Model::getCustomVariableIndexFromFieldName('custom_var_k0'));
        $this->assertSame(0, Model::getCustomVariableIndexFromFieldName('custom_var_v0'));
        $this->assertSame(5, Model::getCustomVariableIndexFromFieldName('custom_var_k5'));
        $this->assertSame(5, Model::getCustomVariableIndexFromFieldName('custom_var_v5'));
        $this->assertSame(938, Model::getCustomVariableIndexFromFieldName('custom_var_k938'));
        $this->assertSame(938, Model::getCustomVariableIndexFromFieldName('custom_var_v938'));
        $this->assertSame(null, Model::getCustomVariableIndexFromFieldName('otherfield'));
    }

    public function testGetScopeName()
    {
        $this->assertEquals('Page', $this->getPageScope()->getScopeName());
        $this->assertEquals('Visit', $this->getVisitScope()->getScopeName());
        $this->assertEquals('Conversion', $this->getConversionScope()->getScopeName());
    }

    public function test_getCurrentNumCustomVars()
    {
        $this->assertEquals(5, $this->getPageScope()->getCurrentNumCustomVars());
        $this->assertEquals(5, $this->getVisitScope()->getCurrentNumCustomVars());
        $this->assertEquals(5, $this->getConversionScope()->getCurrentNumCustomVars());

        $this->getPageScope()->addCustomVariable();
        $this->getConversionScope()->removeCustomVariable();

        $this->assertEquals(6, $this->getPageScope()->getCurrentNumCustomVars());
        $this->assertEquals(5, $this->getVisitScope()->getCurrentNumCustomVars());
        $this->assertEquals(4, $this->getConversionScope()->getCurrentNumCustomVars());
    }

    public function test_getCustomVarIndexes()
    {
        $this->assertEquals(array(1,2,3,4,5), $this->getPageScope()->getCustomVarIndexes());
        $this->assertEquals(array(1,2,3,4,5), $this->getVisitScope()->getCustomVarIndexes());
        $this->assertEquals(array(1,2,3,4,5), $this->getConversionScope()->getCustomVarIndexes());

        $this->getPageScope()->addCustomVariable();
        $this->getConversionScope()->removeCustomVariable();

        $this->assertEquals(array(1,2,3,4,5,6), $this->getPageScope()->getCustomVarIndexes());
        $this->assertEquals(array(1,2,3,4,5), $this->getVisitScope()->getCustomVarIndexes());
        $this->assertEquals(array(1,2,3,4), $this->getConversionScope()->getCustomVarIndexes());
    }

    public function test_getHighestCustomVarIndex_addCustomVariable_removeCustomVariable()
    {
        $this->assertEquals(5, $this->getPageScope()->getHighestCustomVarIndex());
        $this->assertEquals(5, $this->getVisitScope()->getHighestCustomVarIndex());
        $this->assertEquals(5, $this->getConversionScope()->getHighestCustomVarIndex());

        $this->getPageScope()->addCustomVariable();
        $this->getConversionScope()->removeCustomVariable();

        $this->assertEquals(6, $this->getPageScope()->getHighestCustomVarIndex());
        $this->assertEquals(5, $this->getVisitScope()->getHighestCustomVarIndex());
        $this->assertEquals(4, $this->getConversionScope()->getHighestCustomVarIndex());

        $this->getConversionScope()->removeCustomVariable();
        $this->getPageScope()->addCustomVariable();
        $this->getVisitScope()->addCustomVariable();
        $this->getPageScope()->addCustomVariable();
        $this->getConversionScope()->removeCustomVariable();

        $this->assertEquals(8, $this->getPageScope()->getHighestCustomVarIndex());
        $this->assertEquals(6, $this->getVisitScope()->getHighestCustomVarIndex());
        $this->assertEquals(2, $this->getConversionScope()->getHighestCustomVarIndex());
    }

    public function test_removeCustomVariable_shouldNotFailIfRemovesMoreThanExist()
    {
        $scope = $this->getPageScope();

        $this->assertEquals(5, $scope->getHighestCustomVarIndex());

        for ($index = 0; $index < 5; $index++) {
            $scope->removeCustomVariable();
            $this->assertEquals(4 - $index, $scope->getHighestCustomVarIndex());
        }

        $this->assertNull($scope->removeCustomVariable());
        $this->assertEquals(0, $scope->getHighestCustomVarIndex());
        $this->assertEquals(0, $scope->getCurrentNumCustomVars());
    }

    public function test_removeCustomVariable_addCustomVariable_ReturnsIndex()
    {
        $scopeToAdd = $this->getPageScope();
        $scopeToRemove = $this->getVisitScope();

        for ($index = 0; $index < 5; $index++) {
            $this->assertEquals(5 - $index, $scopeToRemove->removeCustomVariable());
            $this->assertEquals(6 + $index, $scopeToAdd->addCustomVariable());
        }
    }

    private function getPageScope()
    {
        return new Model(Model::SCOPE_PAGE);
    }

    private function getVisitScope()
    {
        return new Model(Model::SCOPE_VISIT);
    }

    private function getConversionScope()
    {
        return new Model(Model::SCOPE_CONVERSION);
    }

}