0
0
Fork 0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-04-30 14:40:03 +00:00

Applied latest changes from styleCI

This commit is contained in:
Dan Brown 2021-10-20 10:49:45 +01:00
parent 7bbcaa7cbc
commit 859934d6a3
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
5 changed files with 60 additions and 55 deletions

View file

@ -17,16 +17,16 @@ class AttachmentApiController extends ApiController
protected $rules = [ protected $rules = [
'create' => [ 'create' => [
'name' => 'required|min:1|max:255|string', 'name' => 'required|min:1|max:255|string',
'uploaded_to' => 'required|integer|exists:pages,id', 'uploaded_to' => 'required|integer|exists:pages,id',
'file' => 'required_without:link|file', 'file' => 'required_without:link|file',
'link' => 'required_without:file|min:1|max:255|safe_url' 'link' => 'required_without:file|min:1|max:255|safe_url',
], ],
'update' => [ 'update' => [
'name' => 'min:1|max:255|string', 'name' => 'min:1|max:255|string',
'uploaded_to' => 'integer|exists:pages,id', 'uploaded_to' => 'integer|exists:pages,id',
'file' => 'file', 'file' => 'file',
'link' => 'min:1|max:255|safe_url' 'link' => 'min:1|max:255|safe_url',
], ],
]; ];
@ -72,11 +72,14 @@ class AttachmentApiController extends ApiController
$attachment = $this->attachmentService->saveNewUpload($uploadedFile, $page->id); $attachment = $this->attachmentService->saveNewUpload($uploadedFile, $page->id);
} else { } else {
$attachment = $this->attachmentService->saveNewFromLink( $attachment = $this->attachmentService->saveNewFromLink(
$requestData['name'], $requestData['link'], $page->id $requestData['name'],
$requestData['link'],
$page->id
); );
} }
$this->attachmentService->updateFile($attachment, $requestData); $this->attachmentService->updateFile($attachment, $requestData);
return response()->json($attachment); return response()->json($attachment);
} }
@ -140,6 +143,7 @@ class AttachmentApiController extends ApiController
} }
$this->attachmentService->updateFile($attachment, $requestData); $this->attachmentService->updateFile($attachment, $requestData);
return response()->json($attachment); return response()->json($attachment);
} }
@ -158,5 +162,4 @@ class AttachmentApiController extends ApiController
return response('', 204); return response('', 204);
} }
} }

View file

@ -12,15 +12,15 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
/** /**
* @property int $id * @property int $id
* @property string $name * @property string $name
* @property string $path * @property string $path
* @property string $extension * @property string $extension
* @property ?Page $page * @property ?Page $page
* @property bool $external * @property bool $external
* @property int $uploaded_to * @property int $uploaded_to
* @property User $updatedBy * @property User $updatedBy
* @property User $createdBy * @property User $createdBy
* *
* @method static Entity|Builder visible() * @method static Entity|Builder visible()
*/ */
@ -90,6 +90,7 @@ class Attachment extends Model
public function scopeVisible(): Builder public function scopeVisible(): Builder
{ {
$permissionService = app()->make(PermissionService::class); $permissionService = app()->make(PermissionService::class);
return $permissionService->filterRelatedEntity( return $permissionService->filterRelatedEntity(
Page::class, Page::class,
Attachment::query(), Attachment::query(),

View file

@ -171,6 +171,7 @@ class AttachmentService
} }
$attachment->save(); $attachment->save();
return $attachment->refresh(); return $attachment->refresh();
} }

View file

@ -18,17 +18,17 @@ class AttachmentsApiTest extends TestCase
$this->actingAsApiEditor(); $this->actingAsApiEditor();
$page = Page::query()->first(); $page = Page::query()->first();
$attachment = $this->createAttachmentForPage($page, [ $attachment = $this->createAttachmentForPage($page, [
'name' => 'My test attachment', 'name' => 'My test attachment',
'external' => true, 'external' => true,
]); ]);
$resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id'); $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
$resp->assertJson(['data' => [ $resp->assertJson(['data' => [
[ [
'id' => $attachment->id, 'id' => $attachment->id,
'name' => 'My test attachment', 'name' => 'My test attachment',
'uploaded_to' => $page->id, 'uploaded_to' => $page->id,
'external' => true, 'external' => true,
], ],
]]); ]]);
} }
@ -39,7 +39,7 @@ class AttachmentsApiTest extends TestCase
/** @var Page $page */ /** @var Page $page */
$page = Page::query()->first(); $page = Page::query()->first();
$attachment = $this->createAttachmentForPage($page, [ $attachment = $this->createAttachmentForPage($page, [
'name' => 'My test attachment', 'name' => 'My test attachment',
'external' => true, 'external' => true,
]); ]);
@ -69,9 +69,9 @@ class AttachmentsApiTest extends TestCase
$page = Page::query()->first(); $page = Page::query()->first();
$details = [ $details = [
'name' => 'My attachment', 'name' => 'My attachment',
'uploaded_to' => $page->id, 'uploaded_to' => $page->id,
'link' => 'https://cats.example.com', 'link' => 'https://cats.example.com',
]; ];
$resp = $this->postJson($this->baseEndpoint, $details); $resp = $this->postJson($this->baseEndpoint, $details);
@ -89,7 +89,7 @@ class AttachmentsApiTest extends TestCase
$file = $this->getTestFile('textfile.txt'); $file = $this->getTestFile('textfile.txt');
$details = [ $details = [
'name' => 'My attachment', 'name' => 'My attachment',
'uploaded_to' => $page->id, 'uploaded_to' => $page->id,
]; ];
@ -110,14 +110,14 @@ class AttachmentsApiTest extends TestCase
$details = [ $details = [
'uploaded_to' => $page->id, 'uploaded_to' => $page->id,
'link' => 'https://example.com', 'link' => 'https://example.com',
]; ];
$resp = $this->postJson($this->baseEndpoint, $details); $resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(422); $resp->assertStatus(422);
$resp->assertJson([ $resp->assertJson([
'error' => [ 'error' => [
'message' => 'The given data was invalid.', 'message' => 'The given data was invalid.',
'validation' => [ 'validation' => [
'name' => ['The name field is required.'], 'name' => ['The name field is required.'],
], ],
@ -133,7 +133,7 @@ class AttachmentsApiTest extends TestCase
$page = Page::query()->first(); $page = Page::query()->first();
$details = [ $details = [
'name' => 'my attachment', 'name' => 'my attachment',
'uploaded_to' => $page->id, 'uploaded_to' => $page->id,
]; ];
@ -141,10 +141,10 @@ class AttachmentsApiTest extends TestCase
$resp->assertStatus(422); $resp->assertStatus(422);
$resp->assertJson([ $resp->assertJson([
'error' => [ 'error' => [
'message' => 'The given data was invalid.', 'message' => 'The given data was invalid.',
'validation' => [ 'validation' => [
"file" => ["The file field is required when link is not present."], 'file' => ['The file field is required when link is not present.'],
"link" => ["The link field is required when file is not present."], 'link' => ['The link field is required when file is not present.'],
], ],
'code' => 422, 'code' => 422,
], ],
@ -158,8 +158,8 @@ class AttachmentsApiTest extends TestCase
$page = Page::query()->first(); $page = Page::query()->first();
$attachment = $this->createAttachmentForPage($page, [ $attachment = $this->createAttachmentForPage($page, [
'name' => 'my attachment', 'name' => 'my attachment',
'path' => 'https://example.com', 'path' => 'https://example.com',
'order' => 1, 'order' => 1,
]); ]);
@ -167,20 +167,20 @@ class AttachmentsApiTest extends TestCase
$resp->assertStatus(200); $resp->assertStatus(200);
$resp->assertJson([ $resp->assertJson([
'id' => $attachment->id, 'id' => $attachment->id,
'content' => 'https://example.com', 'content' => 'https://example.com',
'external' => true, 'external' => true,
'uploaded_to' => $page->id, 'uploaded_to' => $page->id,
'order' => 1, 'order' => 1,
'created_by' => [ 'created_by' => [
'name' => $attachment->createdBy->name, 'name' => $attachment->createdBy->name,
], ],
'updated_by' => [ 'updated_by' => [
'name' => $attachment->createdBy->name, 'name' => $attachment->createdBy->name,
], ],
'links' => [ 'links' => [
"html" => "<a target=\"_blank\" href=\"http://localhost/attachments/{$attachment->id}\">my attachment</a>", 'html' => "<a target=\"_blank\" href=\"http://localhost/attachments/{$attachment->id}\">my attachment</a>",
"markdown" => "[my attachment](http://localhost/attachments/{$attachment->id})" 'markdown' => "[my attachment](http://localhost/attachments/{$attachment->id})",
], ],
]); ]);
} }
@ -193,7 +193,7 @@ class AttachmentsApiTest extends TestCase
$file = $this->getTestFile('textfile.txt'); $file = $this->getTestFile('textfile.txt');
$details = [ $details = [
'name' => 'My file attachment', 'name' => 'My file attachment',
'uploaded_to' => $page->id, 'uploaded_to' => $page->id,
]; ];
$this->call('POST', $this->baseEndpoint, $details, [], ['file' => $file]); $this->call('POST', $this->baseEndpoint, $details, [], ['file' => $file]);
@ -204,20 +204,20 @@ class AttachmentsApiTest extends TestCase
$resp->assertStatus(200); $resp->assertStatus(200);
$resp->assertJson([ $resp->assertJson([
'id' => $attachment->id, 'id' => $attachment->id,
'content' => base64_encode(file_get_contents(storage_path($attachment->path))), 'content' => base64_encode(file_get_contents(storage_path($attachment->path))),
'external' => false, 'external' => false,
'uploaded_to' => $page->id, 'uploaded_to' => $page->id,
'order' => 1, 'order' => 1,
'created_by' => [ 'created_by' => [
'name' => $attachment->createdBy->name, 'name' => $attachment->createdBy->name,
], ],
'updated_by' => [ 'updated_by' => [
'name' => $attachment->updatedBy->name, 'name' => $attachment->updatedBy->name,
], ],
'links' => [ 'links' => [
"html" => "<a target=\"_blank\" href=\"http://localhost/attachments/{$attachment->id}\">My file attachment</a>", 'html' => "<a target=\"_blank\" href=\"http://localhost/attachments/{$attachment->id}\">My file attachment</a>",
"markdown" => "[My file attachment](http://localhost/attachments/{$attachment->id})" 'markdown' => "[My file attachment](http://localhost/attachments/{$attachment->id})",
], ],
]); ]);
@ -250,7 +250,6 @@ class AttachmentsApiTest extends TestCase
$attachment = $this->createAttachmentForPage($page); $attachment = $this->createAttachmentForPage($page);
$file = $this->getTestFile('textfile.txt'); $file = $this->getTestFile('textfile.txt');
$resp = $this->call('PUT', "{$this->baseEndpoint}/{$attachment->id}", ['name' => 'My updated file'], [], ['file' => $file]); $resp = $this->call('PUT', "{$this->baseEndpoint}/{$attachment->id}", ['name' => 'My updated file'], [], ['file' => $file]);
$resp->assertStatus(200); $resp->assertStatus(200);
@ -278,7 +277,7 @@ class AttachmentsApiTest extends TestCase
$details = [ $details = [
'name' => 'My updated API attachment', 'name' => 'My updated API attachment',
'link' => 'https://cats.example.com' 'link' => 'https://cats.example.com',
]; ];
$resp = $this->putJson("{$this->baseEndpoint}/{$attachment->id}", $details); $resp = $this->putJson("{$this->baseEndpoint}/{$attachment->id}", $details);
@ -310,13 +309,14 @@ class AttachmentsApiTest extends TestCase
/** @var Attachment $attachment */ /** @var Attachment $attachment */
$attachment = $page->attachments()->forceCreate(array_merge([ $attachment = $page->attachments()->forceCreate(array_merge([
'uploaded_to' => $page->id, 'uploaded_to' => $page->id,
'name' => 'test attachment', 'name' => 'test attachment',
'external' => true, 'external' => true,
'order' => 1, 'order' => 1,
'created_by' => $admin->id, 'created_by' => $admin->id,
'updated_by' => $admin->id, 'updated_by' => $admin->id,
'path' => 'https://attachment.example.com' 'path' => 'https://attachment.example.com',
], $attributes)); ], $attributes));
return $attachment; return $attachment;
} }

View file

@ -614,7 +614,7 @@ class PageContentTest extends TestCase
$page = Page::query()->first(); $page = Page::query()->first();
$this->put($page->getUrl(), [ $this->put($page->getUrl(), [
'name' => $page->name, 'summary' => '', 'name' => $page->name, 'summary' => '',
'markdown' => 'test ![test](data:image/jpeg;base64,' . $this->base64Jpeg . ')', 'markdown' => 'test ![test](data:image/jpeg;base64,' . $this->base64Jpeg . ')',
]); ]);
@ -636,7 +636,7 @@ class PageContentTest extends TestCase
$page = Page::query()->first(); $page = Page::query()->first();
$this->put($page->getUrl(), [ $this->put($page->getUrl(), [
'name' => $page->name, 'summary' => '', 'name' => $page->name, 'summary' => '',
'markdown' => 'test ![test](data:image/jiff;base64,' . $this->base64Jpeg . ')', 'markdown' => 'test ![test](data:image/jiff;base64,' . $this->base64Jpeg . ')',
]); ]);