From 3b962d95d59ee68215d10a088cf08935521dd54a Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@googlemail.com>
Date: Sat, 9 Aug 2014 17:02:04 +0200
Subject: [PATCH] fixes #5951 throw an exception in getRowEvolution if a report
 is request that does not have a dimension

---
 CHANGELOG.md                           |  1 +
 plugins/API/RowEvolution.php           |  4 +++
 plugins/API/tests/RowEvolutionTest.php | 44 ++++++++++++++++++++++++++
 3 files changed, 49 insertions(+)
 create mode 100644 plugins/API/tests/RowEvolutionTest.php

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1adb5b64a4..ab36db73d8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ This is a changelog for Piwik platform developers. All changes for our HTTP API'
 * The [settings](http://developer.piwik.org/guides/piwik-configuration) API will receive the actual entered value and will no longer convert characters like `&` to `&amp;`. If you still want this behavior - for instance to prevent XSS - you can define a filter by setting the `transform` property like this:
   `$setting->transform = function ($value) { return Common::sanitizeInputValue($value); }`
 * Config setting `disable_merged_assets` moved from `Debug` section to `Development`. The updater will automatically change the section for you.
+* `API.getRowEvolution` will throw an exception if a report is requested that does not have a dimension, for instance `VisitsSummary.get`. This is a fix as an invalid format was returned before see [#5951](https://github.com/piwik/piwik/issues/5951)
 
 ### Deprecations
 The following events are considered as deprecated and the new structure should be used in the future. We have not scheduled when those events will be removed but probably in Piwik 3.0 which is not scheduled yet and won't be soon. New features will be added only to the new classes.
diff --git a/plugins/API/RowEvolution.php b/plugins/API/RowEvolution.php
index 84bc431c47..7a5290c44f 100644
--- a/plugins/API/RowEvolution.php
+++ b/plugins/API/RowEvolution.php
@@ -331,6 +331,10 @@ class RowEvolution
             $metrics = $metrics + $reportMetadata['processedMetrics'];
         }
 
+        if (empty($reportMetadata['dimension'])) {
+            throw new Exception(sprintf('Reports like %s.%s which do not have a dimension are not supported by row evolution', $apiModule, $apiAction));
+        }
+
         $dimension = $reportMetadata['dimension'];
 
         return compact('metrics', 'dimension');
diff --git a/plugins/API/tests/RowEvolutionTest.php b/plugins/API/tests/RowEvolutionTest.php
new file mode 100644
index 0000000000..b341efd91a
--- /dev/null
+++ b/plugins/API/tests/RowEvolutionTest.php
@@ -0,0 +1,44 @@
+<?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;
+use Piwik\Plugins\API\RowEvolution;
+use Piwik\Tests\Fixture;
+
+/**
+ * @group API
+ * @group RowEvolutionTest
+ * @group Database
+ */
+class RowEvolutionTest extends \DatabaseTestCase
+{
+
+    public function setUp()
+    {
+        parent::setUp();
+        Fixture::createWebsite('2014-01-01 00:00:00');
+    }
+
+    /**
+     * @expectedException \Exception
+     * @expectedExceptionMessage Reports like VisitsSummary.get which do not have a dimension are not supported by row evolution
+     */
+    public function test_getRowEvolution_shouldTriggerAnException_IfReportHasNoDimension()
+    {
+        $rowEvolution = new RowEvolution();
+        $rowEvolution->getRowEvolution(1, 'day', 'last7', 'VisitsSummary', 'get');
+    }
+
+    public function test_getRowEvolution_shouldNotTriggerAnException_IfReportHasADimension()
+    {
+        $rowEvolution = new RowEvolution();
+        $table = $rowEvolution->getRowEvolution(1, 'day', 'last7', 'Actions', 'getPageUrls');
+        $this->assertNotEmpty($table);
+    }
+
+}
-- 
GitLab