feat: E.164 phone validation and storage with libphonenumber
- Add giggsey/libphonenumber-for-php, PhoneNumberNormalizer, ValidPhoneNumber rule - Store subscribers as E.164; mutator normalizes on save; optional phone required from form block - Migration to normalize legacy subscriber phones; Mailwizz/search/UI/tests updated - Add run-deploy-from-local.sh and PREREGISTER_DEFAULT_PHONE_REGION in .env.example Made-with: Cursor
This commit is contained in:
@@ -170,7 +170,7 @@ class PublicPageTest extends TestCase
|
||||
$response->assertJsonValidationErrors(['phone']);
|
||||
}
|
||||
|
||||
public function test_subscribe_normalizes_phone_to_digits(): void
|
||||
public function test_subscribe_stores_phone_as_e164(): void
|
||||
{
|
||||
$page = $this->makePage([
|
||||
'start_date' => now()->subHour(),
|
||||
@@ -189,7 +189,7 @@ class PublicPageTest extends TestCase
|
||||
$this->assertDatabaseHas('subscribers', [
|
||||
'preregistration_page_id' => $page->id,
|
||||
'email' => 'phoneuser@example.com',
|
||||
'phone' => '31612345678',
|
||||
'phone' => '+31612345678',
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Jobs\SyncSubscriberToMailwizz;
|
||||
use App\Models\MailwizzConfig;
|
||||
use App\Models\PreregistrationPage;
|
||||
use App\Models\Subscriber;
|
||||
@@ -91,6 +92,47 @@ class SyncSubscriberToMailwizzTest extends TestCase
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $configOverrides
|
||||
*/
|
||||
|
||||
29
tests/Unit/SubscriberPhoneDisplayTest.php
Normal file
29
tests/Unit/SubscriberPhoneDisplayTest.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Subscriber;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SubscriberPhoneDisplayTest extends TestCase
|
||||
{
|
||||
public function test_phone_display_keeps_e164_with_plus(): void
|
||||
{
|
||||
$subscriber = new Subscriber(['phone' => '+31613210095']);
|
||||
$this->assertSame('+31613210095', $subscriber->phoneDisplay());
|
||||
}
|
||||
|
||||
public function test_phone_display_prefixes_plus_for_legacy_digit_only_storage(): void
|
||||
{
|
||||
$subscriber = new Subscriber(['phone' => '31613210095']);
|
||||
$this->assertSame('+31613210095', $subscriber->phoneDisplay());
|
||||
}
|
||||
|
||||
public function test_phone_display_returns_null_when_empty(): void
|
||||
{
|
||||
$subscriber = new Subscriber(['phone' => null]);
|
||||
$this->assertNull($subscriber->phoneDisplay());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user