‪TYPO3CMS  9.5
LocallangXmlParserTest.php
Go to the documentation of this file.
1 <?php
2 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 
18 use Prophecy\Argument;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪LocallangXmlParserTest extends UnitTestCase
32 {
36  protected function ‪setUp()
37  {
38  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xml';
39 
40  $cacheManagerProphecy = $this->prophesize(CacheManager::class);
41  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
42  $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
43  $cacheManagerProphecy->getCache('l10n')->willReturn($cacheFrontendProphecy->reveal());
44  $cacheFrontendProphecy->get(Argument::cetera())->willReturn(false);
45  $cacheFrontendProphecy->set(Argument::cetera())->willReturn(null);
46 
47  GeneralUtility::makeInstance(LanguageStore::class)->initialize();
48  }
52  protected function ‪tearDown()
53  {
54  GeneralUtility::purgeInstances();
55  parent::tearDown();
56  }
57 
58  protected static function ‪getFixtureFilePath($filename)
59  {
60  // We have to take the whole relative path as otherwise this test fails on Windows systems
61  return ‪Environment::getFrameworkBasePath() . '/core/Tests/Unit/Localization/Parser/Fixtures/' . $filename;
62  }
63 
67  public function ‪canParseLlxmlInEnglish()
68  {
69  $LOCAL_LANG = (new ‪LocallangXmlParser)->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default');
70  $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
71  $expectedLabels = [
72  'label1' => 'This is label #1',
73  'label2' => 'This is label #2',
74  'label3' => 'This is label #3'
75  ];
76  foreach ($expectedLabels as $key => $expectedLabel) {
77  $this->assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
78  }
79  }
80 
84  public function ‪canParseLlxmlInMd5Code()
85  {
86  $LOCAL_LANG = (new ‪LocallangXmlParser)->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5');
87  $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
88  $expectedLabels = [
89  'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
90  'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
91  'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
92  ];
93  foreach ($expectedLabels as $key => $expectedLabel) {
94  $this->assertEquals($expectedLabel, $LOCAL_LANG['md5'][$key][0]['target']);
95  }
96  }
97 
102  {
103  $localLang = (new ‪LocallangXmlParser)->getParsedData(
104  self::getFixtureFilePath('locallangOnlyDefaultLanguage.xml'),
105  'fr'
106  );
107  // This test case is odd: The system under test does NOT
108  // return 'target' at all if there is no such translation.
109  // @todo: Either change / fix subject, or adapt test and test name!
110  $this->assertNull($localLang['fr']['label1'][0]['target'] ?? null);
111  $this->assertNull($localLang['fr']['label2'][0]['target'] ?? null);
112  $this->assertNull($localLang['fr']['label3'][0]['target'] ?? null);
113  }
114 
118  public function ‪canOverrideLlxml()
119  {
121  $factory = new ‪LocalizationFactory;
122 
123  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][‪self::getFixtureFilePath('locallang.xml')][] = ‪self::getFixtureFilePath('locallang_override.xml');
124  $LOCAL_LANG = array_merge(
125  $factory->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default'),
126  $factory->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5')
127  );
128  $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
129  $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
130  $expectedLabels = [
131  'default' => [
132  'label1' => 'This is my 1st label',
133  'label2' => 'This is my 2nd label',
134  'label3' => 'This is label #3'
135  ],
136  'md5' => [
137  'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
138  'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
139  'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
140  ]
141  ];
142  foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
143  foreach ($expectedLanguageLabels as $key => $expectedLabel) {
144  $this->assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
145  }
146  }
147  }
148 
149  public function ‪numericKeysDataProvider()
150  {
151  return [
152  'Numeric key 1' => [
153  1,
154  'This is label #1 [FR]'
155  ],
156  'Numeric key 2' => [
157  2,
158  'This is label #2 [FR]'
159  ],
160  'Numeric key 3' => [
161  3,
162  'This is label #3 [FR]'
163  ],
164  'Numeric key 5' => [
165  5,
166  'This is label #5 [FR]'
167  ],
168  'Numeric key 10' => [
169  10,
170  'This is label #10 [FR]'
171  ],
172  ];
173  }
174 
179  public function ‪canTranslateNumericKeys($key, $expectedResult)
180  {
182  $factory = new ‪LocalizationFactory;
183 
184  $LOCAL_LANG = $factory->‪getParsedData(self::getFixtureFilePath('locallangNumericKeys.xml'), 'fr');
185 
186  $this->assertEquals($expectedResult, $LOCAL_LANG['fr'][$key][0]['target']);
187  }
188 }
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\canTranslateNumericKeys
‪canTranslateNumericKeys($key, $expectedResult)
Definition: LocallangXmlParserTest.php:179
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\canParseLlxmlInEnglish
‪canParseLlxmlInEnglish()
Definition: LocallangXmlParserTest.php:67
‪TYPO3\CMS\Core\Localization\LocalizationFactory
Definition: LocalizationFactory.php:25
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\canParseLlxmlInMd5Code
‪canParseLlxmlInMd5Code()
Definition: LocallangXmlParserTest.php:84
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\tearDown
‪tearDown()
Definition: LocallangXmlParserTest.php:52
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser
Definition: LocallangXmlParserTest.php:3
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest
Definition: LocallangXmlParserTest.php:32
‪TYPO3\CMS\Core\Localization\LocalizationFactory\getParsedData
‪array bool getParsedData($fileReference, $languageKey, $charset='', $errorMode=null, $isLocalizationOverride=false)
Definition: LocalizationFactory.php:61
‪TYPO3\CMS\Core\Core\Environment\getFrameworkBasePath
‪static string getFrameworkBasePath()
Definition: Environment.php:234
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\canParseLlxmlInFrenchAndReturnsNullLabelsIfNoTranslationIsFound
‪canParseLlxmlInFrenchAndReturnsNullLabelsIfNoTranslationIsFound()
Definition: LocallangXmlParserTest.php:101
‪TYPO3\CMS\Core\Localization\LanguageStore
Definition: LanguageStore.php:26
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\numericKeysDataProvider
‪numericKeysDataProvider()
Definition: LocallangXmlParserTest.php:149
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\getFixtureFilePath
‪static getFixtureFilePath($filename)
Definition: LocallangXmlParserTest.php:58
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:34
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\canOverrideLlxml
‪canOverrideLlxml()
Definition: LocallangXmlParserTest.php:118
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Tests\Unit\Localization\Parser\LocallangXmlParserTest\setUp
‪setUp()
Definition: LocallangXmlParserTest.php:36
‪TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser
Definition: LocallangXmlParser.php:27