FilesDropPluginTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\Files\Sharing;
  7. use OC\Files\View;
  8. use OCA\DAV\Files\Sharing\FilesDropPlugin;
  9. use OCP\Share\IAttributes;
  10. use OCP\Share\IShare;
  11. use Sabre\DAV\Exception\MethodNotAllowed;
  12. use Sabre\DAV\Server;
  13. use Sabre\HTTP\RequestInterface;
  14. use Sabre\HTTP\ResponseInterface;
  15. use Test\TestCase;
  16. class FilesDropPluginTest extends TestCase {
  17. /** @var View|\PHPUnit\Framework\MockObject\MockObject */
  18. private $view;
  19. /** @var IShare|\PHPUnit\Framework\MockObject\MockObject */
  20. private $share;
  21. /** @var Server|\PHPUnit\Framework\MockObject\MockObject */
  22. private $server;
  23. /** @var FilesDropPlugin */
  24. private $plugin;
  25. /** @var RequestInterface|\PHPUnit\Framework\MockObject\MockObject */
  26. private $request;
  27. /** @var ResponseInterface|\PHPUnit\Framework\MockObject\MockObject */
  28. private $response;
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->view = $this->createMock(View::class);
  32. $this->share = $this->createMock(IShare::class);
  33. $this->server = $this->createMock(Server::class);
  34. $this->plugin = new FilesDropPlugin();
  35. $this->request = $this->createMock(RequestInterface::class);
  36. $this->response = $this->createMock(ResponseInterface::class);
  37. $this->response->expects($this->never())
  38. ->method($this->anything());
  39. $attributes = $this->createMock(IAttributes::class);
  40. $this->share->expects($this->any())
  41. ->method('getAttributes')
  42. ->willReturn($attributes);
  43. }
  44. public function testInitialize(): void {
  45. $this->server->expects($this->once())
  46. ->method('on')
  47. ->with(
  48. $this->equalTo('beforeMethod:*'),
  49. $this->equalTo([$this->plugin, 'beforeMethod']),
  50. $this->equalTo(999)
  51. );
  52. $this->plugin->initialize($this->server);
  53. }
  54. public function testNotEnabled(): void {
  55. $this->view->expects($this->never())
  56. ->method($this->anything());
  57. $this->request->expects($this->never())
  58. ->method($this->anything());
  59. $this->plugin->beforeMethod($this->request, $this->response);
  60. }
  61. public function testValid(): void {
  62. $this->plugin->enable();
  63. $this->plugin->setView($this->view);
  64. $this->plugin->setShare($this->share);
  65. $this->request->method('getMethod')
  66. ->willReturn('PUT');
  67. $this->request->method('getPath')
  68. ->willReturn('file.txt');
  69. $this->request->method('getBaseUrl')
  70. ->willReturn('https://example.com');
  71. $this->view->method('file_exists')
  72. ->with('/file.txt')
  73. ->willReturn(false);
  74. $this->request->expects($this->once())
  75. ->method('setUrl')
  76. ->with('https://example.com/file.txt');
  77. $this->plugin->beforeMethod($this->request, $this->response);
  78. }
  79. public function testFileAlreadyExistsValid(): void {
  80. $this->plugin->enable();
  81. $this->plugin->setView($this->view);
  82. $this->plugin->setShare($this->share);
  83. $this->request->method('getMethod')
  84. ->willReturn('PUT');
  85. $this->request->method('getPath')
  86. ->willReturn('file.txt');
  87. $this->request->method('getBaseUrl')
  88. ->willReturn('https://example.com');
  89. $this->view->method('file_exists')
  90. ->willReturnCallback(function ($path) {
  91. if ($path === 'file.txt' || $path === '/file.txt') {
  92. return true;
  93. } else {
  94. return false;
  95. }
  96. });
  97. $this->request->expects($this->once())
  98. ->method('setUrl')
  99. ->with($this->equalTo('https://example.com/file (2).txt'));
  100. $this->plugin->beforeMethod($this->request, $this->response);
  101. }
  102. public function testNoMKCOL(): void {
  103. $this->plugin->enable();
  104. $this->plugin->setView($this->view);
  105. $this->plugin->setShare($this->share);
  106. $this->request->method('getMethod')
  107. ->willReturn('MKCOL');
  108. $this->expectException(MethodNotAllowed::class);
  109. $this->plugin->beforeMethod($this->request, $this->response);
  110. }
  111. public function testNoSubdirPut(): void {
  112. $this->plugin->enable();
  113. $this->plugin->setView($this->view);
  114. $this->plugin->setShare($this->share);
  115. $this->request->method('getMethod')
  116. ->willReturn('PUT');
  117. $this->request->method('getPath')
  118. ->willReturn('folder/file.txt');
  119. $this->request->method('getBaseUrl')
  120. ->willReturn('https://example.com');
  121. $this->view->method('file_exists')
  122. ->willReturnCallback(function ($path) {
  123. if ($path === 'file.txt' || $path === '/file.txt') {
  124. return true;
  125. } else {
  126. return false;
  127. }
  128. });
  129. $this->request->expects($this->once())
  130. ->method('setUrl')
  131. ->with($this->equalTo('https://example.com/file (2).txt'));
  132. $this->plugin->beforeMethod($this->request, $this->response);
  133. }
  134. }