1<?php
2
3namespace Sabre\DAV;
4use Sabre\HTTP;
5
6class ServerUpdatePropertiesTest extends \PHPUnit_Framework_TestCase {
7
8    function testUpdatePropertiesFail() {
9
10        $tree = array(
11            new SimpleCollection('foo'),
12        );
13        $server = new Server($tree);
14
15        $result = $server->updateProperties('foo', array(
16            '{DAV:}foo' => 'bar'
17        ));
18
19        $expected = array(
20            '{DAV:}foo' => 403,
21        );
22        $this->assertEquals($expected, $result);
23
24    }
25
26    function testUpdatePropertiesProtected() {
27
28        $tree = array(
29            new SimpleCollection('foo'),
30        );
31        $server = new Server($tree);
32
33        $server->on('propPatch', function($path, PropPatch $propPatch) {
34            $propPatch->handleRemaining(function() { return true; });
35        });
36        $result = $server->updateProperties('foo', array(
37            '{DAV:}getetag' => 'bla',
38            '{DAV:}foo' => 'bar'
39        ));
40
41        $expected = array(
42            '{DAV:}getetag' => 403,
43            '{DAV:}foo' => 424,
44        );
45        $this->assertEquals($expected, $result);
46
47    }
48
49    function testUpdatePropertiesEventFail() {
50
51        $tree = array(
52            new SimpleCollection('foo'),
53        );
54        $server = new Server($tree);
55        $server->on('propPatch', function($path, PropPatch $propPatch) {
56            $propPatch->setResultCode('{DAV:}foo', 404);
57            $propPatch->handleRemaining(function() { return true; });
58        });
59
60        $result = $server->updateProperties('foo', array(
61            '{DAV:}foo' => 'bar',
62            '{DAV:}foo2' => 'bla',
63        ));
64
65        $expected = array(
66            '{DAV:}foo' => 404,
67            '{DAV:}foo2' => 424,
68        );
69        $this->assertEquals($expected, $result);
70
71    }
72
73    function updatePropFail(&$propertyDelta, &$result, $node) {
74
75        $result[404] = array(
76            '{DAV:}foo' => null,
77        );
78        unset($propertyDelta['{DAV:}foo']);
79        return false;
80
81    }
82
83
84    function testUpdatePropertiesEventSuccess() {
85
86        $tree = array(
87            new SimpleCollection('foo'),
88        );
89        $server = new Server($tree);
90        $server->on('propPatch', function($path, PropPatch $propPatch) {
91
92            $propPatch->handle(['{DAV:}foo', '{DAV:}foo2'], function() {
93                return [
94                    '{DAV:}foo' => 200,
95                    '{DAV:}foo2' => 201,
96                ];
97            });
98
99        });
100
101        $result = $server->updateProperties('foo', array(
102            '{DAV:}foo' => 'bar',
103            '{DAV:}foo2' => 'bla',
104        ));
105
106        $expected = array(
107            '{DAV:}foo' => 200,
108            '{DAV:}foo2' => 201,
109        );
110        $this->assertEquals($expected, $result);
111
112    }
113
114    function updatePropSuccess(&$propertyDelta, &$result, $node) {
115
116        $result[200] = array(
117            '{DAV:}foo' => null,
118        );
119        $result[201] = array(
120            '{DAV:}foo2' => null,
121        );
122        unset($propertyDelta['{DAV:}foo']);
123        unset($propertyDelta['{DAV:}foo2']);
124        return;
125
126    }
127}
128