function test_clean() { $tests = array("" => '', "\n" => '', " a \n" => 'a', " "it's" " => '"it\'s"', "<tag>" => '<tag>', "…" => '…'); foreach ($tests as $data => $expected) { $this->assertEqual(Piwik_TranslationWriter::clean($data), $expected, "not {$expected}"); } }
function addMenus() { $goals = Piwik_Tracker_GoalManager::getGoalDefinitions(Piwik_Common::getRequestVar('idSite', null, 'int')); if (count($goals) == 0) { Piwik_AddMenu('Goals_Goals', '', array('module' => 'Goals', 'action' => 'addNewGoal'), true, 25); Piwik_AddMenu('Goals_Goals', 'Goals_AddNewGoal', array('module' => 'Goals', 'action' => 'addNewGoal')); } else { Piwik_AddMenu('Goals_Goals', '', array('module' => 'Goals', 'action' => 'index'), true, 25); Piwik_AddMenu('Goals_Goals', 'Goals_Overview', array('module' => 'Goals', 'action' => 'index'), true, 1); foreach ($goals as $goal) { Piwik_AddMenu('Goals_Goals', str_replace('%', '%%', Piwik_TranslationWriter::clean($goal['name'])), array('module' => 'Goals', 'action' => 'goalReport', 'idGoal' => $goal['idgoal'])); } } }
/** * @group Core * @group TranslationWriter * @dataProvider getCleanTestData */ public function testClean($data, $expected) { $this->assertEquals($expected, Piwik_TranslationWriter::clean($data)); }
function addMenus() { $idSite = Piwik_Common::getRequestVar('idSite', null, 'int'); $goals = Piwik_Goals_API::getInstance()->getGoals($idSite); $mainGoalMenu = $this->getGoalCategoryName($idSite); $site = new Piwik_Site($idSite); if (count($goals) == 0) { Piwik_AddMenu($mainGoalMenu, '', array('module' => 'Goals', 'action' => $site->isEcommerceEnabled() ? 'ecommerceReport' : 'addNewGoal', 'idGoal' => $site->isEcommerceEnabled() ? Piwik_Archive::LABEL_ECOMMERCE_ORDER : null), true, 25); if ($site->isEcommerceEnabled()) { Piwik_AddMenu($mainGoalMenu, 'Goals_Ecommerce', array('module' => 'Goals', 'action' => 'ecommerceReport', 'idGoal' => Piwik_Archive::LABEL_ECOMMERCE_ORDER), true, 1); } Piwik_AddMenu($mainGoalMenu, 'Goals_AddNewGoal', array('module' => 'Goals', 'action' => 'addNewGoal')); } else { Piwik_AddMenu($mainGoalMenu, '', array('module' => 'Goals', 'action' => $site->isEcommerceEnabled() ? 'ecommerceReport' : 'index', 'idGoal' => $site->isEcommerceEnabled() ? Piwik_Archive::LABEL_ECOMMERCE_ORDER : null), true, 25); if ($site->isEcommerceEnabled()) { Piwik_AddMenu($mainGoalMenu, 'Goals_Ecommerce', array('module' => 'Goals', 'action' => 'ecommerceReport', 'idGoal' => Piwik_Archive::LABEL_ECOMMERCE_ORDER), true, 1); } Piwik_AddMenu($mainGoalMenu, 'Goals_GoalsOverview', array('module' => 'Goals', 'action' => 'index'), true, 2); foreach ($goals as $goal) { Piwik_AddMenu($mainGoalMenu, str_replace('%', '%%', Piwik_TranslationWriter::clean($goal['name'])), array('module' => 'Goals', 'action' => 'goalReport', 'idGoal' => $goal['idgoal'])); } } }
function test_getTranslationsForLanguages() { $allLanguages = Piwik_Common::getLanguagesList(); $allCountries = Piwik_Common::getCountriesList(); $englishStrings = Piwik_LanguagesManager_API::getInstance()->getTranslationsForLanguage('en'); $englishStringsWithParameters = array(); $expectedLanguageKeys = array(); foreach ($englishStrings as $englishString) { $stringLabel = $englishString['label']; $stringValue = $englishString['value']; $count = $this->getCountParametersToReplace($stringValue); if ($count > 0) { $englishStringsWithParameters[$stringLabel] = $count; } $englishStringsIndexed[$stringLabel] = $stringValue; $expectedLanguageKeys[] = $stringLabel; } // we also test that none of the language php files outputs any character on the screen (eg. space before the <?php) $languages = Piwik_LanguagesManager_API::getInstance()->getAvailableLanguages(); foreach ($languages as $language) { ob_start(); $writeCleanedFile = false; $strings = Piwik_LanguagesManager_API::getInstance()->getTranslationsForLanguage($language); $content = ob_get_flush(); $serializedStrings = serialize($strings); $invalids = array("<script", 'document.', 'javascript:', 'src=', 'BACKGROUND=', 'onload='); foreach ($invalids as $invalid) { $this->assertTrue(stripos($serializedStrings, $invalid) === false, "{$language}: language file containing javascript"); } $this->assertTrue(count($strings) > 100, "{$language}: expecting at least 100 translations in the language file"); $this->assertTrue(strlen($content) == 0, "{$language}: buffer was " . strlen($content) . " long but should be zero. Translation file for '{$language}' must be buggy."); $cleanedStrings = array(); foreach ($strings as $string) { $stringLabel = $string['label']; $stringValue = $string['value']; $plugin = substr($stringLabel, 0, strpos($stringLabel, '_')); $plugins[$plugin] = true; // Testing that the translated string is not empty => '', if (empty($stringValue) || trim($stringValue) === '') { $writeCleanedFile = true; echo "{$language}: The string {$stringLabel} is empty in the translation file, removing the line. <br/>\n"; $cleanedStrings[$stringLabel] = false; } elseif (!in_array($stringLabel, $expectedLanguageKeys) && !in_array($plugin, array('GeoIP', 'Forecast', 'EntryPage', 'UserLanguage'))) { $writeCleanedFile = true; echo "{$language}: The string {$stringLabel} was not found in the English language file, removing the line. <br/>\n"; $cleanedStrings[$stringLabel] = false; } else { // checking that translated strings have the same number of %s as the english source strings if (isset($englishStringsWithParameters[$stringLabel])) { $englishParametersCount = $englishStringsWithParameters[$stringLabel]; $countTranslation = $this->getCountParametersToReplace($stringValue); if ($englishParametersCount != $countTranslation) { // Write fixed file in given location // Will trigger a ->fail() $writeCleanedFile = true; echo "{$language}: The string {$stringLabel} has {$englishParametersCount} parameters in English, but {$countTranslation} in this translation. <br/>\n"; } else { $cleanedStrings[$stringLabel] = $stringValue; } } else { $cleanedStrings[$stringLabel] = $stringValue; } } // If the translation is the same as in English, we remove it from the translation file (as it might have been copied by // the translator but this would skew translation stats if (isset($englishStringsIndexed[$stringLabel]) && $englishStringsIndexed[$stringLabel] == $stringValue && $language == 'fa') { $writeCleanedFile = true; echo "{$language}: The string {$stringLabel} is the same as in English, removing... <br/>\n"; $cleanedStrings[$stringLabel] = false; } // remove excessive line breaks (and leading/trailing whitespace) from translations if (!empty($cleanedStrings[$stringLabel])) { $stringNoLineBreak = trim($cleanedStrings[$stringLabel]); if ($stringLabel != 'Login_MailPasswordRecoveryBody') { $stringNoLineBreak = str_replace(array("\n", "\r"), " ", $stringNoLineBreak); } if ($cleanedStrings[$stringLabel] !== $stringNoLineBreak) { echo "{$language}: found unnecessary whitespace in some strings in {$stringLabel} <br/>\n"; $writeCleanedFile = true; $cleanedStrings[$stringLabel] = $stringNoLineBreak; } } // Test locale if ($stringLabel == 'General_Locale' && !empty($cleanedStrings[$stringLabel])) { if (!preg_match('/^([a-z]{2})_([A-Z]{2})\\.UTF-8$/', $cleanedStrings[$stringLabel], $matches)) { $this->fail("{$language}: invalid locale in {$stringLabel}"); } else { if (!array_key_exists($matches[1], $allLanguages)) { $this->fail("{$language}: invalid language code in {$stringLabel}"); } else { if (!array_key_exists(strtolower($matches[2]), $allCountries)) { $this->fail("{$language}: invalid region (country code) in {$stringLabel}"); } } } } if (isset($cleanedStrings[$stringLabel])) { $currentString = $cleanedStrings[$stringLabel]; $decoded = Piwik_TranslationWriter::clean($currentString); if ($currentString != $decoded) { echo "{$language}: found encoded entities in {$stringLabel}, converting entities to characters <br/>\n"; $writeCleanedFile = true; $cleanedStrings[$stringLabel] = $decoded; } } } $this->assertTrue(!empty($cleanedStrings['General_TranslatorName']), "{$language}: translator info not specified"); $this->assertTrue(!empty($cleanedStrings['General_TranslatorEmail']), "{$language}: translator info not specified"); if (!empty($cleanedStrings['General_LayoutDirection']) && !in_array($cleanedStrings['General_LayoutDirection'], array('rtl', 'ltr'))) { $writeCleanedFile = true; $cleanedStrings['General_LayoutDirection'] = false; echo "{$language}: General_LayoutDirection must be rtl or ltr"; } if ($writeCleanedFile) { $this->writeCleanedTranslationFile($cleanedStrings, $language); } } // var_dump('Unique plugins found: ' . var_export($plugins, 1)); $this->pass(); }