LockingTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests;
  8. use OC\Files\Filesystem;
  9. use OC\Files\View;
  10. use OCP\Lock\ILockingProvider;
  11. use OCP\Share\IShare;
  12. /**
  13. * Class LockingTest
  14. *
  15. * @group DB
  16. *
  17. * @package OCA\Files_Sharing\Tests
  18. */
  19. class LockingTest extends TestCase {
  20. /**
  21. * @var \Test\Util\User\Dummy
  22. */
  23. private $userBackend;
  24. private $ownerUid;
  25. private $recipientUid;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->userBackend = new \Test\Util\User\Dummy();
  29. \OC::$server->getUserManager()->registerBackend($this->userBackend);
  30. $this->ownerUid = $this->getUniqueID('owner_');
  31. $this->recipientUid = $this->getUniqueID('recipient_');
  32. $this->userBackend->createUser($this->ownerUid, '');
  33. $this->userBackend->createUser($this->recipientUid, '');
  34. $this->loginAsUser($this->ownerUid);
  35. Filesystem::mkdir('/foo');
  36. Filesystem::file_put_contents('/foo/bar.txt', 'asd');
  37. $fileId = Filesystem::getFileInfo('/foo/bar.txt')->getId();
  38. $this->share(
  39. IShare::TYPE_USER,
  40. '/foo/bar.txt',
  41. $this->ownerUid,
  42. $this->recipientUid,
  43. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  44. );
  45. $this->loginAsUser($this->recipientUid);
  46. $this->assertTrue(Filesystem::file_exists('bar.txt'));
  47. }
  48. protected function tearDown(): void {
  49. \OC::$server->getUserManager()->removeBackend($this->userBackend);
  50. parent::tearDown();
  51. }
  52. public function testLockAsRecipient(): void {
  53. $this->expectException(\OCP\Lock\LockedException::class);
  54. $this->loginAsUser($this->ownerUid);
  55. Filesystem::initMountPoints($this->recipientUid);
  56. $recipientView = new View('/' . $this->recipientUid . '/files');
  57. $recipientView->lockFile('bar.txt', ILockingProvider::LOCK_EXCLUSIVE);
  58. Filesystem::rename('/foo', '/asd');
  59. }
  60. public function testUnLockAsRecipient(): void {
  61. $this->loginAsUser($this->ownerUid);
  62. Filesystem::initMountPoints($this->recipientUid);
  63. $recipientView = new View('/' . $this->recipientUid . '/files');
  64. $recipientView->lockFile('bar.txt', ILockingProvider::LOCK_EXCLUSIVE);
  65. $recipientView->unlockFile('bar.txt', ILockingProvider::LOCK_EXCLUSIVE);
  66. $this->assertTrue(Filesystem::rename('/foo', '/asd'));
  67. }
  68. public function testChangeLock(): void {
  69. Filesystem::initMountPoints($this->recipientUid);
  70. $recipientView = new View('/' . $this->recipientUid . '/files');
  71. $recipientView->lockFile('bar.txt', ILockingProvider::LOCK_SHARED);
  72. $recipientView->changeLock('bar.txt', ILockingProvider::LOCK_EXCLUSIVE);
  73. $recipientView->unlockFile('bar.txt', ILockingProvider::LOCK_EXCLUSIVE);
  74. $this->addToAssertionCount(1);
  75. }
  76. }