2017-02-04 06:58:42 -05:00
|
|
|
<?php namespace Tests;
|
2016-07-01 15:11:49 -04:00
|
|
|
|
2017-01-25 14:35:40 -05:00
|
|
|
class ImageTest extends BrowserKitTest
|
2016-07-01 15:11:49 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a test image that can be uploaded
|
|
|
|
* @param $fileName
|
|
|
|
* @return \Illuminate\Http\UploadedFile
|
|
|
|
*/
|
|
|
|
protected function getTestImage($fileName)
|
|
|
|
{
|
2016-10-23 10:25:04 -04:00
|
|
|
return new \Illuminate\Http\UploadedFile(base_path('tests/test-data/test-image.jpg'), $fileName, 'image/jpeg', 5238);
|
2016-07-01 15:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the path for a test image.
|
|
|
|
* @param $type
|
|
|
|
* @param $fileName
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getTestImagePath($type, $fileName)
|
|
|
|
{
|
|
|
|
return '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/' . $fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads an image with the given name.
|
|
|
|
* @param $name
|
|
|
|
* @param int $uploadedTo
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function uploadImage($name, $uploadedTo = 0)
|
|
|
|
{
|
|
|
|
$file = $this->getTestImage($name);
|
|
|
|
$this->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
|
|
|
|
return $this->getTestImagePath('gallery', $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an uploaded image.
|
|
|
|
* @param $relPath
|
|
|
|
*/
|
|
|
|
protected function deleteImage($relPath)
|
|
|
|
{
|
|
|
|
unlink(public_path($relPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function test_image_upload()
|
|
|
|
{
|
|
|
|
$page = \BookStack\Page::first();
|
|
|
|
$this->asAdmin();
|
|
|
|
$admin = $this->getAdmin();
|
|
|
|
$imageName = 'first-image.jpg';
|
|
|
|
|
|
|
|
$relPath = $this->uploadImage($imageName, $page->id);
|
|
|
|
$this->assertResponseOk();
|
|
|
|
|
2016-10-30 08:58:01 -04:00
|
|
|
$this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image not found at path: '. public_path($relPath));
|
2016-07-01 15:11:49 -04:00
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
$this->deleteImage($relPath);
|
|
|
|
|
2016-07-01 15:11:49 -04:00
|
|
|
$this->seeInDatabase('images', [
|
2016-09-17 13:22:04 -04:00
|
|
|
'url' => $this->baseUrl . $relPath,
|
2016-07-01 15:11:49 -04:00
|
|
|
'type' => 'gallery',
|
|
|
|
'uploaded_to' => $page->id,
|
|
|
|
'path' => $relPath,
|
|
|
|
'created_by' => $admin->id,
|
|
|
|
'updated_by' => $admin->id,
|
|
|
|
'name' => $imageName
|
|
|
|
]);
|
2016-09-17 13:22:04 -04:00
|
|
|
|
2016-07-01 15:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_image_delete()
|
|
|
|
{
|
|
|
|
$page = \BookStack\Page::first();
|
|
|
|
$this->asAdmin();
|
|
|
|
$imageName = 'first-image.jpg';
|
|
|
|
|
|
|
|
$relPath = $this->uploadImage($imageName, $page->id);
|
|
|
|
$image = \BookStack\Image::first();
|
|
|
|
|
|
|
|
$this->call('DELETE', '/images/' . $image->id);
|
|
|
|
$this->assertResponseOk();
|
|
|
|
|
|
|
|
$this->dontSeeInDatabase('images', [
|
2016-09-17 13:22:04 -04:00
|
|
|
'url' => $this->baseUrl . $relPath,
|
2016-07-01 15:11:49 -04:00
|
|
|
'type' => 'gallery'
|
|
|
|
]);
|
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
$this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
|
2016-07-01 15:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|