/tests/Sabre/DAV/TemporaryFileFilterTest.php

https://github.com/fruux/sabre-dav · PHP · 199 lines · 132 code · 61 blank · 6 comment · 0 complexity · fb0548ae25cd29b2a93f57380a768a9b MD5 · raw file

  1. <?php declare (strict_types=1);
  2. namespace Sabre\DAV;
  3. use Sabre\HTTP;
  4. class TemporaryFileFilterTest extends AbstractServer {
  5. function setUp() {
  6. parent::setUp();
  7. $plugin = new TemporaryFileFilterPlugin(SABRE_TEMPDIR . '/tff');
  8. $this->server->addPlugin($plugin);
  9. }
  10. function testPutNormal() {
  11. $request = new HTTP\Request('PUT', '/testput.txt', [], 'Testing new file');
  12. $this->server->httpRequest = ($request);
  13. $this->server->exec();
  14. $this->assertEquals('', $this->response->body);
  15. $this->assertEquals(201, $this->response->status);
  16. $this->assertEquals('0', $this->response->getHeader('Content-Length'));
  17. $this->assertEquals('Testing new file', file_get_contents(SABRE_TEMPDIR . '/testput.txt'));
  18. }
  19. function testPutTemp() {
  20. // mimicking an OS/X resource fork
  21. $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
  22. $this->server->httpRequest = ($request);
  23. $this->server->exec();
  24. $this->assertEquals('', $this->response->body);
  25. $this->assertEquals(201, $this->response->status);
  26. $this->assertEquals([
  27. 'X-Sabre-Temp' => ['true'],
  28. ], $this->response->getHeaders());
  29. $this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testput.txt'), '._testput.txt should not exist in the regular file structure.');
  30. }
  31. function testPutTempIfNoneMatch() {
  32. // mimicking an OS/X resource fork
  33. $request = new HTTP\Request('PUT', '/._testput.txt', ['If-None-Match' => '*'], 'Testing new file');
  34. $this->server->httpRequest = ($request);
  35. $this->server->exec();
  36. $this->assertEquals('', $this->response->body);
  37. $this->assertEquals(201, $this->response->status);
  38. $this->assertEquals([
  39. 'X-Sabre-Temp' => ['true'],
  40. ], $this->response->getHeaders());
  41. $this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testput.txt'), '._testput.txt should not exist in the regular file structure.');
  42. $this->server->exec();
  43. $this->assertEquals(412, $this->response->status);
  44. $this->assertEquals([
  45. 'X-Sabre-Temp' => ['true'],
  46. 'Content-Type' => ['application/xml; charset=utf-8'],
  47. ], $this->response->getHeaders());
  48. }
  49. function testPutGet() {
  50. // mimicking an OS/X resource fork
  51. $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
  52. $this->server->httpRequest = ($request);
  53. $this->server->exec();
  54. $this->assertEquals('', $this->response->body);
  55. $this->assertEquals(201, $this->response->status);
  56. $this->assertEquals([
  57. 'X-Sabre-Temp' => ['true'],
  58. ], $this->response->getHeaders());
  59. $request = new HTTP\Request('GET', '/._testput.txt');
  60. $this->server->httpRequest = $request;
  61. $this->server->exec();
  62. $this->assertEquals(200, $this->response->status);
  63. $this->assertEquals([
  64. 'X-Sabre-Temp' => ['true'],
  65. 'Content-Length' => [16],
  66. 'Content-Type' => ['application/octet-stream'],
  67. ], $this->response->getHeaders());
  68. $this->assertEquals('Testing new file', stream_get_contents($this->response->body));
  69. }
  70. function testLockNonExistant() {
  71. mkdir(SABRE_TEMPDIR . '/locksdir');
  72. $locksBackend = new Locks\Backend\File(SABRE_TEMPDIR . '/locks');
  73. $locksPlugin = new Locks\Plugin($locksBackend);
  74. $this->server->addPlugin($locksPlugin);
  75. // mimicking an OS/X resource fork
  76. $request = new HTTP\Request('LOCK', '/._testput.txt');
  77. $request->setBody('<?xml version="1.0"?>
  78. <D:lockinfo xmlns:D="DAV:">
  79. <D:lockscope><D:exclusive/></D:lockscope>
  80. <D:locktype><D:write/></D:locktype>
  81. <D:owner>
  82. <D:href>http://example.org/~ejw/contact.html</D:href>
  83. </D:owner>
  84. </D:lockinfo>');
  85. $this->server->httpRequest = ($request);
  86. $this->server->exec();
  87. $this->assertEquals(201, $this->response->status);
  88. $this->assertEquals('application/xml; charset=utf-8', $this->response->getHeader('Content-Type'));
  89. $this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/', $this->response->getHeader('Lock-Token')) === 1, 'We did not get a valid Locktoken back (' . $this->response->getHeader('Lock-Token') . ')');
  90. $this->assertEquals('true', $this->response->getHeader('X-Sabre-Temp'));
  91. $this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testlock.txt'), '._testlock.txt should not exist in the regular file structure.');
  92. }
  93. function testPutDelete() {
  94. // mimicking an OS/X resource fork
  95. $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
  96. $this->server->httpRequest = $request;
  97. $this->server->exec();
  98. $this->assertEquals('', $this->response->body);
  99. $this->assertEquals(201, $this->response->status);
  100. $this->assertEquals([
  101. 'X-Sabre-Temp' => ['true'],
  102. ], $this->response->getHeaders());
  103. $request = new HTTP\Request('DELETE', '/._testput.txt');
  104. $this->server->httpRequest = $request;
  105. $this->server->exec();
  106. $this->assertEquals(204, $this->response->status, "Incorrect status code received. Full body:\n" . $this->response->body);
  107. $this->assertEquals([
  108. 'X-Sabre-Temp' => ['true'],
  109. ], $this->response->getHeaders());
  110. $this->assertEquals('', $this->response->body);
  111. }
  112. function testPutPropfind() {
  113. // mimicking an OS/X resource fork
  114. $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
  115. $this->server->httpRequest = $request;
  116. $this->server->exec();
  117. $this->assertEquals('', $this->response->body);
  118. $this->assertEquals(201, $this->response->status);
  119. $this->assertEquals([
  120. 'X-Sabre-Temp' => ['true'],
  121. ], $this->response->getHeaders());
  122. $request = new HTTP\Request('PROPFIND', '/._testput.txt');
  123. $this->server->httpRequest = ($request);
  124. $this->server->exec();
  125. $this->assertEquals(207, $this->response->status, 'Incorrect status code returned. Body: ' . $this->response->body);
  126. $this->assertEquals([
  127. 'X-Sabre-Temp' => ['true'],
  128. 'Content-Type' => ['application/xml; charset=utf-8'],
  129. ], $this->response->getHeaders());
  130. $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
  131. $xml = simplexml_load_string($body);
  132. $xml->registerXPathNamespace('d', 'urn:DAV');
  133. list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
  134. $this->assertEquals('/._testput.txt', (string)$data, 'href element should have been /._testput.txt');
  135. $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
  136. $this->assertEquals(1, count($data));
  137. }
  138. }