Skip to content

Commit

Permalink
FIX Handle moving files to root folder
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 6, 2025
1 parent e18df1d commit 5607329
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 5 additions & 2 deletions code/Controller/AssetAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,15 +435,18 @@ public function apiMove(HTTPRequest $request): HTTPResponse
}
$ids = $this->getPostedJsonValue($request, 'ids');
$folderID = $this->getPostedJsonValue($request, 'folderID');
$folder = $this->getFileByID($folderID, true, 400);
$folder = null;
if ($folderID !== 0) {
$folder = $this->getFileByID($folderID, true, 400);
}
$files = $this->getFilesByIDs($ids, true, 400);
foreach ($files as $file) {
if (!$file->canEdit()) {
$this->jsonError(403);
}
}
foreach ($files as $file) {
$file->ParentID = $folder->ID;
$file->ParentID = $folder ? $folder->ID : 0;
$file->write();
}
return $this->jsonSuccess(204);
Expand Down
23 changes: 22 additions & 1 deletion tests/php/Controller/AssetAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -793,58 +793,75 @@ public function testApiDelete(
public static function provideApiMove(): array
{
return [
'Valid' => [
'Valid non-root' => [
'idsType' => 'existing',
'folderToType' => 'non-root',
'fail' => '',
'expectedCode' => 204,
],
'Valid root' => [
'idsType' => 'existing',
'folderToType' => 'root',
'fail' => '',
'expectedCode' => 204,
],
'Reject fail canEdit()' => [
'idsType' => 'existing',
'folderToType' => 'non-root',
'fail' => 'can-edit',
'expectedCode' => 403,
],
'Reject fail csrf-token' => [
'idsType' => 'existing',
'folderToType' => 'non-root',
'fail' => 'csrf-token',
'expectedCode' => 400,
],
'Reject ids not passed' => [
'idsType' => 'existing',
'folderToType' => 'non-root',
'fail' => 'ids-not-passed',
'expectedCode' => 400,
],
'Reject ids is not array' => [
'idsType' => 'existing',
'folderToType' => 'non-root',
'fail' => 'ids-not-array',
'expectedCode' => 400,
],
'Reject ids is empty' => [
'idsType' => 'existing',
'folderToType' => 'non-root',
'fail' => 'ids-is-empty',
'expectedCode' => 400,
],
'Reject invalid ID' => [
'idsType' => 'second-is-invalid',
'folderToType' => 'non-root',
'fail' => '',
'expectedCode' => 400,
],
'Reject non-numeric ID' => [
'idsType' => 'second-is-non-numeric',
'folderToType' => 'non-root',
'fail' => '',
'expectedCode' => 400,
],
'Reject new record ID' => [
'idsType' => 'second-is-new-record',
'folderToType' => 'non-root',
'fail' => '',
'expectedCode' => 400,
],
'Reject folderID not passed' => [
'idsType' => 'existing',
'folderToType' => 'non-root',
'fail' => 'folder-id-not-passed',
'expectedCode' => 400,
],
'Reject folder does not exist' => [
'idsType' => 'existing',
'folderToType' => 'non-root',
'fail' => 'folder-not-exist',
'expectedCode' => 400,
],
Expand All @@ -854,6 +871,7 @@ public static function provideApiMove(): array
#[DataProvider('provideApiMove')]
public function testApiMove(
string $idsType,
string $folderToType,
string $fail,
int $expectedCode
): void {
Expand All @@ -862,6 +880,9 @@ public function testApiMove(
$this->idFromFixture(Folder::class, 'ApiFolder01'),
$this->idFromFixture(Folder::class, 'ApiFolder02'),
];
if ($folderToType === 'root') {
$folderIDs[1] = 0;
}
$existingIDs = $this->getIDs('existing');
TestFile::$fail = $fail;
$url = '/admin/assets/api/move';
Expand Down

0 comments on commit 5607329

Please sign in to comment.