From 8b01230fc5f0424e2dc8721c29737f7f4b25498c Mon Sep 17 00:00:00 2001
From: sgiehl <stefan@piwik.org>
Date: Fri, 30 Aug 2013 18:44:34 +0200
Subject: [PATCH] added translations filter to filter encoded entities

---
 core/Translate/Filter/EncodedEntities.php     |  49 ++++++++
 .../Translate/Filter/EncodedEntitiesTest.php  | 108 ++++++++++++++++++
 2 files changed, 157 insertions(+)
 create mode 100644 core/Translate/Filter/EncodedEntities.php
 create mode 100644 tests/PHPUnit/Core/Translate/Filter/EncodedEntitiesTest.php

diff --git a/core/Translate/Filter/EncodedEntities.php b/core/Translate/Filter/EncodedEntities.php
new file mode 100644
index 0000000000..cf2935268b
--- /dev/null
+++ b/core/Translate/Filter/EncodedEntities.php
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ * @category Piwik
+ * @package Piwik
+ */
+
+namespace Piwik\Translate\Filter;
+
+use Piwik\Translate\Filter\FilterAbstract;
+use Piwik\TranslationWriter;
+
+/**
+ * @package Piwik
+ * @subpackage Piwik_Translate
+ */
+class EncodedEntities extends FilterAbstract
+{
+    /**
+     * Filter the given translations
+     *
+     * @param array $translations
+     *
+     * @return array   filtered translations
+     *
+     */
+    public function filter($translations)
+    {
+        foreach ($translations AS $pluginName => $pluginTranslations) {
+            foreach ($pluginTranslations AS $key => $translation) {
+
+                // remove encoded entities
+                $decoded = TranslationWriter::clean($translation);
+                if ($translation != $decoded) {
+                    $this->_filteredData[$pluginName][$key] = $translation;
+                    $translations[$pluginName][$key] = $decoded;
+                    continue;
+                }
+
+            }
+        }
+
+        return $translations;
+    }
+}
\ No newline at end of file
diff --git a/tests/PHPUnit/Core/Translate/Filter/EncodedEntitiesTest.php b/tests/PHPUnit/Core/Translate/Filter/EncodedEntitiesTest.php
new file mode 100644
index 0000000000..afaf1142ca
--- /dev/null
+++ b/tests/PHPUnit/Core/Translate/Filter/EncodedEntitiesTest.php
@@ -0,0 +1,108 @@
+<?php
+use Piwik\Translate\Filter\EncodedEntities;
+
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+class EncodedEntitiesTest extends PHPUnit_Framework_TestCase
+{
+    public function getFilterTestData()
+    {
+        return array(
+            // empty stays empty - nothing to filter
+            array(
+                array(),
+                array(),
+                array()
+            ),
+            // empty plugin is removed
+            array(
+                array(
+                    'test' => array()
+                ),
+                array(
+                    'test' => array()
+                ),
+                array(),
+            ),
+            // no entites - nothing to filter
+            array(
+                array(
+                    'test' => array(
+                        'key' => 'val%sue',
+                        'test' => 'test'
+                    )
+                ),
+                array(
+                    'test' => array(
+                        'key' => 'val%sue',
+                        'test' => 'test'
+                    )
+                ),
+                array(),
+            ),
+            // entities needs to be decodded
+            array(
+                array(
+                    'test' => array(
+                        'test' => 'te&amp;st'
+                    )
+                ),
+                array(
+                    'test' => array(
+                        'test' => 'te&st'
+                    )
+                ),
+                array(
+                    'test' => array(
+                        'test' => 'te&amp;st'
+                    )
+                ),
+            ),
+            array(
+                array(
+                    'empty' => array(
+                        'test' => 't&uuml;sest'
+                    ),
+                    'test' => array(
+                        'test' => '%1$stest',
+                        'empty' => '&tilde;',
+                    )
+                ),
+                array(
+                    'empty' => array(
+                        'test' => 'tüsest'
+                    ),
+                    'test' => array(
+                        'test' => '%1$stest',
+                        'empty' => '˜',
+                    )
+                ),
+                array(
+                    'empty' => array(
+                        'test' => 't&uuml;sest'
+                    ),
+                    'test' => array(
+                        'empty' => '&tilde;',
+                    )
+                ),
+            ),
+        );
+    }
+
+    /**
+     * @dataProvider getFilterTestData
+     * @group Core
+     * @group Translate
+     */
+    public function testFilter($translations, $expected, $filteredData)
+    {
+        $filter = new EncodedEntities();
+        $result = $filter->filter($translations);
+        $this->assertEquals($expected, $result);
+        $this->assertEquals($filteredData, $filter->getFilteredData());
+    }
+}
-- 
GitLab