Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LanguagesManager.test.php « tests « LanguagesManager « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b7a0c84711f997ed2df3ffdb1f4f3a7244b9b185 (plain)
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
<?php
if(!defined("PIWIK_PATH_TEST_TO_ROOT")) {
	define('PIWIK_PATH_TEST_TO_ROOT', getcwd().'/../../..');
}
if(!defined('PIWIK_CONFIG_TEST_INCLUDED'))
{
	require_once PIWIK_PATH_TEST_TO_ROOT . "/tests/config_test.php";
}

require_once 'LanguagesManager/API.php';

class Test_Languages_Manager extends UnitTestCase
{
	function __construct( $title = '')
	{
		parent::__construct( $title );
	}
	
	// test all languages
	function test_getTranslationsForLanguages()
	{
		$englishStrings = Piwik_LanguagesManager_API::getInstance()->getTranslationsForLanguage('en');
		$englishStringsWithParameters = array();
		foreach($englishStrings as $englishString)
		{
			$stringLabel = $englishString['label'];
			$stringValue = $englishString['value'];
			$count = $this->getCountParametersToReplace($stringValue);
			if($count > 0)
			{
				$englishStringsWithParameters[$stringLabel] = $count;
			}
		}
		
		// 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(); 
			$strings = Piwik_LanguagesManager_API::getInstance()->getTranslationsForLanguage($language);
			$content = ob_get_flush();
			$this->assertTrue(strpos(serialize($strings), "<script") === false, " language file containing javascript");
			$this->assertTrue(count($strings) > 100); // at least 100 translations in the language file
			$this->assertTrue(strlen($content) == 0, "buffer was ".strlen($content)." long but should be zero. Translation file for '$language' must be buggy.");
			
			// checking that translated strings have the same number of %s as the english source strings
			foreach($strings as $string)
			{
				$stringLabel = $string['label'];
				$stringValue = $string['value'];
				
				if(!empty($stringValue)
					&& isset($englishStringsWithParameters[$stringLabel]))
				{
					$englishParametersCount = $englishStringsWithParameters[$stringLabel];
					$countTranslation = $this->getCountParametersToReplace($stringValue);
					//$this->assertEqual($countTranslation, $englishParametersCount,
					//			"The string $stringLabel has $englishParametersCount parameters in English, but $countTranslation in the language $language.");
				}
			}
		}
		$this->pass();
	}
	
	private function getCountParametersToReplace($string)
	{
		$sprintfParameters = array('%s', '%1$s', '%2$s', '%3$s', '%4$s', '%5$s', '%6$s');
		$count = 0;
		foreach($sprintfParameters as $parameter)
		{
			$count += substr_count($string, $parameter);
		}
		return $count;
	}
	//test language when it's not defined
	function test_getTranslationsForLanguages_not()
	{
		$this->assertFalse(Piwik_LanguagesManager_API::getInstance()->getTranslationsForLanguage("../no-language"));
	}
}