/tests/Sabre/DAV/FSExt/ServerTest.php

https://github.com/fruux/sabre-dav · PHP · 267 lines · 184 code · 76 blank · 7 comment · 0 complexity · 8207bf2451a38edeb28dee904458526b MD5 · raw file

  1. <?php declare (strict_types=1);
  2. namespace Sabre\DAV\FSExt;
  3. use Sabre\DAV;
  4. use Sabre\HTTP;
  5. require_once 'Sabre/DAV/AbstractServer.php';
  6. class ServerTest extends DAV\AbstractServer{
  7. protected function getRootNode() {
  8. return new Directory($this->tempDir);
  9. }
  10. function testGet() {
  11. $request = new HTTP\Request('GET', '/test.txt');
  12. $filename = $this->tempDir . '/test.txt';
  13. $this->server->httpRequest = $request;
  14. $this->server->exec();
  15. $this->assertEquals(200, $this->response->getStatus(), 'Invalid status code received.');
  16. $this->assertEquals([
  17. 'X-Sabre-Version' => [DAV\Version::VERSION],
  18. 'Content-Type' => ['application/octet-stream'],
  19. 'Content-Length' => [13],
  20. 'Last-Modified' => [HTTP\toDate(new \DateTime('@' . filemtime($filename)))],
  21. 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'],
  22. ],
  23. $this->response->getHeaders()
  24. );
  25. $this->assertEquals('Test contents', stream_get_contents($this->response->body));
  26. }
  27. function testHEAD() {
  28. $request = new HTTP\Request('HEAD', '/test.txt');
  29. $filename = $this->tempDir . '/test.txt';
  30. $this->server->httpRequest = ($request);
  31. $this->server->exec();
  32. $this->assertEquals([
  33. 'X-Sabre-Version' => [DAV\Version::VERSION],
  34. 'Content-Type' => ['application/octet-stream'],
  35. 'Content-Length' => [13],
  36. 'Last-Modified' => [HTTP\toDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))],
  37. 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'],
  38. ],
  39. $this->response->getHeaders()
  40. );
  41. $this->assertEquals(200, $this->response->status);
  42. $this->assertEquals('', $this->response->body);
  43. }
  44. function testPut() {
  45. $request = new HTTP\Request('PUT', '/testput.txt');
  46. $filename = $this->tempDir . '/testput.txt';
  47. $request->setBody('Testing new file');
  48. $this->server->httpRequest = ($request);
  49. $this->server->exec();
  50. $this->assertEquals([
  51. 'X-Sabre-Version' => [DAV\Version::VERSION],
  52. 'Content-Length' => ['0'],
  53. 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'],
  54. ], $this->response->getHeaders());
  55. $this->assertEquals(201, $this->response->status);
  56. $this->assertEquals('', $this->response->body);
  57. $this->assertEquals('Testing new file', file_get_contents($filename));
  58. }
  59. function testPutAlreadyExists() {
  60. $request = new HTTP\Request('PUT', '/test.txt', ['If-None-Match' => '*']);
  61. $request->setBody('Testing new file');
  62. $this->server->httpRequest = ($request);
  63. $this->server->exec();
  64. $this->assertEquals([
  65. 'X-Sabre-Version' => [DAV\Version::VERSION],
  66. 'Content-Type' => ['application/xml; charset=utf-8'],
  67. ], $this->response->getHeaders());
  68. $this->assertEquals(412, $this->response->status);
  69. $this->assertNotEquals('Testing new file', file_get_contents($this->tempDir . '/test.txt'));
  70. }
  71. function testMkcol() {
  72. $request = new HTTP\Request('MKCOL', '/testcol');
  73. $this->server->httpRequest = ($request);
  74. $this->server->exec();
  75. $this->assertEquals([
  76. 'X-Sabre-Version' => [DAV\Version::VERSION],
  77. 'Content-Length' => ['0'],
  78. ], $this->response->getHeaders());
  79. $this->assertEquals(201, $this->response->status);
  80. $this->assertEquals('', $this->response->body);
  81. $this->assertTrue(is_dir($this->tempDir . '/testcol'));
  82. }
  83. function testPutUpdate() {
  84. $request = new HTTP\Request('PUT', '/test.txt');
  85. $request->setBody('Testing updated file');
  86. $this->server->httpRequest = ($request);
  87. $this->server->exec();
  88. $this->assertEquals('0', $this->response->getHeader('Content-Length'));
  89. $this->assertEquals(204, $this->response->status);
  90. $this->assertEquals('', $this->response->body);
  91. $this->assertEquals('Testing updated file', file_get_contents($this->tempDir . '/test.txt'));
  92. }
  93. function testDelete() {
  94. $request = new HTTP\Request('DELETE', '/test.txt');
  95. $this->server->httpRequest = ($request);
  96. $this->server->exec();
  97. $this->assertEquals([
  98. 'X-Sabre-Version' => [DAV\Version::VERSION],
  99. 'Content-Length' => ['0'],
  100. ], $this->response->getHeaders());
  101. $this->assertEquals(204, $this->response->status);
  102. $this->assertEquals('', $this->response->body);
  103. $this->assertFalse(file_exists($this->tempDir . '/test.txt'));
  104. }
  105. function testDeleteDirectory() {
  106. mkdir($this->tempDir . '/testcol');
  107. file_put_contents($this->tempDir . '/testcol/test.txt', 'Hi! I\'m a file with a short lifespan');
  108. $request = new HTTP\Request('DELETE', '/testcol');
  109. $this->server->httpRequest = ($request);
  110. $this->server->exec();
  111. $this->assertEquals([
  112. 'X-Sabre-Version' => [DAV\Version::VERSION],
  113. 'Content-Length' => ['0'],
  114. ], $this->response->getHeaders());
  115. $this->assertEquals(204, $this->response->status);
  116. $this->assertEquals('', $this->response->body);
  117. $this->assertFalse(file_exists($this->tempDir . '/testcol'));
  118. }
  119. function testOptions() {
  120. $request = new HTTP\Request('OPTIONS', '/');
  121. $this->server->httpRequest = ($request);
  122. $this->server->exec();
  123. $this->assertEquals([
  124. 'DAV' => ['1, 3, extended-mkcol'],
  125. 'MS-Author-Via' => ['DAV'],
  126. 'Allow' => ['OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT'],
  127. 'Accept-Ranges' => ['bytes'],
  128. 'Content-Length' => ['0'],
  129. 'X-Sabre-Version' => [DAV\Version::VERSION],
  130. ], $this->response->getHeaders());
  131. $this->assertEquals(200, $this->response->status);
  132. $this->assertEquals('', $this->response->body);
  133. }
  134. function testMove() {
  135. mkdir($this->tempDir . '/testcol');
  136. $request = new HTTP\Request('MOVE', '/test.txt', ['Destination' => '/testcol/test2.txt']);
  137. $this->server->httpRequest = ($request);
  138. $this->server->exec();
  139. $this->assertEquals(201, $this->response->status);
  140. $this->assertEquals('', $this->response->body);
  141. $this->assertEquals([
  142. 'Content-Length' => ['0'],
  143. 'X-Sabre-Version' => [DAV\Version::VERSION],
  144. ], $this->response->getHeaders());
  145. $this->assertTrue(
  146. is_file($this->tempDir . '/testcol/test2.txt')
  147. );
  148. }
  149. /**
  150. * This test checks if it's possible to move a non-FSExt collection into a
  151. * FSExt collection.
  152. *
  153. * The moveInto function *should* ignore the object and let sabredav itself
  154. * execute the slow move.
  155. */
  156. function testMoveOtherObject() {
  157. mkdir($this->tempDir . '/tree1');
  158. mkdir($this->tempDir . '/tree2');
  159. $tree = new DAV\Tree(new DAV\SimpleCollection('root', [
  160. new DAV\FS\Directory($this->tempDir . '/tree1'),
  161. new DAV\FSExt\Directory($this->tempDir . '/tree2'),
  162. ]));
  163. $this->server->tree = $tree;
  164. $request = new HTTP\Request('MOVE', '/tree1', ['Destination' => '/tree2/tree1']);
  165. $this->server->httpRequest = ($request);
  166. $this->server->exec();
  167. $this->assertEquals(201, $this->response->status);
  168. $this->assertEquals('', $this->response->body);
  169. $this->assertEquals([
  170. 'Content-Length' => ['0'],
  171. 'X-Sabre-Version' => [DAV\Version::VERSION],
  172. ], $this->response->getHeaders());
  173. $this->assertTrue(
  174. is_dir($this->tempDir . '/tree2/tree1')
  175. );
  176. }
  177. function testCopy() {
  178. mkdir($this->tempDir . '/testcol');
  179. $request = new HTTP\Request('COPY', '/test.txt', ['Destination' => '/testcol/test2.txt']);
  180. $this->server->httpRequest = ($request);
  181. $this->server->exec();
  182. $this->assertEquals(201, $this->response->status);
  183. $this->assertEquals('', $this->response->body);
  184. $this->assertEquals([
  185. 'Content-Length' => ['0'],
  186. 'X-Sabre-Version' => [DAV\Version::VERSION],
  187. ], $this->response->getHeaders());
  188. $this->assertTrue(is_file($this->tempDir . '/test.txt'));
  189. $this->assertTrue(is_file($this->tempDir . '/testcol/test2.txt'));
  190. }
  191. }