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