FilesDropPluginTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\Tests\Files\Sharing;
  27. use OC\Files\View;
  28. use OCA\DAV\Files\Sharing\FilesDropPlugin;
  29. use Sabre\DAV\Exception\MethodNotAllowed;
  30. use Sabre\DAV\Server;
  31. use Sabre\HTTP\RequestInterface;
  32. use Sabre\HTTP\ResponseInterface;
  33. use Test\TestCase;
  34. class FilesDropPluginTest extends TestCase {
  35. /** @var View|\PHPUnit\Framework\MockObject\MockObject */
  36. private $view;
  37. /** @var Server|\PHPUnit\Framework\MockObject\MockObject */
  38. private $server;
  39. /** @var FilesDropPlugin */
  40. private $plugin;
  41. /** @var RequestInterface|\PHPUnit\Framework\MockObject\MockObject */
  42. private $request;
  43. /** @var ResponseInterface|\PHPUnit\Framework\MockObject\MockObject */
  44. private $response;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->view = $this->createMock(View::class);
  48. $this->server = $this->createMock(Server::class);
  49. $this->plugin = new FilesDropPlugin();
  50. $this->request = $this->createMock(RequestInterface::class);
  51. $this->response = $this->createMock(ResponseInterface::class);
  52. $this->response->expects($this->never())
  53. ->method($this->anything());
  54. }
  55. public function testInitialize() {
  56. $this->server->expects($this->once())
  57. ->method('on')
  58. ->with(
  59. $this->equalTo('beforeMethod:*'),
  60. $this->equalTo([$this->plugin, 'beforeMethod']),
  61. $this->equalTo(999)
  62. );
  63. $this->plugin->initialize($this->server);
  64. }
  65. public function testNotEnabled() {
  66. $this->view->expects($this->never())
  67. ->method($this->anything());
  68. $this->request->expects($this->never())
  69. ->method($this->anything());
  70. $this->plugin->beforeMethod($this->request, $this->response);
  71. }
  72. public function testValid() {
  73. $this->plugin->enable();
  74. $this->plugin->setView($this->view);
  75. $this->request->method('getMethod')
  76. ->willReturn('PUT');
  77. $this->request->method('getPath')
  78. ->willReturn('file.txt');
  79. $this->request->method('getBaseUrl')
  80. ->willReturn('https://example.com');
  81. $this->view->method('file_exists')
  82. ->with('/file.txt')
  83. ->willReturn(false);
  84. $this->request->expects($this->once())
  85. ->method('setUrl')
  86. ->with('https://example.com/file.txt');
  87. $this->plugin->beforeMethod($this->request, $this->response);
  88. }
  89. public function testFileAlreadyExistsValid() {
  90. $this->plugin->enable();
  91. $this->plugin->setView($this->view);
  92. $this->request->method('getMethod')
  93. ->willReturn('PUT');
  94. $this->request->method('getPath')
  95. ->willReturn('file.txt');
  96. $this->request->method('getBaseUrl')
  97. ->willReturn('https://example.com');
  98. $this->view->method('file_exists')
  99. ->willReturnCallback(function ($path) {
  100. if ($path === 'file.txt' || $path === '/file.txt') {
  101. return true;
  102. } else {
  103. return false;
  104. }
  105. });
  106. $this->request->expects($this->once())
  107. ->method('setUrl')
  108. ->with($this->equalTo('https://example.com/file (2).txt'));
  109. $this->plugin->beforeMethod($this->request, $this->response);
  110. }
  111. public function testNoMKCOL() {
  112. $this->plugin->enable();
  113. $this->plugin->setView($this->view);
  114. $this->request->method('getMethod')
  115. ->willReturn('MKCOL');
  116. $this->expectException(MethodNotAllowed::class);
  117. $this->plugin->beforeMethod($this->request, $this->response);
  118. }
  119. public function testNoSubdirPut() {
  120. $this->plugin->enable();
  121. $this->plugin->setView($this->view);
  122. $this->request->method('getMethod')
  123. ->willReturn('PUT');
  124. $this->request->method('getPath')
  125. ->willReturn('folder/file.txt');
  126. $this->request->method('getBaseUrl')
  127. ->willReturn('https://example.com');
  128. $this->view->method('file_exists')
  129. ->willReturnCallback(function ($path) {
  130. if ($path === 'file.txt' || $path === '/file.txt') {
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. });
  136. $this->request->expects($this->once())
  137. ->method('setUrl')
  138. ->with($this->equalTo('https://example.com/file (2).txt'));
  139. $this->plugin->beforeMethod($this->request, $this->response);
  140. }
  141. }