Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Зуев Егор
/
wiki.dev
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Authored by
Dan Brown
2016-10-23 15:25:04 +0100
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Commit
91220239e5d545115ae5844fa978eb5541e4d77e
91220239
1 parent
7ee695d7
Added in attachment tests
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
205 additions
and
3 deletions
app/Http/Controllers/FileController.php
resources/assets/js/controllers.js
resources/views/pages/sidebar-tree-list.blade.php
tests/AttachmentTest.php
tests/ImageTest.php
tests/test-data/test-file.txt
tests/test-image.jpg → tests/test-data/test-image.jpg
app/Http/Controllers/FileController.php
View file @
9122023
...
...
@@ -34,7 +34,6 @@ class FileController extends Controller
*/
public
function
upload
(
Request
$request
)
{
// TODO - ensure uploads are deleted on page delete.
$this
->
validate
(
$request
,
[
'uploaded_to'
=>
'required|integer|exists:pages,id'
,
'file'
=>
'required|file'
...
...
resources/assets/js/controllers.js
View file @
9122023
...
...
@@ -639,6 +639,7 @@ module.exports = function (ngApp, events) {
* @param fileLink
*/
$scope
.
attachLinkSubmit
=
function
(
file
)
{
file
.
uploaded_to
=
pageId
;
$http
.
post
(
'/files/link'
,
file
).
then
(
resp
=>
{
$scope
.
files
.
unshift
(
resp
.
data
);
events
.
emit
(
'success'
,
'Link attached'
);
...
...
resources/views/pages/sidebar-tree-list.blade.php
View file @
9122023
<div
class=
"book-tree"
ng-non-bindable
>
@if ($page->files->count() > 0)
@if (
isset($page)
&&
$page->files->count() > 0)
<h6
class=
"text-muted"
>
Attachments
</h6>
@foreach($page->files as $file)
<div
class=
"attachment"
>
...
...
tests/AttachmentTest.php
0 → 100644
View file @
9122023
<?php
class
AttachmentTest
extends
TestCase
{
/**
* Get a test file that can be uploaded
* @param $fileName
* @return \Illuminate\Http\UploadedFile
*/
protected
function
getTestFile
(
$fileName
)
{
return
new
\Illuminate\Http\UploadedFile
(
base_path
(
'tests/test-data/test-file.txt'
),
$fileName
,
'text/plain'
,
55
,
null
,
true
);
}
/**
* Uploads a file with the given name.
* @param $name
* @param int $uploadedTo
* @return string
*/
protected
function
uploadFile
(
$name
,
$uploadedTo
=
0
)
{
$file
=
$this
->
getTestFile
(
$name
);
return
$this
->
call
(
'POST'
,
'/files/upload'
,
[
'uploaded_to'
=>
$uploadedTo
],
[],
[
'file'
=>
$file
],
[]);
}
/**
* Get the expected upload path for a file.
* @param $fileName
* @return string
*/
protected
function
getUploadPath
(
$fileName
)
{
return
'uploads/files/'
.
Date
(
'Y-m-M'
)
.
'/'
.
$fileName
;
}
/**
* Delete all uploaded files.
* To assist with cleanup.
*/
protected
function
deleteUploads
()
{
$fileService
=
$this
->
app
->
make
(
\BookStack\Services\FileService
::
class
);
foreach
(
\BookStack\File
::
all
()
as
$file
)
{
$fileService
->
deleteFile
(
$file
);
}
}
public
function
test_file_upload
()
{
$page
=
\BookStack\Page
::
first
();
$this
->
asAdmin
();
$admin
=
$this
->
getAdmin
();
$fileName
=
'upload_test_file.txt'
;
$expectedResp
=
[
'name'
=>
$fileName
,
'uploaded_to'
=>
$page
->
id
,
'extension'
=>
'txt'
,
'order'
=>
1
,
'created_by'
=>
$admin
->
id
,
'updated_by'
=>
$admin
->
id
,
'path'
=>
$this
->
getUploadPath
(
$fileName
)
];
$this
->
uploadFile
(
$fileName
,
$page
->
id
);
$this
->
assertResponseOk
();
$this
->
seeJsonContains
(
$expectedResp
);
$this
->
seeInDatabase
(
'files'
,
$expectedResp
);
$this
->
deleteUploads
();
}
public
function
test_file_display_and_access
()
{
$page
=
\BookStack\Page
::
first
();
$this
->
asAdmin
();
$admin
=
$this
->
getAdmin
();
$fileName
=
'upload_test_file.txt'
;
$this
->
uploadFile
(
$fileName
,
$page
->
id
);
$this
->
assertResponseOk
();
$this
->
visit
(
$page
->
getUrl
())
->
seeLink
(
$fileName
)
->
click
(
$fileName
)
->
see
(
'Hi, This is a test file for testing the upload process.'
);
$this
->
deleteUploads
();
}
public
function
test_attaching_link_to_page
()
{
$page
=
\BookStack\Page
::
first
();
$admin
=
$this
->
getAdmin
();
$this
->
asAdmin
();
$this
->
call
(
'POST'
,
'files/link'
,
[
'link'
=>
'https://example.com'
,
'name'
=>
'Example Attachment Link'
,
'uploaded_to'
=>
$page
->
id
,
]);
$expectedResp
=
[
'path'
=>
'https://example.com'
,
'name'
=>
'Example Attachment Link'
,
'uploaded_to'
=>
$page
->
id
,
'created_by'
=>
$admin
->
id
,
'updated_by'
=>
$admin
->
id
,
'external'
=>
true
,
'order'
=>
1
,
'extension'
=>
''
];
$this
->
assertResponseOk
();
$this
->
seeJsonContains
(
$expectedResp
);
$this
->
seeInDatabase
(
'files'
,
$expectedResp
);
$this
->
visit
(
$page
->
getUrl
())
->
seeLink
(
'Example Attachment Link'
)
->
click
(
'Example Attachment Link'
)
->
seePageIs
(
'https://example.com'
);
$this
->
deleteUploads
();
}
public
function
test_attachment_updating
()
{
$page
=
\BookStack\Page
::
first
();
$this
->
asAdmin
();
$this
->
call
(
'POST'
,
'files/link'
,
[
'link'
=>
'https://example.com'
,
'name'
=>
'Example Attachment Link'
,
'uploaded_to'
=>
$page
->
id
,
]);
$attachmentId
=
\BookStack\File
::
first
()
->
id
;
$this
->
call
(
'PUT'
,
'files/'
.
$attachmentId
,
[
'uploaded_to'
=>
$page
->
id
,
'name'
=>
'My new attachment name'
,
'link'
=>
'https://test.example.com'
]);
$expectedResp
=
[
'path'
=>
'https://test.example.com'
,
'name'
=>
'My new attachment name'
,
'uploaded_to'
=>
$page
->
id
];
$this
->
assertResponseOk
();
$this
->
seeJsonContains
(
$expectedResp
);
$this
->
seeInDatabase
(
'files'
,
$expectedResp
);
$this
->
deleteUploads
();
}
public
function
test_file_deletion
()
{
$page
=
\BookStack\Page
::
first
();
$this
->
asAdmin
();
$fileName
=
'deletion_test.txt'
;
$this
->
uploadFile
(
$fileName
,
$page
->
id
);
$filePath
=
base_path
(
'storage/'
.
$this
->
getUploadPath
(
$fileName
));
$this
->
assertTrue
(
file_exists
(
$filePath
),
'File at path '
.
$filePath
.
' does not exist'
);
$attachmentId
=
\BookStack\File
::
first
()
->
id
;
$this
->
call
(
'DELETE'
,
'files/'
.
$attachmentId
);
$this
->
dontSeeInDatabase
(
'files'
,
[
'name'
=>
$fileName
]);
$this
->
assertFalse
(
file_exists
(
$filePath
),
'File at path '
.
$filePath
.
' was not deleted as expected'
);
$this
->
deleteUploads
();
}
public
function
test_attachment_deletion_on_page_deletion
()
{
$page
=
\BookStack\Page
::
first
();
$this
->
asAdmin
();
$fileName
=
'deletion_test.txt'
;
$this
->
uploadFile
(
$fileName
,
$page
->
id
);
$filePath
=
base_path
(
'storage/'
.
$this
->
getUploadPath
(
$fileName
));
$this
->
assertTrue
(
file_exists
(
$filePath
),
'File at path '
.
$filePath
.
' does not exist'
);
$this
->
seeInDatabase
(
'files'
,
[
'name'
=>
$fileName
]);
$this
->
call
(
'DELETE'
,
$page
->
getUrl
());
$this
->
dontSeeInDatabase
(
'files'
,
[
'name'
=>
$fileName
]);
$this
->
assertFalse
(
file_exists
(
$filePath
),
'File at path '
.
$filePath
.
' was not deleted as expected'
);
$this
->
deleteUploads
();
}
}
tests/ImageTest.php
View file @
9122023
...
...
@@ -10,7 +10,7 @@ class ImageTest extends TestCase
*/
protected
function
getTestImage
(
$fileName
)
{
return
new
\Illuminate\Http\UploadedFile
(
base_path
(
'tests/test-image.jpg'
),
$fileName
,
'image/jpeg'
,
5238
);
return
new
\Illuminate\Http\UploadedFile
(
base_path
(
'tests/test-
data/test-
image.jpg'
),
$fileName
,
'image/jpeg'
,
5238
);
}
/**
...
...
tests/test-data/test-file.txt
0 → 100644
View file @
9122023
Hi, This is a test file for testing the upload process.
\ No newline at end of file
tests/test-image.jpg
→
tests/test-
data/test-
image.jpg
View file @
9122023
5.12 KB
Please
register
or
sign in
to post a comment