HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/tests/Feature/CustomerSendingServerTypesTest.php
<?php

/**
 * Regression guard for "Choose a server type" page only listing 3 of 15
 * vendors (smtp / sendmail / gmail-oauth).
 *
 * Bug: Customer::getSendingServertypes() built a $typeToEntitlement map
 * keyed by vendor *family* ('amazon', 'sendgrid', ...) but the manifest
 * SendingServer::types() returns vendor+variant keys ('amazon-api',
 * 'amazon-smtp', ...). Lookup `$typeToEntitlement[$type] ?? null` returned
 * null for every *-api / *-smtp variant → entitlement check skipped → type
 * never reached the picker.
 */

use App\Model\Customer;
use App\Model\Plan;
use App\Model\PlanEntitlement;
use App\Model\SendingServer;
use App\Model\Subscription;
use App\Services\Plans\Entitlements\EntitlementKey;
use App\Services\Plans\Lifecycle\SubscriptionLifecycle;
use Tests\TestCase;

uses(TestCase::class);

function makeCustomerWithSendingEntitlements(array $allowKeys): Customer
{
    $plan = Plan::forceCreate([
        'uid'              => uniqid('sst_p_'),
        'name'             => 'sst-plan',
        'currency_id'      => 1,
        'frequency_amount' => 1,
        'frequency_unit'   => 'month',
        'price'            => 0,
        'status'           => 'active',
    ]);

    foreach (EntitlementKey::cases() as $ek) {
        PlanEntitlement::create([
            'plan_id'         => $plan->id,
            'entitlement_key' => $ek->value,
            'enabled'         => in_array($ek->value, $allowKeys, true),
        ]);
    }

    $customer = Customer::factory()->create();
    $sub = Subscription::create([
        'uid'                    => (string) \Illuminate\Support\Str::uuid(),
        'customer_id'            => $customer->id,
        'plan_id'                => $plan->id,
        'status'                 => 'active',
        'current_period_ends_at' => now()->addMonth(),
    ]);
    app(SubscriptionLifecycle::class)->onSubscribe($sub);

    return $customer;
}

test('all 15 driver-registered types appear when every ALLOW_SENDING_* entitlement is granted', function () {
    \App\Model\Setting::set('delivery.sendmail', 'yes');

    $allEnts = array_map(
        fn (EntitlementKey $k) => $k->value,
        array_filter(
            EntitlementKey::cases(),
            fn (EntitlementKey $k) => str_starts_with($k->value, 'allow_sending_')
        )
    );
    $customer = makeCustomerWithSendingEntitlements($allEnts);

    $types = array_keys($customer->getSendingServertypes());

    // All 15 core driver types must appear when every ALLOW_SENDING_* is on.
    // Plugin-registered drivers (e.g. rencontru-postal) may add extras —
    // assert presence, not exact equality.
    foreach ([
        SendingServer::TYPE_SMTP,
        SendingServer::TYPE_SENDMAIL,
        SendingServer::TYPE_AMAZON_API,
        SendingServer::TYPE_AMAZON_SMTP,
        SendingServer::TYPE_SENDGRID_API,
        SendingServer::TYPE_SENDGRID_SMTP,
        SendingServer::TYPE_MAILGUN_API,
        SendingServer::TYPE_MAILGUN_SMTP,
        SendingServer::TYPE_ELASTICEMAIL_API,
        SendingServer::TYPE_ELASTICEMAIL_SMTP,
        SendingServer::TYPE_SPARKPOST_API,
        SendingServer::TYPE_SPARKPOST_SMTP,
        SendingServer::TYPE_BLASTENGINE_API,
        SendingServer::TYPE_BLASTENGINE_SMTP,
        SendingServer::TYPE_GMAIL_OAUTH,
    ] as $expected) {
        expect($types)->toContain($expected);
    }
});

test('granting only ALLOW_SENDING_AMAZON exposes both amazon-api and amazon-smtp variants', function () {
    $customer = makeCustomerWithSendingEntitlements([
        EntitlementKey::ALLOW_SENDING_AMAZON->value,
    ]);

    $types = array_keys($customer->getSendingServertypes());

    expect($types)->toContain(SendingServer::TYPE_AMAZON_API);
    expect($types)->toContain(SendingServer::TYPE_AMAZON_SMTP);
    expect($types)->not->toContain(SendingServer::TYPE_SENDGRID_API);
    expect($types)->not->toContain(SendingServer::TYPE_MAILGUN_API);
});

test('disabled delivery.sendmail global setting hides sendmail type even when entitlement granted', function () {
    \App\Model\Setting::set('delivery.sendmail', 'no');

    $customer = makeCustomerWithSendingEntitlements([
        EntitlementKey::ALLOW_SENDING_SENDMAIL->value,
        EntitlementKey::ALLOW_SENDING_SMTP->value,
    ]);

    $types = array_keys($customer->getSendingServertypes());

    expect($types)->not->toContain(SendingServer::TYPE_SENDMAIL);
    expect($types)->toContain(SendingServer::TYPE_SMTP);
});

test('plugin-registered driver outside core entitlement map is allowed through', function () {
    \App\SendingServers\DriverRegistry::register(
        'fake-plugin-driver',
        \App\SendingServers\Drivers\Vendors\Generic\SmtpDriver::class
    );

    $customer = makeCustomerWithSendingEntitlements([
        EntitlementKey::ALLOW_SENDING_SMTP->value,
    ]);

    $types = array_keys($customer->getSendingServertypes());

    expect($types)->toContain('fake-plugin-driver');
    expect($types)->toContain(SendingServer::TYPE_SMTP);
});