mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-28 01:34:43 +02:00
* refactor: migration from laravel-enum package to native php enums * style: fix StyleCI findings
103 lines
2.6 KiB
PHP
103 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\Http\Api\Wiki\Image;
|
|
|
|
use App\Enums\Auth\CrudPermission;
|
|
use App\Enums\Models\Wiki\ImageFacet;
|
|
use App\Http\Api\Field\Wiki\Image\ImageFileField;
|
|
use App\Models\Auth\User;
|
|
use App\Models\Wiki\Image;
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Laravel\Sanctum\Sanctum;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Class ImageStoreTest.
|
|
*/
|
|
class ImageStoreTest extends TestCase
|
|
{
|
|
use WithFaker;
|
|
|
|
/**
|
|
* The Image Store Endpoint shall be protected by sanctum.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testProtected(): void
|
|
{
|
|
$image = Image::factory()->makeOne();
|
|
|
|
$response = $this->post(route('api.image.store', $image->toArray()));
|
|
|
|
$response->assertUnauthorized();
|
|
}
|
|
|
|
/**
|
|
* The Image Store Endpoint shall forbid users without the create image permission.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testForbidden(): void
|
|
{
|
|
$image = Image::factory()->makeOne();
|
|
|
|
$user = User::factory()->createOne();
|
|
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->post(route('api.image.store', $image->toArray()));
|
|
|
|
$response->assertForbidden();
|
|
}
|
|
|
|
/**
|
|
* The Image Store Endpoint shall require the file field.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testRequiredFields(): void
|
|
{
|
|
$user = User::factory()->withPermissions(CrudPermission::CREATE->format(Image::class))->createOne();
|
|
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->post(route('api.image.store'));
|
|
|
|
$response->assertJsonValidationErrors([
|
|
ImageFileField::ATTRIBUTE_FILE,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* The Image Store Endpoint shall create an image.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testCreate(): void
|
|
{
|
|
$fs = Storage::fake(Config::get('image.disk'));
|
|
|
|
$facet = Arr::random(ImageFacet::cases());
|
|
|
|
$parameters = [Image::ATTRIBUTE_FACET => $facet->localize()];
|
|
|
|
$user = User::factory()->withPermissions(CrudPermission::CREATE->format(Image::class))->createOne();
|
|
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->post(route('api.image.store', $parameters), [
|
|
ImageFileField::ATTRIBUTE_FILE => UploadedFile::fake()->image($this->faker->word().'.jpg'),
|
|
]);
|
|
|
|
$response->assertCreated();
|
|
static::assertCount(1, $fs->allFiles());
|
|
static::assertDatabaseCount(Image::class, 1);
|
|
}
|
|
}
|