3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-08-18 03:06:23 +00:00
Files
snipe-it/tests/Feature/AssetModels/Ui/RestoreAssetModelTest.php

95 lines
3.4 KiB
PHP

<?php
namespace Tests\Feature\AssetModels\Ui;
use App\Models\Actionlog;
use App\Models\AssetModel;
use App\Models\User;
use Tests\TestCase;
/**
* Regression coverage for the double-logging bug on the web-side asset
* model restore endpoint. Two separate sources were writing action_log
* rows for a single restore:
*
* - The controller wrote a manual `restore` log after $model->restore().
* - AssetModelObserver's `restoring` hook also wrote a `restore` log.
* - AssetModelObserver's `updating` hook fired via save() and wrote an
* `update` log showing `deleted_at` flipping from a timestamp to null.
*
* Three log rows per restore instead of one. The manual controller write
* was removed, and the updating observer now filters `deleted_at` +
* `updated_at` out of its diff so it no longer fires on restore. Tests
* pin both behaviors down.
*/
class RestoreAssetModelTest extends TestCase
{
public function test_permission_required_to_restore_asset_model(): void
{
$model = AssetModel::factory()->create();
$model->delete();
$this->actingAs(User::factory()->create())
->post(route('models.restore.store', $model->id))
->assertForbidden();
}
public function test_soft_deleted_asset_model_can_be_restored_via_web(): void
{
$model = AssetModel::factory()->create();
$model->delete();
$this->assertNotNull($model->fresh()->deleted_at);
$this->actingAs(User::factory()->superuser()->create())
->post(route('models.restore.store', $model->id))
->assertRedirect()
->assertSessionHas('success');
$this->assertNull($model->fresh()->deleted_at);
}
public function test_restore_writes_exactly_one_restore_action_log(): void
{
$model = AssetModel::factory()->create();
$model->delete();
$before = Actionlog::where('item_type', AssetModel::class)
->where('item_id', $model->id)
->where('action_type', 'restore')
->count();
$this->actingAs(User::factory()->superuser()->create())
->post(route('models.restore.store', $model->id))
->assertRedirect();
$after = Actionlog::where('item_type', AssetModel::class)
->where('item_id', $model->id)
->where('action_type', 'restore')
->count();
$this->assertSame($before + 1, $after, 'Expected exactly one restore action_log entry (regression: was 2 pre-fix, one from the controller and one from the observer).');
}
public function test_restore_does_not_write_an_update_action_log(): void
{
$model = AssetModel::factory()->create();
$model->delete();
$before = Actionlog::where('item_type', AssetModel::class)
->where('item_id', $model->id)
->where('action_type', 'update')
->count();
$this->actingAs(User::factory()->superuser()->create())
->post(route('models.restore.store', $model->id))
->assertRedirect();
$after = Actionlog::where('item_type', AssetModel::class)
->where('item_id', $model->id)
->where('action_type', 'update')
->count();
$this->assertSame($before, $after, 'Expected no update action_log entry on restore (regression: was 1 pre-fix, from the updating observer catching deleted_at flipping to null via save()).');
}
}