SizePropagationTest.php 3.8 KB

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