1<?php
2
3namespace Sabre\DAV\Locks;
4
5use Sabre\HTTP;
6use Sabre\DAV;
7
8require_once 'Sabre/HTTP/ResponseMock.php';
9require_once 'Sabre/TestUtil.php';
10
11class MSWordTest extends \PHPUnit_Framework_TestCase {
12
13    function testLockEtc() {
14
15        mkdir(SABRE_TEMPDIR . '/mstest');
16        $tree = new DAV\FS\Directory(SABRE_TEMPDIR . '/mstest');
17
18        $server = new DAV\Server($tree);
19        $server->debugExceptions = true;
20        $locksBackend = new Backend\File(SABRE_TEMPDIR . '/locksdb');
21        $locksPlugin = new Plugin($locksBackend);
22        $server->addPlugin($locksPlugin);
23
24        $response1 = new HTTP\ResponseMock();
25
26        $server->httpRequest = $this->getLockRequest();
27        $server->httpResponse = $response1;
28        $server->sapi = new HTTP\SapiMock();
29        $server->exec();
30
31        $this->assertEquals(201, $server->httpResponse->getStatus(), 'Full response body:' . $response1->getBodyAsString());
32        $this->assertTrue(!!$server->httpResponse->getHeaders('Lock-Token'));
33        $lockToken = $server->httpResponse->getHeader('Lock-Token');
34
35        //sleep(10);
36
37        $response2 = new HTTP\ResponseMock();
38
39        $server->httpRequest = $this->getLockRequest2();
40        $server->httpResponse = $response2;
41        $server->exec();
42
43        $this->assertEquals(201, $server->httpResponse->status);
44        $this->assertTrue(!!$server->httpResponse->getHeaders('Lock-Token'));
45
46        //sleep(10);
47
48        $response3 = new HTTP\ResponseMock();
49        $server->httpRequest = $this->getPutRequest($lockToken);
50        $server->httpResponse = $response3;
51        $server->exec();
52
53        $this->assertEquals(204, $server->httpResponse->status);
54
55    }
56
57    function tearDown() {
58
59        \Sabre\TestUtil::clearTempDir();
60
61    }
62
63    function getLockRequest() {
64
65        $request = HTTP\Sapi::createFromServerArray(array(
66           'REQUEST_METHOD'    => 'LOCK',
67           'HTTP_CONTENT_TYPE' => 'application/xml',
68           'HTTP_TIMEOUT'      => 'Second-3600',
69           'REQUEST_URI'       => '/Nouveau%20Microsoft%20Office%20Excel%20Worksheet.xlsx',
70        ));
71
72        $request->setBody('<D:lockinfo xmlns:D="DAV:">
73    <D:lockscope>
74        <D:exclusive />
75    </D:lockscope>
76    <D:locktype>
77        <D:write />
78    </D:locktype>
79    <D:owner>
80        <D:href>PC-Vista\User</D:href>
81    </D:owner>
82</D:lockinfo>');
83
84        return $request;
85
86    }
87    function getLockRequest2() {
88
89        $request = HTTP\Sapi::createFromServerArray(array(
90           'REQUEST_METHOD'    => 'LOCK',
91           'HTTP_CONTENT_TYPE' => 'application/xml',
92           'HTTP_TIMEOUT'      => 'Second-3600',
93           'REQUEST_URI'       => '/~$Nouveau%20Microsoft%20Office%20Excel%20Worksheet.xlsx',
94        ));
95
96        $request->setBody('<D:lockinfo xmlns:D="DAV:">
97    <D:lockscope>
98        <D:exclusive />
99    </D:lockscope>
100    <D:locktype>
101        <D:write />
102    </D:locktype>
103    <D:owner>
104        <D:href>PC-Vista\User</D:href>
105    </D:owner>
106</D:lockinfo>');
107
108        return $request;
109
110    }
111
112    function getPutRequest($lockToken) {
113
114        $request = HTTP\Sapi::createFromServerArray(array(
115           'REQUEST_METHOD'    => 'PUT',
116           'REQUEST_URI'       => '/Nouveau%20Microsoft%20Office%20Excel%20Worksheet.xlsx',
117           'HTTP_IF'           => 'If: ('.$lockToken.')',
118        ));
119        $request->setBody('FAKE BODY');
120        return $request;
121
122    }
123
124}
125