Skip to content

Commit 60a354f

Browse files
committed
fix: Track controller tests
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 97dbfd3 commit 60a354f

4 files changed

Lines changed: 12 additions & 33 deletions

File tree

β€Žlib/Controller/DevicesApiController.phpβ€Ž

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,14 @@
2424
use OCP\IRequest;
2525

2626
class DevicesApiController extends ApiController {
27-
28-
private Folder $userfolder;
29-
3027
public function __construct(
3128
string $appName,
3229
IRequest $request,
3330
private IL10N $l,
3431
private DevicesService $devicesService,
3532
private ?string $userId,
36-
IRootFolder $rootFolder,
3733
) {
38-
parent::__construct($appName, $request,
39-
'PUT, POST, GET, DELETE, PATCH, OPTIONS',
40-
'Authorization, Content-Type, Accept',
41-
1728000);
42-
if ($userId !== '' && $userId !== null) {
43-
// path of user files folder relative to DATA folder
44-
$this->userfolder = $rootFolder->getUserFolder($userId);
45-
}
34+
parent::__construct($appName, $request, 'PUT, POST, GET, DELETE, PATCH, OPTIONS');
4635
}
4736

4837
/**

β€Žlib/Service/TracksService.phpβ€Ž

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OC\Files\Search\SearchComparison;
1717
use OC\Files\Search\SearchQuery;
1818
use OCP\DB\QueryBuilder\IQueryBuilder;
19+
use OCP\Files\File;
1920
use OCP\Files\FileInfo;
2021
use OCP\Files\Folder;
2122
use OCP\Files\IRootFolder;
@@ -61,9 +62,8 @@ public function addByFile(Node $file) {
6162

6263
// add the file for its owner and users that have access
6364
// check if it's already in DB before adding
64-
public function safeAddByFile(Node $file) {
65+
public function safeAddByFile(File $file): bool {
6566
$ownerId = $file->getOwner()->getUID();
66-
$userFolder = $this->root->getUserFolder($ownerId);
6767
if ($this->isTrack($file)) {
6868
$this->safeAddTrack($file, $ownerId);
6969
// is the file accessible to other users ?
@@ -106,7 +106,7 @@ public function safeAddByFolderIdUserId($folderId, $userId) {
106106
}
107107

108108
// avoid adding track if it already exists in the DB
109-
private function safeAddTrack($track, $userId) {
109+
private function safeAddTrack(File $track, string $userId): void {
110110
// filehooks are triggered several times (2 times for file creation)
111111
// so we need to be sure it's not inserted several times
112112
// by checking if it already exists in DB
@@ -117,7 +117,7 @@ private function safeAddTrack($track, $userId) {
117117
}
118118

119119
// add all tracks of a folder taking care of shared accesses
120-
public function safeAddByFolder($folder) {
120+
public function safeAddByFolder(Folder $folder): void {
121121
$tracks = $this->gatherTrackFiles($folder, true);
122122
foreach ($tracks as $track) {
123123
$this->safeAddByFile($track);
@@ -165,11 +165,11 @@ public function safeDeleteByFolderIdUserId($folderId, $userId) {
165165
}
166166
}
167167

168-
private function gatherTrackFiles($folder, $recursive) {
168+
private function gatherTrackFiles(Folder $folder, bool $recursive) {
169169
$notes = [];
170170
$nodes = $folder->getDirectoryListing();
171171
foreach ($nodes as $node) {
172-
if ($node->getType() === FileInfo::TYPE_FOLDER and $recursive) {
172+
if ($node instanceof Folder and $recursive) {
173173
try {
174174
$notes = array_merge($notes, $this->gatherTrackFiles($node, $recursive));
175175
} catch (\OCP\Files\StorageNotAvailableException|\Exception $e) {
@@ -186,14 +186,11 @@ private function gatherTrackFiles($folder, $recursive) {
186186
return $notes;
187187
}
188188

189-
private function isTrack($file) {
190-
if ($file->getType() !== \OCP\Files\FileInfo::TYPE_FILE) {
189+
private function isTrack(Node $file): bool {
190+
if (!$file instanceof File) {
191191
return false;
192192
}
193-
if (!in_array($file->getMimetype(), self::TRACK_MIME_TYPES)) {
194-
return false;
195-
}
196-
return true;
193+
return in_array($file->getMimetype(), self::TRACK_MIME_TYPES);
197194
}
198195

199196
private function dbRowToTrack($row, $folder, $userFolder, $defaultMap, $ignoredPaths) {
@@ -415,7 +412,7 @@ public function getTrackByFileIDFromDB(int $fileId, ?string $userId = null) {
415412
return $track;
416413
}
417414

418-
public function addTrackToDB($userId, $fileId, $file) {
415+
public function addTrackToDB(string $userId, int $fileId, File $file): int {
419416
$metadata = '';
420417
$etag = $file->getEtag();
421418
$qb = $this->dbconnection->getQueryBuilder();

β€Žtests/Unit/Controller/DevicesApiControllerTest.phpβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ protected function setUp(): void {
7373
$c->get(IFactory::class)->get('maps'),
7474
$c->get(DevicesService::class),
7575
'test',
76-
$c->get(IRootFolder::class),
7776
);
7877

7978
// delete

β€Žtests/Unit/Controller/TracksControllerTest.phpβ€Ž

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,8 @@ public function testAddGetTracks(): void {
147147

148148
$filename = 'tests/test_files/testFile1.gpx';
149149
$content1 = file_get_contents($filename);
150-
$file = $userfolder->newFile('testFile1.gpxx');
150+
$file = $userfolder->newFile('testFile1.gpx');
151151
$file->putContent($content1);
152-
//$file->touch();
153-
154-
$file = $userfolder->get('testFile1.gpxx');
155-
$file->move($userfolder->getPath() . '/testFile1.gpx');
156-
//echo 'I MOVE TO '.$userfolder->getPath().'/testFile1.gpx'."\n";
157-
$file = $userfolder->get('testFile1.gpx');
158152
$file->touch();
159153

160154
$resp = $this->tracksController->getTracks();

0 commit comments

Comments
Β (0)