Files
coolify/tests/Feature/AuditChangedFieldsTest.php
Andras Bacsai 16a8c7999d feat(audit): log account, resource, and instance settings changes
Add audit events for account actions, server and application settings,
services, destinations, storage, backup schedules, and scheduled tasks.
Track changed fields and skip unchanged saves without logging file content.

Add regression coverage for audit events and changed-field detection.
2026-10-04 17:32:50 +02:00

59 lines
1.6 KiB
PHP

<?php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Crypt;
/**
* Builds an unsaved model whose stored state is the given raw attributes.
*
* @param array<string, mixed> $attributes
*/
function auditChangedFieldsModel(array $attributes): Model
{
$model = new class extends Model
{
protected $casts = [
'secret' => 'encrypted',
'labels' => 'array',
];
};
$model->setRawAttributes($attributes, true);
return $model;
}
it('ignores timestamps and loosely equal values', function () {
$model = auditChangedFieldsModel(['flag' => 0, 'name' => 'web', 'updated_at' => '2026-01-01 00:00:00']);
$model->flag = false;
$model->name = 'web';
$model->updated_at = '2026-02-01 00:00:00';
expect(auditChangedFields($model))->toBe([]);
$model->flag = true;
$model->name = 'api';
expect(auditChangedFields($model))->toBe(['flag', 'name']);
});
it('compares decrypted values of encrypted attributes', function () {
$model = auditChangedFieldsModel(['secret' => Crypt::encryptString('token')]);
$model->secret = 'token';
expect(auditChangedFields($model))->toBe([]);
$model->secret = 'other';
expect(auditChangedFields($model))->toBe(['secret']);
});
it('compares decoded values of array attributes', function () {
$model = auditChangedFieldsModel(['labels' => json_encode(['linux', 'x64'])]);
$model->labels = ['linux', 'x64'];
expect(auditChangedFields($model))->toBe([]);
$model->labels = ['linux', 'arm64'];
expect(auditChangedFields($model))->toBe(['labels']);
});