Newer
Older
<?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\Tests\Integration;
use Piwik\API\Proxy;
use Piwik\Plugin\Report;
use Piwik\Plugins\ExampleReport\Reports\GetExampleReport;
use Piwik\Plugins\Actions\Columns\ExitPageUrl;
use Piwik\Piwik;
use Piwik\Plugins\ExampleTracker\Columns\ExampleDimension;
use Piwik\Plugins\Referrers\Columns\Keyword;
Thomas Steur
a validé
use Piwik\Plugin\ReportsProvider;
use Piwik\Report\ReportWidgetFactory;
use Piwik\Translate;
use Piwik\Plugin\Manager as PluginManager;
Thomas Steur
a validé
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Widget\WidgetsList;
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->categoryId = 'Goals_Goals';
$this->actionToLoadSubTables = 'invalidReport';
}
}
class GetAdvancedReport extends GetBasicReport
{
protected function init()
{
parent::init();
$this->action = 'getAdvancedReport';
$this->subcategoryId = '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 configureWidgets(WidgetsList $widgetsList, ReportWidgetFactory $factory)
{
$widget = $factory->createWidget()->setName('Actions_WidgetPageTitlesFollowingSearch');
$widgetsList->addWidgetConfig($widget);
}
public function set($param, $value)
{
$this->$param = $value;
}
}
class GetDisabledReport extends GetBasicReport
{
public function isEnabled()
{
return false;
}
}
class ReportTest extends IntegrationTestCase
private $exampleReport;
/**
* @var GetDisabledReport
*/
private $disabledReport;
/**
* @var GetBasicReport
*/
private $basicReport;
/**
* @var GetAdvancedReport
*/
private $advancedReport;
public function setUp()
{
parent::setUp();
Thomas Steur
a validé
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();
Proxy::unsetInstance();
public function tearDown()
Thomas Steur
a validé
unset($_GET['idSite']);
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
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_getCategory_shouldReturnTranslatedCategory()
{
$this->assertEquals('Goals_Goals', $this->advancedReport->getCategoryId());
}
public function test_getMetrics_shouldUseDefaultMetrics()
$this->assertEquals(Metrics::getDefaultMetrics(), $this->basicReport->getMetrics());
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
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',
'nb_users' => 'General_ColumnNbUsersDocumentation',
'nb_actions_per_visit' => 'General_ColumnActionsPerVisitDocumentation',
'avg_time_on_site' => 'General_ColumnAvgTimeOnSiteDocumentation',
'bounce_rate' => 'General_ColumnBounceRateDocumentation',
'conversion_rate' => 'General_ColumnConversionRateDocumentation',
),
'processedMetrics' => array(
'nb_actions_per_visit' => 'General_ColumnActionsPerVisit',
'avg_time_on_site' => 'General_ColumnAvgTimeOnSite',
'bounce_rate' => 'General_ColumnBounceRate',
'conversion_rate' => 'General_ColumnConversionRate',
),
'actionToLoadSubTables' => 'invalidReport',
'order' => 20,
'subcategory' => null
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
)
), $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',
'conversion_rate' => 'General_ColumnConversionRateDocumentation',
'bounce_rate' => 'General_ColumnBounceRateDocumentation',
),
'processedMetrics' => array(
'conversion_rate' => 'General_ColumnConversionRate',
'bounce_rate' => 'General_ColumnBounceRate',
),
'actionToLoadSubTables' => 'GetBasicReport',
'constantRowsCount' => true,
'order' => '20',
'subcategory' => 'Actions_SubmenuPageTitles'
)
), $reports);
}
public function test_factory_shouldCreateReport_WhenActionNameUsed()
{
$this->loadExampleReportPlugin();
$module = 'ExampleReport';
$action = 'getExampleReport';
Thomas Steur
a validé
$report = ReportsProvider::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
Thomas Steur
a validé
$report = ReportsProvider::factory($module, ucfirst($action));
$this->assertInstanceOf('Piwik\Plugins\ExampleReport\Reports\GetExampleReport', $report);
$this->assertEquals($module, $report->getModule());
$this->assertEquals($action, $report->getAction());
Thomas Steur
a validé
}
public function test_getSubtableDimension_ShouldReturnNullIfNoSubtableActionExists()
{
$report = new GetExampleReport();
$this->assertNull($report->getSubtableDimension());
}
public function test_getSubtableDimension_ShouldReturnNullIfSubtableActionIsInvalid()
{
$report = new GetBasicReport();
$this->assertNull($report->getSubtableDimension());
}
public function test_getSubtableDimension_ShouldReturnCorrectDimensionIfSubtableActionIsDefinedAndCorrect()
{
PluginManager::getInstance()->loadPlugins(array('Referrers'));
Thomas Steur
a validé
$report = ReportsProvider::factory('Referrers', 'getSearchEngines');
$subtableDimension = $report->getSubtableDimension();
$this->assertNotNull($subtableDimension);
$this->assertInstanceOf("Piwik\\Plugins\\Referrers\\Columns\\Keyword", $subtableDimension);
}
public function test_fetch_ShouldUseCorrectApiUrl()
{
PluginManager::getInstance()->loadPlugins(array('API', 'ExampleReport'));
$proxyMock = $this->getMockBuilder('stdClass')->setMethods(array('call', '__construct'))->getMock();
$proxyMock->expects($this->once())->method('call')->with(
'\\Piwik\\Plugins\\ExampleReport\\API', 'getExampleReport', array(
'idSite' => 1,
'date' => '2012-01-02',
'format' => 'original',
'module' => 'API',
'format_metrics' => 'bc',
'serialize' => '0'
)
)->willReturn("result");
Proxy::setSingletonInstance($proxyMock);
$report = new GetExampleReport();
$result = $report->fetch(array('idSite' => 1, 'date' => '2012-01-02'));
$this->assertEquals("result", $result);
}
public function test_fetchSubtable_ShouldUseCorrectApiUrl()
{
PluginManager::getInstance()->loadPlugins(array('API', 'Referrers'));
$proxyMock = $this->getMockBuilder('stdClass')->setMethods(array('call', '__construct'))->getMock();
$proxyMock->expects($this->once())->method('call')->with(
'\\Piwik\\Plugins\\Referrers\\API', 'getSearchEnginesFromKeywordId', array(
'idSubtable' => 23,
'idSite' => 1,
'date' => '2012-01-02',
'format' => 'original',
'module' => 'API',
'format_metrics' => 'bc',
'serialize' => '0'
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
)
)->willReturn("result");
Proxy::setSingletonInstance($proxyMock);
$report = new \Piwik\Plugins\Referrers\Reports\GetKeywords();
$result = $report->fetchSubtable(23, array('idSite' => 1, 'date' => '2012-01-02'));
$this->assertEquals("result", $result);
}
public function test_getForDimension_ShouldReturnCorrectInstanceTypeIfAssociatedReportExists()
{
PluginManager::getInstance()->loadPlugins(array('Referrers'));
$report = Report::getForDimension(new Keyword());
$this->assertInstanceOf("Piwik\\Plugins\\Referrers\\Reports\\GetKeywords", $report);
}
public function test_getForDimension_ShouldReturnNullIfNoReportExistsForDimension()
{
$this->loadExampleReportPlugin();
$this->loadMorePlugins();
$report = Report::getForDimension(new ExampleDimension());
$this->assertNull($report);
}
public function test_getForDimension_ShouldReturnNullIfReportPluginNotLoaded()
{
PluginManager::getInstance()->loadPlugins(array());
$report = Report::getForDimension(new Keyword());
$this->assertNull($report);
}
private function loadExampleReportPlugin()
{
Thomas Steur
a validé
PluginManager::getInstance()->loadPlugins(array('ExampleReport'));
}
private function loadMorePlugins()
{
Thomas Steur
a validé
PluginManager::getInstance()->loadPlugins(array('Actions', 'DevicesDetection', 'CoreVisualizations', 'API', 'Morpheus'));
}
private function unloadAllPlugins()
{
PluginManager::getInstance()->unloadPlugins();
}