Logging: Fixed FD-55757 - added log_meta for licenses and accessories
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Models\Location;
|
||||
use App\Models\Manufacturer;
|
||||
use App\Models\Supplier;
|
||||
use App\Models\User;
|
||||
use App\Models\Actionlog;
|
||||
use Tests\Concerns\TestsFullMultipleCompaniesSupport;
|
||||
use Tests\Concerns\TestsPermissionsRequirement;
|
||||
use Tests\TestCase;
|
||||
@@ -137,4 +138,47 @@ class UpdateAccessoryTest extends TestCase implements TestsFullMultipleCompanies
|
||||
$this->assertEquals($manufacturerB->id, $accessory->manufacturer_id);
|
||||
$this->assertEquals($supplierB->id, $accessory->supplier_id);
|
||||
}
|
||||
|
||||
public function test_update_logs_changed_fields_in_log_meta()
|
||||
{
|
||||
$accessory = Accessory::factory()->create(['qty' => 5, 'name' => 'Old Name']);
|
||||
|
||||
$this->actingAsForApi(User::factory()->editAccessories()->create())
|
||||
->patchJson(route('api.accessories.update', $accessory), ['qty' => 10, 'name' => 'New Name']);
|
||||
|
||||
$log = Actionlog::where('item_type', Accessory::class)
|
||||
->where('item_id', $accessory->id)
|
||||
->where('action_type', 'update')
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($log, 'No update log entry was created');
|
||||
$this->assertNotNull($log->log_meta, 'log_meta was not stored');
|
||||
|
||||
$meta = json_decode($log->log_meta, true);
|
||||
$this->assertEquals('5', $meta['qty']['old']);
|
||||
$this->assertEquals('10', $meta['qty']['new']);
|
||||
$this->assertEquals('Old Name', $meta['name']['old']);
|
||||
$this->assertEquals('New Name', $meta['name']['new']);
|
||||
}
|
||||
|
||||
public function test_no_op_update_does_not_create_log_entry()
|
||||
{
|
||||
$accessory = Accessory::factory()->create(['qty' => 5, 'name' => 'Same Name']);
|
||||
|
||||
$before = Actionlog::where('item_type', Accessory::class)
|
||||
->where('item_id', $accessory->id)
|
||||
->where('action_type', 'update')
|
||||
->count();
|
||||
|
||||
$this->actingAsForApi(User::factory()->editAccessories()->create())
|
||||
->patchJson(route('api.accessories.update', $accessory), ['qty' => 5, 'name' => 'Same Name']);
|
||||
|
||||
$after = Actionlog::where('item_type', Accessory::class)
|
||||
->where('item_id', $accessory->id)
|
||||
->where('action_type', 'update')
|
||||
->count();
|
||||
|
||||
$this->assertEquals($before, $after, 'A spurious log entry was created for a no-op update');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Tests\Feature\Accessories\Ui;
|
||||
|
||||
use App\Models\Accessory;
|
||||
use App\Models\Actionlog;
|
||||
use App\Models\Category;
|
||||
use App\Models\Company;
|
||||
use App\Models\Location;
|
||||
@@ -124,4 +125,67 @@ class UpdateAccessoryTest extends TestCase
|
||||
'notes' => 'A new note',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_logs_changed_fields_in_log_meta()
|
||||
{
|
||||
$accessory = Accessory::factory()->create([
|
||||
'qty' => 5,
|
||||
'name' => 'Old Name',
|
||||
'model_number' => null,
|
||||
'location_id' => null,
|
||||
]);
|
||||
|
||||
$this->actingAs(User::factory()->editAccessories()->create())
|
||||
->put(route('accessories.update', $accessory), [
|
||||
'redirect_option' => 'index',
|
||||
'name' => 'New Name',
|
||||
'qty' => '10',
|
||||
'category_id' => (string) $accessory->category_id,
|
||||
]);
|
||||
|
||||
$log = Actionlog::where('item_type', Accessory::class)
|
||||
->where('item_id', $accessory->id)
|
||||
->where('action_type', 'update')
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($log, 'No update log entry was created');
|
||||
$this->assertNotNull($log->log_meta, 'log_meta was not stored');
|
||||
|
||||
$meta = json_decode($log->log_meta, true);
|
||||
$this->assertEquals('5', $meta['qty']['old']);
|
||||
$this->assertEquals('10', $meta['qty']['new']);
|
||||
$this->assertEquals('Old Name', $meta['name']['old']);
|
||||
$this->assertEquals('New Name', $meta['name']['new']);
|
||||
}
|
||||
|
||||
public function test_no_op_update_does_not_create_log_entry()
|
||||
{
|
||||
$accessory = Accessory::factory()->create([
|
||||
'qty' => 5,
|
||||
'name' => 'Same Name',
|
||||
'model_number' => null,
|
||||
'location_id' => null,
|
||||
]);
|
||||
|
||||
$before = Actionlog::where('item_type', Accessory::class)
|
||||
->where('item_id', $accessory->id)
|
||||
->where('action_type', 'update')
|
||||
->count();
|
||||
|
||||
$this->actingAs(User::factory()->editAccessories()->create())
|
||||
->put(route('accessories.update', $accessory), [
|
||||
'redirect_option' => 'index',
|
||||
'name' => 'Same Name',
|
||||
'qty' => '5',
|
||||
'category_id' => (string) $accessory->category_id,
|
||||
]);
|
||||
|
||||
$after = Actionlog::where('item_type', Accessory::class)
|
||||
->where('item_id', $accessory->id)
|
||||
->where('action_type', 'update')
|
||||
->count();
|
||||
|
||||
$this->assertEquals($before, $after, 'A spurious log entry was created for a no-op update');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Licenses\Api;
|
||||
|
||||
use App\Models\Actionlog;
|
||||
use App\Models\Category;
|
||||
use App\Models\License;
|
||||
use App\Models\User;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UpdateLicenseTest extends TestCase
|
||||
{
|
||||
public function test_update_logs_changed_fields_in_log_meta()
|
||||
{
|
||||
$license = License::factory()->create(['name' => 'Old Name', 'seats' => 5]);
|
||||
|
||||
$this->actingAsForApi(User::factory()->editLicenses()->create())
|
||||
->patchJson(route('api.licenses.update', $license), [
|
||||
'name' => 'New Name',
|
||||
'seats' => 10,
|
||||
'category_id' => $license->category_id,
|
||||
]);
|
||||
|
||||
$log = Actionlog::where('item_type', License::class)
|
||||
->where('item_id', $license->id)
|
||||
->where('action_type', 'update')
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($log, 'No update log entry was created');
|
||||
$this->assertNotNull($log->log_meta, 'log_meta was not stored');
|
||||
|
||||
$meta = json_decode($log->log_meta, true);
|
||||
$this->assertEquals('Old Name', $meta['name']['old']);
|
||||
$this->assertEquals('New Name', $meta['name']['new']);
|
||||
}
|
||||
|
||||
public function test_no_op_update_does_not_create_log_entry()
|
||||
{
|
||||
$license = License::factory()->create(['name' => 'Same Name']);
|
||||
|
||||
$before = Actionlog::where('item_type', License::class)
|
||||
->where('item_id', $license->id)
|
||||
->where('action_type', 'update')
|
||||
->count();
|
||||
|
||||
$this->actingAsForApi(User::factory()->editLicenses()->create())
|
||||
->patchJson(route('api.licenses.update', $license), [
|
||||
'name' => 'Same Name',
|
||||
'category_id' => $license->category_id,
|
||||
]);
|
||||
|
||||
$after = Actionlog::where('item_type', License::class)
|
||||
->where('item_id', $license->id)
|
||||
->where('action_type', 'update')
|
||||
->count();
|
||||
|
||||
$this->assertEquals($before, $after, 'A spurious log entry was created for a no-op update');
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Tests\Feature\Licenses\Ui;
|
||||
|
||||
use App\Models\Actionlog;
|
||||
use App\Models\Category;
|
||||
use App\Models\License;
|
||||
use App\Models\User;
|
||||
@@ -99,4 +100,63 @@ class UpdateLicenseTest extends TestCase
|
||||
$this->assertEquals($license->licenseseats()->count(), $license->seats);
|
||||
$this->assertEquals($license->licenseseats()->count(), 5000);
|
||||
}
|
||||
|
||||
public function test_update_logs_changed_fields_in_log_meta()
|
||||
{
|
||||
$license = License::factory()->create(['name' => 'Old Name', 'seats' => 5]);
|
||||
|
||||
$this->actingAs(User::factory()->editLicenses()->create())
|
||||
->put(route('licenses.update', $license), [
|
||||
'name' => 'New Name',
|
||||
'seats' => 10,
|
||||
'category_id' => $license->category_id,
|
||||
]);
|
||||
|
||||
$log = Actionlog::where('item_type', License::class)
|
||||
->where('item_id', $license->id)
|
||||
->where('action_type', 'update')
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($log, 'No update log entry was created');
|
||||
$this->assertNotNull($log->log_meta, 'log_meta was not stored');
|
||||
|
||||
$meta = json_decode($log->log_meta, true);
|
||||
$this->assertEquals('Old Name', $meta['name']['old']);
|
||||
$this->assertEquals('New Name', $meta['name']['new']);
|
||||
}
|
||||
|
||||
public function test_no_op_update_does_not_create_log_entry()
|
||||
{
|
||||
$license = License::factory()->create([
|
||||
'name' => 'Same Name',
|
||||
'seats' => 5,
|
||||
'license_email' => null,
|
||||
'notes' => null,
|
||||
'order_number' => null,
|
||||
'purchase_date' => null,
|
||||
'reassignable' => 0,
|
||||
'serial' => null,
|
||||
'supplier_id' => null,
|
||||
]);
|
||||
|
||||
$before = Actionlog::where('item_type', License::class)
|
||||
->where('item_id', $license->id)
|
||||
->where('action_type', 'update')
|
||||
->count();
|
||||
|
||||
$this->actingAs(User::factory()->editLicenses()->create())
|
||||
->put(route('licenses.update', $license), [
|
||||
'name' => 'Same Name',
|
||||
'seats' => 5,
|
||||
'category_id' => $license->category_id,
|
||||
]);
|
||||
|
||||
$after = Actionlog::where('item_type', License::class)
|
||||
->where('item_id', $license->id)
|
||||
->where('action_type', 'update')
|
||||
->count();
|
||||
|
||||
$this->assertEquals($before, $after, 'A spurious log entry was created for a no-op update');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user