trypost/tests/Feature/Actions/User/CreateUserTest.php
Paulo Castellano cd28ac4025 feat: explicit POSTHOG_ENABLED gate for self-hosted safety
Self-hosted installs that inherited POSTHOG_API_KEY from an example or
older deploy were still seeing SyncUser/SendEvent jobs run because the
gate was based on the api key alone. Switches the gate to an explicit
'services.posthog.enabled' flag (env: POSTHOG_ENABLED, default false)
and requires both enabled=true AND api_key for tracking to fire.

Backend gating:
- PostHogService::isEnabled() — single static helper used everywhere.
- AppServiceProvider::configurePostHog — skips PostHog::init when off.
- CreateUser::execute — does not enqueue SyncUser when off.
- SyncUser::handle, TrackBilling::handle, SendEvent::handle — early
  return before any DB query so the queue worker does no work.

Frontend gating:
- New VITE_POSTHOG_ENABLED env var mirrored from POSTHOG_ENABLED.
- initializePostHog, syncPostHogContext, capturePageview all gated.

Tests updated to set both flags on the happy path; adds explicit
'CreateUser does not dispatch SyncUser when PostHog is disabled'.

Deploy note: the trypost.it cloud .env must set POSTHOG_ENABLED=true
before this branch is merged or analytics will go dark.
2026-05-07 12:42:35 -03:00

75 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
use App\Actions\User\CreateUser;
use App\Jobs\PostHog\SyncUser;
use App\Models\Account;
use App\Models\Workspace;
use Illuminate\Support\Facades\Bus;
test('CreateUser creates user with account but no workspace and no current_workspace_id', function () {
$user = CreateUser::execute([
'name' => 'Jane Doe',
'email' => 'jane@example.com',
'password' => 'secret123',
]);
expect($user->name)->toBe('Jane Doe');
expect($user->email)->toBe('jane@example.com');
expect($user->current_workspace_id)->toBeNull();
expect($user->account_id)->not->toBeNull();
expect(Workspace::count())->toBe(0);
expect(Account::find($user->account_id))->not->toBeNull();
});
test('CreateUser sets account owner_id to the new user', function () {
$user = CreateUser::execute([
'name' => 'Jane Doe',
'email' => 'jane2@example.com',
'password' => 'secret123',
]);
expect($user->account->owner_id)->toBe($user->id);
});
test('CreateUser invite-style still creates user without workspace (workspace assignment happens via invite acceptance)', function () {
$user = CreateUser::execute([
'name' => 'Invited',
'email' => 'invited@example.com',
'password' => 'secret123',
'is_invite' => true,
]);
expect($user->email_verified_at)->not->toBeNull();
expect(Workspace::count())->toBe(0);
});
test('CreateUser dispatches SyncUser with the new user id when PostHog is enabled', function () {
config(['services.posthog.enabled' => true, 'services.posthog.api_key' => 'phc_test_key']);
Bus::fake([SyncUser::class]);
$user = CreateUser::execute([
'name' => 'Jane Doe',
'email' => 'jane.posthog@example.com',
'password' => 'secret123',
]);
Bus::assertDispatched(
SyncUser::class,
fn ($job) => $job->userId === (string) $user->id,
);
});
test('CreateUser does not dispatch SyncUser when PostHog is disabled', function () {
config(['services.posthog.enabled' => false]);
Bus::fake([SyncUser::class]);
CreateUser::execute([
'name' => 'Jane Doe',
'email' => 'jane.posthog.disabled@example.com',
'password' => 'secret123',
]);
Bus::assertNotDispatched(SyncUser::class);
});