StorageTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\Storage;
  25. use OCP\Files\IRootFolder;
  26. use OCP\Files\NotFoundException;
  27. use Test\TestCase;
  28. use Test\Traits\UserTrait;
  29. /**
  30. * @group DB
  31. */
  32. class StorageTest extends TestCase {
  33. use UserTrait;
  34. private $versionsRoot;
  35. private $userFolder;
  36. private $expireTimestamp = 10;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $expiration = $this->createMock(Expiration::class);
  40. $expiration->method('getMaxAgeAsTimestamp')
  41. ->willReturnCallback(function () {
  42. return $this->expireTimestamp;
  43. });
  44. $this->overwriteService(Expiration::class, $expiration);
  45. \OC::$server->boot();
  46. $this->createUser('version_test', '');
  47. $this->loginAsUser('version_test');
  48. /** @var IRootFolder $root */
  49. $root = \OC::$server->get(IRootFolder::class);
  50. $this->userFolder = $root->getUserFolder('version_test');
  51. }
  52. protected function createPastFile(string $path, int $mtime) {
  53. try {
  54. $file = $this->userFolder->get($path);
  55. } catch (NotFoundException $e) {
  56. $file = $this->userFolder->newFile($path);
  57. }
  58. $file->putContent((string)$mtime);
  59. $file->touch($mtime);
  60. }
  61. public function testExpireMaxAge() {
  62. $this->userFolder->newFolder('folder1');
  63. $this->userFolder->newFolder('folder1/sub1');
  64. $this->userFolder->newFolder('folder2');
  65. $this->createPastFile('file1', 100);
  66. $this->createPastFile('file1', 500);
  67. $this->createPastFile('file1', 900);
  68. $this->createPastFile('folder1/file2', 100);
  69. $this->createPastFile('folder1/file2', 200);
  70. $this->createPastFile('folder1/file2', 300);
  71. $this->createPastFile('folder1/sub1/file3', 400);
  72. $this->createPastFile('folder1/sub1/file3', 500);
  73. $this->createPastFile('folder1/sub1/file3', 600);
  74. $this->createPastFile('folder2/file4', 100);
  75. $this->createPastFile('folder2/file4', 600);
  76. $this->createPastFile('folder2/file4', 800);
  77. $this->assertCount(2, Storage::getVersions('version_test', 'file1'));
  78. $this->assertCount(2, Storage::getVersions('version_test', 'folder1/file2'));
  79. $this->assertCount(2, Storage::getVersions('version_test', 'folder1/sub1/file3'));
  80. $this->assertCount(2, Storage::getVersions('version_test', 'folder2/file4'));
  81. $this->expireTimestamp = 150;
  82. Storage::expireOlderThanMaxForUser('version_test');
  83. $this->assertCount(1, Storage::getVersions('version_test', 'file1'));
  84. $this->assertCount(1, Storage::getVersions('version_test', 'folder1/file2'));
  85. $this->assertCount(2, Storage::getVersions('version_test', 'folder1/sub1/file3'));
  86. $this->assertCount(1, Storage::getVersions('version_test', 'folder2/file4'));
  87. $this->expireTimestamp = 550;
  88. Storage::expireOlderThanMaxForUser('version_test');
  89. $this->assertCount(0, Storage::getVersions('version_test', 'file1'));
  90. $this->assertCount(0, Storage::getVersions('version_test', 'folder1/file2'));
  91. $this->assertCount(0, Storage::getVersions('version_test', 'folder1/sub1/file3'));
  92. $this->assertCount(1, Storage::getVersions('version_test', 'folder2/file4'));
  93. }
  94. }