010784a6f7d47951053238505f0841d623df54c9
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Localization / LocalizationFactoryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Localization;
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 */
16
17 use TYPO3\CMS\Core\Localization\LocalizationFactory;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Test case
22 */
23 class LocalizationFactoryTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
24 {
25 /**
26 * @test
27 */
28 public function getParsedDataHandlesLocallangXMLOverride()
29 {
30 /** @var $subject LocalizationFactory */
31 $subject = new LocalizationFactory;
32
33 $unique = 'locallangXMLOverrideTest' . substr($this->getUniqueId(), 0, 10);
34 $xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
35 <T3locallang>
36 <data type="array">
37 <languageKey index="default" type="array">
38 <label index="buttons.logout">EXIT</label>
39 </languageKey>
40 </data>
41 </T3locallang>';
42 $file = PATH_site . 'typo3temp/var/tests/' . $unique . '.xml';
43 GeneralUtility::writeFileToTypo3tempDir($file, $xml);
44 // Make sure there is no cached version of the label
45 GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('l10n')->flush();
46 // Get default value
47 $defaultLL = $subject->getParsedData('EXT:lang/Resources/Private/Language/locallang_core.xlf', 'default');
48 // Clear language cache again
49 GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('l10n')->flush();
50 // Set override file
51 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['EXT:lang/Resources/Private/Language/locallang_core.xlf'][$unique] = $file;
52 /** @var $store \TYPO3\CMS\Core\Localization\LanguageStore */
53 $store = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageStore::class);
54 $store->flushData('EXT:lang/Resources/Private/Language/locallang_core.xlf');
55 // Get override value
56 $overrideLL = $subject->getParsedData('EXT:lang/Resources/Private/Language/locallang_core.xlf', 'default');
57 // Clean up again
58 unlink($file);
59 $this->assertNotEquals($overrideLL['default']['buttons.logout'][0]['target'], '');
60 $this->assertNotEquals($defaultLL['default']['buttons.logout'][0]['target'], $overrideLL['default']['buttons.logout'][0]['target']);
61 $this->assertEquals($overrideLL['default']['buttons.logout'][0]['target'], 'EXIT');
62 }
63
64 /**
65 * @test
66 */
67 public function getParsedDataCallsLocalizationOverrideIfFileNotFoundExceptionIsThrown()
68 {
69 /** @var $subject LocalizationFactory */
70 $localizationFactory = $this->getAccessibleMock(LocalizationFactory::class, ['localizationOverride']);
71 $languageStore = $this->getMockBuilder(\TYPO3\CMS\Core\Localization\LanguageStore::class)
72 ->setMethods(['hasData', 'setConfiguration', 'getData', 'setData'])
73 ->getMock();
74 $cacheInstance = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
75 ->setMethods(['get', 'set'])
76 ->disableOriginalConstructor()
77 ->getMock();
78 $localizationFactory->_set('store', $languageStore);
79 $localizationFactory->_set('cacheInstance', $cacheInstance);
80 $languageStore->method('hasData')->willReturn(false);
81 $languageStore->method('getData')->willReturn([]);
82 $languageStore->method('setConfiguration')->willThrowException(new \TYPO3\CMS\Core\Localization\Exception\FileNotFoundException('testing', 1476049512));
83 $cacheInstance->method('get')->willReturn(false);
84 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'] = ['foo' => 'bar'];
85
86 $localizationFactory->expects($this->once())->method('localizationOverride');
87 $localizationFactory->getParsedData('EXT:backend/Resources/Private/Language/locallang_layout.xlf', 'default');
88 }
89 }