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