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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user