1<?php
2
3namespace Sabre\DAV;
4
5use Sabre\HTTP\Request;
6use Sabre\HTTP\Response;
7
8require_once 'Sabre/DAV/ClientMock.php';
9
10class ClientTest extends \PHPUnit_Framework_TestCase {
11
12    function testConstruct() {
13
14        $client = new ClientMock(array(
15            'baseUri' => '/',
16        ));
17        $this->assertInstanceOf('Sabre\DAV\ClientMock', $client);
18
19    }
20
21    /**
22     * @expectedException InvalidArgumentException
23     */
24    function testConstructNoBaseUri() {
25
26        $client = new ClientMock(array());
27
28    }
29
30    function testAuth() {
31
32        $client = new ClientMock([
33            'baseUri' => '/',
34            'userName' => 'foo',
35            'password' => 'bar',
36        ]);
37
38        $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
39        $this->assertEquals(CURLAUTH_BASIC | CURLAUTH_DIGEST, $client->curlSettings[CURLOPT_HTTPAUTH]);
40
41    }
42
43    function testBasicAuth() {
44
45        $client = new ClientMock([
46            'baseUri' => '/',
47            'userName' => 'foo',
48            'password' => 'bar',
49            'authType' => Client::AUTH_BASIC
50        ]);
51
52        $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
53        $this->assertEquals(CURLAUTH_BASIC, $client->curlSettings[CURLOPT_HTTPAUTH]);
54
55    }
56
57    function testDigestAuth() {
58
59        $client = new ClientMock([
60            'baseUri' => '/',
61            'userName' => 'foo',
62            'password' => 'bar',
63            'authType' => Client::AUTH_DIGEST
64        ]);
65
66        $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
67        $this->assertEquals(CURLAUTH_DIGEST, $client->curlSettings[CURLOPT_HTTPAUTH]);
68
69    }
70
71    function testNTLMAuth() {
72
73        $client = new ClientMock([
74            'baseUri' => '/',
75            'userName' => 'foo',
76            'password' => 'bar',
77            'authType' => Client::AUTH_NTLM
78        ]);
79
80        $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
81        $this->assertEquals(CURLAUTH_NTLM, $client->curlSettings[CURLOPT_HTTPAUTH]);
82
83    }
84
85    function testProxy() {
86
87        $client = new ClientMock([
88            'baseUri' => '/',
89            'proxy' => 'localhost:8888',
90        ]);
91
92        $this->assertEquals("localhost:8888", $client->curlSettings[CURLOPT_PROXY]);
93
94    }
95
96    function testEncoding() {
97
98        $client = new ClientMock([
99            'baseUri' => '/',
100            'encoding' => Client::ENCODING_IDENTITY | Client::ENCODING_GZIP | Client::ENCODING_DEFLATE,
101        ]);
102
103        $this->assertEquals("identity,deflate,gzip", $client->curlSettings[CURLOPT_ENCODING]);
104
105    }
106
107    function testPropFind() {
108
109        $client = new ClientMock([
110            'baseUri' => '/',
111        ]);
112
113        $responseBody = <<<XML
114<?xml version="1.0"?>
115<multistatus xmlns="DAV:">
116<response>
117  <href>/foo</href>
118  <propstat>
119    <prop>
120      <displayname>bar</displayname>
121    </prop>
122    <status>HTTP/1.1 200 OK</status>
123  </propstat>
124</response>
125</multistatus>
126XML;
127
128        $client->response = new Response(207, [], $responseBody);
129        $result = $client->propfind('foo', ['{DAV:}displayname', '{urn:zim}gir']);
130
131        $this->assertEquals(['{DAV:}displayname' => 'bar'], $result);
132
133        $request = $client->request;
134        $this->assertEquals('PROPFIND', $request->getMethod());
135        $this->assertEquals('/foo', $request->getUrl());
136        $this->assertEquals([
137            'Depth' => ['0'],
138            'Content-Type' => ['application/xml'],
139        ], $request->getHeaders());
140
141    }
142
143    /**
144     * @expectedException \Sabre\DAV\Exception
145     */
146    function testPropFindError() {
147
148        $client = new ClientMock([
149            'baseUri' => '/',
150        ]);
151
152        $client->response = new Response(405, []);
153        $client->propfind('foo', ['{DAV:}displayname', '{urn:zim}gir']);
154
155    }
156
157    function testPropFindDepth1() {
158
159        $client = new ClientMock([
160            'baseUri' => '/',
161        ]);
162
163        $responseBody = <<<XML
164<?xml version="1.0"?>
165<multistatus xmlns="DAV:">
166<response>
167  <href>/foo</href>
168  <propstat>
169    <prop>
170      <displayname>bar</displayname>
171    </prop>
172    <status>HTTP/1.1 200 OK</status>
173  </propstat>
174</response>
175</multistatus>
176XML;
177
178        $client->response = new Response(207, [], $responseBody);
179        $result = $client->propfind('foo', ['{DAV:}displayname', '{urn:zim}gir'], 1);
180
181        $this->assertEquals([
182            '/foo' => [
183            '{DAV:}displayname' => 'bar'
184            ],
185        ], $result);
186
187        $request = $client->request;
188        $this->assertEquals('PROPFIND', $request->getMethod());
189        $this->assertEquals('/foo', $request->getUrl());
190        $this->assertEquals([
191            'Depth' => ['1'],
192            'Content-Type' => ['application/xml'],
193        ], $request->getHeaders());
194
195    }
196
197    function testPropPatch() {
198
199        $client = new ClientMock([
200            'baseUri' => '/',
201        ]);
202
203        $responseBody = <<<XML
204<?xml version="1.0"?>
205<multistatus xmlns="DAV:">
206<response>
207  <href>/foo</href>
208  <propstat>
209    <prop>
210      <displayname>bar</displayname>
211    </prop>
212    <status>HTTP/1.1 200 OK</status>
213  </propstat>
214</response>
215</multistatus>
216XML;
217
218        $client->response = new Response(207, [], $responseBody);
219        $result = $client->propPatch('foo', ['{DAV:}displayname' => 'hi', '{urn:zim}gir' => null], 1);
220        $request = $client->request;
221        $this->assertEquals('PROPPATCH', $request->getMethod());
222        $this->assertEquals('/foo', $request->getUrl());
223        $this->assertEquals([
224            'Content-Type' => ['application/xml'],
225        ], $request->getHeaders());
226
227    }
228
229    function testOPTIONS() {
230
231        $client = new ClientMock([
232            'baseUri' => '/',
233        ]);
234
235        $client->response = new Response(207, [
236            'DAV' => 'calendar-access, extended-mkcol',
237        ]);
238        $result = $client->options();
239
240        $this->assertEquals(
241            ['calendar-access', 'extended-mkcol'],
242            $result
243        );
244
245        $request = $client->request;
246        $this->assertEquals('OPTIONS', $request->getMethod());
247        $this->assertEquals('/', $request->getUrl());
248        $this->assertEquals([
249        ], $request->getHeaders());
250
251    }
252}
253