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/AccountManagement/AccountProvisioningSmokeTest.php
<?php

namespace Tests\Feature\AccountManagement;

use App\Services\AccountManagement\AccountProvisioningService;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class AccountProvisioningSmokeTest extends TestCase
{
    protected string $dbPath;

    protected int $languageId;
    protected int $roleId;
    protected string $roleUid;

    protected function setUp(): void
    {
        parent::setUp();

        $this->dbPath = '/tmp/account_provisioning_smoke_' . uniqid() . '.sqlite';
        touch($this->dbPath);

        config()->set('database.default', 'mysql');
        config()->set('database.connections.mysql.driver', 'sqlite');
        config()->set('database.connections.mysql.database', $this->dbPath);
        config()->set('database.connections.mysql.prefix', '');
        config()->set('database.connections.mysql.foreign_key_constraints', false);

        DB::purge('mysql');
        DB::setDefaultConnection('mysql');
        DB::reconnect('mysql');
        $this->app['validation.presence']->setConnection('mysql');

        $this->createSchema();
        $this->seedDefaults();
    }

    protected function tearDown(): void
    {
        DB::disconnect('mysql');

        if (isset($this->dbPath) && file_exists($this->dbPath)) {
            unlink($this->dbPath);
        }

        parent::tearDown();
    }

    protected function createSchema(): void
    {
        Schema::connection('mysql')->create('settings', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->text('value')->nullable();
            $table->timestamps();
        });

        Schema::connection('mysql')->create('languages', function (Blueprint $table) {
            $table->increments('id');
            $table->string('uid')->nullable();
            $table->string('name');
            $table->string('code')->nullable();
            $table->string('region_code')->nullable();
            $table->string('status')->nullable();
            $table->timestamps();
        });

        Schema::connection('mysql')->create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('uid')->nullable();
            $table->string('name');
            $table->string('code')->nullable();
            $table->text('description')->nullable();
            $table->string('status');
            $table->boolean('readonly')->default(false);
            $table->boolean('is_global')->default(true);
            $table->unsignedInteger('customer_id')->nullable();
            $table->boolean('default')->default(false);
            $table->timestamps();
        });

        Schema::connection('mysql')->create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('uid')->nullable();
            $table->unsignedInteger('customer_id')->nullable();
            $table->string('email')->unique();
            $table->string('password');
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('phone')->nullable();
            $table->boolean('activated')->default(true);
            $table->string('status')->default('active');
            $table->string('api_token')->nullable();
            $table->timestamps();
        });

        Schema::connection('mysql')->create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->string('uid')->nullable();
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('admin_group_id');
            $table->unsignedInteger('creator_id')->nullable();
            $table->string('status')->default('active');
            $table->string('timezone')->nullable();
            $table->unsignedInteger('language_id')->nullable();
            $table->string('menu_layout')->nullable();
            $table->string('create_customer_account')->nullable();
            $table->timestamps();
        });

        Schema::connection('mysql')->create('customers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('uid')->nullable();
            $table->unsignedInteger('admin_id')->nullable();
            $table->unsignedInteger('contact_id')->nullable();
            $table->string('name')->nullable();
            $table->string('status')->default('active');
            $table->string('timezone')->nullable();
            $table->unsignedInteger('language_id')->nullable();
            $table->string('menu_layout')->nullable();
            $table->timestamps();
        });

        Schema::connection('mysql')->create('user_role', function (Blueprint $table) {
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('role_id');
            $table->timestamps();
        });

        Schema::connection('mysql')->create('webhooks', function (Blueprint $table) {
            $table->increments('id');
            $table->string('uid')->nullable();
            $table->string('event')->nullable();
            $table->string('status')->nullable();
            $table->string('endpoint')->nullable();
            $table->timestamps();
        });
    }

    protected function seedDefaults(): void
    {
        DB::connection('mysql')->table('settings')->insert([
            ['name' => 'layout.menu_bar', 'value' => 'left', 'created_at' => now(), 'updated_at' => now()],
            ['name' => 'user.require_mobile_phone', 'value' => 'no', 'created_at' => now(), 'updated_at' => now()],
        ]);

        $this->languageId = DB::connection('mysql')->table('languages')->insertGetId([
            'uid' => uniqid(),
            'name' => 'English',
            'code' => 'en',
            'region_code' => 'US',
            'status' => 'active',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $this->roleUid = uniqid('role_');
        $this->roleId = DB::connection('mysql')->table('roles')->insertGetId([
            'uid' => $this->roleUid,
            'name' => 'Organization Admin',
            'code' => 'organization_admin',
            'status' => 'active',
            'readonly' => 0,
            'is_global' => 1,
            'customer_id' => null,
            'default' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    public function test_smoke_create_customer_persists_records()
    {
        $service = app(AccountProvisioningService::class);
        $email = 'customer.smoke.' . uniqid() . '@example.com';
        $this->assertSame(0, DB::connection('mysql')->table('users')->count());

        [$validator, $customer, $user] = $service->createCustomer(
            null,
            'Acme Customer',
            'UTC',
            $this->languageId,
            $email,
            'secret123',
            'secret123',
            'John',
            'Customer',
            null,
            $this->roleUid,
            null
        );

        $this->assertTrue($validator->errors()->isEmpty(), json_encode($validator->errors()->toArray()));
        $this->assertDatabaseHas('customers', ['id' => $customer->id, 'name' => 'Acme Customer']);
        $this->assertDatabaseHas('users', ['id' => $user->id, 'email' => $email, 'customer_id' => $customer->id]);
        $this->assertDatabaseHas('user_role', ['user_id' => $user->id, 'role_id' => $this->roleId]);
    }

    public function test_smoke_create_admin_persists_records()
    {
        $service = app(AccountProvisioningService::class);
        $email = 'admin.smoke.' . uniqid() . '@example.com';
        $this->assertSame(0, DB::connection('mysql')->table('users')->count());

        [$validator, $admin, $user] = $service->createAdmin(
            $email,
            'secret123',
            'secret123',
            'Alice',
            'Admin',
            null,
            3,
            null,
            'UTC',
            $this->languageId,
            null
        );

        $this->assertTrue($validator->errors()->isEmpty(), json_encode($validator->errors()->toArray()));
        $this->assertDatabaseHas('users', ['id' => $user->id, 'email' => $email]);
        $this->assertDatabaseHas('admins', ['id' => $admin->id, 'user_id' => $user->id, 'admin_group_id' => 3]);
        $this->assertDatabaseHas('user_role', ['user_id' => $user->id, 'role_id' => $this->roleId]);
    }

    public function test_smoke_create_install_account_persists_linked_records()
    {
        $service = app(AccountProvisioningService::class);
        $email = 'install.smoke.' . uniqid() . '@example.com';

        [$user, $admin, $customer] = $service->createInstallAccount([
            'email' => $email,
            'password' => 'secret123',
            'first_name' => 'Install',
            'last_name' => 'Owner',
            'timezone' => 'UTC',
            'name' => 'Install Customer',
        ], $this->languageId);

        $this->assertDatabaseHas('users', ['id' => $user->id, 'customer_id' => $customer->id]);
        $this->assertDatabaseHas('admins', ['id' => $admin->id, 'user_id' => $user->id]);
        $this->assertDatabaseHas('customers', ['id' => $customer->id, 'admin_id' => $admin->id, 'name' => 'Install Customer']);
        $this->assertDatabaseHas('user_role', ['user_id' => $user->id, 'role_id' => $this->roleId]);
    }
}