Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Handle moving files to root folder #1534

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading