ServerPropsTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Sabre\DAV;
  3. use Sabre\HTTP;
  4. require_once 'Sabre/HTTP/ResponseMock.php';
  5. require_once 'Sabre/DAV/AbstractServer.php';
  6. class ServerPropsTest extends AbstractServer {
  7. protected function getRootNode() {
  8. return new FSExt\Directory(SABRE_TEMPDIR);
  9. }
  10. function setUp() {
  11. if (file_exists(SABRE_TEMPDIR . '../.sabredav')) unlink(SABRE_TEMPDIR . '../.sabredav');
  12. parent::setUp();
  13. file_put_contents(SABRE_TEMPDIR . '/test2.txt', 'Test contents2');
  14. mkdir(SABRE_TEMPDIR . '/col');
  15. file_put_contents(SABRE_TEMPDIR . 'col/test.txt', 'Test contents');
  16. $this->server->addPlugin(new Locks\Plugin(new Locks\Backend\File(SABRE_TEMPDIR . '/.locksdb')));
  17. }
  18. function tearDown() {
  19. parent::tearDown();
  20. if (file_exists(SABRE_TEMPDIR . '../.locksdb')) unlink(SABRE_TEMPDIR . '../.locksdb');
  21. }
  22. private function sendRequest($body, $path = '/', $headers = ['Depth' => '0']) {
  23. $request = new HTTP\Request('PROPFIND', $path, $headers, $body);
  24. $this->server->httpRequest = $request;
  25. $this->server->exec();
  26. }
  27. function testPropFindEmptyBody() {
  28. $this->sendRequest("");
  29. $this->assertEquals(207, $this->response->status);
  30. $this->assertEquals([
  31. 'X-Sabre-Version' => [Version::VERSION],
  32. 'Content-Type' => ['application/xml; charset=utf-8'],
  33. 'DAV' => ['1, 3, extended-mkcol, 2'],
  34. 'Vary' => ['Brief,Prefer'],
  35. ],
  36. $this->response->getHeaders()
  37. );
  38. $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
  39. $xml = simplexml_load_string($body);
  40. $xml->registerXPathNamespace('d', 'urn:DAV');
  41. list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
  42. $this->assertEquals('/', (string)$data, 'href element should have been /');
  43. $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
  44. $this->assertEquals(1, count($data));
  45. }
  46. function testPropFindEmptyBodyFile() {
  47. $this->sendRequest("", '/test2.txt', []);
  48. $this->assertEquals(207, $this->response->status);
  49. $this->assertEquals([
  50. 'X-Sabre-Version' => [Version::VERSION],
  51. 'Content-Type' => ['application/xml; charset=utf-8'],
  52. 'DAV' => ['1, 3, extended-mkcol, 2'],
  53. 'Vary' => ['Brief,Prefer'],
  54. ],
  55. $this->response->getHeaders()
  56. );
  57. $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
  58. $xml = simplexml_load_string($body);
  59. $xml->registerXPathNamespace('d', 'urn:DAV');
  60. list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
  61. $this->assertEquals('/test2.txt', (string)$data, 'href element should have been /test2.txt');
  62. $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength');
  63. $this->assertEquals(1, count($data));
  64. }
  65. function testSupportedLocks() {
  66. $xml = '<?xml version="1.0"?>
  67. <d:propfind xmlns:d="DAV:">
  68. <d:prop>
  69. <d:supportedlock />
  70. </d:prop>
  71. </d:propfind>';
  72. $this->sendRequest($xml);
  73. $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
  74. $xml = simplexml_load_string($body);
  75. $xml->registerXPathNamespace('d', 'urn:DAV');
  76. $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry');
  77. $this->assertEquals(2, count($data), 'We expected two \'d:lockentry\' tags');
  78. $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope');
  79. $this->assertEquals(2, count($data), 'We expected two \'d:lockscope\' tags');
  80. $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype');
  81. $this->assertEquals(2, count($data), 'We expected two \'d:locktype\' tags');
  82. $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:shared');
  83. $this->assertEquals(1, count($data), 'We expected a \'d:shared\' tag');
  84. $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:exclusive');
  85. $this->assertEquals(1, count($data), 'We expected a \'d:exclusive\' tag');
  86. $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype/d:write');
  87. $this->assertEquals(2, count($data), 'We expected two \'d:write\' tags');
  88. }
  89. function testLockDiscovery() {
  90. $xml = '<?xml version="1.0"?>
  91. <d:propfind xmlns:d="DAV:">
  92. <d:prop>
  93. <d:lockdiscovery />
  94. </d:prop>
  95. </d:propfind>';
  96. $this->sendRequest($xml);
  97. $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
  98. $xml = simplexml_load_string($body);
  99. $xml->registerXPathNamespace('d', 'urn:DAV');
  100. $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:lockdiscovery');
  101. $this->assertEquals(1, count($data), 'We expected a \'d:lockdiscovery\' tag');
  102. }
  103. function testUnknownProperty() {
  104. $xml = '<?xml version="1.0"?>
  105. <d:propfind xmlns:d="DAV:">
  106. <d:prop>
  107. <d:macaroni />
  108. </d:prop>
  109. </d:propfind>';
  110. $this->sendRequest($xml);
  111. $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
  112. $xml = simplexml_load_string($body);
  113. $xml->registerXPathNamespace('d', 'urn:DAV');
  114. $pathTests = [
  115. '/d:multistatus',
  116. '/d:multistatus/d:response',
  117. '/d:multistatus/d:response/d:propstat',
  118. '/d:multistatus/d:response/d:propstat/d:status',
  119. '/d:multistatus/d:response/d:propstat/d:prop',
  120. '/d:multistatus/d:response/d:propstat/d:prop/d:macaroni',
  121. ];
  122. foreach ($pathTests as $test) {
  123. $this->assertTrue(count($xml->xpath($test)) == true, 'We expected the ' . $test . ' element to appear in the response, we got: ' . $body);
  124. }
  125. $val = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
  126. $this->assertEquals(1, count($val), $body);
  127. $this->assertEquals('HTTP/1.1 404 Not Found', (string)$val[0]);
  128. }
  129. function testParsePropPatchRequest() {
  130. $body = '<?xml version="1.0"?>
  131. <d:propertyupdate xmlns:d="DAV:" xmlns:s="http://sabredav.org/NS/test">
  132. <d:set><d:prop><s:someprop>somevalue</s:someprop></d:prop></d:set>
  133. <d:remove><d:prop><s:someprop2 /></d:prop></d:remove>
  134. <d:set><d:prop><s:someprop3>removeme</s:someprop3></d:prop></d:set>
  135. <d:remove><d:prop><s:someprop3 /></d:prop></d:remove>
  136. </d:propertyupdate>';
  137. $result = $this->server->xml->parse($body);
  138. $this->assertEquals([
  139. '{http://sabredav.org/NS/test}someprop' => 'somevalue',
  140. '{http://sabredav.org/NS/test}someprop2' => null,
  141. '{http://sabredav.org/NS/test}someprop3' => null,
  142. ], $result->properties);
  143. }
  144. }