TYPO3 CMS  TYPO3_8-7
XliffParserTest.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 
21 class XliffParserTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
22 {
26  protected $parser;
27 
32 
36  protected $l10nPriority;
37 
41  protected $xliffFileNames;
42 
46  protected function setUp()
47  {
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\XliffParser::class);
52 
53  // We have to take the whole relative path as otherwise this test fails on Windows systems
54  $fixturePath = PATH_site . 'typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/';
55  $this->xliffFileNames = [
56  'locallang' => $fixturePath . 'locallang.xlf',
57  'locallang_override' => $fixturePath . 'locallang_override.xlf',
58  'locallang_override_fr' => $fixturePath . 'fr.locallang_override.xlf'
59  ];
60  $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xlf';
61  \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageStore::class)->initialize();
62  // Clear localization cache
63  \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('l10n')->flush();
64  }
65 
69  protected function tearDown()
70  {
71  // Restore locallangXMLOverride and localization format priority
72  $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'] = $this->locallangXMLOverride;
73  $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = $this->l10nPriority;
74  \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageStore::class)->initialize();
75  parent::tearDown();
76  }
77 
81  public function canParseXliffInEnglish()
82  {
83  $LOCAL_LANG = $this->parser->getParsedData($this->xliffFileNames['locallang'], 'default');
84  $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
85  $expectedLabels = [
86  'label1' => 'This is label #1',
87  'label2' => 'This is label #2',
88  'label3' => 'This is label #3'
89  ];
90  foreach ($expectedLabels as $key => $expectedLabel) {
91  $this->assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
92  }
93  }
94 
98  public function canParseXliffInFrench()
99  {
100  $LOCAL_LANG = $this->parser->getParsedData($this->xliffFileNames['locallang'], 'fr');
101  $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
102  $expectedLabels = [
103  'label1' => 'Ceci est le libellé no. 1',
104  'label2' => 'Ceci est le libellé no. 2',
105  'label3' => 'Ceci est le libellé no. 3'
106  ];
107  foreach ($expectedLabels as $key => $expectedLabel) {
108  $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
109  }
110  }
111 
115  public function canOverrideXliff()
116  {
118  $factory = new LocalizationFactory;
119 
120  $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override'];
121  $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['fr'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override_fr'];
122  $LOCAL_LANG = array_merge(
123  $factory->getParsedData($this->xliffFileNames['locallang'], 'default'),
124  $factory->getParsedData($this->xliffFileNames['locallang'], 'fr')
125  );
126  $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
127  $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
128  $expectedLabels = [
129  'default' => [
130  'label1' => 'This is my 1st label',
131  'label2' => 'This is my 2nd label',
132  'label3' => 'This is label #3'
133  ],
134  'fr' => [
135  'label1' => 'Ceci est mon 1er libellé',
136  'label2' => 'Ceci est le libellé no. 2',
137  'label3' => 'Ceci est mon 3e libellé'
138  ]
139  ];
140  foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
141  foreach ($expectedLanguageLabels as $key => $expectedLabel) {
142  $this->assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
143  }
144  }
145  }
146 
153  public function canOverrideXliffWithFrenchOnly()
154  {
156  $factory = new LocalizationFactory;
157 
158  $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['fr'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override_fr'];
159  $LOCAL_LANG = $factory->getParsedData($this->xliffFileNames['locallang'], 'fr');
160  $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
161  $expectedLabels = [
162  'label1' => 'Ceci est mon 1er libellé',
163  'label2' => 'Ceci est le libellé no. 2',
164  'label3' => 'Ceci est mon 3e libellé'
165  ];
166  foreach ($expectedLabels as $key => $expectedLabel) {
167  $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
168  }
169  }
170 }
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']