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

SparklinesConfigTest.php « Integration « tests « CoreVisualizations « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 33fb6be6ab3f43d32268d3218b6deab261d73499 (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
<?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\CoreVisualizations\tests\Integration;

use Piwik\Plugins\CoreVisualizations\Visualizations\Sparklines\Config;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Translate;

/**
 * @group CoreVisualizations
 * @group SparklinesConfigTest
 * @group Plugins
 */
class SparklinesConfigTest extends IntegrationTestCase
{
    /**
     * @var Config
     */
    private $config;

    public function setUp()
    {
        parent::setUp();
        FakeAccess::$superUser = true;

        if (!Fixture::siteCreated(1)) {
            Fixture::createWebsite('2014-01-01 00:00:00');
        }

        $this->config = new Config();

        Translate::loadAllTranslations();
    }

    public function tearDown()
    {
        Translate::reset();

        parent::tearDown();
    }

    public function test_areSparklinesLinkable_byDefaultSparklinesAreLinkable()
    {
        $this->assertTrue($this->config->areSparklinesLinkable());
    }

    public function test_setNotLinkableWithAnyEvolutionGraph_areSparklinesLinkable_sparklinesCanBeMadeNotLinkable()
    {
        $this->config->setNotLinkableWithAnyEvolutionGraph();
        $this->assertFalse($this->config->areSparklinesLinkable());
    }

    public function test_addSparkline_shouldAddAMinimalSparklineWithOneValueAndUseDefaultOrder()
    {
        $this->config->addSparkline($this->sparklineParams(), $value = 10, $description = 'Visits');

        $expectedSparkline = array(
            'url' => '?period=day&date=2012-03-06,2012-04-04&idSite=1&module=CoreHome&action=renderMe&viewDataTable=sparkline',
            'metrics' => array (
                array ('column' => '', 'value' => 10, 'description' => 'Visits'),
            ),
            'order' => 999
        );

        $this->assertSame(array($expectedSparkline), $this->config->getSortedSparklines());
    }

    public function test_addSparkline_shouldAddAMinimalSparklineWithOneValueAndUseDefaultOrderWithColumn()
    {
        $params = $this->sparklineParams();
        $params['columns'] = 'nb_visits';
        $this->config->addSparkline($params, $value = 10, $description = 'Visits');

        $expectedSparkline = array('column' => 'nb_visits', 'value' => 10, 'description' => 'Visits');

        $sparklines = $this->config->getSortedSparklines();
        $this->assertSame(array($expectedSparkline), $sparklines[0]['metrics']);
    }

    public function test_addSparkline_shouldAddSparklineWithMultipleValues()
    {
        $this->config->addSparkline($this->sparklineParams(), $values = array(10, 20), $description = array('Visits', 'Actions'));

        $sparklines = $this->config->getSortedSparklines();

        $this->assertSame(array (
                array ('column' => '', 'value' => 10, 'description' => 'Visits'),
                array ('column' => '', 'value' => 20, 'description' => 'Actions'),
            ), $sparklines[0]['metrics']);
    }

    public function test_addSparkline_shouldAddSparklinesMultipleValuesWithColumns()
    {
        $params = $this->sparklineParams();
        $params['columns'] = array('nb_visits', 'nb_actions');

        $this->config->addSparkline($params, $values = array(10, 20), $description = array('Visits', 'Actions'));

        $expectedSparkline = array(
            array ('column' => 'nb_visits', 'value' => 10, 'description' => 'Visits'),
            array ('column' => 'nb_actions', 'value' => 20, 'description' => 'Actions')
        );

        $sparklines = $this->config->getSortedSparklines();
        $this->assertSame($expectedSparkline, $sparklines[0]['metrics']);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage Values: 10, 20, 30 Descriptions: Visits, Actions
     */
    public function test_addSparkline_shouldThrowAnException_IfValuesDoesNotMatchAmountOfDescriptions()
    {
        $this->config->addSparkline($this->sparklineParams(), $values = array(10, 20, 30), $description = array('Visits', 'Actions'));
    }

    public function test_addSparkline_shouldAddEvolution()
    {
        $evolution = array('currentValue' => 10, 'pastValue' => 21,
                            'tooltip' => '1 visit compared to 2 visits');
        $this->config->addSparkline($this->sparklineParams(), $value = 10, $description = 'Visits', $evolution);

        $sparklines = $this->config->getSortedSparklines();

        $this->assertSame(array (
            'percent'  => '-52.4%',
             'tooltip' => '1 visit compared to 2 visits'
        ), $sparklines[0]['evolution']);
    }

    public function test_addSparkline_shouldAddOrder()
    {
        $this->config->addSparkline($this->sparklineParams(), $value = 10, $description = 'Visits', $evolution = null, $order = '42');

        $sparklines = $this->config->getSortedSparklines();

        $this->assertSame(42, $sparklines[0]['order']);
    }

    public function test_addSparkline_shouldBeAbleToBuildSparklineUrlBasedOnGETparams()
    {
        $oldGet = $_GET;
        $_GET = $this->sparklineParams();
        $this->config->addSparkline(array('columns' => 'nb_visits'), $value = 10, $description = 'Visits');
        $_GET = $oldGet;

        $sparklines = $this->config->getSortedSparklines();

        $this->assertSame('?columns=nb_visits&viewDataTable=sparkline&date=2012-03-06,2012-04-04', $sparklines[0]['url']);
    }

    private function sparklineParams($params = array())
    {
        $params['period'] = 'day';
        $params['date']   = '2012-04-04';
        $params['idSite'] = '1';
        $params['module'] = 'CoreHome';
        $params['action'] = 'renderMe';

        return $params;
    }

    public function provideContainerConfig()
    {
        return array(
            'Piwik\Access' => new FakeAccess()
        );
    }
}