LockingTest.php 2.8 KB

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