1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\LanguagesManager\Test\Integration;
use Piwik\Container\StaticContainer;
use Piwik\Intl\Data\Provider\LanguageDataProvider;
use Piwik\Plugins\LanguagesManager\API;
use \Exception;
use Piwik\Plugins\LanguagesManager\TranslationWriter\Filter\ByParameterCount;
use Piwik\Plugins\LanguagesManager\TranslationWriter\Filter\EmptyTranslations;
use Piwik\Plugins\LanguagesManager\TranslationWriter\Filter\EncodedEntities;
use Piwik\Plugins\LanguagesManager\TranslationWriter\Filter\UnnecassaryWhitespaces;
use Piwik\Plugins\LanguagesManager\TranslationWriter\Validate\CoreTranslations;
use Piwik\Plugins\LanguagesManager\TranslationWriter\Validate\NoScripts;
use Piwik\Plugins\LanguagesManager\TranslationWriter\Writer;
/**
* @group LanguagesManager
*/
class LanguagesManagerTest extends \PHPUnit_Framework_TestCase
{
function getTestDataForLanguageFiles()
{
// we also test that none of the language php files outputs any character on the screen (eg. space before the <?php)
$languages = API::getInstance()->getAvailableLanguages();
$plugins = \Piwik\Plugin\Manager::getInstance()->readPluginsDirectory();
$pluginsWithTranslation = array();
foreach ($plugins as $plugin) {
if (API::getInstance()->getPluginTranslationsForLanguage($plugin, 'en')) {
$pluginsWithTranslation[] = $plugin;
}
}
$return = array();
foreach ($languages as $language) {
if ($language != 'en') {
$return[] = array($language, null);
foreach ($pluginsWithTranslation as $plugin) {
$return[] = array($language, $plugin);
}
}
}
return $return;
}
/**
* test all languages
*
* @group Plugins
*
* @dataProvider getTestDataForLanguageFiles
*/
function testGetTranslationsForLanguages($language, $plugin)
{
$translationWriter = new Writer($language, $plugin);
$baseTranslations = $translationWriter->getTranslations('en');
$translationWriter->addValidator(new NoScripts());
if (empty($plugin)) {
$translationWriter->addValidator(new CoreTranslations($baseTranslations));
}
// prevent build from failing when translations string have been deleted
// $translationWriter->addFilter(new ByBaseTranslations($baseTranslations));
$translationWriter->addFilter(new EmptyTranslations());
$translationWriter->addFilter(new ByParameterCount($baseTranslations));
$translationWriter->addFilter(new UnnecassaryWhitespaces($baseTranslations));
$translationWriter->addFilter(new EncodedEntities());
$translations = $translationWriter->getTranslations($language);
if (empty($translations)) {
return; // skip language / plugin combinations that aren't present
}
$translationWriter->setTranslations($translations);
$this->assertTrue($translationWriter->isValid(), $translationWriter->getValidationMessage());
if ($translationWriter->wasFiltered()) {
$translationWriter->saveTemporary();
$this->markTestSkipped(implode("\n", $translationWriter->getFilterMessages()) . "\n"
. 'Translation file errors detected in ' . $language . "...\n"
. "To synchronise the language files with the english strings, you can manually edit the language files or run the following command may work if you have access to Transifex: \n"
. "$ ./console translations:update [--plugin=XYZ] \n"
);
}
}
/**
* test language when it's not defined
*
* @group Plugins
*
* @expectedException Exception
*/
function testWriterInvalidPlugin()
{
new Writer('de', 'iNvaLiDPluGin'); // invalid plugin throws exception
}
/**
* test language when it's not defined
*
* @group Plugins
*/
function testGetTranslationsForLanguagesNot()
{
$this->assertFalse(API::getInstance()->getTranslationsForLanguage("../no-language"));
}
/**
* test English short name for language
*
* @group Plugins
*/
function testGetLanguageNamesInEnglish()
{
$languages = API::getInstance()->getAvailableLanguages();
/** @var LanguageDataProvider $dataProvider */
$dataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\LanguageDataProvider');
$languagesReference = $dataProvider->getLanguageList();
foreach ($languages as $language) {
$data = file_get_contents(PIWIK_INCLUDE_PATH . "/plugins/Intl/lang/$language.json");
$translations = json_decode($data, true);
$name = $translations['Intl']['EnglishLanguageName'];
if ($language != 'en') {
$this->assertFalse($name == 'English', "for $language");
}
$languageCode = substr($language, 0, 2);
$this->assertTrue(isset($languagesReference[$languageCode]));
$names = $languagesReference[$languageCode];
if (isset($languagesReference[$language])) {
if (is_array($names)) {
$this->assertTrue(in_array($name, $names), "$language: failed because $name not a known language name");
} else {
$this->assertTrue($name == $names, "$language: failed because $name == $names");
}
} else {
if (is_array($names)) {
$this->assertTrue(strpos($name, $names[0]) !== false);
} else {
$this->fail("$language: expected an array of language names");
}
}
}
}
/**
* test format of DataFile/Languages.php
*
* @group Plugins
*/
public function testGetLanguagesList()
{
/** @var LanguageDataProvider $languageDataProvider */
$languageDataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\LanguageDataProvider');
$languages = $languageDataProvider->getLanguageList();
$this->assertTrue(count($languages) > 0);
foreach ($languages as $langCode => $langs) {
$this->assertTrue(strlen($langCode) == 2, "$langCode length = 2");
$this->assertTrue(is_array($langs) && count($langs) >= 1, "$langCode array(names) >= 1");
}
}
}
|