[TASK] Re-work/simplify copyright header in PHP files - Part 4
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Localization / Parser / LocallangXmlParserTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Localization\Parser;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Testcase for class \TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser.
19 *
20 * @author Xavier Perseguers <xavier@typo3.org>
21 */
22 class LocallangXmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 /**
25 * @var \TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser
26 */
27 protected $parser;
28
29 /**
30 * @var array
31 */
32 protected $locallangXMLOverride;
33
34 /**
35 * @var string
36 */
37 protected $l10nPriority;
38
39 protected static function getFixtureFilePath($filename) {
40 // We have to take the whole relative path as otherwise this test fails on Windows systems
41 return PATH_site . 'typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/' . $filename;
42 }
43
44 /**
45 * Prepares the environment before running a test.
46 */
47 public function setUp() {
48 // Backup locallangXMLOverride and localization format priority
49 $this->locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
50 $this->l10nPriority = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'];
51 $this->parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Parser\\LocallangXmlParser');
52
53 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xml';
54 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore')->initialize();
55 // Clear localization cache
56 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')->getCache('l10n')->flush();
57 }
58
59 /**
60 * Cleans up the environment after running a test.
61 */
62 public function tearDown() {
63 // Restore locallangXMLOverride and localization format priority
64 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'] = $this->locallangXMLOverride;
65 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = $this->l10nPriority;
66 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore')->initialize();
67 parent::tearDown();
68 }
69
70 /**
71 * @test
72 */
73 public function canParseLlxmlInEnglish() {
74 $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default');
75 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
76 $expectedLabels = array(
77 'label1' => 'This is label #1',
78 'label2' => 'This is label #2',
79 'label3' => 'This is label #3'
80 );
81 foreach ($expectedLabels as $key => $expectedLabel) {
82 $this->assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
83 }
84 }
85
86 /**
87 * @test
88 */
89 public function canParseLlxmlInMd5Code() {
90 $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5');
91 $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
92 $expectedLabels = array(
93 'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
94 'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
95 'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
96 );
97 foreach ($expectedLabels as $key => $expectedLabel) {
98 $this->assertEquals($expectedLabel, $LOCAL_LANG['md5'][$key][0]['target']);
99 }
100 }
101
102 /**
103 * @test
104 */
105 public function canParseLlxmlInFrenchAndReturnsNullLabelsIfNoTranslationIsFound() {
106 $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallangOnlyDefaultLanguage.xml'), 'fr');
107 $expectedLabels = array(
108 'label1' => NULL,
109 'label2' => NULL,
110 'label3' => NULL
111 );
112 foreach ($expectedLabels as $key => $expectedLabel) {
113 $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
114 }
115 }
116
117 /**
118 * @test
119 */
120 public function canOverrideLlxml() {
121 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][self::getFixtureFilePath('locallang.xml')][] = self::getFixtureFilePath('locallang_override.xml');
122 $LOCAL_LANG = array_merge(\TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile(self::getFixtureFilePath('locallang.xml'), 'default'), \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile(self::getFixtureFilePath('locallang.xml'), 'md5'));
123 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
124 $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
125 $expectedLabels = array(
126 'default' => array(
127 'label1' => 'This is my 1st label',
128 'label2' => 'This is my 2nd label',
129 'label3' => 'This is label #3'
130 ),
131 'md5' => array(
132 'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
133 'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
134 'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
135 )
136 );
137 foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
138 foreach ($expectedLanguageLabels as $key => $expectedLabel) {
139 $this->assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
140 }
141 }
142 }
143
144 public function numericKeysDataProvider() {
145 $LOCAL_LANG = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile(self::getFixtureFilePath('locallangNumericKeys.xml'), 'default');
146 $translations = array();
147
148 foreach ($LOCAL_LANG['default'] as $key => $labelData) {
149 $translations['Numerical key ' . $key] = array($key, $labelData[0]['source'] . ' [FR]');
150 }
151
152 return $translations;
153 }
154
155 /**
156 * @test
157 * @dataProvider numericKeysDataProvider
158 */
159 public function canTranslateNumericKeys($key, $expectedResult) {
160 $LOCAL_LANG = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile(self::getFixtureFilePath('locallangNumericKeys.xml'), 'fr');
161
162 $this->assertEquals($expectedResult, $LOCAL_LANG['fr'][$key][0]['target']);
163 }
164
165 }