feat: Phase 4 - Mailwizz integration with subscriber sync and retry
This commit is contained in:
122
tests/Feature/QueueUnsyncedMailwizzSubscribersTest.php
Normal file
122
tests/Feature/QueueUnsyncedMailwizzSubscribersTest.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
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\Artisan;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class QueueUnsyncedMailwizzSubscribersTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_artisan_dry_run_counts_unsynced_subscribers_with_mailwizz(): void
|
||||
{
|
||||
$page = $this->makePageWithMailwizz();
|
||||
Subscriber::query()->create([
|
||||
'preregistration_page_id' => $page->id,
|
||||
'first_name' => 'A',
|
||||
'last_name' => 'B',
|
||||
'email' => 'a@example.com',
|
||||
'synced_to_mailwizz' => false,
|
||||
]);
|
||||
|
||||
$exit = Artisan::call('mailwizz:queue-unsynced', ['--dry-run' => true]);
|
||||
$this->assertSame(0, $exit);
|
||||
$this->assertStringContainsString('1', Artisan::output());
|
||||
}
|
||||
|
||||
public function test_owner_can_queue_mailwizz_sync_from_subscribers_index(): 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);
|
||||
});
|
||||
|
||||
$user = User::factory()->create(['role' => 'user']);
|
||||
$page = $this->makePageWithMailwizzForUser($user);
|
||||
Subscriber::query()->create([
|
||||
'preregistration_page_id' => $page->id,
|
||||
'first_name' => 'A',
|
||||
'last_name' => 'B',
|
||||
'email' => 'syncme@example.com',
|
||||
'synced_to_mailwizz' => false,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->post(route('admin.pages.subscribers.queue-mailwizz-sync', $page));
|
||||
|
||||
$response->assertRedirect(route('admin.pages.subscribers.index', $page));
|
||||
$response->assertSessionHas('status');
|
||||
$this->assertDatabaseHas('subscribers', [
|
||||
'email' => 'syncme@example.com',
|
||||
'synced_to_mailwizz' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_other_user_cannot_queue_mailwizz_sync(): void
|
||||
{
|
||||
$owner = User::factory()->create(['role' => 'user']);
|
||||
$intruder = User::factory()->create(['role' => 'user']);
|
||||
$page = $this->makePageWithMailwizzForUser($owner);
|
||||
|
||||
$response = $this->actingAs($intruder)->post(route('admin.pages.subscribers.queue-mailwizz-sync', $page));
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
private function makePageWithMailwizz(): PreregistrationPage
|
||||
{
|
||||
$user = User::factory()->create(['role' => 'user']);
|
||||
|
||||
return $this->makePageWithMailwizzForUser($user);
|
||||
}
|
||||
|
||||
private function makePageWithMailwizzForUser(User $user): PreregistrationPage
|
||||
{
|
||||
$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([
|
||||
'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,
|
||||
'tag_field' => 'TAGS',
|
||||
'tag_value' => 'preregister-source',
|
||||
]);
|
||||
|
||||
return $page;
|
||||
}
|
||||
}
|
||||
131
tests/Feature/SyncSubscriberToMailwizzTest.php
Normal file
131
tests/Feature/SyncSubscriberToMailwizzTest.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
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_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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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,
|
||||
'tag_field' => 'TAGS',
|
||||
'tag_value' => 'preregister-source',
|
||||
], $configOverrides));
|
||||
|
||||
return $page->fresh(['mailwizzConfig']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user