gruene-seele/tests/Feature/LogisticMailTest.php
Kevin Adametz a1f94ea427 Warenwirtschaft: Logistik-Mail regeneriert auch fehlende Rechnungs-PDF
Nachtrag zum Lieferschein-Fix (7d77314): Fehlte die Rechnungs-PDF auf der
Platte (Altdaten), brach MailLogistic::build() still ab — die Mail ging ohne
Anhänge bzw. gar nicht raus, obwohl das UI Erfolg meldete. Jetzt wird die
Rechnung wie der Lieferschein mit bestehender Rechnungsnummer regeneriert.
Hinweise-Doku und Plan-Protokoll fortgeschrieben.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-16 13:19:12 +00:00

188 lines
5.9 KiB
PHP

<?php
use App\Mail\MailLogistic;
use App\Models\Country;
use App\Models\Product;
use App\Models\Setting;
use App\Models\Shipping;
use App\Models\ShippingCountry;
use App\Models\ShoppingOrder;
use App\Models\ShoppingOrderItem;
use App\Models\ShoppingUser;
use App\Models\UserAccount;
use App\Repositories\InvoiceRepository;
use App\Services\Invoice;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
function lmMakeOrder(): ShoppingOrder
{
$country = Country::query()->forceCreate([
'code' => 'DE',
'en' => 'Germany',
'de' => 'Deutschland',
'es' => 'Alemania',
'fr' => 'Allemagne',
'it' => 'Germania',
'ru' => 'Germanija',
]);
$shipping = Shipping::query()->forceCreate(['name' => 'Standard']);
$shippingCountry = ShippingCountry::query()->forceCreate([
'shipping_id' => $shipping->id,
'country_id' => $country->id,
]);
$shoppingUser = ShoppingUser::query()->forceCreate([
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_firstname' => 'Lisa',
'billing_lastname' => 'Logistik',
'billing_address' => 'Rechnungsweg 1',
'billing_zipcode' => '11111',
'billing_city' => 'Rechnungsstadt',
'same_as_billing' => false,
'shipping_firstname' => 'Anders',
'shipping_lastname' => 'Empfänger',
'shipping_address' => 'Liefergasse 9',
'shipping_zipcode' => '99999',
'shipping_city' => 'Lieferdorf',
]);
$authUser = User::query()->create([
'email' => uniqid('lm_', true).'@test.example',
'password' => bcrypt('password'),
]);
$account = UserAccount::query()->create([
'm_first_name' => 'Lena',
'm_last_name' => 'Partner',
'country_id' => $country->id,
]);
$authUser->forceFill(['account_id' => $account->id])->save();
$order = ShoppingOrder::query()->forceCreate([
'shopping_user_id' => $shoppingUser->id,
'auth_user_id' => $authUser->id,
'country_id' => $shippingCountry->id,
'shipped' => 0,
'txaction' => 'paid',
'total' => 20,
'user_white_label' => false,
]);
$product = Product::query()->create([
'name' => 'LM Creme',
'title' => 'LM Creme',
'active' => true,
'is_set' => false,
'whitelabel' => false,
]);
ShoppingOrderItem::query()->forceCreate([
'shopping_order_id' => $order->id,
'product_id' => $product->id,
'qty' => 1,
'price' => 10,
]);
return $order->fresh();
}
function lmCreateDocuments(ShoppingOrder $order): void
{
Setting::setContentBySlug('invoice-number', 4000, 'int');
$cwd = getcwd();
chdir(public_path());
try {
(new InvoiceRepository($order))->create([]);
} finally {
chdir($cwd);
}
}
test('logistik-mail haengt den lieferschein immer an - auch ohne white-label', function () {
Storage::fake('public');
$order = lmMakeOrder();
lmCreateDocuments($order);
$order->refresh();
expect($order->user_white_label)->toBeFalse()
->and($order->hasWhitelabelProducts())->toBeFalse();
$mailable = new MailLogistic($order);
$mailable->build();
$names = collect($mailable->attachments)->pluck('options.as');
expect($names)->toHaveCount(2)
->and($names->first(fn ($n) => str_starts_with((string) $n, 'Rechnung-')))->not->toBeNull()
->and($names->first(fn ($n) => str_starts_with((string) $n, 'Lieferschein-')))->not->toBeNull();
});
test('fehlende lieferschein-datei wird fuer die mail regeneriert', function () {
Storage::fake('public');
$order = lmMakeOrder();
lmCreateDocuments($order);
$order->refresh();
$deliveryPath = Invoice::getDownloadPathDelivery($order);
Storage::disk('public')->delete($deliveryPath);
expect(Storage::disk('public')->exists($deliveryPath))->toBeFalse();
$cwd = getcwd();
chdir(public_path());
try {
$mailable = new MailLogistic($order);
$mailable->build();
} finally {
chdir($cwd);
}
expect(Storage::disk('public')->exists($deliveryPath))->toBeTrue();
$names = collect($mailable->attachments)->pluck('options.as');
expect($names->first(fn ($n) => str_starts_with((string) $n, 'Lieferschein-')))->not->toBeNull();
});
test('fehlende rechnungs-datei wird fuer die mail regeneriert', function () {
Storage::fake('public');
$order = lmMakeOrder();
lmCreateDocuments($order);
$order->refresh();
$invoicePath = Invoice::getDownloadPath($order);
Storage::disk('public')->delete($invoicePath);
expect(Storage::disk('public')->exists($invoicePath))->toBeFalse();
$cwd = getcwd();
chdir(public_path());
try {
$mailable = new MailLogistic($order);
$mailable->build();
} finally {
chdir($cwd);
}
expect(Storage::disk('public')->exists($invoicePath))->toBeTrue();
$names = collect($mailable->attachments)->pluck('options.as');
expect($names)->toHaveCount(2)
->and($names->first(fn ($n) => str_starts_with((string) $n, 'Rechnung-')))->not->toBeNull();
});
test('versand-button verschickt die logistik-mail', function () {
Mail::fake();
Storage::fake('public');
$order = lmMakeOrder();
lmCreateDocuments($order);
$admin = User::query()->create([
'email' => uniqid('lmadmin_', true).'@test.example',
'password' => bcrypt('password'),
]);
$admin->forceFill(['admin' => 7, 'confirmed' => true, 'active' => true, 'wizard' => 100, 'blocked' => false])->save();
$this->actingAs($admin->fresh())
->get(route('admin_sales_send_logistic_mail', $order->id))
->assertRedirect(route('admin_sales_detail', $order->id));
Mail::assertSent(MailLogistic::class);
});