1<?php
2/**
3 * Test the date-time attribute handler.
4 *
5 * PHP version 5
6 *
7 * @category   Kolab
8 * @package    Kolab_Format
9 * @subpackage UnitTests
10 * @author     Gunnar Wrobel <wrobel@pardus.de>
11 * @license    http://www.horde.org/licenses/lgpl21 LGPL
12 * @link       http://www.horde.org/libraries/Horde_Kolab_Format
13 */
14
15/**
16 * Test the date-time attribute handler.
17 *
18 * Copyright 2011-2016 Horde LLC (http://www.horde.org/)
19 *
20 * See the enclosed file COPYING for license information (LGPL). If you
21 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
22 *
23 * @category   Kolab
24 * @package    Kolab_Format
25 * @subpackage UnitTests
26 * @author     Gunnar Wrobel <wrobel@pardus.de>
27 * @license    http://www.horde.org/licenses/lgpl21 LGPL
28 * @link       http://www.horde.org/libraries/Horde_Kolab_Format
29 */
30class Horde_Kolab_Format_Unit_Xml_Type_DateTimeTest
31extends Horde_Kolab_Format_TestCase
32{
33    public function setUp()
34    {
35        date_default_timezone_set('Europe/Berlin');
36    }
37
38    public function testLoadDate()
39    {
40        $attributes = $this->load(
41            '<?xml version="1.0" encoding="UTF-8"?>
42<kolab version="1.0" a="b"><datetime>2011-06-29</datetime>c</kolab>'
43        );
44        $this->assertTrue($attributes['datetime']['date-only']);
45    }
46
47    public function testLoadDateValue()
48    {
49        $attributes = $this->load(
50            '<?xml version="1.0" encoding="UTF-8"?>
51<kolab version="1.0" a="b"><datetime>2011-06-29</datetime>c</kolab>'
52        );
53        $this->assertEquals(
54            '2011-06-29T00:00:00+02:00',
55            $attributes['datetime']['date']->format('c')
56        );
57    }
58
59    public function testLoadStrangeDateTime()
60    {
61        $attributes = $this->load(
62            '<?xml version="1.0" encoding="UTF-8"?>
63<kolab version="1.0" a="b"><datetime type="strange"><b/>2011-06-29<a/></datetime>c</kolab>'
64        );
65        $this->assertEquals(
66            '2011-06-29T00:00:00+02:00',
67            $attributes['datetime']['date']->format('c')
68        );
69    }
70
71    /**
72     * @expectedException Horde_Kolab_Format_Exception
73     */
74    public function testLoadEmptyDateTime()
75    {
76        $attributes = $this->load(
77            '<?xml version="1.0" encoding="UTF-8"?>
78<kolab version="1.0"><datetime></datetime></kolab>'
79        );
80    }
81
82    public function testLoadMissingDateTime()
83    {
84        $attributes = $this->load(
85            '<?xml version="1.0" encoding="UTF-8"?>
86<kolab version="1.0"/>'
87        );
88        $this->assertFalse(isset($attributes['datetime']));
89    }
90
91    public function testLoadDefault()
92    {
93        $attributes = $this->loadWithClass(
94            'Horde_Kolab_Format_Stub_DateTimeDefault'
95        );
96        $this->assertInstanceOf('DateTime', $attributes['datetime']['date']);
97    }
98
99    /**
100     * @expectedException Horde_Kolab_Format_Exception_MissingValue
101     */
102    public function testLoadNotEmpty()
103    {
104        $this->loadWithClass('Horde_Kolab_Format_Stub_DateTimeNotEmpty');
105    }
106
107    public function testLoadNotEmptyRelaxed()
108    {
109         $attributes = $this->loadWithClass(
110            'Horde_Kolab_Format_Stub_DateTimeNotEmpty',
111            null,
112            array('relaxed' => true)
113        );
114        $this->assertFalse(isset($attributes['datetime']));
115    }
116
117    public function testSaveDateTime()
118    {
119        $this->assertEquals(
120            '<?xml version="1.0" encoding="UTF-8"?>
121<kolab version="1.0"><datetime>2011-06-29T11:11:11Z</datetime></kolab>
122',
123            $this->saveToXml(
124                null,
125                array(
126                    'datetime' => array(
127                        'date' => new DateTime(
128                            '2011-06-29T11:11:11',
129                            new DateTimeZone('UTC')
130                        )
131                    )
132                )
133            )
134        );
135    }
136
137    public function testSaveTimeZone()
138    {
139        $this->assertEquals(
140            '<?xml version="1.0" encoding="UTF-8"?>
141<kolab version="1.0"><datetime>2011-06-29T09:11:11Z</datetime></kolab>
142',
143            $this->saveToXml(
144                null,
145                array(
146                    'datetime' => array(
147                        'date' => new DateTime(
148                            '2011-06-29T11:11:11',
149                            new DateTimeZone('Europe/Berlin')
150                        )
151                    )
152                )
153            )
154        );
155    }
156
157    public function testSaveOverwritesOldValue()
158    {
159        $this->assertEquals(
160            '<?xml version="1.0" encoding="UTF-8"?>
161<kolab version="1.0" a="b"><datetime type="strange">2011-06-29<b/><a/></datetime>c</kolab>
162',
163            $this->saveToXml(
164                '<?xml version="1.0" encoding="UTF-8"?>
165<kolab version="1.0" a="b"><datetime type="strange"><b/>STRANGE<a/></datetime>c</kolab>',
166                array(
167                    'datetime' => array(
168                        'date' => new DateTime(
169                            '2011-06-29T11:11:11',
170                            new DateTimeZone('Europe/Berlin')
171                        ),
172                        'date-only' => true
173                    )
174                )
175            )
176        );
177    }
178
179    /**
180     * @expectedException Horde_Kolab_Format_Exception_MissingValue
181     */
182    public function testSaveNotEmpty()
183    {
184        $this->saveWithClass('Horde_Kolab_Format_Stub_DateTimeNotEmpty');
185    }
186
187    public function testSaveNotEmptyWithOldValue()
188    {
189        $this->assertInstanceOf(
190            'DOMNode',
191            $this->saveWithClass(
192                'Horde_Kolab_Format_Stub_DateTimeNotEmpty',
193                '<?xml version="1.0" encoding="UTF-8"?>
194<kolab version="1.0" a="b"><datetime type="strange"><b/>STRANGE<a/></datetime>c</kolab>'
195            )
196        );
197    }
198
199    public function testDeleteNode()
200    {
201        $this->assertEquals(
202            '<?xml version="1.0" encoding="UTF-8"?>
203<kolab version="1.0" a="b">c</kolab>
204',
205            $this->saveToXml(
206                '<?xml version="1.0" encoding="UTF-8"?>
207<kolab version="1.0" a="b"><datetime type="strange"><b/>STRANGE<a/></datetime>c</kolab>',
208                array()
209            )
210        );
211    }
212
213    public function testSaveNotEmptyRelaxed()
214    {
215        $this->assertFalse(
216            $this->saveWithClass(
217                'Horde_Kolab_Format_Stub_DateTimeNotEmpty',
218                null,
219                array('relaxed' => true)
220            )
221        );
222    }
223
224    protected function getTypeClass()
225    {
226        return 'Horde_Kolab_Format_Xml_Type_DateTime';
227    }
228}
229