Files
portal-api/tests/Feature/Api/Admin/ActivityLog/ActivityLogManagementTest.php
baodev0andClaude Opus 5.5 46aa138eec feat(admin): IP pool allocation, billing exports, service attachments
- IP pool: sheet import (xlsx/csv) matched by address, bulk allocate/revoke
  with notification email (ip_allocated/ip_revoked), ip_pool_logs history.
- Orders/invoices: Excel export on branded template (BrandedReportWriter),
  shared billing filters, order item service info in list responses.
- Web design quote Excel export; activity log export.
- Service attachments with copy-to-invoice; deployment invoice attachments.
- Customer email attachments stored on disk and attached by path.
- Admin password change endpoint; user.fullname backfill migration;
  orders.user_id backfill from logs.
- Disable deploy workflow until CI/CD is set up.
- Ignore stored customer email and service attachment uploads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 19:58:36 +07:00

104 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\Admin\ActivityLog;
use App\Models\ActivityLog;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Spatie\Permission\Models\Role;
use Tests\TestCase;
class ActivityLogManagementTest extends TestCase
{
use RefreshDatabase;
private User $admin;
protected function setUp(): void
{
parent::setUp();
Role::firstOrCreate(['name' => 'Full Administrator', 'guard_name' => 'admin']);
$this->admin = User::factory()->create();
$this->admin->assignRole('Full Administrator');
}
public function test_export_uses_branded_template_and_selected_filters(): void
{
$this->createLog(['log_name' => 'orders', 'description' => 'Tạo đơn hàng']);
$this->createLog(['log_name' => 'customers', 'description' => 'Cập nhật khách hàng']);
$response = $this->actingAs($this->admin, 'admin')
->get('/api/v1/administrator/activity-logs/export?log_name=orders')
->assertOk();
$sheet = IOFactory::load($response->baseResponse->getFile()->getPathname())->getActiveSheet();
$this->assertStringStartsWith('CÔNG TY', $sheet->getCell('C1')->getValue());
$this->assertCount(1, $sheet->getDrawingCollection());
$this->assertSame('LỊCH SỬ HOẠT ĐỘNG', $sheet->getCell('A6')->getValue());
$this->assertSame('ID', $sheet->getCell('A7')->getValue());
$this->assertSame('orders', $sheet->getCell('B8')->getValue());
$this->assertSame(8, $sheet->getHighestDataRow());
}
public function test_delete_by_date_range_keeps_logs_outside_range(): void
{
$selected = $this->createLog(['log_name' => 'orders']);
$remaining = $this->createLog(['log_name' => 'customers']);
$remaining->forceFill(['created_at' => now()->subDays(10)])->saveQuietly();
$this->actingAs($this->admin, 'admin')
->deleteJson('/api/v1/administrator/activity-logs', [
'mode' => 'date_range',
'date_from' => now()->toDateString(),
'date_to' => now()->toDateString(),
])
->assertOk();
$this->assertDatabaseMissing('activity_logs', ['id' => $selected->id]);
$this->assertDatabaseHas('activity_logs', ['id' => $remaining->id]);
}
public function test_delete_all_removes_every_log(): void
{
$this->createLog();
$this->createLog();
$total = ActivityLog::query()->count();
$this->actingAs($this->admin, 'admin')
->deleteJson('/api/v1/administrator/activity-logs', ['mode' => 'all'])
->assertOk()
->assertJsonPath('data.deleted', $total);
$this->assertDatabaseCount('activity_logs', 0);
}
public function test_delete_date_range_requires_valid_dates(): void
{
$this->actingAs($this->admin, 'admin')
->deleteJson('/api/v1/administrator/activity-logs', [
'mode' => 'date_range',
'date_from' => '2026-10-15',
'date_to' => '2026-10-14',
])
->assertUnprocessable();
}
private function createLog(array $attributes = []): ActivityLog
{
return ActivityLog::query()->create(array_merge([
'log_name' => 'system',
'event' => 'updated',
'description' => 'Thay đổi dữ liệu',
'causer_type' => 'admin',
'causer_id' => $this->admin->id,
'causer_name' => $this->admin->name,
'ip_address' => '127.0.0.1',
], $attributes));
}
}