1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-31 15:24:31 +03:00

Added testing for avatar fetching systems & config

Abstracts imageservice http interaction.
Closes #1193
This commit is contained in:
Dan Brown
2018-12-23 15:34:38 +00:00
parent 866187830a
commit 68017e2553
11 changed files with 217 additions and 71 deletions

View File

@ -0,0 +1,84 @@
<?php namespace Tests\Uploads;
use BookStack\Auth\User;
use BookStack\Uploads\HttpFetcher;
use Tests\TestCase;
class AvatarTest extends TestCase
{
use UsesImages;
protected function createUserRequest($user)
{
$resp = $this->asAdmin()->post('/settings/users/create', [
'name' => $user->name,
'email' => $user->email,
'password' => 'testing',
'password-confirm' => 'testing',
]);
return User::where('email', '=', $user->email)->first();
}
protected function assertImageFetchFrom(string $url)
{
$http = \Mockery::mock(HttpFetcher::class);
$this->app->instance(HttpFetcher::class, $http);
$http->shouldReceive('fetch')
->once()->with($url)
->andReturn($this->getTestImageContent());
}
protected function deleteUserImage(User $user)
{
$this->deleteImage($user->avatar->path);
}
public function test_gravatar_fetched_on_user_create()
{
config()->set([
'services.disable_services' => false,
]);
$user = factory(User::class)->make();
$this->assertImageFetchFrom('https://www.gravatar.com/avatar/'.md5(strtolower($user->email)).'?s=500&d=identicon');
$user = $this->createUserRequest($user);
$this->assertDatabaseHas('images', [
'type' => 'user',
'created_by' => $user->id
]);
$this->deleteUserImage($user);
}
public function test_custom_url_used_if_set()
{
config()->set([
'services.avatar_url' => 'https://example.com/${email}/${hash}/${size}',
]);
$user = factory(User::class)->make();
$url = 'https://example.com/'. urlencode(strtolower($user->email)) .'/'. md5(strtolower($user->email)).'/500';
$this->assertImageFetchFrom($url);
$user = $this->createUserRequest($user);
$this->deleteUserImage($user);
}
public function test_avatar_not_fetched_if_no_custom_url_and_services_disabled()
{
config()->set([
'services.disable_services' => true,
]);
$user = factory(User::class)->make();
$http = \Mockery::mock(HttpFetcher::class);
$this->app->instance(HttpFetcher::class, $http);
$http->shouldNotReceive('fetch');
$this->createUserRequest($user);
}
}

View File

@ -1,67 +1,15 @@
<?php namespace Tests;
<?php namespace Tests\Uploads;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Uploads\Image;
use BookStack\Entities\Page;
use BookStack\Entities\Repos\EntityRepo;
use BookStack\Uploads\ImageService;
use Tests\TestCase;
class ImageTest extends TestCase
{
/**
* Get the path to our basic test image.
* @return string
*/
protected function getTestImageFilePath()
{
return base_path('tests/test-data/test-image.png');
}
/**
* Get a test image that can be uploaded
* @param $fileName
* @return \Illuminate\Http\UploadedFile
*/
protected function getTestImage($fileName)
{
return new \Illuminate\Http\UploadedFile($this->getTestImageFilePath(), $fileName, 'image/png', 5238);
}
/**
* 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 \Illuminate\Foundation\Testing\TestResponse
*/
protected function uploadImage($name, $uploadedTo = 0)
{
$file = $this->getTestImage($name);
return $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
}
/**
* Delete an uploaded image.
* @param $relPath
*/
protected function deleteImage($relPath)
{
$path = public_path($relPath);
if (file_exists($path)) {
unlink($path);
}
}
use UsesImages;
public function test_image_upload()
{

View File

@ -0,0 +1,69 @@
<?php namespace Tests\Uploads;
trait UsesImages
{
/**
* Get the path to our basic test image.
* @return string
*/
protected function getTestImageFilePath()
{
return base_path('tests/test-data/test-image.png');
}
/**
* Get a test image that can be uploaded
* @param $fileName
* @return \Illuminate\Http\UploadedFile
*/
protected function getTestImage($fileName)
{
return new \Illuminate\Http\UploadedFile($this->getTestImageFilePath(), $fileName, 'image/png', 5238);
}
/**
* Get the raw file data for the test image.
* @return false|string
*/
protected function getTestImageContent()
{
return file_get_contents($this->getTestImageFilePath());
}
/**
* 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 \Illuminate\Foundation\Testing\TestResponse
*/
protected function uploadImage($name, $uploadedTo = 0)
{
$file = $this->getTestImage($name);
return $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
}
/**
* Delete an uploaded image.
* @param $relPath
*/
protected function deleteImage($relPath)
{
$path = public_path($relPath);
if (file_exists($path)) {
unlink($path);
}
}
}