1<?php
2
3namespace Sabre\DAV\Browser;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8require_once 'Sabre/DAV/AbstractServer.php';
9
10class PluginTest extends DAV\AbstractServer{
11
12    protected $plugin;
13
14    function setUp() {
15
16        parent::setUp();
17        $this->server->addPlugin($this->plugin = new Plugin());
18        $this->server->tree->getNodeForPath('')->createDirectory('dir2');
19
20    }
21
22    function testCollectionGet() {
23
24        $request = new HTTP\Request('GET', '/dir');
25        $this->server->httpRequest = $request;
26        $this->server->exec();
27
28        $this->assertEquals(200, $this->response->getStatus(), "Incorrect status received. Full response body: " . $this->response->getBodyAsString());
29        $this->assertEquals(
30            [
31                'X-Sabre-Version' => [DAV\Version::VERSION],
32                'Content-Type' => ['text/html; charset=utf-8'],
33                'Content-Security-Policy' => ["img-src 'self'; style-src 'self';"]
34            ],
35            $this->response->getHeaders()
36        );
37
38        $body = $this->response->getBodyAsString();
39        $this->assertTrue(strpos($body, '<title>dir') !== false, $body);
40        $this->assertTrue(strpos($body, '<a href="/dir/child.txt">')!==false);
41
42    }
43
44    /**
45     * Adding the If-None-Match should have 0 effect, but it threw an error.
46     */
47    function testCollectionGetIfNoneMatch() {
48
49        $request = new HTTP\Request('GET', '/dir');
50        $request->setHeader('If-None-Match', '"foo-bar"');
51        $this->server->httpRequest = $request;
52        $this->server->exec();
53
54        $this->assertEquals(200, $this->response->getStatus(), "Incorrect status received. Full response body: " . $this->response->getBodyAsString());
55        $this->assertEquals(
56            [
57                'X-Sabre-Version' => [DAV\Version::VERSION],
58                'Content-Type' => ['text/html; charset=utf-8'],
59                'Content-Security-Policy' => ["img-src 'self'; style-src 'self';"]
60            ],
61            $this->response->getHeaders()
62        );
63
64        $body = $this->response->getBodyAsString();
65        $this->assertTrue(strpos($body, '<title>dir') !== false, $body);
66        $this->assertTrue(strpos($body, '<a href="/dir/child.txt">')!==false);
67
68    }
69    function testCollectionGetRoot() {
70
71        $request = new HTTP\Request('GET', '/');
72        $this->server->httpRequest = ($request);
73        $this->server->exec();
74
75        $this->assertEquals(200, $this->response->status, "Incorrect status received. Full response body: " . $this->response->getBodyAsString());
76        $this->assertEquals(
77            [
78                'X-Sabre-Version' => [DAV\Version::VERSION],
79                'Content-Type' => ['text/html; charset=utf-8'],
80                'Content-Security-Policy' => ["img-src 'self'; style-src 'self';"]
81            ],
82            $this->response->getHeaders()
83        );
84
85        $body = $this->response->getBodyAsString();
86        $this->assertTrue(strpos($body, '<title>/') !== false, $body);
87        $this->assertTrue(strpos($body, '<a href="/dir/">')!==false);
88        $this->assertTrue(strpos($body, '<span class="btn disabled">')!==false);
89
90    }
91
92    function testGETPassthru() {
93
94        $request = new HTTP\Request('GET', '/random');
95        $response = new HTTP\Response();
96        $this->assertNull(
97            $this->plugin->httpGet($request, $response)
98        );
99
100    }
101
102    function testPostOtherContentType() {
103
104        $request = new HTTP\Request('POST', '/', ['Content-Type' => 'text/xml']);
105        $this->server->httpRequest = $request;
106        $this->server->exec();
107
108        $this->assertEquals(501, $this->response->status);
109
110    }
111
112    function testPostNoSabreAction() {
113
114        $request = new HTTP\Request('POST', '/', ['Content-Type' => 'application/x-www-form-urlencoded']);
115        $request->setPostData([]);
116        $this->server->httpRequest = $request;
117        $this->server->exec();
118
119        $this->assertEquals(501, $this->response->status);
120
121    }
122
123    function testPostMkCol() {
124
125        $serverVars = array(
126            'REQUEST_URI'    => '/',
127            'REQUEST_METHOD' => 'POST',
128            'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
129        );
130        $postVars = array(
131            'sabreAction' => 'mkcol',
132            'name' => 'new_collection',
133        );
134
135        $request = HTTP\Sapi::createFromServerArray($serverVars);
136        $request->setPostData($postVars);
137        $this->server->httpRequest = $request;
138        $this->server->exec();
139
140        $this->assertEquals(302, $this->response->status);
141        $this->assertEquals(array(
142            'X-Sabre-Version' => [DAV\Version::VERSION],
143            'Location' => ['/'],
144        ), $this->response->getHeaders());
145
146        $this->assertTrue(is_dir(SABRE_TEMPDIR . '/new_collection'));
147
148    }
149
150    function testGetAsset() {
151
152        $request = new HTTP\Request('GET', '/?sabreAction=asset&assetName=favicon.ico');
153        $this->server->httpRequest = $request;
154        $this->server->exec();
155
156        $this->assertEquals(200, $this->response->getStatus(), 'Error: ' . $this->response->body);
157        $this->assertEquals([
158            'X-Sabre-Version' => [DAV\Version::VERSION],
159            'Content-Type' => ['image/vnd.microsoft.icon'],
160            'Content-Length' => ['4286'],
161            'Cache-Control' => ['public, max-age=1209600'],
162            'Content-Security-Policy' => ["img-src 'self'; style-src 'self';"]
163        ], $this->response->getHeaders());
164
165    }
166
167    function testGetAsset404() {
168
169        $request = new HTTP\Request('GET', '/?sabreAction=asset&assetName=flavicon.ico');
170        $this->server->httpRequest = $request;
171        $this->server->exec();
172
173        $this->assertEquals(404, $this->response->getStatus(), 'Error: ' . $this->response->body);
174
175    }
176
177    function testGetAssetEscapeBasePath() {
178
179        $request = new HTTP\Request('GET', '/?sabreAction=asset&assetName=./../assets/favicon.ico');
180        $this->server->httpRequest = $request;
181        $this->server->exec();
182
183        $this->assertEquals(404, $this->response->getStatus(), 'Error: ' . $this->response->body);
184
185    }
186}
187