2016-07-01 21:11:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ImageTest extends TestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a test image that can be uploaded
|
|
|
|
* @param $fileName
|
|
|
|
* @return \Illuminate\Http\UploadedFile
|
|
|
|
*/
|
|
|
|
protected function getTestImage($fileName)
|
|
|
|
{
|
2016-10-23 16:25:04 +02:00
|
|
|
return new \Illuminate\Http\UploadedFile(base_path('tests/test-data/test-image.jpg'), $fileName, 'image/jpeg', 5238);
|
2016-07-01 21:11:49 +02: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 13:58:01 +01:00
|
|
|
$this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image not found at path: '. public_path($relPath));
|
2016-07-01 21:11:49 +02:00
|
|
|
|
2016-09-17 19:22:04 +02:00
|
|
|
$this->deleteImage($relPath);
|
|
|
|
|
2016-07-01 21:11:49 +02:00
|
|
|
$this->seeInDatabase('images', [
|
2016-09-17 19:22:04 +02:00
|
|
|
'url' => $this->baseUrl . $relPath,
|
2016-07-01 21:11:49 +02:00
|
|
|
'type' => 'gallery',
|
|
|
|
'uploaded_to' => $page->id,
|
|
|
|
'path' => $relPath,
|
|
|
|
'created_by' => $admin->id,
|
|
|
|
'updated_by' => $admin->id,
|
|
|
|
'name' => $imageName
|
|
|
|
]);
|
2016-09-17 19:22:04 +02:00
|
|
|
|
2016-07-01 21:11:49 +02: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 19:22:04 +02:00
|
|
|
'url' => $this->baseUrl . $relPath,
|
2016-07-01 21:11:49 +02:00
|
|
|
'type' => 'gallery'
|
|
|
|
]);
|
|
|
|
|
2017-01-02 12:07:27 +01:00
|
|
|
$this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
|
2016-07-01 21:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|