b73be56ad8639a57fb67a45bfbb7017309d12afa
[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 * Copyright notice
6 *
7 * (c) 2011-2013 Xavier Perseguers <xavier@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Testcase for class \TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser.
29 *
30 * @author Xavier Perseguers <xavier@typo3.org>
31 */
32 class LocallangXmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * @var \TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser
36 */
37 protected $parser;
38
39 /**
40 * @var array
41 */
42 protected $locallangXMLOverride;
43
44 /**
45 * @var string
46 */
47 protected $l10nPriority;
48
49 protected static function getFixtureFilePath($filename) {
50 // We have to take the whole relative path as otherwise this test fails on Windows systems
51 return PATH_site . 'typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/' . $filename;
52 }
53
54 /**
55 * Prepares the environment before running a test.
56 */
57 public function setUp() {
58 // Backup locallangXMLOverride and localization format priority
59 $this->locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
60 $this->l10nPriority = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'];
61 $this->parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Parser\\LocallangXmlParser');
62
63 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xml';
64 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore')->initialize();
65 // Clear localization cache
66 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')->getCache('l10n')->flush();
67 }
68
69 /**
70 * Cleans up the environment after running a test.
71 */
72 public function tearDown() {
73 // Restore locallangXMLOverride and localization format priority
74 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'] = $this->locallangXMLOverride;
75 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = $this->l10nPriority;
76 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore')->initialize();
77 parent::tearDown();
78 }
79
80 /**
81 * @test
82 */
83 public function canParseLlxmlInEnglish() {
84 $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default');
85 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
86 $expectedLabels = array(
87 'label1' => 'This is label #1',
88 'label2' => 'This is label #2',
89 'label3' => 'This is label #3'
90 );
91 foreach ($expectedLabels as $key => $expectedLabel) {
92 $this->assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
93 }
94 }
95
96 /**
97 * @test
98 */
99 public function canParseLlxmlInMd5Code() {
100 $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5');
101 $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
102 $expectedLabels = array(
103 'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
104 'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
105 'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
106 );
107 foreach ($expectedLabels as $key => $expectedLabel) {
108 $this->assertEquals($expectedLabel, $LOCAL_LANG['md5'][$key][0]['target']);
109 }
110 }
111
112 /**
113 * @test
114 */
115 public function canParseLlxmlInFrenchAndReturnsNullLabelsIfNoTranslationIsFound() {
116 $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallangOnlyDefaultLanguage.xml'), 'fr');
117 $expectedLabels = array(
118 'label1' => NULL,
119 'label2' => NULL,
120 'label3' => NULL
121 );
122 foreach ($expectedLabels as $key => $expectedLabel) {
123 $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
124 }
125 }
126
127 /**
128 * @test
129 */
130 public function canOverrideLlxml() {
131 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][self::getFixtureFilePath('locallang.xml')][] = self::getFixtureFilePath('locallang_override.xml');
132 $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'));
133 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
134 $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
135 $expectedLabels = array(
136 'default' => array(
137 'label1' => 'This is my 1st label',
138 'label2' => 'This is my 2nd label',
139 'label3' => 'This is label #3'
140 ),
141 'md5' => array(
142 'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
143 'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
144 'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
145 )
146 );
147 foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
148 foreach ($expectedLanguageLabels as $key => $expectedLabel) {
149 $this->assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
150 }
151 }
152 }
153
154 public function numericKeysDataProvider() {
155 $LOCAL_LANG = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile(self::getFixtureFilePath('locallangNumericKeys.xml'), 'default');
156 $translations = array();
157
158 foreach ($LOCAL_LANG['default'] as $key => $labelData) {
159 $translations['Numerical key ' . $key] = array($key, $labelData[0]['source'] . ' [FR]');
160 }
161
162 return $translations;
163 }
164
165 /**
166 * @test
167 * @dataProvider numericKeysDataProvider
168 */
169 public function canTranslateNumericKeys($key, $expectedResult) {
170 $LOCAL_LANG = \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile(self::getFixtureFilePath('locallangNumericKeys.xml'), 'fr');
171
172 $this->assertEquals($expectedResult, $LOCAL_LANG['fr'][$key][0]['target']);
173 }
174
175 }