UploadTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Connector\Sabre\RequestTest;
  8. use OCP\AppFramework\Http;
  9. use OCP\Lock\ILockingProvider;
  10. /**
  11. * Class UploadTest
  12. *
  13. * @group DB
  14. *
  15. * @package OCA\DAV\Tests\unit\Connector\Sabre\RequestTest
  16. */
  17. class UploadTest extends RequestTestCase {
  18. public function testBasicUpload(): void {
  19. $user = $this->getUniqueID();
  20. $view = $this->setupUser($user, 'pass');
  21. $this->assertFalse($view->file_exists('foo.txt'));
  22. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
  23. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  24. $this->assertTrue($view->file_exists('foo.txt'));
  25. $this->assertEquals('asd', $view->file_get_contents('foo.txt'));
  26. $info = $view->getFileInfo('foo.txt');
  27. $this->assertInstanceOf('\OC\Files\FileInfo', $info);
  28. $this->assertEquals(3, $info->getSize());
  29. }
  30. public function testUploadOverWrite(): void {
  31. $user = $this->getUniqueID();
  32. $view = $this->setupUser($user, 'pass');
  33. $view->file_put_contents('foo.txt', 'foobar');
  34. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
  35. $this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus());
  36. $this->assertEquals('asd', $view->file_get_contents('foo.txt'));
  37. $info = $view->getFileInfo('foo.txt');
  38. $this->assertInstanceOf('\OC\Files\FileInfo', $info);
  39. $this->assertEquals(3, $info->getSize());
  40. }
  41. public function testUploadOverWriteReadLocked(): void {
  42. $user = $this->getUniqueID();
  43. $view = $this->setupUser($user, 'pass');
  44. $view->file_put_contents('foo.txt', 'bar');
  45. $view->lockFile('/foo.txt', ILockingProvider::LOCK_SHARED);
  46. $result = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
  47. $this->assertEquals(Http::STATUS_LOCKED, $result->getStatus());
  48. }
  49. public function testUploadOverWriteWriteLocked(): void {
  50. $user = $this->getUniqueID();
  51. $view = $this->setupUser($user, 'pass');
  52. $this->loginAsUser($user);
  53. $view->file_put_contents('foo.txt', 'bar');
  54. $view->lockFile('/foo.txt', ILockingProvider::LOCK_EXCLUSIVE);
  55. $result = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
  56. $this->assertEquals(Http::STATUS_LOCKED, $result->getStatus());
  57. }
  58. }