1<?php
2/**
3 * Test the handler for attributes with composite values.
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 handler for attributes with composite values.
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_CompositeTest
31extends Horde_Kolab_Format_TestCase
32{
33    public function testLoadComposite()
34    {
35        $attributes = $this->load(
36            '<?xml version="1.0" encoding="UTF-8"?>
37<kolab version="1.0"><composite><uid>a&amp;</uid><test>TEST</test></composite></kolab>',
38            array(
39                'array' => array(
40                    'uid' => 'Horde_Kolab_Format_Xml_Type_String_Empty',
41                    'test' => 'Horde_Kolab_Format_Xml_Type_String_Empty',
42                ),
43                'value' => Horde_Kolab_Format_Xml::VALUE_NOT_EMPTY,
44            )
45        );
46        $this->assertEquals(array('test' => 'TEST', 'uid' => 'a&'), $attributes['composite']);
47    }
48
49    public function testLoadDefault()
50    {
51        $attributes = $this->load(
52            '<?xml version="1.0" encoding="UTF-8"?>
53<kolab version="1.0"/>',
54            array(
55                'array' => array(),
56                'value' => Horde_Kolab_Format_Xml::VALUE_DEFAULT,
57                'default' => array('X' => 'Y'),
58            )
59        );
60        $this->assertEquals(array('X' => 'Y'), $attributes['composite']);
61    }
62
63    /**
64     * @expectedException Horde_Kolab_Format_Exception
65     */
66    public function testLoadNotEmpty()
67    {
68        $attributes = $this->load(
69            '<?xml version="1.0" encoding="UTF-8"?>
70<kolab version="1.0"/>',
71            array(
72                'array' => array(),
73                'value' => Horde_Kolab_Format_Xml::VALUE_NOT_EMPTY,
74            )
75        );
76    }
77
78    public function testLoadNotEmptyRelaxed()
79    {
80        $attributes = $this->load(
81            '<?xml version="1.0" encoding="UTF-8"?>
82<kolab version="1.0"/>',
83            array(
84                'array' => array(
85                    'type' => Horde_Kolab_Format_Xml::TYPE_STRING,
86                ),
87                'value' => Horde_Kolab_Format_Xml::VALUE_NOT_EMPTY,
88                'relaxed' => true
89            )
90        );
91        $this->assertFalse(isset($attributes['composite']));
92    }
93
94    public function testSaveComposite()
95    {
96        $this->assertEquals(
97            '<?xml version="1.0" encoding="UTF-8"?>
98<kolab version="1.0"><composite><uid></uid><test></test></composite></kolab>
99',
100            $this->saveToXml(
101                null,
102                array('composite' => array()),
103                array(
104                    'array' => array(
105                        'uid' => 'Horde_Kolab_Format_Xml_Type_String_Empty',
106                        'test' => 'Horde_Kolab_Format_Xml_Type_String_Empty',
107                    ),
108                    'value' => Horde_Kolab_Format_Xml::VALUE_NOT_EMPTY,
109                )
110            )
111        );
112    }
113
114    public function testSaveMaybeMissing()
115    {
116        $this->assertEquals(
117            '<?xml version="1.0" encoding="UTF-8"?>
118<kolab version="1.0"><composite><uid>1</uid><test>&amp;</test></composite></kolab>
119',
120            $this->saveToXml(
121                null,
122                array('composite' => array('uid' => 1, 'test' => '&')),
123                array(
124                    'array' => array(
125                        'uid' => 'Horde_Kolab_Format_Xml_Type_String_Empty',
126                        'test' => 'Horde_Kolab_Format_Xml_Type_String_Empty',
127                    ),
128                    'value' => Horde_Kolab_Format_Xml::VALUE_MAYBE_MISSING,
129                )
130            )
131        );
132    }
133
134    public function testSaveModifiesOldValue()
135    {
136        $this->assertEquals(
137            '<?xml version="1.0" encoding="UTF-8"?>
138<kolab version="1.0" a="b"><composite type="strange"><b/>STRANGE<a/><uid>1</uid><test>&amp;</test></composite>c</kolab>
139',
140            $this->saveToXml(
141                '<?xml version="1.0" encoding="UTF-8"?>
142<kolab version="1.0" a="b"><composite type="strange"><b/>STRANGE<a/></composite>c</kolab>',
143                array('composite' => array('uid' => 1, 'test' => '&')),
144                array(
145                    'array' => array(
146                        'uid' => 'Horde_Kolab_Format_Xml_Type_String_Empty',
147                        'test' => 'Horde_Kolab_Format_Xml_Type_String_Empty',
148                    ),
149                    'value' => Horde_Kolab_Format_Xml::VALUE_NOT_EMPTY,
150                )
151            )
152        );
153    }
154
155    /**
156     * @expectedException Horde_Kolab_Format_Exception_MissingValue
157     */
158    public function testSaveNotEmpty()
159    {
160        $this->saveToXml(
161            '<?xml version="1.0" encoding="UTF-8"?>
162<kolab version="1.0"/>',
163            array(),
164            array(
165                'array' => array(),
166                'value' => Horde_Kolab_Format_Xml::VALUE_NOT_EMPTY,
167            )
168        );
169    }
170
171    public function testSaveNotEmptyWithOldValue()
172    {
173        $this->assertInstanceOf(
174            'DOMNode',
175            $this->saveToReturn(
176                '<?xml version="1.0" encoding="UTF-8"?>
177<kolab version="1.0" a="b"><composite type="strange"><b/>STRANGE<a/></composite>c</kolab>',
178                array(),
179                array(
180                    'array' => array(),
181                    'value' => Horde_Kolab_Format_Xml::VALUE_NOT_EMPTY,
182                )
183            )
184        );
185    }
186
187    public function testSaveNotEmptyRelaxed()
188    {
189        $this->assertFalse(
190            $this->saveToReturn(
191                '<?xml version="1.0" encoding="UTF-8"?>
192<kolab version="1.0"/>',
193                array(),
194                array(
195                    'array' => array(),
196                    'value' => Horde_Kolab_Format_Xml::VALUE_NOT_EMPTY,
197                    'relaxed' => true,
198                )
199            )
200        );
201    }
202
203    public function testDeleteComposite()
204    {
205        $this->assertEquals(
206            '<?xml version="1.0" encoding="UTF-8"?>
207<kolab version="1.0" a="b">c<y/></kolab>
208',
209            $this->saveToXml(
210                '<?xml version="1.0" encoding="UTF-8"?>
211<kolab version="1.0" a="b"><composite type="strange"><b/>STRANGE<a/></composite>c<y/><composite>a</composite></kolab>',
212                array(),
213                array(
214                    'array' => array(),
215                    'value' => Horde_Kolab_Format_Xml::VALUE_MAYBE_MISSING,
216                )
217            )
218        );
219    }
220
221    protected function getTypeClass()
222    {
223        return 'Horde_Kolab_Format_Stub_Composite';
224    }
225}
226