StorageTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\files_versions\tests;
  8. use OCA\Files_Versions\Expiration;
  9. use OCA\Files_Versions\Storage;
  10. use OCP\Files\IRootFolder;
  11. use OCP\Files\NotFoundException;
  12. use Test\TestCase;
  13. use Test\Traits\UserTrait;
  14. /**
  15. * @group DB
  16. */
  17. class StorageTest extends TestCase {
  18. use UserTrait;
  19. private $versionsRoot;
  20. private $userFolder;
  21. private $expireTimestamp = 10;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $expiration = $this->createMock(Expiration::class);
  25. $expiration->method('getMaxAgeAsTimestamp')
  26. ->willReturnCallback(function () {
  27. return $this->expireTimestamp;
  28. });
  29. $this->overwriteService(Expiration::class, $expiration);
  30. \OC::$server->boot();
  31. $this->createUser('version_test', '');
  32. $this->loginAsUser('version_test');
  33. /** @var IRootFolder $root */
  34. $root = \OC::$server->get(IRootFolder::class);
  35. $this->userFolder = $root->getUserFolder('version_test');
  36. }
  37. protected function createPastFile(string $path, int $mtime) {
  38. try {
  39. $file = $this->userFolder->get($path);
  40. } catch (NotFoundException $e) {
  41. $file = $this->userFolder->newFile($path);
  42. }
  43. $file->putContent((string)$mtime);
  44. $file->touch($mtime);
  45. }
  46. public function testExpireMaxAge(): void {
  47. $this->userFolder->newFolder('folder1');
  48. $this->userFolder->newFolder('folder1/sub1');
  49. $this->userFolder->newFolder('folder2');
  50. $this->createPastFile('file1', 100);
  51. $this->createPastFile('file1', 500);
  52. $this->createPastFile('file1', 900);
  53. $this->createPastFile('folder1/file2', 100);
  54. $this->createPastFile('folder1/file2', 200);
  55. $this->createPastFile('folder1/file2', 300);
  56. $this->createPastFile('folder1/sub1/file3', 400);
  57. $this->createPastFile('folder1/sub1/file3', 500);
  58. $this->createPastFile('folder1/sub1/file3', 600);
  59. $this->createPastFile('folder2/file4', 100);
  60. $this->createPastFile('folder2/file4', 600);
  61. $this->createPastFile('folder2/file4', 800);
  62. $this->assertCount(2, Storage::getVersions('version_test', 'file1'));
  63. $this->assertCount(2, Storage::getVersions('version_test', 'folder1/file2'));
  64. $this->assertCount(2, Storage::getVersions('version_test', 'folder1/sub1/file3'));
  65. $this->assertCount(2, Storage::getVersions('version_test', 'folder2/file4'));
  66. $this->expireTimestamp = 150;
  67. Storage::expireOlderThanMaxForUser('version_test');
  68. $this->assertCount(1, Storage::getVersions('version_test', 'file1'));
  69. $this->assertCount(1, Storage::getVersions('version_test', 'folder1/file2'));
  70. $this->assertCount(2, Storage::getVersions('version_test', 'folder1/sub1/file3'));
  71. $this->assertCount(1, Storage::getVersions('version_test', 'folder2/file4'));
  72. $this->expireTimestamp = 550;
  73. Storage::expireOlderThanMaxForUser('version_test');
  74. $this->assertCount(0, Storage::getVersions('version_test', 'file1'));
  75. $this->assertCount(0, Storage::getVersions('version_test', 'folder1/file2'));
  76. $this->assertCount(0, Storage::getVersions('version_test', 'folder1/sub1/file3'));
  77. $this->assertCount(1, Storage::getVersions('version_test', 'folder2/file4'));
  78. }
  79. }