‪TYPO3CMS  10.4
LocallangXmlParserTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Prophecy\Argument;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
33 class ‪LocallangXmlParserTest extends UnitTestCase
34 {
38  protected ‪$cacheManagerProphecy;
39 
43  protected function ‪setUp(): void
44  {
45  parent::setUp();
46  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xml';
47  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser']['xml'] = \TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser::class;
48 
49  $this->cacheManagerProphecy = $this->prophesize(CacheManager::class);
50  $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
51  $this->cacheManagerProphecy->getCache('l10n')->willReturn($cacheFrontendProphecy->reveal());
52  $cacheFrontendProphecy->get(Argument::cetera())->willReturn(false);
53  $cacheFrontendProphecy->set(Argument::cetera())->willReturn(null);
54 
55  GeneralUtility::makeInstance(LanguageStore::class)->initialize();
56  }
60  protected function ‪tearDown(): void
61  {
62  GeneralUtility::purgeInstances();
63  parent::tearDown();
64  }
65 
66  protected static function ‪getFixtureFilePath($filename)
67  {
68  // We have to take the whole relative path as otherwise this test fails on Windows systems
69  return ‪Environment::getFrameworkBasePath() . '/core/Tests/UnitDeprecated/Localization/Parser/Fixtures/' . $filename;
70  }
71 
75  public function ‪canParseLlxmlInEnglish()
76  {
77  $LOCAL_LANG = (new ‪LocallangXmlParser())->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default');
78  self::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  self::assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
86  }
87  }
88 
92  public function ‪canParseLlxmlInMd5Code()
93  {
94  $LOCAL_LANG = (new LocallangXmlParser())->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5');
95  self::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  self::assertEquals($expectedLabel, $LOCAL_LANG['md5'][$key][0]['target']);
103  }
104  }
105 
110  {
111  $localLang = (new LocallangXmlParser())->getParsedData(
112  self::getFixtureFilePath('locallangOnlyDefaultLanguage.xml'),
113  'fr'
114  );
115  // This test case is odd: The system under test does NOT
116  // return 'target' at all if there is no such translation.
117  // @todo: Either change / fix subject, or adapt test and test name!
118  self::assertNull($localLang['fr']['label1'][0]['target'] ?? null);
119  self::assertNull($localLang['fr']['label2'][0]['target'] ?? null);
120  self::assertNull($localLang['fr']['label3'][0]['target'] ?? null);
121  }
122 
126  public function ‪canOverrideLlxml()
127  {
129  $factory = new LocalizationFactory(new LanguageStore(), $this->cacheManagerProphecy->reveal());
130 
131  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][‪self::getFixtureFilePath('locallang.xml')][] = ‪self::getFixtureFilePath('locallang_override.xml');
132  $LOCAL_LANG = array_merge(
133  $factory->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default'),
134  $factory->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5')
135  );
136  self::assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
137  self::assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
138  $expectedLabels = [
139  'default' => [
140  'label1' => 'This is my 1st label',
141  'label2' => 'This is my 2nd label',
142  'label3' => 'This is label #3'
143  ],
144  'md5' => [
145  'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
146  'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
147  'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
148  ]
149  ];
150  foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
151  foreach ($expectedLanguageLabels as $key => $expectedLabel) {
152  self::assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
153  }
154  }
155  }
156 
157  public function ‪numericKeysDataProvider()
158  {
159  return [
160  'Numeric key 1' => [
161  1,
162  'This is label #1 [FR]'
163  ],
164  'Numeric key 2' => [
165  2,
166  'This is label #2 [FR]'
167  ],
168  'Numeric key 3' => [
169  3,
170  'This is label #3 [FR]'
171  ],
172  'Numeric key 5' => [
173  5,
174  'This is label #5 [FR]'
175  ],
176  'Numeric key 10' => [
177  10,
178  'This is label #10 [FR]'
179  ],
180  ];
181  }
182 
187  public function ‪canTranslateNumericKeys($key, $expectedResult)
188  {
190  $factory = new ‪LocalizationFactory(new ‪LanguageStore(), $this->cacheManagerProphecy->reveal());
191 
192  $LOCAL_LANG = $factory->getParsedData(self::getFixtureFilePath('locallangNumericKeys.xml'), 'fr');
193 
194  self::assertEquals($expectedResult, $LOCAL_LANG['fr'][$key][0]['target']);
195  }
196 }
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\canTranslateNumericKeys
‪canTranslateNumericKeys($key, $expectedResult)
Definition: LocallangXmlParserTest.php:186
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\$cacheManagerProphecy
‪ObjectProphecy CacheManager $cacheManagerProphecy
Definition: LocallangXmlParserTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\canParseLlxmlInEnglish
‪canParseLlxmlInEnglish()
Definition: LocallangXmlParserTest.php:74
‪TYPO3\CMS\Core\Localization\LocalizationFactory
Definition: LocalizationFactory.php:28
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\canParseLlxmlInMd5Code
‪canParseLlxmlInMd5Code()
Definition: LocallangXmlParserTest.php:91
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\tearDown
‪tearDown()
Definition: LocallangXmlParserTest.php:59
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser
Definition: XliffParserTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest
Definition: LocallangXmlParserTest.php:34
‪TYPO3\CMS\Core\Core\Environment\getFrameworkBasePath
‪static string getFrameworkBasePath()
Definition: Environment.php:261
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\canParseLlxmlInFrenchAndReturnsNullLabelsIfNoTranslationIsFound
‪canParseLlxmlInFrenchAndReturnsNullLabelsIfNoTranslationIsFound()
Definition: LocallangXmlParserTest.php:108
‪TYPO3\CMS\Core\Localization\LanguageStore
Definition: LanguageStore.php:27
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\numericKeysDataProvider
‪numericKeysDataProvider()
Definition: LocallangXmlParserTest.php:156
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\getFixtureFilePath
‪static getFixtureFilePath($filename)
Definition: LocallangXmlParserTest.php:65
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪$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\Tests\Unit\Localization\Parser\LocallangXmlParserTest\canOverrideLlxml
‪canOverrideLlxml()
Definition: LocallangXmlParserTest.php:125
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\setUp
‪setUp()
Definition: LocallangXmlParserTest.php:42
‪TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser
Definition: LocallangXmlParser.php:29