‪TYPO3CMS  10.4
XliffParserTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Prophecy\Argument;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 class ‪XliffParserTest extends UnitTestCase
31 {
35  protected ‪$xliffFileNames;
36 
40  protected ‪$cacheManagerProphecy;
41 
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48  // We have to take the whole relative path as otherwise this test fails on Windows systems
49  $fixturePath = ‪Environment::getFrameworkBasePath() . '/core/Tests/Unit/Localization/Parser/Fixtures/';
50  $this->xliffFileNames = [
51  'locallang' => $fixturePath . 'locallang.xlf',
52  'locallang_override' => $fixturePath . 'locallang_override.xlf',
53  'locallang_override_fr' => $fixturePath . 'fr.locallang_override.xlf'
54  ];
55  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xlf';
56 
57  $this->languageStoreProphecy = $this->prophesize(LanguageStore::class);
58  $this->cacheManagerProphecy = $this->prophesize(CacheManager::class);
59  $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
60  $this->cacheManagerProphecy->getCache('l10n')->willReturn($cacheFrontendProphecy->reveal());
61  $cacheFrontendProphecy->get(Argument::cetera())->willReturn(false);
62  $cacheFrontendProphecy->set(Argument::cetera())->willReturn(null);
63  $cacheFrontendProphecy->flush()->willReturn(null);
64  }
65 
69  public function ‪canParseXliffInEnglish()
70  {
71  $LOCAL_LANG = (new ‪XliffParser())->getParsedData($this->xliffFileNames['locallang'], 'default');
72  self::assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
73  $expectedLabels = [
74  'label1' => 'This is label #1',
75  'label2' => 'This is label #2',
76  'label3' => 'This is label #3'
77  ];
78  foreach ($expectedLabels as $key => $expectedLabel) {
79  self::assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
80  }
81  }
82 
86  public function ‪canParseXliffInFrench()
87  {
88  $LOCAL_LANG = (new ‪XliffParser())->getParsedData($this->xliffFileNames['locallang'], 'fr');
89  self::assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
90  $expectedLabels = [
91  'label1' => 'Ceci est le libellé no. 1',
92  'label2' => 'Ceci est le libellé no. 2',
93  'label3' => 'Ceci est le libellé no. 3'
94  ];
95  foreach ($expectedLabels as $key => $expectedLabel) {
96  self::assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
97  }
98  }
99 
103  public function ‪canOverrideXliff()
104  {
106  $factory = new ‪LocalizationFactory(new ‪LanguageStore(), $this->cacheManagerProphecy->reveal());
107 
108  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override'];
109  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['fr'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override_fr'];
110  $LOCAL_LANG = array_merge(
111  $factory->getParsedData($this->xliffFileNames['locallang'], 'default'),
112  $factory->getParsedData($this->xliffFileNames['locallang'], 'fr')
113  );
114  self::assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
115  self::assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
116  $expectedLabels = [
117  'default' => [
118  'label1' => 'This is my 1st label',
119  'label2' => 'This is my 2nd label',
120  'label3' => 'This is label #3'
121  ],
122  'fr' => [
123  'label1' => 'Ceci est mon 1er libellé',
124  'label2' => 'Ceci est le libellé no. 2',
125  'label3' => 'Ceci est mon 3e libellé'
126  ]
127  ];
128  foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
129  foreach ($expectedLanguageLabels as $key => $expectedLabel) {
130  self::assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
131  }
132  }
133  }
134 
141  public function ‪canOverrideXliffWithFrenchOnly()
142  {
143  $factory = new ‪LocalizationFactory(new ‪LanguageStore(), $this->cacheManagerProphecy->reveal());
144 
145  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['fr'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override_fr'];
146  $LOCAL_LANG = $factory->getParsedData($this->xliffFileNames['locallang'], 'fr');
147  self::assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
148  $expectedLabels = [
149  'label1' => 'Ceci est mon 1er libellé',
150  'label2' => 'Ceci est le libellé no. 2',
151  'label3' => 'Ceci est mon 3e libellé'
152  ];
153  foreach ($expectedLabels as $key => $expectedLabel) {
154  self::assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
155  }
156  }
157 }
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\XliffParserTest\canOverrideXliffWithFrenchOnly
‪canOverrideXliffWithFrenchOnly()
Definition: XliffParserTest.php:139
‪TYPO3\CMS\Core\Localization\LocalizationFactory
Definition: LocalizationFactory.php:28
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\XliffParserTest\canParseXliffInFrench
‪canParseXliffInFrench()
Definition: XliffParserTest.php:84
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser
Definition: XliffParserTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\XliffParserTest\canOverrideXliff
‪canOverrideXliff()
Definition: XliffParserTest.php:101
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\XliffParserTest\$xliffFileNames
‪array $xliffFileNames
Definition: XliffParserTest.php:34
‪TYPO3\CMS\Core\Core\Environment\getFrameworkBasePath
‪static string getFrameworkBasePath()
Definition: Environment.php:261
‪TYPO3\CMS\Core\Localization\LanguageStore
Definition: LanguageStore.php:27
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\XliffParserTest
Definition: XliffParserTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\XliffParserTest\canParseXliffInEnglish
‪canParseXliffInEnglish()
Definition: XliffParserTest.php:67
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\XliffParserTest\$cacheManagerProphecy
‪ObjectProphecy CacheManager $cacheManagerProphecy
Definition: XliffParserTest.php:38
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\XliffParserTest\setUp
‪setUp()
Definition: XliffParserTest.php:43
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Localization\Parser\XliffParser
Definition: XliffParser.php:23