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

DimensionTest.php « Columns « Integration « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0e5117767305cb15bd6653640e0f99c6500288a4 (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
<?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\Test\Columns
{
    // there is a test that requires the class to be defined in a plugin

    use Piwik\Columns\Dimension;
    use Piwik\Plugin\Segment;

    class DimensionTest extends Dimension
    {
        protected $columnName  = 'test_dimension';
        protected $columnType  = 'INTEGER (10) DEFAULT 0';

        public function set($param, $value)
        {
            $this->$param = $value;
        }

        protected function configureSegments()
        {
            $segment = new Segment();
            $segment->setSegment('exitPageUrl');
            $segment->setName('Actions_ColumnExitPageURL');
            $segment->setCategory('General_Visit');
            $this->addSegment($segment);

            // custom type and sqlSegment
            $segment = new Segment();
            $segment->setSegment('exitPageUrl');
            $segment->setSqlSegment('customValue');
            $segment->setType(Segment::TYPE_METRIC);
            $segment->setName('Actions_ColumnExitPageURL');
            $segment->setCategory('General_Visit');
            $this->addSegment($segment);
        }
    }
}

namespace Piwik\Tests\Integration\Columns
{
    use Piwik\Columns\Dimension;
    use Piwik\Config;
    use Piwik\Plugin\Dimension\ActionDimension;
    use Piwik\Plugin\Dimension\ConversionDimension;
    use Piwik\Plugin\Dimension\VisitDimension;
    use Piwik\Plugin\Segment;
    use Piwik\Plugin\Manager;
    use Piwik\Plugins\Test\Columns\DimensionTest;
    use Piwik\Plugins\Test\FakeActionDimension;
    use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

    /**
     * @group Core
     */
    class ColumnDimensionTest extends IntegrationTestCase
    {
        /**
         * @var FakeActionDimension
         */
        private $dimension;

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

            Manager::getInstance()->unloadPlugins();
            Manager::getInstance()->doNotLoadAlwaysActivatedPlugins();

            $this->dimension = new DimensionTest();
        }

        public function test_hasImplementedEvent_shouldDetectWhetherAMethodWasOverwrittenInTheActualPluginClass()
        {
            $this->assertTrue($this->dimension->hasImplementedEvent('set'));
            $this->assertTrue($this->dimension->hasImplementedEvent('configureSegments'));

            $this->assertFalse($this->dimension->hasImplementedEvent('getSegments'));
        }

        public function test_getColumnName_shouldReturnTheNameOfTheColumn()
        {
            $this->assertSame('test_dimension', $this->dimension->getColumnName());
        }

        public function test_hasColumnType_shouldDetectWhetherAColumnTypeIsSet()
        {
            $this->assertTrue($this->dimension->hasColumnType());

            $this->dimension->set('columnType', '');
            $this->assertFalse($this->dimension->hasColumnType());
        }

        public function test_getName_ShouldNotReturnANameByDefault()
        {
            $this->assertSame('', $this->dimension->getName());
        }

        public function test_getAllDimensions_shouldReturnAllKindOfDimensions()
        {
            Manager::getInstance()->loadPlugins(array('Actions', 'Events', 'DevicesDetector', 'Goals', 'CustomVariables'));

            $dimensions = Dimension::getAllDimensions();

            $this->assertGreaterThan(20, count($dimensions));

            $foundConversion = false;
            $foundVisit      = false;
            $foundAction     = false;
            $foundNormal     = false;

            foreach ($dimensions as $dimension) {
                if ($dimension instanceof ConversionDimension) {
                    $foundConversion = true;
                } else if ($dimension instanceof ActionDimension) {
                    $foundAction = true;
                } else if ($dimension instanceof VisitDimension) {
                    $foundVisit = true;
                } else if ($dimension instanceof Dimension) {
                    $foundNormal = true;
                } else {
                    $this->fail('Unexpected dimension class found');
                }

                $this->assertRegExp('/Piwik.Plugins.(Actions|Events|DevicesDetector|Goals|CustomVariables).Columns/', get_class($dimension));
            }

            $this->assertTrue($foundConversion);
            $this->assertTrue($foundAction);
            $this->assertTrue($foundVisit);
            $this->assertTrue($foundNormal);
        }

        public function test_getDimensions_shouldReturnAllKindOfDimensionsThatBelongToASpecificPlugin()
        {
            Manager::getInstance()->loadPlugins(array('Actions', 'Events', 'DevicesDetector', 'Goals'));

            $dimensions = Dimension::getDimensions(Manager::getInstance()->loadPlugin('Actions'));

            $this->assertGreaterThan(10, count($dimensions));

            $foundVisit      = false;
            $foundAction     = false;

            foreach ($dimensions as $dimension) {
                if ($dimension instanceof ActionDimension) {
                    $foundAction = true;
                } else if ($dimension instanceof VisitDimension) {
                    $foundVisit = true;
                }

                $this->assertRegExp('/Piwik.Plugins.Actions.Columns/', get_class($dimension));
            }

            $this->assertTrue($foundAction);
            $this->assertTrue($foundVisit);
        }

        public function test_getDimensions_shouldReturnConversionDimensionsThatBelongToASpecificPlugin()
        {
            Manager::getInstance()->loadPlugins(array('Actions', 'Events', 'DevicesDetector', 'Goals'));

            $dimensions = Dimension::getDimensions(Manager::getInstance()->loadPlugin('Goals'));

            $this->assertGreaterThan(2, count($dimensions));

            $foundConversion = false;

            foreach ($dimensions as $dimension) {
                if ($dimension instanceof ConversionDimension) {
                    $foundConversion = true;
                }

                $this->assertRegExp('/Piwik.Plugins.Goals.Columns/', get_class($dimension));
            }

            $this->assertTrue($foundConversion);
        }

        public function test_getSegment_ShouldReturnConfiguredSegments()
        {
            $segments = $this->dimension->getSegments();

            $this->assertCount(2, $segments);
            $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[0]);
            $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[1]);
        }

        public function test_addSegment_ShouldPrefilSomeSegmentValuesIfNotDefinedYet()
        {
            $segments = $this->dimension->getSegments();

            $this->assertEquals(Segment::TYPE_DIMENSION, $segments[0]->getType());
        }

        public function test_addSegment_ShouldNotOverwritePreAssignedValues()
        {
            $segments = $this->dimension->getSegments();

            $this->assertEquals(Segment::TYPE_METRIC, $segments[1]->getType());
        }

        public function test_getId_ShouldCorrectlyGenerateIdFromDimensionsQualifiedClassName()
        {
            $this->assertEquals("Test.DimensionTest", $this->dimension->getId());
        }

        public function test_factory_ShouldCreateDimensionFromDimensionId()
        {
            Manager::getInstance()->loadPlugins(array('ExampleTracker'));

            $dimension = Dimension::factory("ExampleTracker.ExampleDimension");
            $this->assertInstanceOf('Piwik\Plugins\ExampleTracker\Columns\ExampleDimension', $dimension);
        }
   }
}