ChunkingPluginTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2017 ownCloud GmbH
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Upload;
  8. use OCA\DAV\Connector\Sabre\Directory;
  9. use OCA\DAV\Upload\ChunkingPlugin;
  10. use OCA\DAV\Upload\FutureFile;
  11. use Sabre\DAV\Exception\NotFound;
  12. use Sabre\HTTP\RequestInterface;
  13. use Sabre\HTTP\ResponseInterface;
  14. use Test\TestCase;
  15. class ChunkingPluginTest extends TestCase {
  16. /**
  17. * @var \Sabre\DAV\Server | \PHPUnit\Framework\MockObject\MockObject
  18. */
  19. private $server;
  20. /**
  21. * @var \Sabre\DAV\Tree | \PHPUnit\Framework\MockObject\MockObject
  22. */
  23. private $tree;
  24. /**
  25. * @var ChunkingPlugin
  26. */
  27. private $plugin;
  28. /** @var RequestInterface | \PHPUnit\Framework\MockObject\MockObject */
  29. private $request;
  30. /** @var ResponseInterface | \PHPUnit\Framework\MockObject\MockObject */
  31. private $response;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->server = $this->getMockBuilder('\Sabre\DAV\Server')
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->tree = $this->getMockBuilder('\Sabre\DAV\Tree')
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->server->tree = $this->tree;
  41. $this->plugin = new ChunkingPlugin();
  42. $this->request = $this->createMock(RequestInterface::class);
  43. $this->response = $this->createMock(ResponseInterface::class);
  44. $this->server->httpRequest = $this->request;
  45. $this->server->httpResponse = $this->response;
  46. $this->plugin->initialize($this->server);
  47. }
  48. public function testBeforeMoveFutureFileSkip(): void {
  49. $node = $this->createMock(Directory::class);
  50. $this->tree->expects($this->any())
  51. ->method('getNodeForPath')
  52. ->with('source')
  53. ->willReturn($node);
  54. $this->response->expects($this->never())
  55. ->method('setStatus');
  56. $this->assertNull($this->plugin->beforeMove('source', 'target'));
  57. }
  58. public function testBeforeMoveDestinationIsDirectory(): void {
  59. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  60. $this->expectExceptionMessage('The given destination target is a directory.');
  61. $sourceNode = $this->createMock(FutureFile::class);
  62. $targetNode = $this->createMock(Directory::class);
  63. $this->tree->expects($this->exactly(2))
  64. ->method('getNodeForPath')
  65. ->withConsecutive(
  66. ['source'],
  67. ['target'],
  68. )
  69. ->willReturnOnConsecutiveCalls(
  70. $sourceNode,
  71. $targetNode,
  72. );
  73. $this->response->expects($this->never())
  74. ->method('setStatus');
  75. $this->assertNull($this->plugin->beforeMove('source', 'target'));
  76. }
  77. public function testBeforeMoveFutureFileSkipNonExisting(): void {
  78. $sourceNode = $this->createMock(FutureFile::class);
  79. $sourceNode->expects($this->once())
  80. ->method('getSize')
  81. ->willReturn(4);
  82. $this->tree->expects($this->exactly(2))
  83. ->method('getNodeForPath')
  84. ->withConsecutive(
  85. ['source'],
  86. ['target'],
  87. )
  88. ->willReturnOnConsecutiveCalls(
  89. $sourceNode,
  90. $this->throwException(new NotFound()),
  91. );
  92. $this->tree->expects($this->any())
  93. ->method('nodeExists')
  94. ->with('target')
  95. ->willReturn(false);
  96. $this->response->expects($this->once())
  97. ->method('setHeader')
  98. ->with('Content-Length', '0');
  99. $this->response->expects($this->once())
  100. ->method('setStatus')
  101. ->with(201);
  102. $this->request->expects($this->once())
  103. ->method('getHeader')
  104. ->with('OC-Total-Length')
  105. ->willReturn(4);
  106. $this->assertFalse($this->plugin->beforeMove('source', 'target'));
  107. }
  108. public function testBeforeMoveFutureFileMoveIt(): void {
  109. $sourceNode = $this->createMock(FutureFile::class);
  110. $sourceNode->expects($this->once())
  111. ->method('getSize')
  112. ->willReturn(4);
  113. $this->tree->expects($this->exactly(2))
  114. ->method('getNodeForPath')
  115. ->withConsecutive(
  116. ['source'],
  117. ['target'],
  118. )
  119. ->willReturnOnConsecutiveCalls(
  120. $sourceNode,
  121. $this->throwException(new NotFound()),
  122. );
  123. $this->tree->expects($this->any())
  124. ->method('nodeExists')
  125. ->with('target')
  126. ->willReturn(true);
  127. $this->tree->expects($this->once())
  128. ->method('move')
  129. ->with('source', 'target');
  130. $this->response->expects($this->once())
  131. ->method('setHeader')
  132. ->with('Content-Length', '0');
  133. $this->response->expects($this->once())
  134. ->method('setStatus')
  135. ->with(204);
  136. $this->request->expects($this->once())
  137. ->method('getHeader')
  138. ->with('OC-Total-Length')
  139. ->willReturn('4');
  140. $this->assertFalse($this->plugin->beforeMove('source', 'target'));
  141. }
  142. public function testBeforeMoveSizeIsWrong(): void {
  143. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  144. $this->expectExceptionMessage('Chunks on server do not sum up to 4 but to 3 bytes');
  145. $sourceNode = $this->createMock(FutureFile::class);
  146. $sourceNode->expects($this->once())
  147. ->method('getSize')
  148. ->willReturn(3);
  149. $this->tree->expects($this->exactly(2))
  150. ->method('getNodeForPath')
  151. ->withConsecutive(
  152. ['source'],
  153. ['target'],
  154. )
  155. ->willReturnOnConsecutiveCalls(
  156. $sourceNode,
  157. $this->throwException(new NotFound()),
  158. );
  159. $this->request->expects($this->once())
  160. ->method('getHeader')
  161. ->with('OC-Total-Length')
  162. ->willReturn('4');
  163. $this->assertFalse($this->plugin->beforeMove('source', 'target'));
  164. }
  165. }