1<?php
2
3namespace Sabre\DAV\PropertyStorage\Backend;
4
5use Sabre\DAV\PropFind;
6use Sabre\DAV\PropPatch;
7use Sabre\DAV\Xml\Property\Complex;
8use Sabre\DAV\Xml\Property\Href;
9
10abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
11
12    /**
13     * Should return an instance of \PDO with the current tables initialized,
14     * and some test records.
15     */
16    abstract function getPDO();
17
18    function getBackend() {
19
20        return new PDO($this->getPDO());
21
22    }
23
24    function testPropFind() {
25
26        $backend = $this->getBackend();
27
28        $propFind = new PropFind('dir', ['{DAV:}displayname']);
29        $backend->propFind('dir', $propFind);
30
31        $this->assertEquals('Directory', $propFind->get('{DAV:}displayname'));
32
33    }
34
35    function testPropFindNothingToDo() {
36
37        $backend = $this->getBackend();
38
39        $propFind = new PropFind('dir', ['{DAV:}displayname']);
40        $propFind->set('{DAV:}displayname', 'foo');
41        $backend->propFind('dir', $propFind);
42
43        $this->assertEquals('foo', $propFind->get('{DAV:}displayname'));
44
45    }
46
47    /**
48     * @depends testPropFind
49     */
50    function testPropPatchUpdate() {
51
52        $backend = $this->getBackend();
53
54        $propPatch = new PropPatch(['{DAV:}displayname' => 'bar']);
55        $backend->propPatch('dir', $propPatch);
56        $propPatch->commit();
57
58        $propFind = new PropFind('dir', ['{DAV:}displayname']);
59        $backend->propFind('dir', $propFind);
60
61        $this->assertEquals('bar', $propFind->get('{DAV:}displayname'));
62
63    }
64
65    /**
66     * @depends testPropPatchUpdate
67     */
68    function testPropPatchComplex() {
69
70        $backend = $this->getBackend();
71
72        $complex = new Complex('<foo xmlns="DAV:">somevalue</foo>');
73
74        $propPatch = new PropPatch(['{DAV:}complex' => $complex]);
75        $backend->propPatch('dir', $propPatch);
76        $propPatch->commit();
77
78        $propFind = new PropFind('dir', ['{DAV:}complex']);
79        $backend->propFind('dir', $propFind);
80
81        $this->assertEquals($complex, $propFind->get('{DAV:}complex'));
82
83    }
84
85
86    /**
87     * @depends testPropPatchComplex
88     */
89    function testPropPatchCustom() {
90
91        $backend = $this->getBackend();
92
93        $custom = new Href('/foo/bar/');
94
95        $propPatch = new PropPatch(['{DAV:}custom' => $custom]);
96        $backend->propPatch('dir', $propPatch);
97        $propPatch->commit();
98
99        $propFind = new PropFind('dir', ['{DAV:}custom']);
100        $backend->propFind('dir', $propFind);
101
102        $this->assertEquals($custom, $propFind->get('{DAV:}custom'));
103
104    }
105
106    /**
107     * @depends testPropFind
108     */
109    function testPropPatchRemove() {
110
111        $backend = $this->getBackend();
112
113        $propPatch = new PropPatch(['{DAV:}displayname' => null]);
114        $backend->propPatch('dir', $propPatch);
115        $propPatch->commit();
116
117        $propFind = new PropFind('dir', ['{DAV:}displayname']);
118        $backend->propFind('dir', $propFind);
119
120        $this->assertEquals(null, $propFind->get('{DAV:}displayname'));
121
122    }
123
124    /**
125     * @depends testPropFind
126     */
127    function testDelete() {
128
129        $backend = $this->getBackend();
130        $backend->delete('dir');
131
132        $propFind = new PropFind('dir', ['{DAV:}displayname']);
133        $backend->propFind('dir', $propFind);
134
135        $this->assertEquals(null, $propFind->get('{DAV:}displayname'));
136
137    }
138
139    /**
140     * @depends testPropFind
141     */
142    function testMove() {
143
144        $backend = $this->getBackend();
145        // Creating a new child property.
146        $propPatch = new PropPatch(['{DAV:}displayname' => 'child']);
147        $backend->propPatch('dir/child', $propPatch);
148        $propPatch->commit();
149
150        $backend->move('dir','dir2');
151
152        // Old 'dir'
153        $propFind = new PropFind('dir', ['{DAV:}displayname']);
154        $backend->propFind('dir', $propFind);
155        $this->assertEquals(null, $propFind->get('{DAV:}displayname'));
156
157        // Old 'dir/child'
158        $propFind = new PropFind('dir/child', ['{DAV:}displayname']);
159        $backend->propFind('dir/child', $propFind);
160        $this->assertEquals(null, $propFind->get('{DAV:}displayname'));
161
162        // New 'dir2'
163        $propFind = new PropFind('dir2', ['{DAV:}displayname']);
164        $backend->propFind('dir2', $propFind);
165        $this->assertEquals('Directory', $propFind->get('{DAV:}displayname'));
166
167        // New 'dir2/child'
168        $propFind = new PropFind('dir2/child', ['{DAV:}displayname']);
169        $backend->propFind('dir2/child', $propFind);
170        $this->assertEquals('child', $propFind->get('{DAV:}displayname'));
171    }
172
173    /**
174     * @depends testPropFind
175     */
176    function testDeepDelete() {
177
178        $backend = $this->getBackend();
179        $propPatch = new PropPatch(['{DAV:}displayname' => 'child']);
180        $backend->propPatch('dir/child', $propPatch);
181        $propPatch->commit();
182        $backend->delete('dir');
183
184        $propFind = new PropFind('dir/child', ['{DAV:}displayname']);
185        $backend->propFind('dir/child', $propFind);
186
187        $this->assertEquals(null, $propFind->get('{DAV:}displayname'));
188
189    }
190}
191