From 014d205d84b7a59bf6317dca35ed75476a2f09ae Mon Sep 17 00:00:00 2001
From: diosmosis <benaka@piwik.pro>
Date: Tue, 9 Dec 2014 21:44:52 -0800
Subject: [PATCH] Fixes #5178, enable STRICT_TRANS_TABLES in travis and fix
 remaining tests that do not pass w/ this mysql setting. Includes fixes to
 PrivacyManager plugin and fix for tracker when non-unicode site search
 keyword is stored in the DB (instead of storing the data as non-utf strings,
 we store the data as urlencoded if the keyword is not valid utf-8).

---
 core/Tracker/PageUrl.php                     | 31 +++++++++++++++-----
 plugins/Actions/Actions/ActionSiteSearch.php |  9 ++++--
 plugins/TestRunner/templates/travis.yml.twig |  2 --
 tests/PHPUnit/System/PrivacyManagerTest.php  | 12 ++++----
 4 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/core/Tracker/PageUrl.php b/core/Tracker/PageUrl.php
index ae55b48aac..e629ef55bf 100644
--- a/core/Tracker/PageUrl.php
+++ b/core/Tracker/PageUrl.php
@@ -256,13 +256,18 @@ class PageUrl
      */
     public static function reencodeParameters(&$queryParameters, $encoding = false)
     {
-        // if query params are encoded w/ non-utf8 characters (due to browser bug or whatever),
-        // encode to UTF-8.
-        if (false !== $encoding
-            && 'utf-8' != strtolower($encoding)
-            && function_exists('mb_check_encoding')
-        ) {
-            $queryParameters = PageUrl::reencodeParametersArray($queryParameters, $encoding);
+        if (function_exists('mb_check_encoding')) {
+            // if query params are encoded w/ non-utf8 characters (due to browser bug or whatever),
+            // encode to UTF-8.
+            if ($encoding != 'utf-8'
+                && $encoding != false
+            ) {
+                Common::printDebug("Encoding page URL query parameters to $encoding.");
+
+                $queryParameters = PageUrl::reencodeParametersArray($queryParameters, $encoding);
+            }
+        } else {
+            Common::printDebug("Page charset supplied in tracking request, but mbstring extension is not available.");
         }
 
         return $queryParameters;
@@ -349,5 +354,15 @@ class PageUrl
 
         return array();
     }
-}
 
+    public static function urldecodeValidUtf8($value)
+    {
+        $value = urldecode($value);
+        if (function_exists('mb_check_encoding')
+            && !@mb_check_encoding($value, 'utf-8')
+        ) {
+            return urlencode($value);
+        }
+        return $value;
+    }
+}
\ No newline at end of file
diff --git a/plugins/Actions/Actions/ActionSiteSearch.php b/plugins/Actions/Actions/ActionSiteSearch.php
index d19cfaa6fc..392e3777f8 100644
--- a/plugins/Actions/Actions/ActionSiteSearch.php
+++ b/plugins/Actions/Actions/ActionSiteSearch.php
@@ -180,14 +180,19 @@ class ActionSiteSearch extends Action
         if (is_array($actionName)) {
             $actionName = reset($actionName);
         }
-        $actionName = trim(urldecode($actionName));
+
+        $actionName = PageUrl::urldecodeValidUtf8($actionName);
+        $actionName = trim($actionName);
         if (empty($actionName)) {
             return false;
         }
+
         if (is_array($categoryName)) {
             $categoryName = reset($categoryName);
         }
-        $categoryName = trim(urldecode($categoryName));
+        $categoryName = PageUrl::urldecodeValidUtf8($categoryName);
+        $categoryName = trim($categoryName);
+
         return array($url, $actionName, $categoryName, $count);
     }
 
diff --git a/plugins/TestRunner/templates/travis.yml.twig b/plugins/TestRunner/templates/travis.yml.twig
index 1972feb6c8..c7faa74372 100644
--- a/plugins/TestRunner/templates/travis.yml.twig
+++ b/plugins/TestRunner/templates/travis.yml.twig
@@ -146,8 +146,6 @@ before_script:
   # configure mysql
   - mysql -e "SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES'" # Travis default
 
-  # Uncomment to enable sql_mode STRICT_TRANS_TABLES (new default in Mysql 5.6)
-  - mysql -e "SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION'"
   - mysql -e "SELECT @@sql_mode;"
   - mysql -e "SHOW GLOBAL VARIABLES;"
 {% if pluginName is empty %}
diff --git a/tests/PHPUnit/System/PrivacyManagerTest.php b/tests/PHPUnit/System/PrivacyManagerTest.php
index 18b197a410..0d99d0816d 100644
--- a/tests/PHPUnit/System/PrivacyManagerTest.php
+++ b/tests/PHPUnit/System/PrivacyManagerTest.php
@@ -374,7 +374,7 @@ class PrivacyManagerTest extends SystemTestCase
 
         // perform checks
         $this->checkLogDataPurged();
-        $this->_checkReportsAndMetricsPurged($janBlobsRemaining = 5, $janNumericRemaining = 68); // 5 blobs for 5 days
+        $this->_checkReportsAndMetricsPurged($janBlobsRemaining = 5, $janNumericRemaining = 69); // 5 blobs for 5 days
     }
 
     /**
@@ -574,7 +574,7 @@ class PrivacyManagerTest extends SystemTestCase
 
         // perform checks
         $this->checkLogDataPurged();
-        $this->_checkReportsAndMetricsPurged($janBlobsRemaining = 6, $janNumericRemaining = 70); // 1 segmented blob + 5 day blobs
+        $this->_checkReportsAndMetricsPurged($janBlobsRemaining = 6, $janNumericRemaining = 71); // 1 segmented blob + 5 day blobs
     }
 
     // --- utility functions follow ---
@@ -720,15 +720,15 @@ class PrivacyManagerTest extends SystemTestCase
 
         // one metric for jan & one for feb
         Db::query(sprintf($sql, Common::prefixTable($archiveTables['numeric'][0])),
-            array(self::GARBAGE_FIELD, $janDate1, $janDate1, $janDate1, 1, 100));
+            array(self::GARBAGE_FIELD, $janDate1, $janDate1, 1, $janDate1, 100));
         Db::query(sprintf($sql, Common::prefixTable($archiveTables['numeric'][1])),
-            array(self::GARBAGE_FIELD, $febDate1, $febDate1, $febDate1, 1, 200));
+            array(self::GARBAGE_FIELD, $febDate1, $febDate1, 1, $febDate1, 200));
 
         // add garbage reports
         Db::query(sprintf($sql, Common::prefixTable($archiveTables['blob'][0])),
-            array(self::GARBAGE_FIELD, $janDate1, $janDate1, $janDate1, 10, 'blobval'));
+            array(self::GARBAGE_FIELD, $janDate1, $janDate1, 10, $janDate1, 'blobval'));
         Db::query(sprintf($sql, Common::prefixTable($archiveTables['blob'][1])),
-            array(self::GARBAGE_FIELD, $febDate1, $febDate1, $febDate1, 20, 'blobval'));
+            array(self::GARBAGE_FIELD, $febDate1, $febDate1, 20, $febDate1, 'blobval'));
     }
 
     protected function _checkNoDataChanges()
-- 
GitLab