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
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
use Piwik\Common;
use Piwik\Plugins\LanguagesManager\API;
use Piwik\Translate\Writer;
use Piwik\PluginsManager;
require_once 'LanguagesManager/API.php';
class Test_LanguagesManager extends PHPUnit_Framework_TestCase
{
public function setUp()
{
parent::setUp();
include PIWIK_INCLUDE_PATH . '/core/DataFiles/Languages.php';
}
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 = PluginsManager::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
* @group LanguagesManager
* @dataProvider getTestDataForLanguageFiles
*/
function testGetTranslationsForLanguages($language, $plugin)
{
$translationWriter = new Writer($language, $plugin);
if ($translationWriter->hasErrors()) {
$translationWriter->saveTemporary();
$this->fail(implode("\n", $translationWriter->getErrors()) . "\n" . 'Translation file errors detected in ' . $language . "...\n");
}
}
/**
* test language when it's not defined
*
* @group Plugins
* @group LanguagesManager
* @expectedException Exception
*/
function testWriterInvalidPlugin()
{
$writer = new Writer('de', 'iNvaLiDPluGin'); // invalid plugin throws exception
}
/**
* test language when it's not defined
*
* @group Plugins
* @group LanguagesManager
*/
function testGetTranslationsForLanguagesNot()
{
$this->assertFalse(API::getInstance()->getTranslationsForLanguage("../no-language"));
}
/**
* test English short name for language
*
* @group Plugins
* @group LanguagesManager
*/
function testGetLanguageNamesInEnglish()
{
$languages = API::getInstance()->getAvailableLanguages();
foreach ($languages as $language) {
$data = file_get_contents(PIWIK_INCLUDE_PATH . "/lang/$language.json");
$translations = json_decode($data, true);
$name = $translations['General']['EnglishLanguageName'];
if ($language != 'en') {
$this->assertFalse($name == 'English', "for $language");
}
$languageCode = substr($language, 0, 2);
$this->assertTrue(isset($GLOBALS['Piwik_LanguageList'][$languageCode]));
$names = $GLOBALS['Piwik_LanguageList'][$languageCode];
if (isset($GLOBALS['Piwik_LanguageList'][$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
* @group LanguagesManager
*/
function testGetLanguagesList()
{
$languages = Common::getLanguagesList();
$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");
}
}
}
|