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

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

use Piwik\Access;
use Piwik\DataTable;
use Piwik\Plugins\API\Renderer\Rss;
use Piwik\Tests\Fixture;

/**
 * @group Plugin
 * @group API
 */
class RssRendererTest extends \IntegrationTestCase
{
    /**
     * @var Rss
     */
    private $builder;

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

        $pseudoMockAccess = new \FakeAccess();
        \FakeAccess::setSuperUserAccess(true);
        Access::setSingletonInstance($pseudoMockAccess);

        $idSite = Fixture::createWebsite('2014-01-01 00:00:00');

        $this->builder = $this->makeBuilder(array('method' => 'MultiSites_getAll', 'idSite' => $idSite));
    }

    public function test_renderSuccess_shouldIncludeMessage()
    {
        $response = $this->builder->renderSuccess('ok');

        $this->assertEquals('Success:ok', $response);
    }

    public function test_renderException_shouldIncludeTheMessageAndNotExceptionMessage()
    {
        $response = $this->builder->renderException("The error message", new \Exception('The other message'));

        $this->assertEquals('Error: The error message', $response);
    }

    public function test_renderObject_shouldReturAnError()
    {
        $response = $this->builder->renderObject(new \stdClass());

        $this->assertEquals('Error: The API cannot handle this data structure.', $response);
    }

    public function test_renderResource_shouldReturAnError()
    {
        $response = $this->builder->renderResource(new \stdClass());

        $this->assertEquals('Error: The API cannot handle this data structure.', $response);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage RSS feeds can be generated for one specific website
     */
    public function test_renderScalar_shouldFailForBooleanScalar()
    {
        $this->builder->renderScalar(true);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage RSS feeds can be generated for one specific website
     */
    public function test_renderScalar_shouldFailForIntegerScalar()
    {
        $this->builder->renderScalar(5);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage RSS feeds can be generated for one specific website
     */
    public function test_renderScalar_shouldFailForStringScalar()
    {
        $this->builder->renderScalar('string');
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage RSS feeds can be generated for one specific website
     */
    public function test_renderDataTable_shouldFailForDataTable()
    {
        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));

        $this->builder->renderDataTable($dataTable);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage RSS feeds can be generated for one specific website
     */
    public function test_renderDataTable_shouldFailForSubtables()
    {
        $subtable = new DataTable();
        $subtable->addRowFromSimpleArray(array('nb_visits' => 2, 'nb_random' => 6));

        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
        $dataTable->getFirstRow()->setSubtable($subtable);

        $this->builder->renderDataTable($dataTable);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage RSS feeds can be generated for one specific website
     */
    public function test_renderDataTable_shouldFail_IfKeynameIsNotDate()
    {
        $map = new DataTable\Map();

        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));

        $map->addTable($dataTable, 'table1');
        $map->addTable($dataTable, 'table2');

        $this->builder->renderDataTable($map);
    }

    public function test_renderDataTable_shouldRenderDataTableMaps_IfKeynameIsDate()
    {
        $map = new DataTable\Map();
        $map->setKeyName('date');
        $_GET['period'] = 'day';

        $response = $this->builder->renderDataTable($map);

        unset($_GET['period']);

        $response = preg_replace(array('/<pubDate>(.*)<\/pubDate>/','/<lastBuildDate>(.*)<\/lastBuildDate>/'), '', $response);

        $this->assertEquals('<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>piwik statistics - RSS</title>
    <link>http://piwik.org</link>
    <description>Piwik RSS feed</description>
    
    <generator>piwik</generator>
    <language>en</language>
    	</channel>
</rss>', $response);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage RSS feeds can be generated for one specific website
     */
    public function test_renderDataTable_shouldFailForSimpleDataTable()
    {
        $dataTable = new DataTable\Simple();
        $dataTable->addRowsFromArray(array('nb_visits' => 3, 'nb_random' => 6));

        $this->builder->renderDataTable($dataTable);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage RSS feeds can be generated for one specific website
     */
    public function test_renderArray_ShouldFailForArrays()
    {
        $input = array(1, 2, 5, 'string', 10);

        $this->builder->renderArray($input);
    }

    private function makeBuilder($request)
    {
        return new Rss($request);
    }
}