128 lines
4.8 KiB
PHP
128 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Models;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Modules\Company\Models\Company;
|
|
use Modules\Platform\app\Models\LicenseKey;
|
|
use Modules\Platform\app\Models\Payment;
|
|
use Modules\Platform\app\Models\PlatformPrice;
|
|
use Modules\Warehouse\app\Models\Warehouse;
|
|
use Modules\Warehouse\app\Models\WarehouseValidationRule;
|
|
use Tests\TestCase;
|
|
|
|
class PlatformAndWarehouseModelsTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
/**
|
|
* Ensure company defaults are generated and relationships are wired.
|
|
*/
|
|
public function test_company_defaults_relationships_and_active_license_logic(): void
|
|
{
|
|
$company = Company::create(['name' => 'Model Tenant']);
|
|
$user = User::factory()->create(['company_id' => $company->id]);
|
|
$warehouse = Warehouse::create(['company_id' => $company->id, 'code' => 'M1', 'name' => 'Model Warehouse']);
|
|
$payment = Payment::create(['company_id' => $company->id, 'amount' => 1000, 'currency' => 'gbp', 'status' => 'paid']);
|
|
|
|
$this->assertNotNull($company->uuid);
|
|
$this->assertSame('model-tenant', $company->slug);
|
|
$this->assertSame('active', $company->status);
|
|
$this->assertSame('Europe/London', $company->timezone);
|
|
$this->assertFalse($company->hasActiveLicense());
|
|
|
|
LicenseKey::issueFor($company, 'Model licence');
|
|
|
|
$this->assertTrue($company->fresh()->hasActiveLicense());
|
|
$this->assertTrue($company->users->contains($user));
|
|
$this->assertTrue($company->warehouses->contains($warehouse));
|
|
$this->assertTrue($company->payments->contains($payment));
|
|
$this->assertCount(1, $company->licenseKeys);
|
|
}
|
|
|
|
/**
|
|
* Ensure system companies bypass licence checks.
|
|
*/
|
|
public function test_system_company_is_always_licensed(): void
|
|
{
|
|
$company = Company::create(['name' => 'MPBA Limited', 'slug' => 'mpba-limited', 'is_system_company' => true]);
|
|
|
|
$this->assertTrue($company->hasActiveLicense());
|
|
}
|
|
|
|
/**
|
|
* Ensure revoked or expired licences do not count as active.
|
|
*/
|
|
public function test_inactive_expired_or_revoked_licenses_are_not_active(): void
|
|
{
|
|
$company = Company::create(['name' => 'Expired Tenant', 'slug' => 'expired-tenant']);
|
|
|
|
LicenseKey::create([
|
|
'company_id' => $company->id,
|
|
'key_hash' => hash('sha256', 'expired'),
|
|
'key_prefix' => 'expired',
|
|
'label' => 'Expired',
|
|
'plan' => 'standard',
|
|
'max_users' => 10,
|
|
'is_active' => true,
|
|
'starts_at' => now()->subYears(2),
|
|
'expires_at' => now()->subDay(),
|
|
]);
|
|
|
|
$this->assertFalse($company->hasActiveLicense());
|
|
}
|
|
|
|
/**
|
|
* Ensure user helper methods work for names and platform admins.
|
|
*/
|
|
public function test_user_initials_and_platform_admin_detection(): void
|
|
{
|
|
$admin = User::factory()->mpbaPlatformAdmin()->create(['name' => 'Mark Gregory']);
|
|
$ordinary = User::factory()->create(['name' => 'Warehouse Operator']);
|
|
|
|
$this->assertSame('MG', $admin->initials());
|
|
$this->assertTrue($admin->isMpbaPlatformAdmin());
|
|
$this->assertSame('WO', $ordinary->initials());
|
|
$this->assertFalse($ordinary->isMpbaPlatformAdmin());
|
|
}
|
|
|
|
/**
|
|
* Ensure warehouse defaults and validation rule relationships work.
|
|
*/
|
|
public function test_warehouse_defaults_and_validation_rule_relationships(): void
|
|
{
|
|
$company = Company::create(['name' => 'Warehouse Model', 'slug' => 'warehouse-model']);
|
|
$warehouse = Warehouse::create(['company_id' => $company->id, 'code' => 'WM', 'name' => 'Warehouse Model']);
|
|
$rule = WarehouseValidationRule::create([
|
|
'warehouse_id' => $warehouse->id,
|
|
'name' => 'Check quantity',
|
|
'rule_key' => 'quantity-check',
|
|
'severity' => 'warning',
|
|
'configuration' => ['minimum' => 1],
|
|
]);
|
|
|
|
$this->assertNotNull($warehouse->uuid);
|
|
$this->assertSame('active', $warehouse->status);
|
|
$this->assertSame('Europe/London', $warehouse->timezone);
|
|
$this->assertTrue($warehouse->company->is($company));
|
|
$this->assertTrue($warehouse->validationRules->contains($rule));
|
|
$this->assertTrue($rule->warehouse->is($warehouse));
|
|
$this->assertSame(['minimum' => 1], $rule->configuration);
|
|
}
|
|
|
|
/**
|
|
* Ensure price helpers filter blank and non-string feature rows.
|
|
*/
|
|
public function test_platform_price_feature_rows_are_normalised(): void
|
|
{
|
|
$price = new PlatformPrice([
|
|
'currency' => 'gbp',
|
|
'features' => [' Two warehouses ', '', null, 'Ten users'],
|
|
]);
|
|
|
|
$this->assertSame(['Two warehouses', 'Ten users'], $price->featureRows());
|
|
$this->assertSame('£49.00', $price->formatAmount(4900));
|
|
}
|
|
}
|