Queue Weeztix/Mailwizz after the HTTP response and catch dispatch errors. Jobs log Mailwizz/Weeztix API failures without rethrowing so sync driver and terminating callbacks do not surface 500s after a successful create. Add JS fallback for non-JSON error responses, deployment note, and a regression test for failing Mailwizz under QUEUE_CONNECTION=sync. Made-with: Cursor
233 lines
8.3 KiB
PHP
233 lines
8.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Jobs\SyncSubscriberToMailwizz;
|
|
use App\Models\MailwizzConfig;
|
|
use App\Models\PreregistrationPage;
|
|
use App\Models\Subscriber;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\Client\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Str;
|
|
use Tests\TestCase;
|
|
|
|
class SyncSubscriberToMailwizzTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_subscribe_returns_ok_when_mailwizz_api_fails_under_sync_queue(): void
|
|
{
|
|
Http::fake([
|
|
'*' => Http::response(['status' => 'error', 'message' => 'service unavailable'], 503),
|
|
]);
|
|
|
|
$page = $this->makePageWithMailwizz();
|
|
|
|
$this->postJson(route('public.subscribe', ['publicPage' => $page->slug]), [
|
|
'first_name' => 'Broken',
|
|
'last_name' => 'Mailwizz',
|
|
'email' => 'broken-mailwizz@example.com',
|
|
])
|
|
->assertOk()
|
|
->assertJson(['success' => true]);
|
|
|
|
$subscriber = Subscriber::query()->where('email', 'broken-mailwizz@example.com')->first();
|
|
$this->assertNotNull($subscriber);
|
|
$this->assertFalse($subscriber->synced_to_mailwizz);
|
|
}
|
|
|
|
public function test_subscribe_with_mailwizz_config_runs_sync_create_path_and_marks_synced(): void
|
|
{
|
|
Http::fake(function (Request $request) {
|
|
$url = $request->url();
|
|
if (str_contains($url, 'search-by-email')) {
|
|
return Http::response(['status' => 'error']);
|
|
}
|
|
if ($request->method() === 'POST' && preg_match('#/lists/[^/]+/subscribers$#', $url) === 1) {
|
|
return Http::response(['status' => 'success']);
|
|
}
|
|
|
|
return Http::response(['status' => 'error'], 500);
|
|
});
|
|
|
|
$page = $this->makePageWithMailwizz();
|
|
|
|
$this->postJson(route('public.subscribe', ['publicPage' => $page->slug]), [
|
|
'first_name' => 'Ada',
|
|
'last_name' => 'Lovelace',
|
|
'email' => 'ada@example.com',
|
|
])->assertOk();
|
|
|
|
$subscriber = Subscriber::query()->where('email', 'ada@example.com')->first();
|
|
$this->assertNotNull($subscriber);
|
|
$this->assertTrue($subscriber->synced_to_mailwizz);
|
|
$this->assertNotNull($subscriber->synced_at);
|
|
}
|
|
|
|
public function test_subscribe_with_mailwizz_config_runs_sync_update_path_with_tag_merge(): void
|
|
{
|
|
Http::fake(function (Request $request) {
|
|
$url = $request->url();
|
|
if (str_contains($url, 'search-by-email')) {
|
|
return Http::response(['status' => 'success', 'data' => ['subscriber_uid' => 'sub-uid-1']]);
|
|
}
|
|
if ($request->method() === 'GET' && str_contains($url, '/subscribers/sub-uid-1') && ! str_contains($url, 'search-by-email')) {
|
|
return Http::response([
|
|
'status' => 'success',
|
|
'data' => [
|
|
'record' => [
|
|
'TAGS' => 'existing-one',
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
if ($request->method() === 'PUT' && str_contains($url, '/subscribers/sub-uid-1')) {
|
|
$body = $request->body();
|
|
$this->assertStringContainsString('TAGS', $body);
|
|
$this->assertStringContainsString('existing-one', $body);
|
|
$this->assertStringContainsString('new-source-tag', $body);
|
|
|
|
return Http::response(['status' => 'success']);
|
|
}
|
|
|
|
return Http::response(['status' => 'error'], 500);
|
|
});
|
|
|
|
$page = $this->makePageWithMailwizz([
|
|
'tag_field' => 'TAGS',
|
|
'tag_value' => 'new-source-tag',
|
|
]);
|
|
|
|
$this->postJson(route('public.subscribe', ['publicPage' => $page->slug]), [
|
|
'first_name' => 'Grace',
|
|
'last_name' => 'Hopper',
|
|
'email' => 'grace@example.com',
|
|
])->assertOk();
|
|
|
|
$subscriber = Subscriber::query()->where('email', 'grace@example.com')->first();
|
|
$this->assertNotNull($subscriber);
|
|
$this->assertTrue($subscriber->synced_to_mailwizz);
|
|
}
|
|
|
|
public function test_mailwizz_sync_sends_phone_with_e164_plus_prefix(): void
|
|
{
|
|
Http::fake(function (Request $request) {
|
|
$url = $request->url();
|
|
if (str_contains($url, 'search-by-email')) {
|
|
return Http::response(['status' => 'error']);
|
|
}
|
|
if ($request->method() === 'POST' && preg_match('#/lists/[^/]+/subscribers$#', $url) === 1) {
|
|
$body = $request->body();
|
|
$this->assertStringContainsString('PHONE', $body);
|
|
$this->assertTrue(
|
|
str_contains($body, '+31612345678') || str_contains($body, '%2B31612345678'),
|
|
'Expected E.164 phone with + in Mailwizz request body'
|
|
);
|
|
|
|
return Http::response(['status' => 'success']);
|
|
}
|
|
|
|
return Http::response(['status' => 'error'], 500);
|
|
});
|
|
|
|
$page = $this->makePageWithMailwizz([
|
|
'field_phone' => 'PHONE',
|
|
]);
|
|
$page->update(['phone_enabled' => true]);
|
|
|
|
$subscriber = Subscriber::query()->create([
|
|
'preregistration_page_id' => $page->id,
|
|
'first_name' => 'Test',
|
|
'last_name' => 'User',
|
|
'email' => 'phone-e164@example.com',
|
|
'phone' => '+31612345678',
|
|
'synced_to_mailwizz' => false,
|
|
]);
|
|
|
|
SyncSubscriberToMailwizz::dispatchSync($subscriber);
|
|
|
|
$subscriber->refresh();
|
|
$this->assertTrue($subscriber->synced_to_mailwizz);
|
|
}
|
|
|
|
public function test_mailwizz_sync_includes_coupon_code_when_mapped(): void
|
|
{
|
|
Http::fake(function (Request $request) {
|
|
$url = $request->url();
|
|
if (str_contains($url, 'search-by-email')) {
|
|
return Http::response(['status' => 'error']);
|
|
}
|
|
if ($request->method() === 'POST' && preg_match('#/lists/[^/]+/subscribers$#', $url) === 1) {
|
|
$body = $request->body();
|
|
$this->assertStringContainsString('COUPON', $body);
|
|
$this->assertStringContainsString('PREREG-TEST99', $body);
|
|
|
|
return Http::response(['status' => 'success']);
|
|
}
|
|
|
|
return Http::response(['status' => 'error'], 500);
|
|
});
|
|
|
|
$page = $this->makePageWithMailwizz([
|
|
'field_coupon_code' => 'COUPON',
|
|
]);
|
|
|
|
$subscriber = Subscriber::query()->create([
|
|
'preregistration_page_id' => $page->id,
|
|
'first_name' => 'Coupon',
|
|
'last_name' => 'User',
|
|
'email' => 'coupon-map@example.com',
|
|
'coupon_code' => 'PREREG-TEST99',
|
|
'synced_to_mailwizz' => false,
|
|
]);
|
|
|
|
SyncSubscriberToMailwizz::dispatchSync($subscriber);
|
|
|
|
$subscriber->refresh();
|
|
$this->assertTrue($subscriber->synced_to_mailwizz);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $configOverrides
|
|
*/
|
|
private function makePageWithMailwizz(array $configOverrides = []): PreregistrationPage
|
|
{
|
|
$user = User::factory()->create(['role' => 'user']);
|
|
|
|
$page = PreregistrationPage::query()->create([
|
|
'slug' => (string) Str::uuid(),
|
|
'user_id' => $user->id,
|
|
'title' => 'Fest',
|
|
'heading' => 'Join',
|
|
'intro_text' => null,
|
|
'thank_you_message' => null,
|
|
'expired_message' => null,
|
|
'ticketshop_url' => null,
|
|
'start_date' => now()->subHour(),
|
|
'end_date' => now()->addMonth(),
|
|
'phone_enabled' => false,
|
|
'is_active' => true,
|
|
]);
|
|
|
|
MailwizzConfig::query()->create(array_merge([
|
|
'preregistration_page_id' => $page->id,
|
|
'api_key' => 'fake-api-key',
|
|
'list_uid' => 'list-uid-1',
|
|
'list_name' => 'Main list',
|
|
'field_email' => 'EMAIL',
|
|
'field_first_name' => 'FNAME',
|
|
'field_last_name' => 'LNAME',
|
|
'field_phone' => null,
|
|
'field_coupon_code' => null,
|
|
'tag_field' => 'TAGS',
|
|
'tag_value' => 'preregister-source',
|
|
], $configOverrides));
|
|
|
|
return $page->fresh(['mailwizzConfig']);
|
|
}
|
|
}
|