SizePropagationTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Files_Sharing\Tests;
  8. use OC\Files\View;
  9. use OCP\Constants;
  10. use OCP\Share\IShare;
  11. use Test\Traits\UserTrait;
  12. /**
  13. * Class SizePropagationTest
  14. *
  15. * @group DB
  16. *
  17. * @package OCA\Files_Sharing\Tests
  18. */
  19. class SizePropagationTest extends TestCase {
  20. use UserTrait;
  21. protected function setupUser($name, $password = '') {
  22. $this->createUser($name, $password);
  23. $tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
  24. $this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
  25. $this->loginAsUser($name);
  26. return new View('/' . $name . '/files');
  27. }
  28. public function testSizePropagationWhenOwnerChangesFile(): void {
  29. $recipientView = $this->setupUser(self::TEST_FILES_SHARING_API_USER1);
  30. $ownerView = $this->setupUser(self::TEST_FILES_SHARING_API_USER2);
  31. $ownerView->mkdir('/sharedfolder/subfolder');
  32. $ownerView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'bar');
  33. $this->share(
  34. IShare::TYPE_USER,
  35. '/sharedfolder',
  36. self::TEST_FILES_SHARING_API_USER2,
  37. self::TEST_FILES_SHARING_API_USER1,
  38. Constants::PERMISSION_ALL
  39. );
  40. $ownerRootInfo = $ownerView->getFileInfo('', false);
  41. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1);
  42. $this->assertTrue($recipientView->file_exists('/sharedfolder/subfolder/foo.txt'));
  43. $recipientRootInfo = $recipientView->getFileInfo('', false);
  44. // when file changed as owner
  45. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
  46. $ownerView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'foobar');
  47. // size of recipient's root stays the same
  48. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1);
  49. $newRecipientRootInfo = $recipientView->getFileInfo('', false);
  50. $this->assertEquals($recipientRootInfo->getSize(), $newRecipientRootInfo->getSize());
  51. // size of owner's root increases
  52. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
  53. $newOwnerRootInfo = $ownerView->getFileInfo('', false);
  54. $this->assertEquals($ownerRootInfo->getSize() + 3, $newOwnerRootInfo->getSize());
  55. }
  56. public function testSizePropagationWhenRecipientChangesFile(): void {
  57. $recipientView = $this->setupUser(self::TEST_FILES_SHARING_API_USER1);
  58. $ownerView = $this->setupUser(self::TEST_FILES_SHARING_API_USER2);
  59. $ownerView->mkdir('/sharedfolder/subfolder');
  60. $ownerView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'bar');
  61. $this->share(
  62. IShare::TYPE_USER,
  63. '/sharedfolder',
  64. self::TEST_FILES_SHARING_API_USER2,
  65. self::TEST_FILES_SHARING_API_USER1,
  66. Constants::PERMISSION_ALL
  67. );
  68. $ownerRootInfo = $ownerView->getFileInfo('', false);
  69. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1);
  70. $this->assertTrue($recipientView->file_exists('/sharedfolder/subfolder/foo.txt'));
  71. $recipientRootInfo = $recipientView->getFileInfo('', false);
  72. $recipientRootInfoWithMounts = $recipientView->getFileInfo('', true);
  73. $oldRecipientSize = $recipientRootInfoWithMounts->getSize();
  74. // when file changed as recipient
  75. $recipientView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'foobar');
  76. // size of recipient's root stays the same
  77. $newRecipientRootInfo = $recipientView->getFileInfo('', false);
  78. $this->assertEquals($recipientRootInfo->getSize(), $newRecipientRootInfo->getSize());
  79. // but the size including mountpoints increases
  80. $newRecipientRootInfo = $recipientView->getFileInfo('', true);
  81. $this->assertEquals($oldRecipientSize + 3, $newRecipientRootInfo->getSize());
  82. // size of owner's root increases
  83. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
  84. $newOwnerRootInfo = $ownerView->getFileInfo('', false);
  85. $this->assertEquals($ownerRootInfo->getSize() + 3, $newOwnerRootInfo->getSize());
  86. }
  87. }