Files
snipe-it/tests/Feature/Maintenances/Api/CreateMaintenanceTest.php
T
snipe c570f656bf Renamed test
Signed-off-by: snipe <snipe@snipe.net>
2025-08-10 12:27:48 +01:00

73 lines
2.3 KiB
PHP

<?php
namespace Tests\Feature\Maintenances\Api;
use App\Models\Asset;
use App\Models\Maintenance;
use App\Models\Category;
use App\Models\Supplier;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class CreateMaintenanceTest extends TestCase
{
public function testRequiresPermissionToCreateMaintenance()
{
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.maintenances.store'))
->assertForbidden();
}
public function testCanCreateMaintenance()
{
Storage::fake('public');
$actor = User::factory()->superuser()->create();
$asset = Asset::factory()->create();
$supplier = Supplier::factory()->create();
$response = $this->actingAsForApi($actor)
->postJson(route('api.maintenances.store'), [
'name' => 'Test Maintenance',
'asset_id' => $asset->id,
'supplier_id' => $supplier->id,
'asset_maintenance_type' => 'Maintenance',
'start_date' => '2021-01-01',
'completion_date' => '2021-01-10',
'is_warranty' => '1',
'cost' => '100.00',
'image' => UploadedFile::fake()->image('test_image.png'),
'notes' => 'A note',
])
->assertOk()
->assertStatus(200);
// Since we rename the file in the ImageUploadRequest, we have to fetch the record from the database
$assetMaintenance = Maintenance::where('title', 'Test Maintenance')->first();
// Assert file was stored...
Storage::disk('public')->assertExists(app('maintenances_path').$assetMaintenance->image);
$this->assertDatabaseHas('asset_maintenances', [
'asset_id' => $asset->id,
'supplier_id' => $supplier->id,
'asset_maintenance_type' => 'Maintenance',
'name' => 'Test Maintenance',
'is_warranty' => 1,
'start_date' => '2021-01-01',
'completion_date' => '2021-01-10',
'notes' => 'A note',
'image' => $assetMaintenance->image,
'created_by' => $actor->id,
]);
}
}