From 53f0ec193131d0a30721f465179aa8737a720e98 Mon Sep 17 00:00:00 2001 From: sgiehl <stefan@piwik.org> Date: Sat, 31 Aug 2013 18:05:42 +0200 Subject: [PATCH] moved clean method to Translate instead of TranslationWriter --- core/Translate.php | 11 ++++++ core/Translate/Filter/EncodedEntities.php | 4 +-- plugins/Goals/Goals.php | 4 +-- tests/PHPUnit/Core/TranslateTest.php | 43 +++++++++++++++++++++++ 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 tests/PHPUnit/Core/TranslateTest.php diff --git a/core/Translate.php b/core/Translate.php index 5348c1a1b3..09d9734da6 100644 --- a/core/Translate.php +++ b/core/Translate.php @@ -33,6 +33,17 @@ class Translate return self::$instance; } + /** + * Clean a string that may contain HTML special chars, single/double quotes, HTML entities, leading/trailing whitespace + * + * @param string $s + * @return string + */ + static public function clean($s) + { + return html_entity_decode(trim($s), ENT_QUOTES, 'UTF-8'); + } + public function loadEnglishTranslation() { $this->loadCoreTranslationFile('en'); diff --git a/core/Translate/Filter/EncodedEntities.php b/core/Translate/Filter/EncodedEntities.php index 5b0c4ee5c7..4a8b757c1f 100644 --- a/core/Translate/Filter/EncodedEntities.php +++ b/core/Translate/Filter/EncodedEntities.php @@ -12,7 +12,7 @@ namespace Piwik\Translate\Filter; use Piwik\Translate\Filter\FilterAbstract; -use Piwik\TranslationWriter; +use Piwik\Translate; /** * @package Piwik @@ -34,7 +34,7 @@ class EncodedEntities extends FilterAbstract foreach ($pluginTranslations AS $key => $translation) { // remove encoded entities - $decoded = TranslationWriter::clean($translation); + $decoded = Translate::clean($translation); if ($translation != $decoded) { $this->_filteredData[$pluginName][$key] = $translation; $translations[$pluginName][$key] = $decoded; diff --git a/plugins/Goals/Goals.php b/plugins/Goals/Goals.php index f15569affb..efc8582685 100644 --- a/plugins/Goals/Goals.php +++ b/plugins/Goals/Goals.php @@ -16,7 +16,7 @@ use Piwik\Common; use Piwik\Plugins\Goals\API; use Piwik\Plugins\Goals\Archiver; use Piwik\Tracker\GoalManager; -use Piwik\TranslationWriter; +use Piwik\Translate; use Piwik\Site; use Piwik\WidgetsList; use Piwik\Db; @@ -471,7 +471,7 @@ class Goals extends \Piwik\Plugin } Piwik_AddMenu($mainGoalMenu, 'Goals_GoalsOverview', array('module' => 'Goals', 'action' => 'index'), true, 2); foreach ($goals as $goal) { - Piwik_AddMenu($mainGoalMenu, str_replace('%', '%%', TranslationWriter::clean($goal['name'])), array('module' => 'Goals', 'action' => 'goalReport', 'idGoal' => $goal['idgoal'])); + Piwik_AddMenu($mainGoalMenu, str_replace('%', '%%', Translate::clean($goal['name'])), array('module' => 'Goals', 'action' => 'goalReport', 'idGoal' => $goal['idgoal'])); } } } diff --git a/tests/PHPUnit/Core/TranslateTest.php b/tests/PHPUnit/Core/TranslateTest.php new file mode 100644 index 0000000000..3acc545f77 --- /dev/null +++ b/tests/PHPUnit/Core/TranslateTest.php @@ -0,0 +1,43 @@ +<?php +use Piwik\Common; +use Piwik\Translate; + +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ +class TranslateTest extends PHPUnit_Framework_TestCase +{ + /** + * Dataprovider for testClean + */ + public function getCleanTestData() + { + return array( + // empty string + array("", ''), + // newline + array("\n", ''), + // leading and trailing whitespace + array(" a \n", 'a'), + // single / double quotes + array(" "it's" ", '"it\'s"'), + // html special characters + array("<tag>", '<tag>'), + // other html entities + array("…", '…'), + ); + } + + /** + * @group Core + * @group Translate + * @dataProvider getCleanTestData + */ + public function testClean($data, $expected) + { + $this->assertEquals($expected, Translate::clean($data)); + } +} \ No newline at end of file -- GitLab