‪TYPO3CMS  ‪main
DefaultValuesTest.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 PHPUnit\Framework\Attributes\Test;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
24 use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\ActionService;
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 
30 final class ‪DefaultValuesTest extends FunctionalTestCase
31 {
33 
34  protected array ‪$testExtensionsToLoad = [
35  'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_defaulttsconfig',
36  ];
37 
38  protected const ‪LANGUAGE_PRESETS = [
39  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
40  'DA' => ['id' => 1, 'title' => 'Dansk', 'locale' => 'da_DK.UTF8'],
41  ];
42 
43  private ActionService ‪$actionService;
44 
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/be_users_admin.csv');
49  $this->importCSVDataSet(__DIR__ . '/DataSet/LiveDefaultPages.csv');
50  $backendUser = $this->setUpBackendUser(1);
51  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
52  $this->actionService = new ActionService();
54  'test',
55  $this->‪buildSiteConfiguration(1),
56  [
57  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
58  $this->‪buildLanguageConfiguration('DA', '/da/'),
59  ]
60  );
61  }
62 
63  #[Test]
65  {
66  ‪$GLOBALS['TCA']['pages']['columns']['subtitle']['config']['default'] = 'tca default subtitle';
67  ‪$GLOBALS['TCA']['tt_content']['columns']['bodytext']['config']['default'] = 'tca default bodytext';
68 
69  // Create page and verify default from TCA is applied
70  $map = $this->actionService->createNewRecord('pages', 88, [
71  'title' => 'A new age',
72  ]);
73  $newPageId = reset($map['pages']);
74  $newPageRecord = BackendUtility::getRecord('pages', $newPageId);
75  self::assertEquals('tca default subtitle', $newPageRecord['subtitle']);
76 
77  // Add content element and verify default from TCA is not applied when value is given
78  $map = $this->actionService->createNewRecord('tt_content', $newPageId, [
79  'bodytext' => '',
80  'title' => 'foo',
81  ]);
82  $newContentId = reset($map['tt_content']);
83  $newContentRecord = BackendUtility::getRecord('tt_content', $newContentId);
84  self::assertEquals('', $newContentRecord['bodytext']);
85 
86  // Add another content element and verify default from TCA is applied
87  $map = $this->actionService->createNewRecord('tt_content', $newPageId, [
88  'title' => 'foo',
89  ]);
90  $newContentId = reset($map['tt_content']);
91  $newContentRecord = BackendUtility::getRecord('tt_content', $newContentId);
92  self::assertEquals('tca default bodytext', $newContentRecord['bodytext']);
93  }
94 
95  #[Test]
97  {
98  // TCAdefaults from ext:test_defaulttsconfig/Configuration/page.tsconfig kick in here
99  $map = $this->actionService->createNewRecord('pages', 88, [
100  'title' => 'A new age',
101  ]);
102  $newPageId = reset($map['pages']);
103  $newPageRecord = BackendUtility::getRecord('pages', $newPageId);
104  self::assertEquals('from pagets, with love', $newPageRecord['keywords']);
105 
106  // Add content element and verify Page TSconfig TCAdefaults are not applied when value is given
107  $map = $this->actionService->createNewRecord('tt_content', $newPageId, [
108  'header' => '',
109  'bodytext' => 'Random bodytext',
110  ]);
111  $newContentId = reset($map['tt_content']);
112  $newContentRecord = BackendUtility::getRecord('tt_content', $newContentId);
113  self::assertEquals('', $newContentRecord['header']);
114 
115  // Add content element and verify Page TSconfig TCAdefaults are applied
116  $map = $this->actionService->createNewRecord('tt_content', $newPageId, [
117  'bodytext' => 'Random bodytext',
118  ]);
119  $newContentId = reset($map['tt_content']);
120  $newContentRecord = BackendUtility::getRecord('tt_content', $newContentId);
121  self::assertEquals('global space', $newContentRecord['header']);
122  }
123 
124  #[Test]
126  {
127  // TCAdefaults from ext:test_defaulttsconfig/Configuration/page.tsconfig kick in here,
128  // but are overridden by specific page record TSconfig here.
129  $this->actionService->modifyRecord('pages', 88, [
130  'TSconfig' => chr(10) .
131  'TCAdefaults.pages.keywords = I am specific, not generic' . chr(10) .
132  'TCAdefaults.tt_content.header = local space',
133  ]);
134 
135  // Add subpage and verify TSconfig from above page kicks in.
136  $map = $this->actionService->createNewRecord('pages', 88, [
137  'title' => 'A new age',
138  ]);
139  $newPageId = reset($map['pages']);
140  $newPageRecord = BackendUtility::getRecord('pages', $newPageId);
141  self::assertEquals('I am specific, not generic', $newPageRecord['keywords']);
142 
143  // Create content element with given header, Page TSconfig TCAdefaults does not kick in.
144  $map = $this->actionService->createNewRecord('tt_content', $newPageId, [
145  'header' => '',
146  'bodytext' => 'Random bodytext',
147  ]);
148  $newContentId = reset($map['tt_content']);
149  $newContentRecord = BackendUtility::getRecord('tt_content', $newContentId);
150  self::assertEquals('', $newContentRecord['header']);
151 
152  // Create content element without given header, Page TSconfig TCAdefaults kicks in.
153  $map = $this->actionService->createNewRecord('tt_content', $newPageId, [
154  'bodytext' => 'Random bodytext',
155  ]);
156  $newContentId = reset($map['tt_content']);
157  $newContentRecord = BackendUtility::getRecord('tt_content', $newContentId);
158  self::assertEquals('local space', $newContentRecord['header']);
159  }
160 
161  #[Test]
162  public function ‪defaultValueForNullTextfieldsIsConsidered(): void
163  {
164  // New content element without bodytext
165  ‪$GLOBALS['TCA']['tt_content']['columns']['bodytext']['l10n_mode'] = 'exclude';
166  ‪$GLOBALS['TCA']['tt_content']['columns']['bodytext']['config']['enableRichtext'] = true;
167  $map = $this->actionService->createNewRecord('tt_content', 88, [
168  'header' => 'Random header',
169  'bodytext' => null,
170  ]);
171  $newContentId = reset($map['tt_content']);
172  $map = $this->actionService->localizeRecord('tt_content', $newContentId, 1);
173  $translatedContentId = reset($map['tt_content']);
174  $newContentRecord = BackendUtility::getRecord('tt_content', $translatedContentId);
175  self::assertNull($newContentRecord['bodytext']);
176  }
177 }
‪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\Core\Tests\Functional\DataHandling\DataHandler\DefaultValuesTest\$actionService
‪ActionService $actionService
Definition: DefaultValuesTest.php:42
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler
Definition: CheckboxValidationTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\DefaultValuesTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: DefaultValuesTest.php:33
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\DefaultValuesTest\defaultValuesFromTCAForNewRecordsIsRespected
‪defaultValuesFromTCAForNewRecordsIsRespected()
Definition: DefaultValuesTest.php:63
‪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\Tests\Functional\DataHandling\DataHandler\DefaultValuesTest\setUp
‪setUp()
Definition: DefaultValuesTest.php:44
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\DefaultValuesTest
Definition: DefaultValuesTest.php:31
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\DefaultValuesTest\defaultValuesFromPageSpecificTSconfigForNewRecordsIsRespected
‪defaultValuesFromPageSpecificTSconfigForNewRecordsIsRespected()
Definition: DefaultValuesTest.php:124
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\DefaultValuesTest\defaultValueForNullTextfieldsIsConsidered
‪defaultValueForNullTextfieldsIsConsidered()
Definition: DefaultValuesTest.php:161
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\DefaultValuesTest\defaultValuesFromGlobalTSconfigForNewRecordsIsRespected
‪defaultValuesFromGlobalTSconfigForNewRecordsIsRespected()
Definition: DefaultValuesTest.php:95
‪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\Tests\Functional\DataHandling\DataHandler\DefaultValuesTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: DefaultValuesTest.php:37