From 8d0205e32a430d4bb028ec14877ff7a7d7b5d22e Mon Sep 17 00:00:00 2001
From: diosmosis <benaka@piwik.pro>
Date: Sat, 20 Sep 2014 13:14:13 -0700
Subject: [PATCH] Refs #6078, order pivot table columns by sum, descending and
 aggregate cut-off columns into 'Others' column.

---
 core/DataTable/Filter/PivotByDimension.php    |  47 ++++---
 ...taTableMaps__Referrers.getKeywords_day.xml |  12 +-
 ...ueryParams__Referrers.getKeywords_week.xml |  12 +-
 ...mnLimiting__Referrers.getKeywords_week.xml |  12 +-
 ...hCsvOutput__Referrers.getKeywords_week.csv | Bin 440 -> 440 bytes
 ...sonOutput__Referrers.getKeywords_week.json |   2 +-
 ...btableIsSelf__Actions.getPageUrls_week.xml | 130 +++++++++---------
 ...PivotTable__Referrers.getKeywords_week.xml |  12 +-
 ...sNotLoaded__Referrers.getKeywords_week.xml |  48 +++----
 ...PivotTable__Referrers.getKeywords_week.xml |  48 +++----
 10 files changed, 168 insertions(+), 155 deletions(-)

diff --git a/core/DataTable/Filter/PivotByDimension.php b/core/DataTable/Filter/PivotByDimension.php
index 902c03f1b5..ba4f0220f6 100644
--- a/core/DataTable/Filter/PivotByDimension.php
+++ b/core/DataTable/Filter/PivotByDimension.php
@@ -17,6 +17,7 @@ use Piwik\DataTable\BaseFilter;
 use Piwik\DataTable\Row;
 use Piwik\Log;
 use Piwik\Metrics;
+use Piwik\Piwik;
 use Piwik\Plugin\Report;
 use Piwik\Plugin\Segment;
 
@@ -211,22 +212,10 @@ class PivotByDimension extends BaseFilter
 
         Log::debug("PivotByDimension::%s: pivoted columns set: %s", __FUNCTION__, $columnSet);
 
-        // limit columns
-        if ($this->pivotByColumnLimit > 0) {
-            arsort($columnSet);
-            $columnSet = array_slice($columnSet, 0, $this->pivotByColumnLimit, $preserveKeys = true);
-        }
-
-        // sort columns by name (to ensure deterministic ordering)
-        ksort($columnSet);
+        $others = Piwik::translate('General_Others');
+        $defaultRow = $this->getPivotTableDefaultRowFromColumnSummary($columnSet, $others);
 
-        // remove column sums from array so it can be used as a default row
-        $columnSet = array_map(function () { return false; }, $columnSet);
-
-        // make sure label column is first
-        $columnSet = array_merge(array('label' => false), $columnSet);
-
-        Log::debug("PivotByDimension::%s: processed pivoted columns: %s", __FUNCTION__, $columnSet);
+        Log::debug("PivotByDimension::%s: processed pivoted columns: %s", __FUNCTION__, $defaultRow);
 
         // post process pivoted datatable
         foreach ($table->getRows() as $row) {
@@ -234,11 +223,13 @@ class PivotByDimension extends BaseFilter
             $row->removeSubtable();
             $row->deleteMetadata('idsubdatatable_in_db');
 
-            // use default row to ensure column ordering and add missing columns
-            $orderedColumns = $columnSet;
+            // use default row to ensure column ordering and add missing columns/aggregate cut-off columns
+            $orderedColumns = $defaultRow;
             foreach ($row->getColumns() as $name => $value) {
                 if (isset($orderedColumns[$name])) {
                     $orderedColumns[$name] = $value;
+                } else {
+                    $orderedColumns[$others] += $value;
                 }
             }
             $row->setColumns($orderedColumns);
@@ -432,6 +423,28 @@ class PivotByDimension extends BaseFilter
         return $params;
     }
 
+    private function getPivotTableDefaultRowFromColumnSummary($columnSet, $othersRowLabel)
+    {
+        // sort columns by sum (to ensure deterministic ordering)
+        arsort($columnSet);
+
+        // limit columns if necessary (adding aggregate Others column at end)
+        if ($this->pivotByColumnLimit > 0
+            && count($columnSet) > $this->pivotByColumnLimit
+        ) {
+            $columnSet = array_slice($columnSet, 0, $this->pivotByColumnLimit - 1, $preserveKeys = true);
+            $columnSet[$othersRowLabel] = 0;
+        }
+
+        // remove column sums from array so it can be used as a default row
+        $columnSet = array_map(function () { return false; }, $columnSet);
+
+        // make sure label column is first
+        $columnSet = array_merge(array('label' => false), $columnSet);
+
+        return $columnSet;
+    }
+
     /**
      * Returns true if pivoting by subtable is supported for a report. Will return true if the report
      * has a subtable dimension and if the subtable dimension is different than the report's dimension.
diff --git a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamPlaysNiceWithDataTableMaps__Referrers.getKeywords_day.xml b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamPlaysNiceWithDataTableMaps__Referrers.getKeywords_day.xml
index a60b9519fb..744fcf397e 100644
--- a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamPlaysNiceWithDataTableMaps__Referrers.getKeywords_day.xml
+++ b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamPlaysNiceWithDataTableMaps__Referrers.getKeywords_day.xml
@@ -6,39 +6,39 @@
 		<result date="2010-01-03">
 			<row>
 				<col name="label">this search term</col>
-				<col name="Melbourne, Victoria, Australia">2</col>
 				<col name="Toronto, Ontario, Canada">0</col>
 				<col name="Yokohama, Kanagawa, Japan">1</col>
+				<col name="Melbourne, Victoria, Australia">2</col>
 			</row>
 			<row>
 				<col name="label">search term 2</col>
-				<col name="Melbourne, Victoria, Australia">0</col>
 				<col name="Toronto, Ontario, Canada">0</col>
 				<col name="Yokohama, Kanagawa, Japan">2</col>
+				<col name="Melbourne, Victoria, Australia">0</col>
 			</row>
 			<row>
 				<col name="label">search term 3</col>
-				<col name="Melbourne, Victoria, Australia">0</col>
 				<col name="Toronto, Ontario, Canada">2</col>
 				<col name="Yokohama, Kanagawa, Japan">0</col>
+				<col name="Melbourne, Victoria, Australia">0</col>
 			</row>
 			<row>
 				<col name="label">search term 4</col>
-				<col name="Melbourne, Victoria, Australia">0</col>
 				<col name="Toronto, Ontario, Canada">2</col>
 				<col name="Yokohama, Kanagawa, Japan">0</col>
+				<col name="Melbourne, Victoria, Australia">0</col>
 			</row>
 			<row>
 				<col name="label">that search term</col>
-				<col name="Melbourne, Victoria, Australia">2</col>
 				<col name="Toronto, Ontario, Canada">0</col>
 				<col name="Yokohama, Kanagawa, Japan">0</col>
+				<col name="Melbourne, Victoria, Australia">2</col>
 			</row>
 			<row>
 				<col name="label">search term 1</col>
-				<col name="Melbourne, Victoria, Australia">0</col>
 				<col name="Toronto, Ontario, Canada">0</col>
 				<col name="Yokohama, Kanagawa, Japan">1</col>
+				<col name="Melbourne, Victoria, Australia">0</col>
 			</row>
 		</result>
 		<result date="2010-01-04" />
diff --git a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamPlaysNiceWithOtherQueryParams__Referrers.getKeywords_week.xml b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamPlaysNiceWithOtherQueryParams__Referrers.getKeywords_week.xml
index 777020b15e..05553ef4f8 100644
--- a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamPlaysNiceWithOtherQueryParams__Referrers.getKeywords_week.xml
+++ b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamPlaysNiceWithOtherQueryParams__Referrers.getKeywords_week.xml
@@ -2,32 +2,32 @@
 <result>
 	<row>
 		<label>this search term</label>
-		<Bing>0</Bing>
 		<Google>1</Google>
+		<Bing>0</Bing>
 	</row>
 	<row>
 		<label>search term 2</label>
-		<Bing>0</Bing>
 		<Google>0</Google>
+		<Bing>0</Bing>
 	</row>
 	<row>
 		<label>search term 3</label>
-		<Bing>0</Bing>
 		<Google>1</Google>
+		<Bing>0</Bing>
 	</row>
 	<row>
 		<label>search term 4</label>
-		<Bing>1</Bing>
 		<Google>0</Google>
+		<Bing>1</Bing>
 	</row>
 	<row>
 		<label>that search term</label>
-		<Bing>0</Bing>
 		<Google>1</Google>
+		<Bing>0</Bing>
 	</row>
 	<row>
 		<label>search term 1</label>
-		<Bing>1</Bing>
 		<Google>0</Google>
+		<Bing>1</Bing>
 	</row>
 </result>
\ No newline at end of file
diff --git a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithColumnLimiting__Referrers.getKeywords_week.xml b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithColumnLimiting__Referrers.getKeywords_week.xml
index 70bcfeed7d..4635e78f76 100644
--- a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithColumnLimiting__Referrers.getKeywords_week.xml
+++ b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithColumnLimiting__Referrers.getKeywords_week.xml
@@ -3,31 +3,31 @@
 	<row>
 		<col name="label">this search term</col>
 		<col name="Toronto, Ontario, Canada">0</col>
-		<col name="Yokohama, Kanagawa, Japan">1</col>
+		<col name="Others">3</col>
 	</row>
 	<row>
 		<col name="label">search term 2</col>
 		<col name="Toronto, Ontario, Canada">0</col>
-		<col name="Yokohama, Kanagawa, Japan">2</col>
+		<col name="Others">2</col>
 	</row>
 	<row>
 		<col name="label">search term 3</col>
 		<col name="Toronto, Ontario, Canada">2</col>
-		<col name="Yokohama, Kanagawa, Japan">0</col>
+		<col name="Others">0</col>
 	</row>
 	<row>
 		<col name="label">search term 4</col>
 		<col name="Toronto, Ontario, Canada">2</col>
-		<col name="Yokohama, Kanagawa, Japan">0</col>
+		<col name="Others">0</col>
 	</row>
 	<row>
 		<col name="label">that search term</col>
 		<col name="Toronto, Ontario, Canada">0</col>
-		<col name="Yokohama, Kanagawa, Japan">0</col>
+		<col name="Others">2</col>
 	</row>
 	<row>
 		<col name="label">search term 1</col>
 		<col name="Toronto, Ontario, Canada">0</col>
-		<col name="Yokohama, Kanagawa, Japan">1</col>
+		<col name="Others">1</col>
 	</row>
 </result>
\ No newline at end of file
diff --git a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithCsvOutput__Referrers.getKeywords_week.csv b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithCsvOutput__Referrers.getKeywords_week.csv
index 9b91feb5eead3c6d577217765e389e2bf3b99fd6..0536708ea8d46329d11063f42fe3dfce86e818ef 100644
GIT binary patch
delta 92
zcmdnNyn~th|GylDM1~}WREC_1zWEb*oF?v3VKrdTVKAJ`%P7xiG+CEXo!1!1Hv(di
cxWQyWMrlqHn8@T<M$O3_j4FJFP<aRq01Z14!~g&Q

delta 94
zcmdnNyn~th|GylDM1~}WREC_1mCh4+@+R(4;WT2<VK4w<!^wh-(!4MpNW=&zB0rgf
aQH9e4s>Wz?ETiUR14b1-L#PN?Cj$U2vJxTy

diff --git a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithJsonOutput__Referrers.getKeywords_week.json b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithJsonOutput__Referrers.getKeywords_week.json
index 3ffebb1d65..9b82ca037a 100644
--- a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithJsonOutput__Referrers.getKeywords_week.json
+++ b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithJsonOutput__Referrers.getKeywords_week.json
@@ -1 +1 @@
-[{"label":"this search term","Melbourne, Victoria, Australia":2,"Toronto, Ontario, Canada":false,"Yokohama, Kanagawa, Japan":1},{"label":"search term 2","Melbourne, Victoria, Australia":false,"Toronto, Ontario, Canada":false,"Yokohama, Kanagawa, Japan":2},{"label":"search term 3","Melbourne, Victoria, Australia":false,"Toronto, Ontario, Canada":2,"Yokohama, Kanagawa, Japan":false},{"label":"search term 4","Melbourne, Victoria, Australia":false,"Toronto, Ontario, Canada":2,"Yokohama, Kanagawa, Japan":false},{"label":"that search term","Melbourne, Victoria, Australia":2,"Toronto, Ontario, Canada":false,"Yokohama, Kanagawa, Japan":false},{"label":"search term 1","Melbourne, Victoria, Australia":false,"Toronto, Ontario, Canada":false,"Yokohama, Kanagawa, Japan":1}]
\ No newline at end of file
+[{"label":"this search term","Toronto, Ontario, Canada":false,"Yokohama, Kanagawa, Japan":1,"Melbourne, Victoria, Australia":2},{"label":"search term 2","Toronto, Ontario, Canada":false,"Yokohama, Kanagawa, Japan":2,"Melbourne, Victoria, Australia":false},{"label":"search term 3","Toronto, Ontario, Canada":2,"Yokohama, Kanagawa, Japan":false,"Melbourne, Victoria, Australia":false},{"label":"search term 4","Toronto, Ontario, Canada":2,"Yokohama, Kanagawa, Japan":false,"Melbourne, Victoria, Australia":false},{"label":"that search term","Toronto, Ontario, Canada":false,"Yokohama, Kanagawa, Japan":false,"Melbourne, Victoria, Australia":2},{"label":"search term 1","Toronto, Ontario, Canada":false,"Yokohama, Kanagawa, Japan":1,"Melbourne, Victoria, Australia":false}]
\ No newline at end of file
diff --git a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithReportWhoseSubtableIsSelf__Actions.getPageUrls_week.xml b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithReportWhoseSubtableIsSelf__Actions.getPageUrls_week.xml
index 45bd7630ea..5697f732c4 100644
--- a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithReportWhoseSubtableIsSelf__Actions.getPageUrls_week.xml
+++ b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotByParamWorksWithReportWhoseSubtableIsSelf__Actions.getPageUrls_week.xml
@@ -2,122 +2,122 @@
 <result>
 	<row>
 		<col name="label">0</col>
-		<col name="/0">1</col>
-		<col name="/1">1</col>
-		<col name="/10">0</col>
-		<col name="/11">0</col>
-		<col name="/12">0</col>
-		<col name="/13">0</col>
+		<col name="/index">1</col>
 		<col name="/14">0</col>
+		<col name="/13">0</col>
+		<col name="/12">0</col>
+		<col name="/11">0</col>
 		<col name="/15">0</col>
 		<col name="/16">0</col>
-		<col name="/17">0</col>
-		<col name="/18">0</col>
 		<col name="/19">0</col>
-		<col name="/2">1</col>
+		<col name="/18">0</col>
+		<col name="/17">0</col>
+		<col name="/10">0</col>
+		<col name="/9">0</col>
 		<col name="/3">1</col>
+		<col name="/2">1</col>
+		<col name="/1">1</col>
 		<col name="/4">0</col>
 		<col name="/5">0</col>
-		<col name="/6">0</col>
-		<col name="/7">0</col>
 		<col name="/8">0</col>
-		<col name="/9">0</col>
-		<col name="/index">1</col>
+		<col name="/7">0</col>
+		<col name="/6">0</col>
+		<col name="/0">1</col>
 	</row>
 	<row>
 		<col name="label">1</col>
-		<col name="/0">0</col>
-		<col name="/1">0</col>
-		<col name="/10">0</col>
-		<col name="/11">0</col>
-		<col name="/12">0</col>
-		<col name="/13">0</col>
+		<col name="/index">1</col>
 		<col name="/14">0</col>
+		<col name="/13">0</col>
+		<col name="/12">0</col>
+		<col name="/11">0</col>
 		<col name="/15">0</col>
 		<col name="/16">0</col>
-		<col name="/17">0</col>
-		<col name="/18">0</col>
 		<col name="/19">0</col>
-		<col name="/2">0</col>
+		<col name="/18">0</col>
+		<col name="/17">0</col>
+		<col name="/10">0</col>
+		<col name="/9">0</col>
 		<col name="/3">0</col>
+		<col name="/2">0</col>
+		<col name="/1">0</col>
 		<col name="/4">1</col>
 		<col name="/5">1</col>
-		<col name="/6">1</col>
-		<col name="/7">1</col>
 		<col name="/8">0</col>
-		<col name="/9">0</col>
-		<col name="/index">1</col>
+		<col name="/7">1</col>
+		<col name="/6">1</col>
+		<col name="/0">0</col>
 	</row>
 	<row>
 		<col name="label">2</col>
-		<col name="/0">0</col>
-		<col name="/1">0</col>
-		<col name="/10">1</col>
-		<col name="/11">1</col>
-		<col name="/12">0</col>
-		<col name="/13">0</col>
+		<col name="/index">1</col>
 		<col name="/14">0</col>
+		<col name="/13">0</col>
+		<col name="/12">0</col>
+		<col name="/11">1</col>
 		<col name="/15">0</col>
 		<col name="/16">0</col>
-		<col name="/17">0</col>
-		<col name="/18">0</col>
 		<col name="/19">0</col>
-		<col name="/2">0</col>
+		<col name="/18">0</col>
+		<col name="/17">0</col>
+		<col name="/10">1</col>
+		<col name="/9">1</col>
 		<col name="/3">0</col>
+		<col name="/2">0</col>
+		<col name="/1">0</col>
 		<col name="/4">0</col>
 		<col name="/5">0</col>
-		<col name="/6">0</col>
-		<col name="/7">0</col>
 		<col name="/8">1</col>
-		<col name="/9">1</col>
-		<col name="/index">1</col>
+		<col name="/7">0</col>
+		<col name="/6">0</col>
+		<col name="/0">0</col>
 	</row>
 	<row>
 		<col name="label">3</col>
-		<col name="/0">0</col>
-		<col name="/1">0</col>
-		<col name="/10">0</col>
-		<col name="/11">0</col>
-		<col name="/12">1</col>
-		<col name="/13">1</col>
+		<col name="/index">1</col>
 		<col name="/14">1</col>
+		<col name="/13">1</col>
+		<col name="/12">1</col>
+		<col name="/11">0</col>
 		<col name="/15">1</col>
 		<col name="/16">0</col>
-		<col name="/17">0</col>
-		<col name="/18">0</col>
 		<col name="/19">0</col>
-		<col name="/2">0</col>
+		<col name="/18">0</col>
+		<col name="/17">0</col>
+		<col name="/10">0</col>
+		<col name="/9">0</col>
 		<col name="/3">0</col>
+		<col name="/2">0</col>
+		<col name="/1">0</col>
 		<col name="/4">0</col>
 		<col name="/5">0</col>
-		<col name="/6">0</col>
-		<col name="/7">0</col>
 		<col name="/8">0</col>
-		<col name="/9">0</col>
-		<col name="/index">1</col>
+		<col name="/7">0</col>
+		<col name="/6">0</col>
+		<col name="/0">0</col>
 	</row>
 	<row>
 		<col name="label">4</col>
-		<col name="/0">0</col>
-		<col name="/1">0</col>
-		<col name="/10">0</col>
-		<col name="/11">0</col>
-		<col name="/12">0</col>
-		<col name="/13">0</col>
+		<col name="/index">1</col>
 		<col name="/14">0</col>
+		<col name="/13">0</col>
+		<col name="/12">0</col>
+		<col name="/11">0</col>
 		<col name="/15">0</col>
 		<col name="/16">1</col>
-		<col name="/17">1</col>
-		<col name="/18">1</col>
 		<col name="/19">1</col>
-		<col name="/2">0</col>
+		<col name="/18">1</col>
+		<col name="/17">1</col>
+		<col name="/10">0</col>
+		<col name="/9">0</col>
 		<col name="/3">0</col>
+		<col name="/2">0</col>
+		<col name="/1">0</col>
 		<col name="/4">0</col>
 		<col name="/5">0</col>
-		<col name="/6">0</col>
-		<col name="/7">0</col>
 		<col name="/8">0</col>
-		<col name="/9">0</col>
-		<col name="/index">1</col>
+		<col name="/7">0</col>
+		<col name="/6">0</col>
+		<col name="/0">0</col>
 	</row>
 </result>
\ No newline at end of file
diff --git a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySegmentCreatesCorrectPivotTable__Referrers.getKeywords_week.xml b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySegmentCreatesCorrectPivotTable__Referrers.getKeywords_week.xml
index 99086f82e8..c6315c7326 100644
--- a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySegmentCreatesCorrectPivotTable__Referrers.getKeywords_week.xml
+++ b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySegmentCreatesCorrectPivotTable__Referrers.getKeywords_week.xml
@@ -2,38 +2,38 @@
 <result>
 	<row>
 		<col name="label">this search term</col>
-		<col name="Melbourne, Victoria, Australia">2</col>
 		<col name="Toronto, Ontario, Canada">0</col>
 		<col name="Yokohama, Kanagawa, Japan">1</col>
+		<col name="Melbourne, Victoria, Australia">2</col>
 	</row>
 	<row>
 		<col name="label">search term 2</col>
-		<col name="Melbourne, Victoria, Australia">0</col>
 		<col name="Toronto, Ontario, Canada">0</col>
 		<col name="Yokohama, Kanagawa, Japan">2</col>
+		<col name="Melbourne, Victoria, Australia">0</col>
 	</row>
 	<row>
 		<col name="label">search term 3</col>
-		<col name="Melbourne, Victoria, Australia">0</col>
 		<col name="Toronto, Ontario, Canada">2</col>
 		<col name="Yokohama, Kanagawa, Japan">0</col>
+		<col name="Melbourne, Victoria, Australia">0</col>
 	</row>
 	<row>
 		<col name="label">search term 4</col>
-		<col name="Melbourne, Victoria, Australia">0</col>
 		<col name="Toronto, Ontario, Canada">2</col>
 		<col name="Yokohama, Kanagawa, Japan">0</col>
+		<col name="Melbourne, Victoria, Australia">0</col>
 	</row>
 	<row>
 		<col name="label">that search term</col>
-		<col name="Melbourne, Victoria, Australia">2</col>
 		<col name="Toronto, Ontario, Canada">0</col>
 		<col name="Yokohama, Kanagawa, Japan">0</col>
+		<col name="Melbourne, Victoria, Australia">2</col>
 	</row>
 	<row>
 		<col name="label">search term 1</col>
-		<col name="Melbourne, Victoria, Australia">0</col>
 		<col name="Toronto, Ontario, Canada">0</col>
 		<col name="Yokohama, Kanagawa, Japan">1</col>
+		<col name="Melbourne, Victoria, Australia">0</col>
 	</row>
 </result>
\ No newline at end of file
diff --git a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySubtableDimensionCreatesCorrectPivotTableWhenEntireHirearchyIsNotLoaded__Referrers.getKeywords_week.xml b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySubtableDimensionCreatesCorrectPivotTableWhenEntireHirearchyIsNotLoaded__Referrers.getKeywords_week.xml
index 4c9fe03b6f..1adf9d722a 100644
--- a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySubtableDimensionCreatesCorrectPivotTableWhenEntireHirearchyIsNotLoaded__Referrers.getKeywords_week.xml
+++ b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySubtableDimensionCreatesCorrectPivotTableWhenEntireHirearchyIsNotLoaded__Referrers.getKeywords_week.xml
@@ -2,56 +2,56 @@
 <result>
 	<row>
 		<col name="label">this search term</col>
-		<col name="Alexa">0</col>
-		<col name="Ask">1</col>
-		<col name="Babylon">0</col>
-		<col name="Bing">0</col>
 		<col name="Google">1</col>
 		<col name="Yahoo!">1</col>
+		<col name="Ask">1</col>
+		<col name="Bing">0</col>
+		<col name="Alexa">0</col>
+		<col name="Babylon">0</col>
 	</row>
 	<row>
 		<col name="label">search term 2</col>
-		<col name="Alexa">1</col>
-		<col name="Ask">0</col>
-		<col name="Babylon">1</col>
-		<col name="Bing">0</col>
 		<col name="Google">0</col>
 		<col name="Yahoo!">0</col>
+		<col name="Ask">0</col>
+		<col name="Bing">0</col>
+		<col name="Alexa">1</col>
+		<col name="Babylon">1</col>
 	</row>
 	<row>
 		<col name="label">search term 3</col>
-		<col name="Alexa">0</col>
-		<col name="Ask">1</col>
-		<col name="Babylon">0</col>
-		<col name="Bing">0</col>
 		<col name="Google">1</col>
 		<col name="Yahoo!">0</col>
+		<col name="Ask">1</col>
+		<col name="Bing">0</col>
+		<col name="Alexa">0</col>
+		<col name="Babylon">0</col>
 	</row>
 	<row>
 		<col name="label">search term 4</col>
-		<col name="Alexa">0</col>
-		<col name="Ask">0</col>
-		<col name="Babylon">0</col>
-		<col name="Bing">1</col>
 		<col name="Google">0</col>
 		<col name="Yahoo!">1</col>
+		<col name="Ask">0</col>
+		<col name="Bing">1</col>
+		<col name="Alexa">0</col>
+		<col name="Babylon">0</col>
 	</row>
 	<row>
 		<col name="label">that search term</col>
-		<col name="Alexa">0</col>
-		<col name="Ask">0</col>
-		<col name="Babylon">0</col>
-		<col name="Bing">0</col>
 		<col name="Google">1</col>
 		<col name="Yahoo!">1</col>
+		<col name="Ask">0</col>
+		<col name="Bing">0</col>
+		<col name="Alexa">0</col>
+		<col name="Babylon">0</col>
 	</row>
 	<row>
 		<col name="label">search term 1</col>
-		<col name="Alexa">0</col>
-		<col name="Ask">0</col>
-		<col name="Babylon">0</col>
-		<col name="Bing">1</col>
 		<col name="Google">0</col>
 		<col name="Yahoo!">0</col>
+		<col name="Ask">0</col>
+		<col name="Bing">1</col>
+		<col name="Alexa">0</col>
+		<col name="Babylon">0</col>
 	</row>
 </result>
\ No newline at end of file
diff --git a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySubtableDimensionCreatesCorrectPivotTable__Referrers.getKeywords_week.xml b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySubtableDimensionCreatesCorrectPivotTable__Referrers.getKeywords_week.xml
index 4c9fe03b6f..1adf9d722a 100644
--- a/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySubtableDimensionCreatesCorrectPivotTable__Referrers.getKeywords_week.xml
+++ b/tests/PHPUnit/Integration/expected/test_PivotByQueryParamTest_testPivotBySubtableDimensionCreatesCorrectPivotTable__Referrers.getKeywords_week.xml
@@ -2,56 +2,56 @@
 <result>
 	<row>
 		<col name="label">this search term</col>
-		<col name="Alexa">0</col>
-		<col name="Ask">1</col>
-		<col name="Babylon">0</col>
-		<col name="Bing">0</col>
 		<col name="Google">1</col>
 		<col name="Yahoo!">1</col>
+		<col name="Ask">1</col>
+		<col name="Bing">0</col>
+		<col name="Alexa">0</col>
+		<col name="Babylon">0</col>
 	</row>
 	<row>
 		<col name="label">search term 2</col>
-		<col name="Alexa">1</col>
-		<col name="Ask">0</col>
-		<col name="Babylon">1</col>
-		<col name="Bing">0</col>
 		<col name="Google">0</col>
 		<col name="Yahoo!">0</col>
+		<col name="Ask">0</col>
+		<col name="Bing">0</col>
+		<col name="Alexa">1</col>
+		<col name="Babylon">1</col>
 	</row>
 	<row>
 		<col name="label">search term 3</col>
-		<col name="Alexa">0</col>
-		<col name="Ask">1</col>
-		<col name="Babylon">0</col>
-		<col name="Bing">0</col>
 		<col name="Google">1</col>
 		<col name="Yahoo!">0</col>
+		<col name="Ask">1</col>
+		<col name="Bing">0</col>
+		<col name="Alexa">0</col>
+		<col name="Babylon">0</col>
 	</row>
 	<row>
 		<col name="label">search term 4</col>
-		<col name="Alexa">0</col>
-		<col name="Ask">0</col>
-		<col name="Babylon">0</col>
-		<col name="Bing">1</col>
 		<col name="Google">0</col>
 		<col name="Yahoo!">1</col>
+		<col name="Ask">0</col>
+		<col name="Bing">1</col>
+		<col name="Alexa">0</col>
+		<col name="Babylon">0</col>
 	</row>
 	<row>
 		<col name="label">that search term</col>
-		<col name="Alexa">0</col>
-		<col name="Ask">0</col>
-		<col name="Babylon">0</col>
-		<col name="Bing">0</col>
 		<col name="Google">1</col>
 		<col name="Yahoo!">1</col>
+		<col name="Ask">0</col>
+		<col name="Bing">0</col>
+		<col name="Alexa">0</col>
+		<col name="Babylon">0</col>
 	</row>
 	<row>
 		<col name="label">search term 1</col>
-		<col name="Alexa">0</col>
-		<col name="Ask">0</col>
-		<col name="Babylon">0</col>
-		<col name="Bing">1</col>
 		<col name="Google">0</col>
 		<col name="Yahoo!">0</col>
+		<col name="Ask">0</col>
+		<col name="Bing">1</col>
+		<col name="Alexa">0</col>
+		<col name="Babylon">0</col>
 	</row>
 </result>
\ No newline at end of file
-- 
GitLab