c9b8bc2aa195a741206ce48a45ab8a8a6ae2831f
[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\Lang\LanguageService;
22
23 /**
24 * Test case
25 */
26 class InputDateTimeElementTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
27 {
28 /**
29 * @var string Selected timezone backup
30 */
31 protected $timezoneBackup = '';
32
33 /**
34 * We're fiddling with hard timestamps in the tests, but time methods in
35 * the system under test do use timezone settings. Therefore we backup the
36 * current timezone setting, set it to UTC explicitly and reconstitute it
37 * again in tearDown()
38 */
39 protected function setUp()
40 {
41 $this->timezoneBackup = date_default_timezone_get();
42 }
43
44 /**
45 * Tear down
46 */
47 protected function tearDown()
48 {
49 date_default_timezone_set($this->timezoneBackup);
50 parent::tearDown();
51 }
52
53 /**
54 * Data provider for renderAppliesCorrectTimestampConversion
55 *
56 * @return array
57 */
58 public function renderAppliesCorrectTimestampConversionDataProvider()
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 /**
80 * @test
81 * @dataProvider renderAppliesCorrectTimestampConversionDataProvider
82 * @param int $input
83 * @param string $serverTimezone
84 * @param int $expectedOutput
85 */
86 public function renderAppliesCorrectTimestampConversion($input, $serverTimezone, $expectedOutput)
87 {
88 date_default_timezone_set($serverTimezone);
89 $data = [
90 'parameterArray' => [
91 'tableName' => 'table_foo',
92 'fieldName' => 'field_bar',
93 'fieldConf' => [
94 'config' => [
95 'type' => 'input',
96 'dbType' => 'datetime',
97 'eval' => 'datetime',
98 'default' => '0000-00-00 00:00:00'
99 ]
100 ],
101 'itemFormElValue' => $input
102 ]
103 ];
104 $abstractNode = $this->prophesize(AbstractNode::class);
105 $abstractNode->render(Argument::cetera())->willReturn([
106 'additionalJavaScriptPost' => [],
107 'additionalJavaScriptSubmit' => [],
108 'additionalHiddenFields' => [],
109 'stylesheetFiles' => [],
110 ]);
111 $nodeFactoryProphecy = $this->prophesize(NodeFactory::class);
112 $nodeFactoryProphecy->create(Argument::cetera())->willReturn($abstractNode->reveal());
113 $languageService = $this->prophesize(LanguageService::class);
114 $GLOBALS['LANG'] = $languageService->reveal();
115 $subject = new InputDateTimeElement($nodeFactoryProphecy->reveal(), $data);
116 $result = $subject->render();
117 $this->assertContains('<input type="hidden" name="" value="' . $expectedOutput . '" />', $result['html']);
118 }
119 }