f5ec1bd50ac0df26e600f48d2cdf28befd2af670
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Localization / Parser / LocallangXmlParserTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Localization\Parser;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Xavier Perseguers <xavier@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Testcase for class \TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser.
29 *
30 * @author Xavier Perseguers <xavier@typo3.org>
31 */
32 class LocallangXmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * @var \TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser
36 */
37 protected $parser;
38
39 /**
40 * @var array
41 */
42 protected $locallangXMLOverride;
43
44 /**
45 * @var string
46 */
47 protected $l10nPriority;
48
49 /**
50 * @var array
51 */
52 protected $llxmlFileNames;
53
54 /**
55 * Prepares the environment before running a test.
56 */
57 public function setUp() {
58 // Backup locallangXMLOverride and localization format priority
59 $this->locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
60 $this->l10nPriority = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'];
61 $this->parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Parser\\LocallangXmlParser');
62
63 // We have to take the whole relative path as otherwise this test fails on Windows systems
64 $fixturePath = PATH_site . 'typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/';
65 $this->llxmlFileNames = array(
66 'locallang' => $fixturePath . 'locallang.xml',
67 'locallang_override' => $fixturePath . 'locallang_override.xml',
68 'locallangOnlyDefaultLanguage' => $fixturePath . 'locallangOnlyDefaultLanguage.xml'
69 );
70 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xml';
71 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore')->initialize();
72 // Clear localization cache
73 $GLOBALS['typo3CacheManager']->getCache('t3lib_l10n')->flush();
74 }
75
76 /**
77 * Cleans up the environment after running a test.
78 */
79 public function tearDown() {
80 unset($this->parser);
81 // Restore locallangXMLOverride and localization format priority
82 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'] = $this->locallangXMLOverride;
83 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = $this->l10nPriority;
84 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore')->initialize();
85 }
86
87 /**
88 * @test
89 */
90 public function canParseLlxmlInEnglish() {
91 $LOCAL_LANG = $this->parser->getParsedData($this->llxmlFileNames['locallang'], 'default');
92 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
93 $expectedLabels = array(
94 'label1' => 'This is label #1',
95 'label2' => 'This is label #2',
96 'label3' => 'This is label #3'
97 );
98 foreach ($expectedLabels as $key => $expectedLabel) {
99 $this->assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
100 }
101 }
102
103 /**
104 * @test
105 */
106 public function canParseLlxmlInFrench() {
107 $LOCAL_LANG = $this->parser->getParsedData($this->llxmlFileNames['locallang'], 'fr');
108 $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
109 $expectedLabels = array(
110 'label1' => 'Ceci est le libellé no. 1',
111 'label2' => 'Ceci est le libellé no. 2',
112 'label3' => 'Ceci est le libellé no. 3'
113 );
114 foreach ($expectedLabels as $key => $expectedLabel) {
115 $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
116 }
117 }
118
119 /**
120 * @test
121 */
122 public function canParseLlxmlInFrenchAndReturnsNullLabelsIfNoTranslationIsFound() {
123 $LOCAL_LANG = $this->parser->getParsedData($this->llxmlFileNames['locallangOnlyDefaultLanguage'], 'fr');
124 $expectedLabels = array(
125 'label1' => NULL,
126 'label2' => NULL,
127 'label3' => NULL
128 );
129 foreach ($expectedLabels as $key => $expectedLabel) {
130 $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
131 }
132 }
133
134 /**
135 * @test
136 */
137 public function canOverrideLlxml() {
138 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$this->llxmlFileNames['locallang']][] = $this->llxmlFileNames['locallang_override'];
139 $LOCAL_LANG = array_merge(\TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($this->llxmlFileNames['locallang'], 'default'), \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($this->llxmlFileNames['locallang'], 'fr'));
140 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
141 $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
142 $expectedLabels = array(
143 'default' => array(
144 'label1' => 'This is my 1st label',
145 'label2' => 'This is my 2nd label',
146 'label3' => 'This is label #3'
147 ),
148 'fr' => array(
149 'label1' => 'Ceci est mon 1er libellé',
150 'label2' => 'Ceci est le libellé no. 2',
151 'label3' => 'Ceci est mon 3e libellé'
152 )
153 );
154 foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
155 foreach ($expectedLanguageLabels as $key => $expectedLabel) {
156 $this->assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
157 }
158 }
159 }
160
161 }
162
163 ?>