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

ReportTest.php « Core « Integration « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 495a76ef0c499f8358ab106869f3afb2a6499e4e (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

use Piwik\Plugin\Report;
use Piwik\Plugins\ExampleReport\Reports\GetExampleReport;
use Piwik\Plugins\Actions\Columns\ExitPageUrl;
use Piwik\Piwik;
use Piwik\Metrics;
use Piwik\WidgetsList;
use Piwik\Translate;
use Piwik\Menu\MenuReporting;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\Tests\Fixture;

class GetBasicReport extends Report
{
    protected function init()
    {
        parent::init();

        $this->name = 'My Custom Report Name';
        $this->order  = 20;
        $this->module = 'TestPlugin';
        $this->action = 'getBasicReport';
        $this->category = 'Goals_Goals';
    }
}

class GetAdvancedReport extends GetBasicReport
{
    protected function init()
    {
        parent::init();

        $this->action      = 'getAdvancedReport';
        $this->widgetTitle = 'Actions_WidgetPageTitlesFollowingSearch';
        $this->menuTitle   = 'Actions_SubmenuPageTitles';
        $this->documentation = Piwik::translate('ExampleReportDocumentation');
        $this->dimension   = new ExitPageUrl();
        $this->metrics     = array('nb_actions', 'nb_visits');
        $this->processedMetrics = array('conversion_rate', 'bounce_rate');
        $this->parameters = array('idGoal' => 1);
        $this->isSubtableReport = true;
        $this->actionToLoadSubTables = 'GetBasicReport';
        $this->constantRowsCount = true;
    }

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

class GetDisabledReport extends GetBasicReport
{
    public function isEnabled()
    {
        return false;
    }
}

/**
 * @group Core
 */
class Plugin_ReportTest extends DatabaseTestCase
{
    /**
     * @var Report
     */
    private $exampleReport;

    /**
     * @var GetDisabledReport
     */
    private $disabledReport;

    /**
     * @var GetBasicReport
     */
    private $basicReport;

    /**
     * @var GetAdvancedReport
     */
    private $advancedReport;

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

        Fixture::createWebsite('2014-01-01 00:00:00');
        $this->unloadAllPlugins();
        $_GET['idSite'] = 1;

        $this->exampleReport  = new GetExampleReport();
        $this->disabledReport = new GetDisabledReport();
        $this->basicReport    = new GetBasicReport();
        $this->advancedReport = new GetAdvancedReport();
    }

    public function tearDown()
    {
        WidgetsList::getInstance()->_reset();
        MenuReporting::getInstance()->unsetInstance();
        Translate::unloadEnglishTranslation();
        unset($_GET['idSite']);
        parent::tearDown();
    }

    public function test_shouldDetectTheModuleOfTheReportAutomatically()
    {
        $this->assertEquals('ExampleReport', $this->exampleReport->getModule());
    }

    public function test_shouldDetectTheActionOfTheReportAutomatiacally()
    {
        $this->assertEquals('getExampleReport', $this->exampleReport->getAction());
    }

    public function test_getName_shouldReturnTheNameOfTheReport()
    {
        $this->assertEquals('My Custom Report Name', $this->basicReport->getName());
    }

    public function test_isEnabled_shouldBeEnabledByDefault()
    {
        $this->assertTrue($this->basicReport->isEnabled());
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage General_ExceptionReportNotEnabled
     */
    public function test_checkIsEnabled_shouldThrowAnExceptionIfReportIsNotEnabled()
    {
        $this->disabledReport->checkIsEnabled();
    }

    public function test_getWidgetTitle_shouldReturnNullIfNoTitleIsSet()
    {
        $this->assertNull($this->basicReport->getWidgetTitle());
    }

    public function test_getWidgetTitle_shouldReturnTranslatedTitleIfSet()
    {
        $this->loadEnglishTranslation();
        $this->assertEquals('Page Titles Following a Site Search', $this->advancedReport->getWidgetTitle());
    }

    public function test_getCategory_shouldReturnTranslatedCategory()
    {
        $this->loadEnglishTranslation();
        $this->assertEquals('Goals', $this->advancedReport->getCategory());
    }

    public function test_configureWidget_shouldNotAddAWidgetIfNoWidgetTitleIsSet()
    {
        $widgets = WidgetsList::get();
        $this->assertCount(0, $widgets);

        $this->basicReport->configureWidget(WidgetsList::getInstance());

        $widgets = WidgetsList::get();
        $this->assertCount(0, $widgets);
    }

    public function test_configureWidget_shouldAddAWidgetIfAWidgetTitleIsSet()
    {
        $widgets = WidgetsList::get();
        $this->assertCount(0, $widgets);

        $this->advancedReport->configureWidget(WidgetsList::getInstance());

        $widgets = WidgetsList::get();
        $this->assertCount(1, $widgets);
        $this->assertEquals(array(array(
            'name'       => 'Actions_WidgetPageTitlesFollowingSearch',
            'uniqueId'   => 'widgetTestPlugingetAdvancedReport',
            'parameters' => array('module' => 'TestPlugin', 'action' => 'getAdvancedReport')
        )), $widgets['Goals_Goals']);
    }

    public function test_configureWidget_shouldMixinWidgetParametersIfSet()
    {
        $widgets = WidgetsList::get();
        $this->assertCount(0, $widgets);

        $this->advancedReport->set('widgetParams', array('foo' => 'bar'));
        $this->advancedReport->configureWidget(WidgetsList::getInstance());

        $widgets = WidgetsList::get();
        $this->assertCount(1, $widgets);
        $this->assertEquals(array('module' => 'TestPlugin', 'action' => 'getAdvancedReport', 'foo' => 'bar'),
                            $widgets['Goals_Goals'][0]['parameters']);
    }

    public function test_configureReportingMenu_shouldNotAddAMenuIfNoWidgetTitleIsSet()
    {
        $menu      = MenuReporting::getInstance();
        $menuItems = $menu->getMenu();
        $this->assertNull($menuItems);

        $this->basicReport->configureReportingMenu($menu);

        $menuItems = $menu->getMenu();
        $this->assertNull($menuItems);
    }

    public function test_configureReportingMenu_shouldAddAMenuIfATitleIsSet()
    {
        $menu      = MenuReporting::getInstance();
        $menuItems = $menu->getMenu();
        $this->assertNull($menuItems);

        $this->advancedReport->configureReportingMenu($menu);

        $menuItems = $menu->getMenu();
        
        $expected = array(
            '_tooltip' => false,
            '_order' => 20,
            '_hasSubmenu' => true,
            'Actions_SubmenuPageTitles' => array(
            '_url' => array(
                'module' => 'TestPlugin',
                'action' => 'menuGetAdvancedReport'
            ),
            '_order' => 20,
            '_name' => 'Actions_SubmenuPageTitles',
            '_tooltip' =>  false,
        ));
        
        $this->assertCount(1, $menuItems);
        $this->assertEquals($expected, $menuItems['Goals_Goals']);
    }

    public function test_getMetrics_shouldUseDefaultMetrics()
    {
        $this->assertEquals(Metrics::getDefaultMetrics(), $this->basicReport->getMetrics());
    }

    public function test_getMetrics_shouldReturnEmptyArray_IfNoMetricsDefined()
    {
        $this->advancedReport->set('metrics', array());
        $this->assertEquals(array(), $this->advancedReport->getMetrics());
    }

    public function test_getMetrics_shouldFindTranslationsForMetricsAndReturnOnlyTheOnesDefinedInSameOrder()
    {
        $expected = array(
            'nb_visits'  => 'General_ColumnNbVisits',
            'nb_actions' => 'General_ColumnNbActions'
        );
        $this->assertEquals($expected, $this->advancedReport->getMetrics());
    }

    public function test_getProcessedMetrics_shouldReturnConfiguredValue_IfNotAnArrayGivenToPreventDefaultMetrics()
    {
        $this->advancedReport->set('processedMetrics', false);
        $this->assertEquals(false, $this->advancedReport->getProcessedMetrics());
    }

    public function test_getProcessedMetrics_shouldReturnEmptyArray_IfNoMetricsDefined()
    {
        $this->advancedReport->set('processedMetrics', array());
        $this->assertEquals(array(), $this->advancedReport->getProcessedMetrics());
    }

    public function test_getProcessedMetrics_reportShouldUseDefaultProcessedMetrics()
    {
        $this->assertEquals(Metrics::getDefaultProcessedMetrics(), $this->basicReport->getProcessedMetrics());
    }

    public function test_getProcessedMetrics_shouldFindTranslationsForMetricsAndReturnOnlyTheOnesDefinedInSameOrder()
    {
        $expected = array(
            'conversion_rate' => 'General_ColumnConversionRate',
            'bounce_rate'     => 'General_ColumnBounceRate'
        );
        $this->assertEquals($expected, $this->advancedReport->getProcessedMetrics());
    }

    public function test_hasGoalMetrics_shouldBeDisabledByDefault()
    {
        $this->assertFalse($this->advancedReport->hasGoalMetrics());
    }

    public function test_hasGoalMetrics_shouldReturnGoalMetricsProperty()
    {
        $this->advancedReport->set('hasGoalMetrics', true);
        $this->assertTrue($this->advancedReport->hasGoalMetrics());
    }

    public function test_configureReportMetadata_shouldNotAddAReportIfReportIsDisabled()
    {
        $reports = array();
        $this->disabledReport->configureReportMetadata($reports, array());
        $this->assertEquals(array(), $reports);
    }

    public function test_configureReportMetadata_shouldAddAReportIfReportIsEnabled()
    {
        $reports = array();
        $this->basicReport->configureReportMetadata($reports, array());
        $this->assertCount(1, $reports);
    }

    public function test_configureReportMetadata_shouldBuiltStructureAndIncludeOnlyFieldsThatAreSet()
    {
        $reports = array();
        $this->basicReport->configureReportMetadata($reports, array());
        $this->assertEquals(array(
            array(
                'category' => 'Goals_Goals',
                'name' => 'My Custom Report Name',
                'module' => 'TestPlugin',
                'action' => 'getBasicReport',
                'metrics' => array(
                    'nb_visits' => 'General_ColumnNbVisits',
                    'nb_uniq_visitors' => 'General_ColumnNbUniqVisitors',
                    'nb_actions' => 'General_ColumnNbActions',
                ),
                'metricsDocumentation' => array(
                    'nb_visits' => 'General_ColumnNbVisitsDocumentation',
                    'nb_uniq_visitors' => 'General_ColumnNbUniqVisitorsDocumentation',
                    'nb_actions' => 'General_ColumnNbActionsDocumentation',
                ),
                'processedMetrics' => array(
                    'nb_actions_per_visit' => 'General_ColumnActionsPerVisit',
                    'avg_time_on_site' => 'General_ColumnAvgTimeOnSite',
                    'bounce_rate' => 'General_ColumnBounceRate',
                    'conversion_rate' => 'General_ColumnConversionRate',
                ),
                'order' => '20'
            )
        ), $reports);
    }

    public function test_configureReportMetadata_shouldBuiltStructureAllFieldsSet()
    {
        $reports = array();
        $this->advancedReport->configureReportMetadata($reports, array());
        $this->assertEquals(array(
            array(
                'category' => 'Goals_Goals',
                'name' => 'My Custom Report Name',
                'module' => 'TestPlugin',
                'action' => 'getAdvancedReport',
                'parameters' => array(
                    'idGoal' => 1
                ),
                'dimension' => 'Actions_ColumnExitPageURL',
                'documentation' => 'ExampleReportDocumentation',
                'isSubtableReport' => true,
                'metrics' => array(
                    'nb_actions' => 'General_ColumnNbActions',
                    'nb_visits' => 'General_ColumnNbVisits'
                ),
                'metricsDocumentation' => array(
                    'nb_actions' => 'General_ColumnNbActionsDocumentation',
                    'nb_visits' => 'General_ColumnNbVisitsDocumentation',
                ),
                'processedMetrics' => array(
                    'conversion_rate' => 'General_ColumnConversionRate',
                    'bounce_rate' => 'General_ColumnBounceRate',
                ),
                'actionToLoadSubTables' => 'GetBasicReport',
                'constantRowsCount' => true,
                'order' => '20'
            )
        ), $reports);
    }

    public function test_factory_shouldNotFindAReportIfReportExistsButPluginIsNotLoaded()
    {
        $this->unloadAllPlugins();

        $report = Report::factory('ExampleReport', 'getExampleReport');

        $this->assertNull($report);
    }

    public function test_factory_shouldFindAReport_ThatExists()
    {
        $this->loadExampleReportPlugin();

        $module = 'ExampleReport';
        $action = 'getExampleReport';

        $report = Report::factory($module, $action);

        $this->assertInstanceOf('Piwik\Plugins\ExampleReport\Reports\GetExampleReport', $report);
        $this->assertEquals($module, $report->getModule());
        $this->assertEquals($action, $report->getAction());

        // action ucfirst should work as well
        $report = Report::factory($module, ucfirst($action));
        $this->assertInstanceOf('Piwik\Plugins\ExampleReport\Reports\GetExampleReport', $report);
    }

    public function test_factory_shouldNotFindAReport_IfPluginIsActivatedButReportNotExists()
    {
        $this->loadExampleReportPlugin();

        $module = 'ExampleReport';
        $action = 'NotExistingReport';

        $report = Report::factory($module, $action);

        $this->assertNull($report);
    }

    public function test_factory_shouldNotFindAReport_IfPluginIsLoadedButNotActivated()
    {
        PluginManager::getInstance()->loadPlugin('ExampleReport');

        $module = 'ExampleReport';
        $action = 'getExampleReport';

        $report = Report::factory($module, $action);

        $this->assertNull($report);
    }

    public function test_getAllReports_shouldNotFindAReport_IfNoPluginLoaded()
    {
        $this->unloadAllPlugins();

        $report = Report::getAllReports();

        $this->assertEquals(array(), $report);
    }

    public function test_getAllReports_ShouldFindAllAvailableReports()
    {
        $this->loadExampleReportPlugin();
        $this->loadMorePlugins();

        $reports = Report::getAllReports();

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

        foreach ($reports as $report) {
            $this->assertInstanceOf('Piwik\Plugin\Report', $report);
        }
    }

    private function loadExampleReportPlugin()
    {
        PluginManager::getInstance()->loadPlugins(array('ExampleReport'));
    }

    private function loadMorePlugins()
    {
        PluginManager::getInstance()->loadPlugins(array('Actions', 'DevicesDetection', 'CoreVisualizations', 'API', 'Morpheus'));
    }

    private function unloadAllPlugins()
    {
        PluginManager::getInstance()->unloadPlugins();
    }

    private function loadEnglishTranslation()
    {
        Translate::reloadLanguage('en');
    }


}