'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)); } }