-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathBackupManagerTest.php
94 lines (83 loc) · 2.83 KB
/
BackupManagerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace OCA\Bookmarks\Tests;
use DateTime;
use OCA\Bookmarks\Exception\AlreadyExistsError;
use OCA\Bookmarks\Exception\UrlParseError;
use OCA\Bookmarks\Exception\UserLimitExceededError;
use OCA\Bookmarks\Service\BackupManager;
use OCA\Bookmarks\Service\BookmarkService;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Utility\ITimeFactory;
class BackupManagerTest extends TestCase {
/**
* @var BookmarkService
*/
private $bookmarks;
/**
* @var BackupManager
*/
private $backupManager;
/**
* @var string
*/
private $userId;
/**
* @var \OC\User\Manager
*/
private $userManager;
/**
* @var string
*/
private $user;
/**
* @throws UrlParseError
* @throws MultipleObjectsReturnedException
* @throws \OCA\Bookmarks\Exception\UnsupportedOperation
* @throws AlreadyExistsError
* @throws \OCP\AppFramework\Db\DoesNotExistException
* @throws UserLimitExceededError
*/
protected function setUp(): void {
parent::setUp();
$this->cleanUp();
$this->bookmarks = \OC::$server->query(BookmarkService::class);
$this->backupManager = \OC::$server->query(BackupManager::class);
$this->time = $this->createStub(ITimeFactory::class);
$this->backupManager->injectTimeFactory($this->time);
$this->userManager = \OC::$server->getUserManager();
$this->user = 'test';
if (!$this->userManager->userExists($this->user)) {
$this->userManager->createUser($this->user, 'password');
}
$this->userId = $this->userManager->get($this->user)->getUID();
$this->bookmarks->create($this->userId, 'https://en.wikipedia.org/');
$this->backupManager->cleanupAllBackups($this->userId);
}
public function testOneDay() {
$this->time->method('getDateTime')->willReturn(new DateTime());
$this->assertEquals(false, $this->backupManager->backupExistsForToday($this->userId));
$this->backupManager->runBackup($this->userId);
$this->assertEquals(true, $this->backupManager->backupExistsForToday($this->userId));
}
public function testSixMonths() {
$today = new DateTime();
$today->modify('first day of');
$sixMonths = \DateTimeImmutable::createFromMutable($today);
$sixMonths = $sixMonths->add(new \DateInterval('P7M'));
for ($i = 0; $today->diff($sixMonths)->days !== 0; $i++) {
$this->time->method('getDateTime')->willReturn($today);
$this->backupManager->runBackup($this->userId);
$this->backupManager->cleanupOldBackups($this->userId);
$today->add(new \DateInterval('P1D'));
}
$backupFolder = $this->backupManager->getBackupFolder($this->userId);
$nodes = $backupFolder->getDirectoryListing();
$backups = array_filter($nodes, function ($node) {
return str_ends_with($node->getName(), '.html');
});
$message = var_export(array_map(function ($node) {
return $node->getName();
}, $backups), true);
$this->assertEqualsWithDelta(7 + 5 + 5, count($backups), 3, $message);
}
}