TYPO3 CMS  TYPO3_7-6
LocallangXmlParserTest.php
Go to the documentation of this file.
1 <?php
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  */
17 
22 {
26  protected $parser;
27 
32 
36  protected $l10nPriority;
37 
38  protected static function getFixtureFilePath($filename)
39  {
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 
47  protected function setUp()
48  {
49  // Backup locallangXMLOverride and localization format priority
50  $this->locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
51  $this->l10nPriority = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'];
52  $this->parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser::class);
53 
54  $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xml';
55  \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageStore::class)->initialize();
56  // Clear localization cache
57  \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('l10n')->flush();
58  }
59 
63  protected function tearDown()
64  {
65  // Restore locallangXMLOverride and localization format priority
66  $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'] = $this->locallangXMLOverride;
67  $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = $this->l10nPriority;
68  \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageStore::class)->initialize();
69  parent::tearDown();
70  }
71 
75  public function canParseLlxmlInEnglish()
76  {
77  $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default');
78  $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
79  $expectedLabels = [
80  'label1' => 'This is label #1',
81  'label2' => 'This is label #2',
82  'label3' => 'This is label #3'
83  ];
84  foreach ($expectedLabels as $key => $expectedLabel) {
85  $this->assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
86  }
87  }
88 
92  public function canParseLlxmlInMd5Code()
93  {
94  $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5');
95  $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
96  $expectedLabels = [
97  'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
98  'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
99  'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
100  ];
101  foreach ($expectedLabels as $key => $expectedLabel) {
102  $this->assertEquals($expectedLabel, $LOCAL_LANG['md5'][$key][0]['target']);
103  }
104  }
105 
110  {
111  $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallangOnlyDefaultLanguage.xml'), 'fr');
112  $expectedLabels = [
113  'label1' => null,
114  'label2' => null,
115  'label3' => null
116  ];
117  foreach ($expectedLabels as $key => $expectedLabel) {
118  $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
119  }
120  }
121 
125  public function canOverrideLlxml()
126  {
128  $factory = new LocalizationFactory;
129 
130  $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][self::getFixtureFilePath('locallang.xml')][] = self::getFixtureFilePath('locallang_override.xml');
131  $LOCAL_LANG = array_merge(
132  $factory->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default'),
133  $factory->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5')
134  );
135  $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
136  $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
137  $expectedLabels = [
138  'default' => [
139  'label1' => 'This is my 1st label',
140  'label2' => 'This is my 2nd label',
141  'label3' => 'This is label #3'
142  ],
143  'md5' => [
144  'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
145  'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
146  'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
147  ]
148  ];
149  foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
150  foreach ($expectedLanguageLabels as $key => $expectedLabel) {
151  $this->assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
152  }
153  }
154  }
155 
156  public function numericKeysDataProvider()
157  {
159  $factory = new LocalizationFactory;
160 
161  $LOCAL_LANG = $factory->getParsedData(self::getFixtureFilePath('locallangNumericKeys.xml'), 'default');
162  $translations = [];
163 
164  foreach ($LOCAL_LANG['default'] as $key => $labelData) {
165  $translations['Numerical key ' . $key] = [$key, $labelData[0]['source'] . ' [FR]'];
166  }
167 
168  return $translations;
169  }
170 
175  public function canTranslateNumericKeys($key, $expectedResult)
176  {
178  $factory = new LocalizationFactory;
179 
180  $LOCAL_LANG = $factory->getParsedData(self::getFixtureFilePath('locallangNumericKeys.xml'), 'fr');
181 
182  $this->assertEquals($expectedResult, $LOCAL_LANG['fr'][$key][0]['target']);
183  }
184 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']