1
0

StorageTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\files_versions\tests;
  23. use OCA\Files_Versions\Expiration;
  24. use OCA\Files_Versions\Hooks;
  25. use OCA\Files_Versions\Storage;
  26. use OCP\Files\IRootFolder;
  27. use OCP\Files\NotFoundException;
  28. use Test\TestCase;
  29. use Test\Traits\UserTrait;
  30. /**
  31. * @group DB
  32. */
  33. class StorageTest extends TestCase {
  34. use UserTrait;
  35. private $versionsRoot;
  36. private $userFolder;
  37. private $expireTimestamp = 10;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $expiration = $this->createMock(Expiration::class);
  41. $expiration->method('getMaxAgeAsTimestamp')
  42. ->willReturnCallback(function () {
  43. return $this->expireTimestamp;
  44. });
  45. $this->overwriteService(Expiration::class, $expiration);
  46. Hooks::connectHooks();
  47. $this->createUser('version_test', '');
  48. $this->loginAsUser('version_test');
  49. /** @var IRootFolder $root */
  50. $root = \OC::$server->get(IRootFolder::class);
  51. $this->userFolder = $root->getUserFolder('version_test');
  52. }
  53. protected function createPastFile(string $path, int $mtime) {
  54. try {
  55. $file = $this->userFolder->get($path);
  56. } catch (NotFoundException $e) {
  57. $file = $this->userFolder->newFile($path);
  58. }
  59. $file->putContent((string)$mtime);
  60. $file->touch($mtime);
  61. }
  62. public function testExpireMaxAge() {
  63. $this->userFolder->newFolder('folder1');
  64. $this->userFolder->newFolder('folder1/sub1');
  65. $this->userFolder->newFolder('folder2');
  66. $this->createPastFile('file1', 100);
  67. $this->createPastFile('file1', 500);
  68. $this->createPastFile('file1', 900);
  69. $this->createPastFile('folder1/file2', 100);
  70. $this->createPastFile('folder1/file2', 200);
  71. $this->createPastFile('folder1/file2', 300);
  72. $this->createPastFile('folder1/sub1/file3', 400);
  73. $this->createPastFile('folder1/sub1/file3', 500);
  74. $this->createPastFile('folder1/sub1/file3', 600);
  75. $this->createPastFile('folder2/file4', 100);
  76. $this->createPastFile('folder2/file4', 600);
  77. $this->createPastFile('folder2/file4', 800);
  78. $this->assertCount(2, Storage::getVersions('version_test', 'file1'));
  79. $this->assertCount(2, Storage::getVersions('version_test', 'folder1/file2'));
  80. $this->assertCount(2, Storage::getVersions('version_test', 'folder1/sub1/file3'));
  81. $this->assertCount(2, Storage::getVersions('version_test', 'folder2/file4'));
  82. $this->expireTimestamp = 150;
  83. Storage::expireOlderThanMaxForUser('version_test');
  84. $this->assertCount(1, Storage::getVersions('version_test', 'file1'));
  85. $this->assertCount(1, Storage::getVersions('version_test', 'folder1/file2'));
  86. $this->assertCount(2, Storage::getVersions('version_test', 'folder1/sub1/file3'));
  87. $this->assertCount(1, Storage::getVersions('version_test', 'folder2/file4'));
  88. $this->expireTimestamp = 550;
  89. Storage::expireOlderThanMaxForUser('version_test');
  90. $this->assertCount(0, Storage::getVersions('version_test', 'file1'));
  91. $this->assertCount(0, Storage::getVersions('version_test', 'folder1/file2'));
  92. $this->assertCount(0, Storage::getVersions('version_test', 'folder1/sub1/file3'));
  93. $this->assertCount(1, Storage::getVersions('version_test', 'folder2/file4'));
  94. }
  95. }