[BUGFIX] Notice free FormEngine testing
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / Element / InputDateTimeElementTest.php
1 <?php
2 namespace typo3\sysext\backend\Tests\Unit\Form\Element;
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 TYPO3\CMS\Backend\Form\AbstractNode;
19 use TYPO3\CMS\Backend\Form\Element\InputDateTimeElement;
20 use TYPO3\CMS\Backend\Form\NodeExpansion\FieldInformation;
21 use TYPO3\CMS\Backend\Form\NodeFactory;
22 use TYPO3\CMS\Core\Localization\LanguageService;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24
25 /**
26 * Test case
27 */
28 class InputDateTimeElementTest extends UnitTestCase
29 {
30 /**
31 * @var string Selected timezone backup
32 */
33 protected $timezoneBackup = '';
34
35 /**
36 * We're fiddling with hard timestamps in the tests, but time methods in
37 * the system under test do use timezone settings. Therefore we backup the
38 * current timezone setting, set it to UTC explicitly and reconstitute it
39 * again in tearDown()
40 */
41 protected function setUp()
42 {
43 $this->timezoneBackup = date_default_timezone_get();
44 }
45
46 /**
47 * Tear down
48 */
49 protected function tearDown()
50 {
51 date_default_timezone_set($this->timezoneBackup);
52 parent::tearDown();
53 }
54
55 /**
56 * Data provider for renderAppliesCorrectTimestampConversion
57 *
58 * @return array
59 */
60 public function renderAppliesCorrectTimestampConversionDataProvider()
61 {
62 // Three elements: input (UTC), timezone of output, expected output
63 return [
64 // German standard time (without DST) is one hour ahead of UTC
65 'date in 2016 in German timezone' => [
66 1457103519, 'Europe/Berlin', '2016-03-04T15:58:39+00:00'
67 ],
68 'date in 1969 in German timezone' => [
69 -7200, 'Europe/Berlin', '1969-12-31T23:00:00+00:00'
70 ],
71 // Los Angeles is 8 hours behind UTC
72 'date in 2016 in Los Angeles timezone' => [
73 1457103519, 'America/Los_Angeles', '2016-03-04T06:58:39+00:00'
74 ],
75 'date in UTC' => [
76 1457103519, 'UTC', '2016-03-04T14:58:39+00:00'
77 ]
78 ];
79 }
80
81 /**
82 * @test
83 * @dataProvider renderAppliesCorrectTimestampConversionDataProvider
84 * @param int $input
85 * @param string $serverTimezone
86 * @param int $expectedOutput
87 */
88 public function renderAppliesCorrectTimestampConversion($input, $serverTimezone, $expectedOutput)
89 {
90 date_default_timezone_set($serverTimezone);
91 $data = [
92 'tableName' => 'table_foo',
93 'fieldName' => 'field_bar',
94 'databaseRow' => [
95 'uid' => 5,
96 ],
97 'parameterArray' => [
98 'tableName' => 'table_foo',
99 'fieldName' => 'field_bar',
100 'fieldConf' => [
101 'config' => [
102 'type' => 'input',
103 'dbType' => 'datetime',
104 'eval' => 'datetime',
105 'default' => '0000-00-00 00:00:00'
106 ]
107 ],
108 'itemFormElName' => 'myItemFormElName',
109 'itemFormElValue' => $input
110 ]
111 ];
112 $abstractNode = $this->prophesize(AbstractNode::class);
113 $abstractNode->render(Argument::cetera())->willReturn([
114 'additionalJavaScriptPost' => [],
115 'additionalJavaScriptSubmit' => [],
116 'additionalHiddenFields' => [],
117 'stylesheetFiles' => [],
118 ]);
119 $nodeFactoryProphecy = $this->prophesize(NodeFactory::class);
120 $nodeFactoryProphecy->create(Argument::cetera())->willReturn($abstractNode->reveal());
121 $fieldInformationProphecy = $this->prophesize(FieldInformation::class);
122 $fieldInformationProphecy->render(Argument::cetera())->willReturn(['html' => '']);
123 $nodeFactoryProphecy->create(Argument::cetera())->willReturn($fieldInformationProphecy->reveal());
124 $languageService = $this->prophesize(LanguageService::class);
125 $GLOBALS['LANG'] = $languageService->reveal();
126 $subject = new InputDateTimeElement($nodeFactoryProphecy->reveal(), $data);
127 $result = $subject->render();
128 $this->assertContains('<input type="hidden" name="myItemFormElName" value="' . $expectedOutput . '" />', $result['html']);
129 }
130 }