‪TYPO3CMS  ‪main
BackendUtilityTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Platforms\PostgreSQLPlatform as DoctrinePostgreSQLPlatform;
21 use Doctrine\DBAL\Platforms\SQLitePlatform as DoctrineSQLitePlatform;
22 use PHPUnit\Framework\Attributes\DataProvider;
23 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\CMS\Backend\Utility\BackendUtility;
30 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
31 
32 final class ‪BackendUtilityTest extends FunctionalTestCase
33 {
35 
36  protected const ‪LANGUAGE_PRESETS = [
37  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
38  'DA' => ['id' => 1, 'title' => 'Dansk', 'locale' => 'da_DK.UTF8'],
39  'DE' => ['id' => 2, 'title' => 'German', 'locale' => 'de_DE.UTF8'],
40  ];
41 
43 
44  public function ‪setUp(): void
45  {
46  parent::setUp();
47  $this->importCSVDataSet(__DIR__ . '/../Fixtures/pages.csv');
48  $this->importCSVDataSet(__DIR__ . '/../Fixtures/tt_content.csv');
49  $this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users.csv');
50  $this->backendUser = $this->setUpBackendUser(1);
51  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($this->backendUser);
52  }
53 
54  #[Test]
55  public function ‪givenPageIdCanBeExpanded(): void
56  {
57  $this->backendUser->groupData['webmounts'] = '1';
58 
59  BackendUtility::openPageTree(5, false);
60 
61  $expectedSiteHash = [
62  '1_5' => '1',
63  '1_1' => '1',
64  '1_0' => '1',
65  ];
66  $actualSiteHash = $this->backendUser->uc['BackendComponents']['States']['Pagetree']['stateHash'];
67  self::assertSame($expectedSiteHash, $actualSiteHash);
68  }
69 
70  #[Test]
71  public function ‪otherBranchesCanBeClosedWhenOpeningPage(): void
72  {
73  $this->backendUser->groupData['webmounts'] = '1';
74 
75  BackendUtility::openPageTree(5, false);
76  BackendUtility::openPageTree(4, true);
77 
78  //the complete branch of uid => 5 should be closed here
79  $expectedSiteHash = [
80  '1_4' => '1',
81  '1_3' => '1',
82  '1_2' => '1',
83  '1_1' => '1',
84  '1_0' => '1',
85  ];
86  $actualSiteHash = $this->backendUser->uc['BackendComponents']['States']['Pagetree']['stateHash'];
87  self::assertSame($expectedSiteHash, $actualSiteHash);
88  }
89 
90  #[Test]
91  public function ‪getProcessedValueForLanguage(): void
92  {
94  'website-local',
95  $this->‪buildSiteConfiguration(1, 'http://localhost/'),
96  [
97  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
98  $this->‪buildLanguageConfiguration('DA', '/da/'),
99  $this->‪buildLanguageConfiguration('DE', '/de/'),
100  ]
101  );
102 
103  self::assertEquals(
104  'Dansk',
105  BackendUtility::getProcessedValue(
106  'pages',
107  'sys_language_uid',
108  '1',
109  0,
110  false,
111  false,
112  1
113  )
114  );
115 
116  self::assertEquals(
117  'German',
118  BackendUtility::getProcessedValue(
119  'tt_content',
120  'sys_language_uid',
121  '2',
122  0,
123  false,
124  false,
125  1
126  )
127  );
128  }
129 
130  #[Test]
131  public function ‪getRecordTitleForUidLabel(): void
132  {
133  ‪$GLOBALS['TCA']['tt_content']['ctrl']['label'] = 'uid';
134  unset(‪$GLOBALS['TCA']['tt_content']['ctrl']['label_alt']);
135 
136  self::assertEquals(
137  '1',
138  BackendUtility::getRecordTitle('tt_content', BackendUtility::getRecord('tt_content', 1))
139  );
140  }
141 
142  public static function ‪enableFieldsStatementIsCorrectDataProvider(): array
143  {
144  // Expected sql should contain identifier escaped in mysql/mariadb identifier quotings "`", which are
145  // replaced by corresponding quoting values for other database systems.
146  return [
147  'disabled' => [
148  [
149  'disabled' => 'disabled',
150  ],
151  false,
152  ' AND `${tableName}`.`disabled` = 0',
153  ],
154  'starttime' => [
155  [
156  'starttime' => 'starttime',
157  ],
158  false,
159  ' AND `${tableName}`.`starttime` <= 1234567890',
160  ],
161  'endtime' => [
162  [
163  'endtime' => 'endtime',
164  ],
165  false,
166  ' AND ((`${tableName}`.`endtime` = 0) OR (`${tableName}`.`endtime` > 1234567890))',
167  ],
168  'disabled, starttime, endtime' => [
169  [
170  'disabled' => 'disabled',
171  'starttime' => 'starttime',
172  'endtime' => 'endtime',
173  ],
174  false,
175  ' AND ((`${tableName}`.`disabled` = 0) AND (`${tableName}`.`starttime` <= 1234567890) AND (((`${tableName}`.`endtime` = 0) OR (`${tableName}`.`endtime` > 1234567890))))',
176  ],
177  'disabled inverted' => [
178  [
179  'disabled' => 'disabled',
180  ],
181  true,
182  ' AND `${tableName}`.`disabled` <> 0',
183  ],
184  'starttime inverted' => [
185  [
186  'starttime' => 'starttime',
187  ],
188  true,
189  ' AND ((`${tableName}`.`starttime` <> 0) AND (`${tableName}`.`starttime` > 1234567890))',
190  ],
191  'endtime inverted' => [
192  [
193  'endtime' => 'endtime',
194  ],
195  true,
196  ' AND ((`${tableName}`.`endtime` <> 0) AND (`${tableName}`.`endtime` <= 1234567890))',
197  ],
198  'disabled, starttime, endtime inverted' => [
199  [
200  'disabled' => 'disabled',
201  'starttime' => 'starttime',
202  'endtime' => 'endtime',
203  ],
204  true,
205  ' AND ((`${tableName}`.`disabled` <> 0) OR (((`${tableName}`.`starttime` <> 0) AND (`${tableName}`.`starttime` > 1234567890))) OR (((`${tableName}`.`endtime` <> 0) AND (`${tableName}`.`endtime` <= 1234567890))))',
206  ],
207  ];
208  }
209 
210  #[DataProvider('enableFieldsStatementIsCorrectDataProvider')]
211  #[Test]
212  public function ‪enableFieldsStatementIsCorrect(array $enableColumns, bool $inverted, string $expectation): void
213  {
214  $platform = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionByName(‪ConnectionPool::DEFAULT_CONNECTION_NAME)->getDatabasePlatform();
215  $tableName = uniqid('table');
216  ‪$GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'] = $enableColumns;
217  ‪$GLOBALS['SIM_ACCESS_TIME'] = 1234567890;
218  $statement = BackendUtility::BEenableFields($tableName, $inverted);
219  $replaces = [
220  '${tableName}' => $tableName,
221  ];
222  // replace mysql identifier quoting with sqlite identifier quoting in expected sql string
223  if ($platform instanceof DoctrineSQLitePlatform || $platform instanceof DoctrinePostgreSQLPlatform) {
224  $replaces['`'] = '"';
225  }
226  $expectation = str_replace(array_keys($replaces), array_values($replaces), $expectation);
227  self::assertSame($expectation, $statement);
228  }
229 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildLanguageConfiguration
‪buildLanguageConfiguration(string $identifier, string $base, array $fallbackIdentifiers=[], string $fallbackType=null)
Definition: SiteBasedTestTrait.php:108
‪TYPO3\CMS\Backend\Tests\Functional\Utility\BackendUtilityTest\givenPageIdCanBeExpanded
‪givenPageIdCanBeExpanded()
Definition: BackendUtilityTest.php:54
‪TYPO3\CMS\Backend\Tests\Functional\Utility\BackendUtilityTest\$backendUser
‪BackendUserAuthentication $backendUser
Definition: BackendUtilityTest.php:41
‪TYPO3\CMS\Backend\Tests\Functional\Utility\BackendUtilityTest\otherBranchesCanBeClosedWhenOpeningPage
‪otherBranchesCanBeClosedWhenOpeningPage()
Definition: BackendUtilityTest.php:70
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:37
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:50
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:88
‪TYPO3\CMS\Core\Database\ConnectionPool\DEFAULT_CONNECTION_NAME
‪const DEFAULT_CONNECTION_NAME
Definition: ConnectionPool.php:50
‪TYPO3\CMS\Backend\Tests\Functional\Utility\BackendUtilityTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: BackendUtilityTest.php:35
‪TYPO3\CMS\Backend\Tests\Functional\Utility\BackendUtilityTest
Definition: BackendUtilityTest.php:33
‪TYPO3\CMS\Backend\Tests\Functional\Utility\BackendUtilityTest\getProcessedValueForLanguage
‪getProcessedValueForLanguage()
Definition: BackendUtilityTest.php:90
‪TYPO3\CMS\Backend\Tests\Functional\Utility\BackendUtilityTest\setUp
‪setUp()
Definition: BackendUtilityTest.php:43
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Tests\Functional\Utility
Definition: BackendUtilityTest.php:18
‪TYPO3\CMS\Backend\Tests\Functional\Utility\BackendUtilityTest\enableFieldsStatementIsCorrectDataProvider
‪static enableFieldsStatementIsCorrectDataProvider()
Definition: BackendUtilityTest.php:141
‪TYPO3\CMS\Backend\Tests\Functional\Utility\BackendUtilityTest\getRecordTitleForUidLabel
‪getRecordTitleForUidLabel()
Definition: BackendUtilityTest.php:130
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Tests\Functional\Utility\BackendUtilityTest\enableFieldsStatementIsCorrect
‪enableFieldsStatementIsCorrect(array $enableColumns, bool $inverted, string $expectation)
Definition: BackendUtilityTest.php:211