[BUGFIX] Notify user about insufficient language configuration
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / DatabaseSystemLanguageRowsTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
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 Prophecy\Argument;
18 use Prophecy\Prophecy\ObjectProphecy;
19 use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseSystemLanguageRows;
20 use TYPO3\CMS\Core\Database\DatabaseConnection;
21 use TYPO3\CMS\Core\Messaging\FlashMessage;
22 use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
23 use TYPO3\CMS\Core\Messaging\FlashMessageService;
24 use TYPO3\CMS\Core\Tests\UnitTestCase;
25 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Lang\LanguageService;
28
29 /**
30 * Test case
31 */
32 class DatabaseSystemLanguageRowsTest extends UnitTestCase {
33
34 /**
35 * @var DatabaseSystemLanguageRows
36 */
37 protected $subject;
38 /**
39 * @var DatabaseConnection | ObjectProphecy
40 */
41 protected $dbProphecy;
42
43 public function setUp() {
44 $this->dbProphecy = $this->prophesize(DatabaseConnection::class);
45 $GLOBALS['TYPO3_DB'] = $this->dbProphecy->reveal();
46
47 $this->subject = new DatabaseSystemLanguageRows();
48 }
49
50 /**
51 * @test
52 */
53 public function addDataThrowsExceptionOnDatabaseError() {
54 $this->dbProphecy->exec_SELECTgetRows(Argument::cetera())->willReturn(NULL);
55 $this->dbProphecy->sql_error(Argument::cetera())->willReturn(NULL);
56 $this->setExpectedException(\UnexpectedValueException::class, $this->anything(), 1438170741);
57 $this->subject->addData([]);
58 }
59
60 /**
61 * @test
62 */
63 public function addDataSetsDefaultLanguageEntry() {
64 $expected = [
65 'systemLanguageRows' => [
66 0 => [
67 'uid' => 0,
68 'title' => 'Default Language',
69 'iso' => 'DEF',
70 ],
71 ],
72 ];
73 $this->dbProphecy->exec_SELECTgetRows(Argument::cetera())->willReturn([]);
74 $this->assertSame($expected, $this->subject->addData([]));
75 }
76
77 /**
78 * @test
79 */
80 public function addDataResolvesLanguageIsocodeFromDatabaseField() {
81 $dbRows = [
82 [
83 'uid' => 3,
84 'title' => 'french',
85 'language_isocode' => 'fr',
86 'static_lang_isocode' => '',
87 ],
88 ];
89 $this->dbProphecy->exec_SELECTgetRows('uid,title,language_isocode,static_lang_isocode', 'sys_language', 'pid=0 AND hidden=0')->willReturn($dbRows);
90 $expected = [
91 'systemLanguageRows' => [
92 0 => [
93 'uid' => 0,
94 'title' => 'Default Language',
95 'iso' => 'DEF',
96 ],
97 3 => [
98 'uid' => 3,
99 'title' => 'french',
100 'iso' => 'fr',
101 ],
102 ],
103 ];
104 $this->assertSame($expected, $this->subject->addData([]));
105 }
106
107 /**
108 * @test
109 */
110 public function addDataResolvesLanguageIsocodeFromStaticInfoTable() {
111 if (ExtensionManagementUtility::isLoaded('static_info_tables') === FALSE) {
112 $this->markTestSkipped('no ext:static_info_tables available');
113 }
114 $dbRows = [
115 [
116 'uid' => 3,
117 'title' => 'french',
118 'language_isocode' => '',
119 'static_lang_isocode' => 42,
120 ],
121 ];
122 $this->dbProphecy->exec_SELECTgetRows('uid,title,language_isocode,static_lang_isocode', 'sys_language', 'pid=0 AND hidden=0')->shouldBeCalled()->willReturn($dbRows);
123 // Needed for backendUtility::getRecord()
124 $GLOBALS['TCA']['static_languages'] = [ 'foo' ];
125 $this->dbProphecy->exec_SELECTgetSingleRow('lg_iso_2', 'static_languages', 'uid=42')->shouldBeCalled()->willReturn( [ 'lg_iso_2' => 'FR' ] );
126 $expected = [
127 'systemLanguageRows' => [
128 0 => [
129 'uid' => 0,
130 'title' => 'Default Language',
131 'iso' => 'DEF',
132 ],
133 3 => [
134 'uid' => 3,
135 'title' => 'french',
136 'iso' => 'FR',
137 ],
138 ],
139 ];
140 $this->assertSame($expected, $this->subject->addData([]));
141 }
142
143 /**
144 * @test
145 */
146 public function addDataAddFlashMessageWithMissingIsoCode() {
147 $dbRows = [
148 [
149 'uid' => 3,
150 'title' => 'french',
151 'language_isocode' => '',
152 'static_lang_isocode' => '',
153 ],
154 ];
155 $this->dbProphecy->exec_SELECTgetRows('uid,title,language_isocode,static_lang_isocode', 'sys_language', 'pid=0 AND hidden=0')->shouldBeCalled()->willReturn($dbRows);
156 // Needed for backendUtility::getRecord()
157 $GLOBALS['TCA']['static_languages'] = [ 'foo' ];
158 $expected = [
159 'systemLanguageRows' => [
160 0 => [
161 'uid' => 0,
162 'title' => 'Default Language',
163 'iso' => 'DEF',
164 ],
165 3 => [
166 'uid' => 3,
167 'title' => 'french',
168 'iso' => '',
169 ],
170 ],
171 ];
172
173 $languageService = $this->prophesize(LanguageService::class);
174 $GLOBALS['LANG'] = $languageService->reveal();
175 $languageService->sL(Argument::cetera())->willReturnArgument(0);
176
177 /** @var FlashMessage|ObjectProphecy $flashMessage */
178 $flashMessage = $this->prophesize(FlashMessage::class);
179 GeneralUtility::addInstance(FlashMessage::class, $flashMessage->reveal());
180 /** @var FlashMessageService|ObjectProphecy $flashMessageService */
181 $flashMessageService = $this->prophesize(FlashMessageService::class);
182 GeneralUtility::setSingletonInstance(FlashMessageService::class, $flashMessageService->reveal());
183 /** @var FlashMessageQueue|ObjectProphecy $flashMessageQueue */
184 $flashMessageQueue = $this->prophesize(FlashMessageQueue::class);
185 $flashMessageService->getMessageQueueByIdentifier(Argument::cetera())->willReturn($flashMessageQueue->reveal());
186
187 $flashMessageQueue->enqueue($flashMessage)->shouldBeCalled();
188
189 $this->assertSame($expected, $this->subject->addData([]));
190 }
191
192 }