1<?php
2/**
3 * Test the creation-date 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 creation-date 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_CreationDateTest
31extends Horde_Kolab_Format_TestCase
32{
33    public function testLoadCreationDate()
34    {
35        $attributes = $this->load(
36            '<?xml version="1.0" encoding="UTF-8"?>
37<kolab version="1.0" a="b"><creation-date>2011-06-28T08:42:11Z</creation-date>c</kolab>',
38            array('element' => 'creation-date')
39        );
40        $this->assertInstanceOf('DateTime', $attributes['creation-date']);
41    }
42
43    public function testLoadCreationDateValue()
44    {
45        $attributes = $this->load(
46            '<?xml version="1.0" encoding="UTF-8"?>
47<kolab version="1.0" a="b"><creation-date>2011-06-28T08:42:11Z</creation-date>c</kolab>',
48            array('element' => 'creation-date')
49        );
50        $this->assertEquals(
51            1309250531,
52            $attributes['creation-date']->format('U')
53        );
54    }
55
56    /**
57     * @expectedException Horde_Kolab_Format_Exception
58     */
59    public function testLoadInvalidCreationDateValue()
60    {
61        $attributes = $this->load(
62            '<?xml version="1.0" encoding="UTF-8"?>
63<kolab version="1.0" a="b"><creation-date>2011A-06-28T08:42:11Z</creation-date>c</kolab>',
64            array('element' => 'creation-date')
65        );
66    }
67
68    public function testLoadInvalidCreationDateValueRelaxed()
69    {
70        $attributes = $this->load(
71            '<?xml version="1.0" encoding="UTF-8"?>
72<kolab version="1.0" a="b"><creation-date>2011A-06-28T08:42:11Z</creation-date>c</kolab>',
73            array(
74                'relaxed' => true,
75                'element' => 'creation-date',
76            )
77        );
78        $this->assertFalse($attributes['creation-date']);
79    }
80
81    public function testLoadStrangeCreationDate()
82    {
83        $attributes = $this->load(
84            '<?xml version="1.0" encoding="UTF-8"?>
85<kolab version="1.0" a="b"><creation-date type="strange"><b/>1970-01-01T00:00:00Z<a/></creation-date>c</kolab>',
86            array('element' => 'creation-date')
87        );
88        $this->assertEquals(0, $attributes['creation-date']->format('U'));
89    }
90
91    public function testLoadMissingCreationDate()
92    {
93        $attributes = $this->load(
94            '<?xml version="1.0" encoding="UTF-8"?>
95<kolab version="1.0"/>',
96            array('element' => 'creation-date')
97        );
98        $this->assertInstanceOf('DateTime', $attributes['creation-date']);
99    }
100
101    public function testSave()
102    {
103        $this->assertInstanceOf(
104            'DOMNode',
105            $this->saveToReturn()
106        );
107    }
108
109    public function testSaveXml()
110    {
111        $this->assertEquals(
112            '<?xml version="1.0" encoding="UTF-8"?>
113<kolab version="1.0"><creation-date>1970-01-01T00:00:00Z</creation-date></kolab>
114',
115            $this->saveToXml(
116                null,
117                array('creation-date' => new DateTime('1970-01-01T00:00:00Z')),
118                array('element' => 'creation-date')
119            )
120        );
121    }
122
123    public function testSaveDoesNotTouchOldValue()
124    {
125        $this->assertEquals(
126            '<?xml version="1.0" encoding="UTF-8"?>
127<kolab version="1.0" a="b"><creation-date type="strange"><b/>1970-01-01T00:00:00Z<a/></creation-date>c</kolab>
128',
129            $this->saveToXml(
130                '<?xml version="1.0" encoding="UTF-8"?>
131<kolab version="1.0" a="b"><creation-date type="strange"><b/>1970-01-01T00:00:00Z<a/></creation-date>c</kolab>',
132                array('creation-date' => new DateTime('1970-01-01T00:00:00Z')),
133                array('element' => 'creation-date')
134            )
135        );
136    }
137
138    /**
139     * @expectedException Horde_Kolab_Format_Exception
140     */
141    public function testSaveFailsOverwritingOldValue()
142    {
143        $this->saveToXml(
144            '<?xml version="1.0" encoding="UTF-8"?>
145<kolab version="1.0" a="b"><creation-date type="strange"><b/>1970-01-01T00:00:00Z<a/></creation-date>c</kolab>',
146            array('creation-date' => new DateTime('1971-01-01T00:00:00Z')),
147            array('element' => 'creation-date')
148        );
149    }
150
151    public function testSaveRelaxedOverwritesOldValue()
152    {
153        $this->assertEquals(
154            '<?xml version="1.0" encoding="UTF-8"?>
155<kolab version="1.0" a="b"><creation-date type="strange">1971-01-01T00:00:00Z<b/><a/></creation-date>c</kolab>
156',
157            $this->saveToXml(
158                '<?xml version="1.0" encoding="UTF-8"?>
159<kolab version="1.0" a="b"><creation-date type="strange"><b/>1970-01-01T00:00:00Z<a/></creation-date>c</kolab>',
160                array('creation-date' => new DateTime('1971-01-01T00:00:00Z')),
161                array(
162                    'relaxed' => true,
163                    'element' => 'creation-date'
164                )
165            )
166        );
167    }
168
169    protected function getTypeClass()
170    {
171        return 'Horde_Kolab_Format_Xml_Type_CreationDate';
172    }
173}
174