-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathBackgroundJobTest.php
193 lines (168 loc) Β· 5.23 KB
/
BackgroundJobTest.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace OCA\Bookmarks\Tests;
use OC\BackgroundJob\JobList;
use OCA\Bookmarks\BackgroundJobs\CrawlJob;
use OCA\Bookmarks\BackgroundJobs\FileCacheGCJob;
use OCA\Bookmarks\Db\Bookmark;
use OCA\Bookmarks\Db\BookmarkMapper;
use OCA\Bookmarks\Db\FolderMapper;
use OCA\Bookmarks\Db\PublicFolderMapper;
use OCA\Bookmarks\Db\ShareMapper;
use OCA\Bookmarks\Service\CrawlService;
use OCA\Bookmarks\Service\FileCache;
use OCA\Bookmarks\Service\UrlNormalizer;
use OCP\AppFramework\App;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use Psr\Log\LoggerInterface;
/**
* Class Test_BackgroundJob
*/
class BackgroundJobTest extends TestCase {
/**
* @var BookmarkMapper
*/
private $bookmarkMapper;
/**
* @var CrawlJob
*/
private $previewsJob;
/**
* @var JobList
*/
private $jobList;
/**
* @var mixed|\stdClass
*/
private $settings;
/**
* @var string
*/
private $userId;
/**
* @var FileCacheGCJob
*/
private $gcJob;
private $bookmarks = [];
/**
* @var IAppData
*/
private $appData;
/**
* @var FileCache
*/
private $fileCache;
/**
* @var ITimeFactory
*/
private $timeFactory;
/**
* @var int
*/
private $time;
/**
* @var \OCP\AppFramework\IAppContainer
*/
private $container;
protected function setUp() :void {
parent::setUp();
$app = new App('bookmarks');
$container = $this->container = $app->getContainer();
// prepare
$this->settings = $container->get(IConfig::class);
$this->settings->setAppValue('bookmarks', 'privacy.enableScraping', 'true');
$this->timeFactory = $this->createStub(ITimeFactory::class);
$this->bookmarkMapper = new BookmarkMapper(
$container->get(\OCP\IDBConnection::class),
$container->get(\OCP\EventDispatcher\IEventDispatcher::class),
$container->get(UrlNormalizer::class),
$this->settings,
$container->get(PublicFolderMapper::class),
$this->timeFactory,
$container->get(FolderMapper::class),
$container->get(ShareMapper::class),
);
$this->previewsJob = new CrawlJob(
$this->settings,
$this->bookmarkMapper,
$container->get(CrawlService::class),
$this->timeFactory
);
$this->appData = $container->get(IAppData::class);
$this->fileCache = new FileCache($this->appData, $this->timeFactory);
$this->gcJob = new FileCacheGCJob($this->fileCache, $container->get(LoggerInterface::class), $this->timeFactory);
$this->jobList = $container->get(JobList::class);
$this->userId = 'test';
$this->cleanUp();
$this->bookmarks = array_map(function ($bm) {
return $this->bookmarkMapper->insert($bm);
}, $this->singleBookmarksProvider());
}
/**
* @throws NotFoundException
* @throws NotPermittedException
*/
public function testPreviewsJob() : void {
$this->fileCache->clear();
$oldCacheSize = 0;
$this->timeFactory->method('getTime')
->willReturn(time());
// generate cached previews
$this->previewsJob->setId(1);
$this->previewsJob->setLastRun(0);
$this->previewsJob->execute($this->jobList);
$folder = $this->appData->getFolder('cache');
$newCacheSize = count($folder->getDirectoryListing());
// should have cached something
self::assertGreaterThan($oldCacheSize, $newCacheSize);
}
/**
* @throws NotFoundException
* @throws NotPermittedException
*/
public function testGCJob() : void {
$this->fileCache->clear();
$initialCacheSize = 0;
$this->timeFactory->method('getTime')
->willReturn(time());
// generate cached previews
$this->previewsJob->setId(1);
$this->previewsJob->setLastRun(0);
$this->previewsJob->execute($this->jobList);
$folder = $this->appData->getFolder('cache');
$cacheSize = count($folder->getDirectoryListing());
// should have cached something
self::assertGreaterThan($initialCacheSize, $cacheSize);
// fast-forward to a time when the previews should be garbage collected
$time = time() + FileCache::TIMEOUT + 60 * 60 * 24;
$this->timeFactory = $this->createStub(ITimeFactory::class);
$this->timeFactory->method('getTime')
->willReturn($time);
$this->fileCache = new FileCache($this->appData, $this->timeFactory);
$this->gcJob = new FileCacheGCJob($this->fileCache, $this->container->get(LoggerInterface::class), $this->timeFactory);
// run GC job
$this->gcJob->setId(3);
$this->gcJob->setLastRun(0);
$this->gcJob->execute($this->jobList);
$newCacheSize = count($folder->getDirectoryListing());
// should have cleaned up the pending cache entries
self::assertLessThan($cacheSize, $newCacheSize);
}
/**
* @return array
*/
public function singleBookmarksProvider() {
return array_map(function ($props) {
return Bookmark::fromArray(array_merge($props, ['userId' => 'test']));
}, [
'Simple URL with title and description' => ['url' => 'https://google.com/', 'title' => 'Google', 'description' => 'Search engine'],
'Simple URL with title' => ['url' => 'https://nextcloud.com/', 'title' => 'Nextcloud', 'description' => ''],
'Simple URL' => ['url' => 'https://php.net/', 'title' => '', 'description' => ''],
'URL with unicode' => ['url' => 'https://de.wikipedia.org/wiki/%C3%9C', 'title' => '', 'description' => ''],
'Non-existent URL' => ['url' => 'https://http://www.bllaala.com/', 'title' => '', 'description' => ''],
]);
}
}