BookStack/tests/AttachmentTest.php

202 lines
5.9 KiB
PHP
Raw Normal View History

2016-10-23 22:25:04 +08:00
<?php
2017-01-26 03:35:40 +08:00
class AttachmentTest extends BrowserKitTest
2016-10-23 22:25:04 +08:00
{
/**
* 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);
2016-11-12 22:12:26 +08:00
return $this->call('POST', '/attachments/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
2016-10-23 22:25:04 +08:00
}
/**
* 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()
{
2016-11-12 22:12:26 +08:00
$fileService = $this->app->make(\BookStack\Services\AttachmentService::class);
foreach (\BookStack\Attachment::all() as $file) {
2016-10-23 22:25:04 +08:00
$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);
2016-11-12 22:12:26 +08:00
$this->seeInDatabase('attachments', $expectedResp);
2016-10-23 22:25:04 +08:00
$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();
2016-11-12 22:12:26 +08:00
$this->call('POST', 'attachments/link', [
2016-10-23 22:25:04 +08:00
'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);
2016-11-12 22:12:26 +08:00
$this->seeInDatabase('attachments', $expectedResp);
2016-10-23 22:25:04 +08:00
$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();
2016-11-12 22:12:26 +08:00
$this->call('POST', 'attachments/link', [
2016-10-23 22:25:04 +08:00
'link' => 'https://example.com',
'name' => 'Example Attachment Link',
'uploaded_to' => $page->id,
]);
2016-11-12 22:12:26 +08:00
$attachmentId = \BookStack\Attachment::first()->id;
2016-10-23 22:25:04 +08:00
2016-11-12 22:12:26 +08:00
$this->call('PUT', 'attachments/' . $attachmentId, [
2016-10-23 22:25:04 +08:00
'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);
2016-11-12 22:12:26 +08:00
$this->seeInDatabase('attachments', $expectedResp);
2016-10-23 22:25:04 +08:00
$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');
2016-11-12 22:12:26 +08:00
$attachmentId = \BookStack\Attachment::first()->id;
$this->call('DELETE', 'attachments/' . $attachmentId);
2016-10-23 22:25:04 +08:00
2016-11-12 22:12:26 +08:00
$this->dontSeeInDatabase('attachments', [
2016-10-23 22:25:04 +08:00
'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');
2016-11-12 22:12:26 +08:00
$this->seeInDatabase('attachments', [
2016-10-23 22:25:04 +08:00
'name' => $fileName
]);
$this->call('DELETE', $page->getUrl());
2016-11-12 22:12:26 +08:00
$this->dontSeeInDatabase('attachments', [
2016-10-23 22:25:04 +08:00
'name' => $fileName
]);
$this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
$this->deleteUploads();
}
}