PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Sabre/DAVServerTest.php

https://github.com/KOLANICH/SabreDAV
PHP | 183 lines | 101 code | 36 blank | 46 comment | 16 complexity | 428ed0bdd49d1b2acb4c257831cefb4c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre;
  3. require_once 'Sabre/HTTP/ResponseMock.php';
  4. require_once 'Sabre/CalDAV/Backend/Mock.php';
  5. require_once 'Sabre/CalDAV/Backend/MockSubscriptionSupport.php';
  6. require_once 'Sabre/CardDAV/Backend/Mock.php';
  7. require_once 'Sabre/DAVACL/PrincipalBackend/Mock.php';
  8. require_once 'Sabre/DAV/Auth/Backend/Mock.php';
  9. /**
  10. * This class may be used as a basis for other webdav-related unittests.
  11. *
  12. * This class is supposed to provide a reasonably big framework to quickly get
  13. * a testing environment running.
  14. *
  15. * @package Sabre
  16. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  17. * @author Evert Pot (http://evertpot.com/)
  18. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  19. */
  20. abstract class DAVServerTest extends \PHPUnit_Framework_TestCase {
  21. protected $setupCalDAV = false;
  22. protected $setupCardDAV = false;
  23. protected $setupACL = false;
  24. protected $setupCalDAVSharing = false;
  25. protected $caldavCalendars = array();
  26. protected $caldavCalendarObjects = array();
  27. protected $carddavAddressBooks = array();
  28. protected $carddavCards = array();
  29. /**
  30. * @var Sabre\DAV\Server
  31. */
  32. protected $server;
  33. protected $tree = array();
  34. protected $caldavBackend;
  35. protected $carddavBackend;
  36. protected $principalBackend;
  37. /**
  38. * @var Sabre\CalDAV\Plugin
  39. */
  40. protected $caldavPlugin;
  41. /**
  42. * @var Sabre\CardDAV\Plugin
  43. */
  44. protected $carddavPlugin;
  45. /**
  46. * @var Sabre\DAVACL\Plugin
  47. */
  48. protected $aclPlugin;
  49. /**
  50. * @var Sabre\CalDAV\SharingPlugin
  51. */
  52. protected $caldavSharingPlugin;
  53. /**
  54. * @var Sabre\DAV\Auth\Plugin
  55. */
  56. protected $authPlugin;
  57. /**
  58. * If this string is set, we will automatically log in the user with this
  59. * name.
  60. */
  61. protected $autoLogin = null;
  62. function setUp() {
  63. $this->setUpBackends();
  64. $this->setUpTree();
  65. $this->server = new DAV\Server($this->tree);
  66. $this->server->debugExceptions = true;
  67. if ($this->setupCalDAV) {
  68. $this->caldavPlugin = new CalDAV\Plugin();
  69. $this->server->addPlugin($this->caldavPlugin);
  70. }
  71. if ($this->setupCalDAVSharing) {
  72. $this->caldavSharingPlugin = new CalDAV\SharingPlugin();
  73. $this->server->addPlugin($this->caldavSharingPlugin);
  74. }
  75. if ($this->setupCardDAV) {
  76. $this->carddavPlugin = new CardDAV\Plugin();
  77. $this->server->addPlugin($this->carddavPlugin);
  78. }
  79. if ($this->setupACL) {
  80. $this->aclPlugin = new DAVACL\Plugin();
  81. $this->server->addPlugin($this->aclPlugin);
  82. }
  83. if ($this->autoLogin) {
  84. $authBackend = new DAV\Auth\Backend\Mock();
  85. $authBackend->defaultUser = $this->autoLogin;
  86. $this->authPlugin = new DAV\Auth\Plugin($authBackend, 'SabreDAV');
  87. $this->server->addPlugin($this->authPlugin);
  88. // This will trigger the actual login procedure
  89. $this->authPlugin->beforeMethod('OPTIONS','/');
  90. }
  91. }
  92. /**
  93. * Makes a request, and returns a response object.
  94. *
  95. * You can either pass an instance of Sabre\HTTP\Request, or an array,
  96. * which will then be used as the _SERVER array.
  97. *
  98. * @param array|\Sabre\HTTP\Request $request
  99. * @return \Sabre\HTTP\Response
  100. */
  101. function request($request) {
  102. if (is_array($request)) {
  103. $request = new HTTP\Request($request);
  104. }
  105. $this->server->httpRequest = $request;
  106. $this->server->httpResponse = new HTTP\ResponseMock();
  107. $this->server->exec();
  108. return $this->server->httpResponse;
  109. }
  110. /**
  111. * Override this to provide your own Tree for your test-case.
  112. */
  113. function setUpTree() {
  114. if ($this->setupCalDAV) {
  115. $this->tree[] = new CalDAV\CalendarRootNode(
  116. $this->principalBackend,
  117. $this->caldavBackend
  118. );
  119. }
  120. if ($this->setupCardDAV) {
  121. $this->tree[] = new CardDAV\AddressBookRoot(
  122. $this->principalBackend,
  123. $this->carddavBackend
  124. );
  125. }
  126. if ($this->setupCardDAV || $this->setupCalDAV) {
  127. $this->tree[] = new DAVACL\PrincipalCollection(
  128. $this->principalBackend
  129. );
  130. }
  131. }
  132. function setUpBackends() {
  133. if ($this->setupCalDAV && is_null($this->caldavBackend)) {
  134. $this->caldavBackend = new CalDAV\Backend\Mock($this->caldavCalendars, $this->caldavCalendarObjects);
  135. }
  136. if ($this->setupCardDAV && is_null($this->carddavBackend)) {
  137. $this->carddavBackend = new CardDAV\Backend\Mock($this->carddavAddressBooks, $this->carddavCards);
  138. }
  139. if ($this->setupCardDAV || $this->setupCalDAV) {
  140. $this->principalBackend = new DAVACL\PrincipalBackend\Mock();
  141. }
  142. }
  143. function assertHTTPStatus($expectedStatus, HTTP\Request $req) {
  144. $resp = $this->request($req);
  145. $this->assertEquals($resp->getStatusMessage($expectedStatus), $resp->status,'Incorrect HTTP status received: ' . $resp->body);
  146. }
  147. }