Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible error when generating reports for custom dimensions #18614

Merged
merged 4 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public static function createDataTableFromArchive($recordName, $idSite, $period,
}

$archive = Archive::build($idSite, $period, $date, $segment, $_restrictSitesToLogin = false);
if ($idSubtable === false) {
if ($idSubtable === false || $idSubtable === '') {
$idSubtable = null;
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/CustomDimensions/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ class API extends \Piwik\Plugin\API
* @param bool|false $segment
* @param bool|false $expanded
* @param bool|false $flat
* @param int|null $idSubtable
* @param int|false $idSubtable
* @return DataTable|DataTable\Map
* @throws \Exception
*/
public function getCustomDimension($idDimension, $idSite, $period, $date, $segment = false, $expanded = false, $flat = false, $idSubtable = null)
public function getCustomDimension($idDimension, $idSite, $period, $date, $segment = false, $expanded = false, $flat = false, $idSubtable = false)
{
Piwik::checkUserHasViewAccess($idSite);

Expand All @@ -62,7 +62,7 @@ public function getCustomDimension($idDimension, $idSite, $period, $date, $segme

$dataTable = Archive::createDataTableFromArchive($record, $idSite, $period, $date, $segment, $expanded, $flat, $idSubtable);

if (isset($idSubtable) && $dataTable->getRowsCount()) {
if (!empty($idSubtable) && $dataTable->getRowsCount()) {
$parentTable = Archive::createDataTableFromArchive($record, $idSite, $period, $date, $segment);
foreach ($parentTable->getRows() as $row) {
if ($row->getIdSubDataTable() == $idSubtable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
*/
namespace Piwik\Plugins\CustomDimensions\tests\Fixtures;

use Piwik\Context;
use Piwik\Date;
use Piwik\Plugins\CustomDimensions\CustomDimensions;
use Piwik\Plugins\CustomDimensions\Dao\Configuration;
use Piwik\Plugins\CustomDimensions\Dimension\Extraction;
use Piwik\Plugins\Goals;
use Piwik\Plugins\ScheduledReports\API as APIScheduledReports;
use Piwik\Plugins\ScheduledReports\ScheduledReports;
use Piwik\ReportRenderer;
use Piwik\Tests\Framework\Fixture;
use Piwik\Plugin;
use Piwik\Tracker\Cache;

/**
Expand All @@ -32,6 +35,7 @@ public function setUp(): void
$this->setUpWebsites();
$this->addGoals();
$this->configureSomeDimensions();
$this->configureScheduledReport();
$this->trackFirstVisit();
$this->trackSecondVisit();
$this->trackThirdVisit();
Expand Down Expand Up @@ -77,6 +81,23 @@ private function configureSomeDimensions()
Cache::clearCacheGeneral();
}

protected function configureScheduledReport()
{
// Context change is needed, as adding the custom dimensions reports looks for the idSite in the request params
Context::changeIdSite(1, function() {
APIScheduledReports::getInstance()->addReport(
$idSite = 1,
'ScheduledReport',
'month',
0,
ScheduledReports::EMAIL_TYPE,
ReportRenderer::PDF_FORMAT,
['VisitsSummary_get', 'CustomDimensions_getCustomDimension_idDimension--1', 'CustomDimensions_getCustomDimension_idDimension--2'],
[ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_TABLES_AND_GRAPHS]
);
});
}

protected function trackFirstVisit()
{
$t = self::getTracker($this->idSite, $this->dateTime, $defaultInit = true);
Expand Down
22 changes: 22 additions & 0 deletions plugins/CustomDimensions/tests/System/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

namespace Piwik\Plugins\CustomDimensions\tests\System;

use Piwik\Context;
use Piwik\Plugins\CustomDimensions\tests\Fixtures\TrackVisitsWithCustomDimensionsFixture;
use Piwik\ReportRenderer;
use Piwik\Tests\Framework\TestCase\SystemTestCase;

/**
Expand Down Expand Up @@ -260,6 +262,26 @@ public function getApiForTesting()
return $apiToTest;
}

public function testScheduledReport()
{
// Context change is needed, as otherwise the customdimension reports are not available
Context::changeIdSite(1, function(){
$this->runApiTests(['ScheduledReports.generateReport'], [
'idSite' => 1,
'date' => self::$fixture->dateTime,
'periods' => ['year'],
'format' => 'original',
'fileExtension' => 'pdf',
'otherRequestParameters' => [
'idReport' => 1,
'reportFormat' => ReportRenderer::PDF_FORMAT,
'outputType' => \Piwik\Plugins\ScheduledReports\API::OUTPUT_RETURN,
'serialize' => 0,
],
]);
});
}

public static function getOutputPrefix()
{
return '';
Expand Down
Binary file not shown.