1<?php
2
3namespace Sabre;
4
5use
6    Sabre\HTTP\Request,
7    Sabre\HTTP\Response,
8    Sabre\HTTP\Sapi;
9
10/**
11 * This class may be used as a basis for other webdav-related unittests.
12 *
13 * This class is supposed to provide a reasonably big framework to quickly get
14 * a testing environment running.
15 *
16 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
17 * @author Evert Pot (http://evertpot.com/)
18 * @license http://sabre.io/license/ Modified BSD License
19 */
20abstract class DAVServerTest extends \PHPUnit_Framework_TestCase {
21
22    protected $setupCalDAV = false;
23    protected $setupCardDAV = false;
24    protected $setupACL = false;
25    protected $setupCalDAVSharing = false;
26    protected $setupCalDAVScheduling = false;
27    protected $setupCalDAVSubscriptions = false;
28    protected $setupCalDAVICSExport = false;
29    protected $setupLocks = false;
30    protected $setupFiles = false;
31
32    /**
33     * An array with calendars. Every calendar should have
34     *   - principaluri
35     *   - uri
36     */
37    protected $caldavCalendars = array();
38    protected $caldavCalendarObjects = array();
39
40    protected $carddavAddressBooks = array();
41    protected $carddavCards = array();
42
43    /**
44     * @var Sabre\DAV\Server
45     */
46    protected $server;
47    protected $tree = array();
48
49    protected $caldavBackend;
50    protected $carddavBackend;
51    protected $principalBackend;
52    protected $locksBackend;
53
54    /**
55     * @var Sabre\CalDAV\Plugin
56     */
57    protected $caldavPlugin;
58
59    /**
60     * @var Sabre\CardDAV\Plugin
61     */
62    protected $carddavPlugin;
63
64    /**
65     * @var Sabre\DAVACL\Plugin
66     */
67    protected $aclPlugin;
68
69    /**
70     * @var Sabre\CalDAV\SharingPlugin
71     */
72    protected $caldavSharingPlugin;
73
74    /**
75     * CalDAV scheduling plugin
76     *
77     * @var CalDAV\Schedule\Plugin
78     */
79    protected $caldavSchedulePlugin;
80
81    /**
82     * @var Sabre\DAV\Auth\Plugin
83     */
84    protected $authPlugin;
85
86    /**
87     * @var Sabre\DAV\Locks\Plugin
88     */
89    protected $locksPlugin;
90
91    /**
92     * If this string is set, we will automatically log in the user with this
93     * name.
94     */
95    protected $autoLogin = null;
96
97    function setUp() {
98
99        $this->setUpBackends();
100        $this->setUpTree();
101
102        $this->server = new DAV\Server($this->tree);
103        $this->server->sapi = new HTTP\SapiMock();
104        $this->server->debugExceptions = true;
105
106        if ($this->setupCalDAV) {
107            $this->caldavPlugin = new CalDAV\Plugin();
108            $this->server->addPlugin($this->caldavPlugin);
109        }
110        if ($this->setupCalDAVSharing) {
111            $this->caldavSharingPlugin = new CalDAV\SharingPlugin();
112            $this->server->addPlugin($this->caldavSharingPlugin);
113        }
114        if ($this->setupCalDAVScheduling) {
115            $this->caldavSchedulePlugin = new CalDAV\Schedule\Plugin();
116            $this->server->addPlugin($this->caldavSchedulePlugin);
117        }
118        if ($this->setupCalDAVSubscriptions) {
119            $this->server->addPlugin(new CalDAV\Subscriptions\Plugin());
120        }
121        if ($this->setupCalDAVICSExport) {
122            $this->caldavICSExportPlugin = new CalDAV\ICSExportPlugin();
123            $this->server->addPlugin($this->caldavICSExportPlugin);
124        }
125        if ($this->setupCardDAV) {
126            $this->carddavPlugin = new CardDAV\Plugin();
127            $this->server->addPlugin($this->carddavPlugin);
128        }
129        if ($this->setupACL) {
130            $this->aclPlugin = new DAVACL\Plugin();
131            $this->server->addPlugin($this->aclPlugin);
132        }
133        if ($this->setupLocks) {
134            $this->locksPlugin = new DAV\Locks\Plugin(
135                $this->locksBackend
136            );
137            $this->server->addPlugin($this->locksPlugin);
138        }
139        if ($this->autoLogin) {
140            $authBackend = new DAV\Auth\Backend\Mock();
141            $authBackend->setPrincipal('principals/' . $this->autoLogin);
142            $this->authPlugin = new DAV\Auth\Plugin($authBackend);
143            $this->server->addPlugin($this->authPlugin);
144
145            // This will trigger the actual login procedure
146            $this->authPlugin->beforeMethod(new Request(), new Response());
147        }
148
149    }
150
151    /**
152     * Makes a request, and returns a response object.
153     *
154     * You can either pass an instance of Sabre\HTTP\Request, or an array,
155     * which will then be used as the _SERVER array.
156     *
157     * @param array|\Sabre\HTTP\Request $request
158     * @return \Sabre\HTTP\Response
159     */
160    function request($request) {
161
162        if (is_array($request)) {
163            $request = HTTP\Request::createFromServerArray($request);
164        }
165        $this->server->httpRequest = $request;
166        $this->server->httpResponse = new HTTP\ResponseMock();
167        $this->server->exec();
168
169        return $this->server->httpResponse;
170
171    }
172
173    /**
174     * Override this to provide your own Tree for your test-case.
175     */
176    function setUpTree() {
177
178        if ($this->setupCalDAV) {
179            $this->tree[] = new CalDAV\CalendarRoot(
180                $this->principalBackend,
181                $this->caldavBackend
182            );
183        }
184        if ($this->setupCardDAV) {
185            $this->tree[] = new CardDAV\AddressBookRoot(
186                $this->principalBackend,
187                $this->carddavBackend
188            );
189        }
190
191        if ($this->setupCardDAV || $this->setupCalDAV) {
192            $this->tree[] = new DAVACL\PrincipalCollection(
193                $this->principalBackend
194            );
195        }
196        if ($this->setupFiles) {
197
198            $this->tree[] = new DAV\Mock\Collection('files');
199
200        }
201
202    }
203
204    function setUpBackends() {
205
206        if ($this->setupCalDAVSharing && is_null($this->caldavBackend)) {
207            $this->caldavBackend = new CalDAV\Backend\MockSharing($this->caldavCalendars, $this->caldavCalendarObjects);
208        }
209        if ($this->setupCalDAVSubscriptions && is_null($this->caldavBackend)) {
210            $this->caldavBackend = new CalDAV\Backend\MockSubscriptionSupport($this->caldavCalendars, $this->caldavCalendarObjects);
211        }
212        if ($this->setupCalDAV && is_null($this->caldavBackend)) {
213            if ($this->setupCalDAVScheduling) {
214                $this->caldavBackend = new CalDAV\Backend\MockScheduling($this->caldavCalendars, $this->caldavCalendarObjects);
215            } else {
216                $this->caldavBackend = new CalDAV\Backend\Mock($this->caldavCalendars, $this->caldavCalendarObjects);
217            }
218        }
219        if ($this->setupCardDAV && is_null($this->carddavBackend)) {
220            $this->carddavBackend = new CardDAV\Backend\Mock($this->carddavAddressBooks, $this->carddavCards);
221        }
222        if ($this->setupCardDAV || $this->setupCalDAV) {
223            $this->principalBackend = new DAVACL\PrincipalBackend\Mock();
224        }
225        if ($this->setupLocks) {
226            $this->locksBackend = new DAV\Locks\Backend\Mock();
227        }
228
229    }
230
231
232    function assertHTTPStatus($expectedStatus, HTTP\Request $req) {
233
234        $resp = $this->request($req);
235        $this->assertEquals((int)$expectedStatus, (int)$resp->status,'Incorrect HTTP status received: ' . $resp->body);
236
237    }
238
239}
240