[TASK] Namespace and move t3lib unit tests - 4
[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 * @package TYPO3
32 */
33 class LocallangXmlParserTest extends \tx_phpunit_testcase {
34
35 /**
36 * @var \TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser
37 */
38 protected $parser;
39
40 /**
41 * @var array
42 */
43 protected $locallangXMLOverride;
44
45 /**
46 * @var string
47 */
48 protected $l10nPriority;
49
50 /**
51 * @var array
52 */
53 protected $llxmlFileNames;
54
55 /**
56 * Prepares the environment before running a test.
57 */
58 public function setUp() {
59 // Backup locallangXMLOverride and localization format priority
60 $this->locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
61 $this->l10nPriority = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'];
62 $this->parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Parser\\LocallangXmlParser');
63
64 // We have to take the whole relative path as otherwise this test fails on Windows systems
65 $fixturePath = PATH_site . 'typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/';
66 $this->llxmlFileNames = array(
67 'locallang' => $fixturePath . 'locallang.xml',
68 'locallang_override' => $fixturePath . 'locallang_override.xml',
69 'locallangOnlyDefaultLanguage' => $fixturePath . 'locallangOnlyDefaultLanguage.xml'
70 );
71 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xml';
72 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore')->initialize();
73 // Clear localization cache
74 $GLOBALS['typo3CacheManager']->getCache('t3lib_l10n')->flush();
75 }
76
77 /**
78 * Cleans up the environment after running a test.
79 */
80 public function tearDown() {
81 unset($this->parser);
82 // Restore locallangXMLOverride and localization format priority
83 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'] = $this->locallangXMLOverride;
84 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = $this->l10nPriority;
85 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore')->initialize();
86 }
87
88 /**
89 * @test
90 */
91 public function canParseLlxmlInEnglish() {
92 $LOCAL_LANG = $this->parser->getParsedData($this->llxmlFileNames['locallang'], 'default');
93 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
94 $expectedLabels = array(
95 'label1' => 'This is label #1',
96 'label2' => 'This is label #2',
97 'label3' => 'This is label #3'
98 );
99 foreach ($expectedLabels as $key => $expectedLabel) {
100 $this->assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
101 }
102 }
103
104 /**
105 * @test
106 */
107 public function canParseLlxmlInFrench() {
108 $LOCAL_LANG = $this->parser->getParsedData($this->llxmlFileNames['locallang'], 'fr');
109 $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
110 $expectedLabels = array(
111 'label1' => 'Ceci est le libellé no. 1',
112 'label2' => 'Ceci est le libellé no. 2',
113 'label3' => 'Ceci est le libellé no. 3'
114 );
115 foreach ($expectedLabels as $key => $expectedLabel) {
116 $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
117 }
118 }
119
120 /**
121 * @test
122 */
123 public function canParseLlxmlInFrenchAndReturnsDefaultLabelsIfNoTranslationIsFound() {
124 $LOCAL_LANG = $this->parser->getParsedData($this->llxmlFileNames['locallangOnlyDefaultLanguage'], 'fr');
125 $expectedLabels = array(
126 'label1' => 'This is label #1',
127 'label2' => 'This is label #2',
128 'label3' => 'This is label #3'
129 );
130 foreach ($expectedLabels as $key => $expectedLabel) {
131 $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
132 }
133 }
134
135 /**
136 * @test
137 */
138 public function canOverrideLlxml() {
139 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$this->llxmlFileNames['locallang']][] = $this->llxmlFileNames['locallang_override'];
140 $LOCAL_LANG = array_merge(\TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($this->llxmlFileNames['locallang'], 'default'), \TYPO3\CMS\Core\Utility\GeneralUtility::readLLfile($this->llxmlFileNames['locallang'], 'fr'));
141 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
142 $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
143 $expectedLabels = array(
144 'default' => array(
145 'label1' => 'This is my 1st label',
146 'label2' => 'This is my 2nd label',
147 'label3' => 'This is label #3'
148 ),
149 'fr' => array(
150 'label1' => 'Ceci est mon 1er libellé',
151 'label2' => 'Ceci est le libellé no. 2',
152 'label3' => 'Ceci est mon 3e libellé'
153 )
154 );
155 foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
156 foreach ($expectedLanguageLabels as $key => $expectedLabel) {
157 $this->assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
158 }
159 }
160 }
161
162 }
163
164 ?>