ChunkingPluginTest.php 4.6 KB

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