'Full Administrator', 'guard_name' => 'admin']); $this->admin = User::factory()->create(); $this->admin->assignRole('Full Administrator'); $customer = Customer::factory()->create(); $order = Order::create([ 'order_num' => 'ORD-' . uniqid(), 'customer_id' => $customer->id, 'sub_total' => 100_000, 'total' => 100_000, 'status' => OrderStatus::PENDING->value, ]); $this->invoice = Invoice::create([ 'invoice_number' => 'INV-' . uniqid(), 'customer_id' => $customer->id, 'order_id' => $order->id, 'invoice_type' => 1, 'sub_total' => 100_000, 'tax' => 0, 'total' => 100_000, 'status' => InvoiceStatus::UNPAID->value, 'due_date' => now()->addDays(5)->toDateString(), ]); } private function sendMail(bool $mailEnabled) { Queue::fake(); Setting::updateOrCreate(['key' => 'email_settings'], ['value' => json_encode(['mail_enabled' => $mailEnabled])]); return $this->actingAs($this->admin, 'admin') ->postJson("/api/v1/administrator/invoices/{$this->invoice->id}/send-mail", ['type' => 'invoice', 'message' => 'Hi']); } public function test_send_mail_is_queued(): void { $this->sendMail(true)->assertOk(); Queue::assertPushed(SendInvoiceMailJob::class, fn ($job) => $job->invoiceId === $this->invoice->id && $job->sentBy === $this->admin->id && $job->html === null); } public function test_send_mail_is_rejected_when_mail_is_disabled(): void { $this->sendMail(false)->assertStatus(422); Queue::assertNothingPushed(); } public function test_job_sends_invoice_mail_and_logs_it(): void { $this->seed(\Database\Seeders\TemplateSeeder::class); Mail::fake(); dispatch_sync(new SendInvoiceMailJob($this->invoice->id, 'invoice', 'Hi', null, null, $this->admin->id)); Mail::assertSent(InvoiceNotificationMail::class); $this->assertDatabaseHas('mail', ['order_id' => $this->invoice->order_id, 'sent_by' => $this->admin->id]); } private function contract(Invoice $invoice, string $type = InvoiceAttachment::TYPE_CONTRACT): InvoiceAttachment { Storage::fake('public'); Storage::disk('public')->put('invoice-attachments/c.pdf', '%PDF-contract'); return InvoiceAttachment::create([ 'invoice_id' => $invoice->id, 'type' => $type, 'filename' => 'Hop-dong.pdf', 'path' => 'invoice-attachments/c.pdf', 'disk' => 'public', 'mime_type' => 'application/pdf', 'size' => 13, 'uploaded_by' => 'admin', ]); } public function test_admin_can_open_invoice_pdf_and_vat_variant_needs_a_vat_number(): void { $this->actingAs($this->admin, 'admin')->get("/api/v1/administrator/invoices/{$this->invoice->id}/pdf") ->assertOk()->assertHeader('content-type', 'application/pdf'); $this->actingAs($this->admin, 'admin')->getJson("/api/v1/administrator/invoices/{$this->invoice->id}/pdf?variant=vat") ->assertStatus(422); $this->invoice->update(['vat_serial' => '1C26TCP', 'vat_number' => '00000007', 'vat_issued_at' => now()->toDateString()]); $this->actingAs($this->admin, 'admin')->get("/api/v1/administrator/invoices/{$this->invoice->id}/pdf?variant=vat") ->assertOk()->assertHeader('content-disposition', 'inline; filename="Hoa-don-VAT-1C26TCP-00000007.pdf"'); } public function test_preview_lists_vat_file_and_contracts_only(): void { $contract = $this->contract($this->invoice); $this->contract($this->invoice, InvoiceAttachment::TYPE_DEPLOYMENT); $this->seed(\Database\Seeders\TemplateSeeder::class); $this->actingAs($this->admin, 'admin') ->postJson("/api/v1/administrator/invoices/{$this->invoice->id}/send-mail/preview", ['type' => 'invoice']) ->assertOk() ->assertJsonPath('data.vat_attachment_filename', null) ->assertJsonCount(1, 'data.contracts') ->assertJsonPath('data.contracts.0.id', $contract->id); } public function test_send_rejects_vat_attachment_without_vat_number_and_ignores_foreign_contracts(): void { Queue::fake(); $this->actingAs($this->admin, 'admin') ->postJson("/api/v1/administrator/invoices/{$this->invoice->id}/send-mail", ['attach_vat' => true]) ->assertStatus(422); $other = Invoice::create([ 'invoice_number' => 'INV-OTHER', 'customer_id' => $this->invoice->customer_id, 'invoice_type' => 1, 'sub_total' => 1, 'tax' => 0, 'total' => 1, 'status' => InvoiceStatus::UNPAID->value, ]); $foreign = $this->contract($other); $mine = $this->contract($this->invoice); $this->actingAs($this->admin, 'admin') ->postJson("/api/v1/administrator/invoices/{$this->invoice->id}/send-mail", ['contract_ids' => [$foreign->id, $mine->id]]) ->assertOk(); Queue::assertPushed(SendInvoiceMailJob::class, fn ($job) => $job->contractIds === [$mine->id]); } public function test_job_attaches_vat_pdf_and_contract_files(): void { $this->seed(\Database\Seeders\TemplateSeeder::class); $this->invoice->update(['vat_serial' => 'AB', 'vat_number' => '0001', 'vat_issued_at' => now()->toDateString()]); $contract = $this->contract($this->invoice); Mail::fake(); dispatch_sync(new SendInvoiceMailJob($this->invoice->id, 'invoice', 'Hi', null, null, $this->admin->id, true, [$contract->id])); Mail::assertSent(InvoiceNotificationMail::class, function (InvoiceNotificationMail $mail) { $names = collect($mail->rawAttachments)->pluck('name')->merge(collect($mail->diskAttachments)->pluck('name')); return $names->contains('Hoa-don-VAT-AB-0001.pdf') && $names->contains('Hop-dong.pdf'); }); } }