2fc02ba4d62f07783415a4735536c6c5ce56ace6
[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\Tests\UnitTestCase;
22 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
23
24 /**
25 * Test case
26 */
27 class DatabaseSystemLanguageRowsTest extends UnitTestCase {
28
29 /**
30 * @var DatabaseSystemLanguageRows
31 */
32 protected $subject;
33 /**
34 * @var DatabaseConnection | ObjectProphecy
35 */
36 protected $dbProphecy;
37
38 public function setUp() {
39 $this->dbProphecy = $this->prophesize(DatabaseConnection::class);
40 $GLOBALS['TYPO3_DB'] = $this->dbProphecy->reveal();
41
42 $this->subject = new DatabaseSystemLanguageRows();
43 }
44
45 /**
46 * @test
47 */
48 public function addDataThrowsExceptionOnDatabaseError() {
49 $this->dbProphecy->exec_SELECTgetRows(Argument::cetera())->willReturn(NULL);
50 $this->dbProphecy->sql_error(Argument::cetera())->willReturn(NULL);
51 $this->setExpectedException(\UnexpectedValueException::class, $this->anything(), 1438170741);
52 $this->subject->addData([]);
53 }
54
55 /**
56 * @test
57 */
58 public function addDataSetsDefaultLanguageEntry() {
59 $expected = [
60 'systemLanguageRows' => [
61 0 => [
62 'uid' => 0,
63 'title' => 'Default Language',
64 'iso' => 'DEF',
65 ],
66 ],
67 ];
68 $this->dbProphecy->exec_SELECTgetRows(Argument::cetera())->willReturn([]);
69 $this->assertSame($expected, $this->subject->addData([]));
70 }
71
72 /**
73 * @test
74 */
75 public function addDataResolvesLanguageIsocodeFromDatabaseField() {
76 $dbRows = [
77 [
78 'uid' => 3,
79 'title' => 'french',
80 'language_isocode' => 'fr',
81 'static_lang_isocode' => '',
82 ],
83 ];
84 $this->dbProphecy->exec_SELECTgetRows('uid,title,language_isocode,static_lang_isocode', 'sys_language', 'pid=0 AND hidden=0')->willReturn($dbRows);
85 $expected = [
86 'systemLanguageRows' => [
87 0 => [
88 'uid' => 0,
89 'title' => 'Default Language',
90 'iso' => 'DEF',
91 ],
92 3 => [
93 'uid' => 3,
94 'title' => 'french',
95 'iso' => 'fr',
96 ],
97 ],
98 ];
99 $this->assertSame($expected, $this->subject->addData([]));
100 }
101
102 /**
103 * @test
104 */
105 public function addDataResolvesLanguageIsocodeFromStaticInfoTable() {
106 if (ExtensionManagementUtility::isLoaded('static_info_tables') === FALSE) {
107 $this->markTestSkipped('no ext:static_info_tables available');
108 }
109 $dbRows = [
110 [
111 'uid' => 3,
112 'title' => 'french',
113 'language_isocode' => '',
114 'static_lang_isocode' => 42,
115 ],
116 ];
117 $this->dbProphecy->exec_SELECTgetRows('uid,title,language_isocode,static_lang_isocode', 'sys_language', 'pid=0 AND hidden=0')->shouldBeCalled()->willReturn($dbRows);
118 // Needed for backendUtility::getRecord()
119 $GLOBALS['TCA']['static_languages'] = [ 'foo' ];
120 $this->dbProphecy->exec_SELECTgetSingleRow('lg_iso_2', 'static_languages', 'uid=42')->shouldBeCalled()->willReturn( [ 'lg_iso_2' => 'FR' ] );
121 $expected = [
122 'systemLanguageRows' => [
123 0 => [
124 'uid' => 0,
125 'title' => 'Default Language',
126 'iso' => 'DEF',
127 ],
128 3 => [
129 'uid' => 3,
130 'title' => 'french',
131 'iso' => 'FR',
132 ],
133 ],
134 ];
135 $this->assertSame($expected, $this->subject->addData([]));
136 }
137
138 }