SizePropagationTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Sharing\Tests;
  26. use OC\Files\View;
  27. use Test\Traits\UserTrait;
  28. /**
  29. * Class SizePropagationTest
  30. *
  31. * @group DB
  32. *
  33. * @package OCA\Files_Sharing\Tests
  34. */
  35. class SizePropagationTest extends TestCase {
  36. use UserTrait;
  37. protected function setupUser($name, $password = '') {
  38. $this->createUser($name, $password);
  39. $tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
  40. $this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
  41. $this->loginAsUser($name);
  42. return new View('/' . $name . '/files');
  43. }
  44. public function testSizePropagationWhenOwnerChangesFile() {
  45. $recipientView = $this->setupUser(self::TEST_FILES_SHARING_API_USER1);
  46. $ownerView = $this->setupUser(self::TEST_FILES_SHARING_API_USER2);
  47. $ownerView->mkdir('/sharedfolder/subfolder');
  48. $ownerView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'bar');
  49. $this->share(
  50. \OCP\Share::SHARE_TYPE_USER,
  51. '/sharedfolder',
  52. self::TEST_FILES_SHARING_API_USER2,
  53. self::TEST_FILES_SHARING_API_USER1,
  54. \OCP\Constants::PERMISSION_ALL
  55. );
  56. $ownerRootInfo = $ownerView->getFileInfo('', false);
  57. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1);
  58. $this->assertTrue($recipientView->file_exists('/sharedfolder/subfolder/foo.txt'));
  59. $recipientRootInfo = $recipientView->getFileInfo('', false);
  60. // when file changed as owner
  61. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
  62. $ownerView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'foobar');
  63. // size of recipient's root stays the same
  64. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1);
  65. $newRecipientRootInfo = $recipientView->getFileInfo('', false);
  66. $this->assertEquals($recipientRootInfo->getSize(), $newRecipientRootInfo->getSize());
  67. // size of owner's root increases
  68. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
  69. $newOwnerRootInfo = $ownerView->getFileInfo('', false);
  70. $this->assertEquals($ownerRootInfo->getSize() + 3, $newOwnerRootInfo->getSize());
  71. }
  72. public function testSizePropagationWhenRecipientChangesFile() {
  73. $recipientView = $this->setupUser(self::TEST_FILES_SHARING_API_USER1);
  74. $ownerView = $this->setupUser(self::TEST_FILES_SHARING_API_USER2);
  75. $ownerView->mkdir('/sharedfolder/subfolder');
  76. $ownerView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'bar');
  77. $this->share(
  78. \OCP\Share::SHARE_TYPE_USER,
  79. '/sharedfolder',
  80. self::TEST_FILES_SHARING_API_USER2,
  81. self::TEST_FILES_SHARING_API_USER1,
  82. \OCP\Constants::PERMISSION_ALL
  83. );
  84. $ownerRootInfo = $ownerView->getFileInfo('', false);
  85. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1);
  86. $this->assertTrue($recipientView->file_exists('/sharedfolder/subfolder/foo.txt'));
  87. $recipientRootInfo = $recipientView->getFileInfo('', false);
  88. $recipientRootInfoWithMounts = $recipientView->getFileInfo('', true);
  89. $oldRecipientSize = $recipientRootInfoWithMounts->getSize();
  90. // when file changed as recipient
  91. $recipientView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'foobar');
  92. // size of recipient's root stays the same
  93. $newRecipientRootInfo = $recipientView->getFileInfo('', false);
  94. $this->assertEquals($recipientRootInfo->getSize(), $newRecipientRootInfo->getSize());
  95. // but the size including mountpoints increases
  96. $newRecipientRootInfo = $recipientView->getFileInfo('', true);
  97. $this->assertEquals($oldRecipientSize +3, $newRecipientRootInfo->getSize());
  98. // size of owner's root increases
  99. $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
  100. $newOwnerRootInfo = $ownerView->getFileInfo('', false);
  101. $this->assertEquals($ownerRootInfo->getSize() + 3, $newOwnerRootInfo->getSize());
  102. }
  103. }