‪TYPO3CMS  ‪main
DatetimeElementTest.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\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 final class ‪DatetimeElementTest extends UnitTestCase
31 {
35  protected string ‪$timezoneBackup = '';
36 
43  protected function ‪setUp(): void
44  {
45  parent::setUp();
46  $this->timezoneBackup = date_default_timezone_get();
47  }
48 
49  protected function ‪tearDown(): void
50  {
51  date_default_timezone_set($this->timezoneBackup);
52  parent::tearDown();
53  }
54 
59  {
60  // Three elements: input (UTC), timezone of output, expected output
61  return [
62  // German standard time (without DST) is one hour ahead of UTC
63  'date in 2016 in German timezone' => [
64  1457103519, 'Europe/Berlin', '2016-03-04T15:58:39+00:00',
65  ],
66  'date in 1969 in German timezone' => [
67  -7200, 'Europe/Berlin', '1969-12-31T23:00:00+00:00',
68  ],
69  // Los Angeles is 8 hours behind UTC
70  'date in 2016 in Los Angeles timezone' => [
71  1457103519, 'America/Los_Angeles', '2016-03-04T06:58:39+00:00',
72  ],
73  'date in UTC' => [
74  1457103519, 'UTC', '2016-03-04T14:58:39+00:00',
75  ],
76  ];
77  }
78 
79  #[DataProvider('renderAppliesCorrectTimestampConversionDataProvider')]
80  #[Test]
81  public function ‪renderAppliesCorrectTimestampConversion(int $input, string $serverTimezone, string $expectedOutput): void
82  {
83  date_default_timezone_set($serverTimezone);
84  $data = [
85  'tableName' => 'table_foo',
86  'fieldName' => 'field_bar',
87  'databaseRow' => [
88  'uid' => 5,
89  ],
90  'parameterArray' => [
91  'tableName' => 'table_foo',
92  'fieldName' => 'field_bar',
93  'fieldConf' => [
94  'label' => 'foo',
95  'config' => [
96  'type' => 'datetime',
97  'dbType' => 'datetime',
98  ],
99  ],
100  'itemFormElName' => 'myItemFormElName',
101  'itemFormElValue' => $input,
102  ],
103  ];
104  $iconFactoryMock = $this->createMock(IconFactory::class);
105  $iconMock = $this->createMock(Icon::class);
106  $iconMock->method('render')->willReturn('');
107  $iconFactoryMock->method('getIcon')->with(self::anything())->willReturn($iconMock);
108  $nodeFactoryMock = $this->createMock(NodeFactory::class);
109  $fieldInformationMock = $this->createMock(FieldInformation::class);
110  $fieldInformationMock->method('render')->willReturn(['html' => '']);
111  $nodeFactoryMock->method('create')->with(self::anything())->willReturn($fieldInformationMock);
112  ‪$GLOBALS['LANG'] = $this->createMock(LanguageService::class);
113 
114  $subject = new ‪DatetimeElement($iconFactoryMock);
115  $subject->injectNodeFactory($nodeFactoryMock);
116  $subject->setData($data);
117  $result = $subject->render();
118 
119  self::assertStringContainsString('<input type="hidden" name="myItemFormElName" value="' . $expectedOutput . '" />', $result['html']);
120  }
121 }
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\DatetimeElementTest\$timezoneBackup
‪string $timezoneBackup
Definition: DatetimeElementTest.php:35
‪TYPO3\CMS\Backend\Form\NodeExpansion\FieldInformation
Definition: FieldInformation.php:33
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:27
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\DatetimeElementTest
Definition: DatetimeElementTest.php:31
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\DatetimeElementTest\tearDown
‪tearDown()
Definition: DatetimeElementTest.php:49
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element
Definition: AbstractFormElementTest.php:18
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:40
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\DatetimeElementTest\renderAppliesCorrectTimestampConversion
‪renderAppliesCorrectTimestampConversion(int $input, string $serverTimezone, string $expectedOutput)
Definition: DatetimeElementTest.php:81
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\DatetimeElementTest\setUp
‪setUp()
Definition: DatetimeElementTest.php:43
‪TYPO3\CMS\Backend\Form\Element\DatetimeElement
Definition: DatetimeElement.php:32
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\DatetimeElementTest\renderAppliesCorrectTimestampConversionDataProvider
‪static renderAppliesCorrectTimestampConversionDataProvider()
Definition: DatetimeElementTest.php:58