From 08cfe92f278e987fb43ca6584b1822d61774dceb Mon Sep 17 00:00:00 2001 From: diosmosis <benaka.moorthi@gmail.com> Date: Wed, 17 Apr 2013 08:27:00 +0000 Subject: [PATCH] Fixes #3868, make sure show/hideColumns query params affect metrics when report metadata doesn't provide a translation for metrics, and make sure if no columns are displayed for a report, API.get doesn't fail. --- plugins/API/API.php | 22 +++++++++++-------- .../Integration/OneVisitorTwoVisitsTest.php | 11 ++++++++++ ...itorTwoVisits_showColumns__API.get_day.xml | 6 +++++ 3 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 tests/PHPUnit/Integration/expected/test_OneVisitorTwoVisits_showColumns__API.get_day.xml diff --git a/plugins/API/API.php b/plugins/API/API.php index 23cf814768..5ca4e2156c 100644 --- a/plugins/API/API.php +++ b/plugins/API/API.php @@ -584,14 +584,6 @@ class Piwik_API_API // set metric documentation to default if it's not set $availableReport['metricsDocumentation'] = $this->getDefaultMetricsDocumentation(); } - - // if hide/show columns specified, hide/show metrics & docs - $availableReport['metrics'] = $this->hideShowMetrics($availableReport['metrics']); - $availableReport['processedMetrics'] = $this->hideShowMetrics($availableReport['processedMetrics']); - if (isset($availableReport['metricsDocumentation'])) { - $availableReport['metricsDocumentation'] = - $this->hideShowMetrics($availableReport['metricsDocumentation']); - } } // Some plugins need to add custom metrics after all plugins hooked in @@ -623,6 +615,16 @@ class Piwik_API_API } $availableReport['metrics'] = $cleanedMetrics; + // if hide/show columns specified, hide/show metrics & docs + $availableReport['metrics'] = $this->hideShowMetrics($availableReport['metrics']); + if (isset($availableReport['processedMetrics'])) { + $availableReport['processedMetrics'] = $this->hideShowMetrics($availableReport['processedMetrics']); + } + if (isset($availableReport['metricsDocumentation'])) { + $availableReport['metricsDocumentation'] = + $this->hideShowMetrics($availableReport['metricsDocumentation']); + } + // Remove array elements that are false (to clean up API output) foreach ($availableReport as $attributeName => $attributeValue) { if (empty($attributeValue)) { @@ -1057,8 +1059,10 @@ class Piwik_API_API $meta = Piwik_API_API::getInstance()->getReportMetadata($idSite, $period, $date); foreach ($meta as $reportMeta) { // scan all *.get reports - if ($reportMeta['action'] == 'get' && !isset($reportMeta['parameters']) + if ($reportMeta['action'] == 'get' + && !isset($reportMeta['parameters']) && $reportMeta['module'] != 'API' + && !empty($reportMeta['metrics']) ) { $plugin = $reportMeta['module']; foreach ($reportMeta['metrics'] as $column => $columnTranslation) { diff --git a/tests/PHPUnit/Integration/OneVisitorTwoVisitsTest.php b/tests/PHPUnit/Integration/OneVisitorTwoVisitsTest.php index f8d1468e39..9bd0eb113d 100755 --- a/tests/PHPUnit/Integration/OneVisitorTwoVisitsTest.php +++ b/tests/PHPUnit/Integration/OneVisitorTwoVisitsTest.php @@ -136,6 +136,17 @@ class Test_Piwik_Integration_OneVisitorTwoVisits extends IntegrationTestCase 'avg_time_generation,nb_hits_with_time_generation', 'expanded' => '1' ))), + + // test showColumns on API.get + array('API.get', array( + 'idSite' => $idSite, + 'date' => $dateTime, + 'periods' => 'day', + 'testSuffix' => '_showColumns', + 'otherRequestParameters' => array( + 'showColumns' => 'nb_uniq_visitors,nb_pageviews,bounce_rate' + ) + )), ); } diff --git a/tests/PHPUnit/Integration/expected/test_OneVisitorTwoVisits_showColumns__API.get_day.xml b/tests/PHPUnit/Integration/expected/test_OneVisitorTwoVisits_showColumns__API.get_day.xml new file mode 100644 index 0000000000..cdd370d354 --- /dev/null +++ b/tests/PHPUnit/Integration/expected/test_OneVisitorTwoVisits_showColumns__API.get_day.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8" ?> +<result> + <nb_uniq_visitors>1</nb_uniq_visitors> + <bounce_rate>50%</bounce_rate> + <nb_pageviews>4</nb_pageviews> +</result> \ No newline at end of file -- GitLab