ChunkingPluginTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, ownCloud GmbH
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\Upload;
  26. use OCA\DAV\Connector\Sabre\Directory;
  27. use OCA\DAV\Upload\ChunkingPlugin;
  28. use OCA\DAV\Upload\FutureFile;
  29. use Sabre\DAV\Exception\NotFound;
  30. use Sabre\HTTP\RequestInterface;
  31. use Sabre\HTTP\ResponseInterface;
  32. use Test\TestCase;
  33. class ChunkingPluginTest extends TestCase {
  34. /**
  35. * @var \Sabre\DAV\Server | \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $server;
  38. /**
  39. * @var \Sabre\DAV\Tree | \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $tree;
  42. /**
  43. * @var ChunkingPlugin
  44. */
  45. private $plugin;
  46. /** @var RequestInterface | \PHPUnit_Framework_MockObject_MockObject */
  47. private $request;
  48. /** @var ResponseInterface | \PHPUnit_Framework_MockObject_MockObject */
  49. private $response;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->server = $this->getMockBuilder('\Sabre\DAV\Server')
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->tree = $this->getMockBuilder('\Sabre\DAV\Tree')
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->server->tree = $this->tree;
  59. $this->plugin = new ChunkingPlugin();
  60. $this->request = $this->createMock(RequestInterface::class);
  61. $this->response = $this->createMock(ResponseInterface::class);
  62. $this->server->httpRequest = $this->request;
  63. $this->server->httpResponse = $this->response;
  64. $this->plugin->initialize($this->server);
  65. }
  66. public function testBeforeMoveFutureFileSkip() {
  67. $node = $this->createMock(Directory::class);
  68. $this->tree->expects($this->any())
  69. ->method('getNodeForPath')
  70. ->with('source')
  71. ->willReturn($node);
  72. $this->response->expects($this->never())
  73. ->method('setStatus');
  74. $this->assertNull($this->plugin->beforeMove('source', 'target'));
  75. }
  76. public function testBeforeMoveDestinationIsDirectory() {
  77. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  78. $this->expectExceptionMessage('The given destination target is a directory.');
  79. $sourceNode = $this->createMock(FutureFile::class);
  80. $targetNode = $this->createMock(Directory::class);
  81. $this->tree->expects($this->at(0))
  82. ->method('getNodeForPath')
  83. ->with('source')
  84. ->willReturn($sourceNode);
  85. $this->tree->expects($this->at(1))
  86. ->method('getNodeForPath')
  87. ->with('target')
  88. ->willReturn($targetNode);
  89. $this->response->expects($this->never())
  90. ->method('setStatus');
  91. $this->assertNull($this->plugin->beforeMove('source', 'target'));
  92. }
  93. public function testBeforeMoveFutureFileSkipNonExisting() {
  94. $sourceNode = $this->createMock(FutureFile::class);
  95. $sourceNode->expects($this->once())
  96. ->method('getSize')
  97. ->willReturn(4);
  98. $this->tree->expects($this->at(0))
  99. ->method('getNodeForPath')
  100. ->with('source')
  101. ->willReturn($sourceNode);
  102. $this->tree->expects($this->at(1))
  103. ->method('getNodeForPath')
  104. ->with('target')
  105. ->willThrowException(new NotFound());
  106. $this->tree->expects($this->any())
  107. ->method('nodeExists')
  108. ->with('target')
  109. ->willReturn(false);
  110. $this->response->expects($this->never())
  111. ->method('setStatus');
  112. $this->request->expects($this->once())
  113. ->method('getHeader')
  114. ->with('OC-Total-Length')
  115. ->willReturn(4);
  116. $this->assertNull($this->plugin->beforeMove('source', 'target'));
  117. }
  118. public function testBeforeMoveFutureFileMoveIt() {
  119. $sourceNode = $this->createMock(FutureFile::class);
  120. $sourceNode->expects($this->once())
  121. ->method('getSize')
  122. ->willReturn(4);
  123. $this->tree->expects($this->at(0))
  124. ->method('getNodeForPath')
  125. ->with('source')
  126. ->willReturn($sourceNode);
  127. $this->tree->expects($this->at(1))
  128. ->method('getNodeForPath')
  129. ->with('target')
  130. ->willThrowException(new NotFound());
  131. $this->tree->expects($this->any())
  132. ->method('nodeExists')
  133. ->with('target')
  134. ->willReturn(true);
  135. $this->tree->expects($this->once())
  136. ->method('move')
  137. ->with('source', 'target');
  138. $this->response->expects($this->once())
  139. ->method('setHeader')
  140. ->with('Content-Length', '0');
  141. $this->response->expects($this->once())
  142. ->method('setStatus')
  143. ->with(204);
  144. $this->request->expects($this->once())
  145. ->method('getHeader')
  146. ->with('OC-Total-Length')
  147. ->willReturn('4');
  148. $this->assertFalse($this->plugin->beforeMove('source', 'target'));
  149. }
  150. public function testBeforeMoveSizeIsWrong() {
  151. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  152. $this->expectExceptionMessage('Chunks on server do not sum up to 4 but to 3 bytes');
  153. $sourceNode = $this->createMock(FutureFile::class);
  154. $sourceNode->expects($this->once())
  155. ->method('getSize')
  156. ->willReturn(3);
  157. $this->tree->expects($this->at(0))
  158. ->method('getNodeForPath')
  159. ->with('source')
  160. ->willReturn($sourceNode);
  161. $this->tree->expects($this->at(1))
  162. ->method('getNodeForPath')
  163. ->with('target')
  164. ->willThrowException(new NotFound());
  165. $this->request->expects($this->once())
  166. ->method('getHeader')
  167. ->with('OC-Total-Length')
  168. ->willReturn('4');
  169. $this->assertFalse($this->plugin->beforeMove('source', 'target'));
  170. }
  171. }