Files
nnterp-react-admin/tests/Unit/BelongsToCompanyTest.php
2026-03-13 20:49:46 +08:00

185 lines
5.5 KiB
PHP

<?php
namespace Tests\Unit;
use Tests\TestCase;
use App\Models\User;
use App\Models\Warehouse;
use App\Models\Setting;
use App\Scopes\CompanyScope;
use Illuminate\Foundation\Testing\RefreshDatabase;
class BelongsToCompanyTest extends TestCase
{
use RefreshDatabase;
private User $superadmin;
private User $companyA;
private User $companyB;
private User $staffA;
protected function setUp(): void
{
parent::setUp();
$this->superadmin = User::create([
'name' => 'Super Admin',
'email' => 'superadmin@test.com',
'password' => bcrypt('password'),
'type' => 'superadmin',
]);
$this->companyA = User::create([
'name' => 'Company A',
'email' => 'companya@test.com',
'password' => bcrypt('password'),
'type' => 'company',
]);
$this->companyB = User::create([
'name' => 'Company B',
'email' => 'companyb@test.com',
'password' => bcrypt('password'),
'type' => 'company',
]);
$this->staffA = User::create([
'name' => 'Staff A',
'email' => 'staffa@test.com',
'password' => bcrypt('password'),
'type' => 'staff',
'created_by' => $this->companyA->id,
]);
}
public function test_scope_filters_by_created_by_for_company_user(): void
{
Warehouse::withoutGlobalScope(CompanyScope::class)->create([
'name' => 'Warehouse A',
'created_by' => $this->companyA->id,
]);
Warehouse::withoutGlobalScope(CompanyScope::class)->create([
'name' => 'Warehouse B',
'created_by' => $this->companyB->id,
]);
$this->actingAs($this->companyA);
$warehouses = Warehouse::all();
$this->assertCount(1, $warehouses);
$this->assertEquals('Warehouse A', $warehouses->first()->name);
}
public function test_scope_filters_by_creator_for_staff_user(): void
{
Warehouse::withoutGlobalScope(CompanyScope::class)->create([
'name' => 'Warehouse A',
'created_by' => $this->companyA->id,
]);
Warehouse::withoutGlobalScope(CompanyScope::class)->create([
'name' => 'Warehouse B',
'created_by' => $this->companyB->id,
]);
$this->actingAs($this->staffA);
$warehouses = Warehouse::all();
$this->assertCount(1, $warehouses);
$this->assertEquals('Warehouse A', $warehouses->first()->name);
}
public function test_superadmin_sees_all_data(): void
{
Warehouse::withoutGlobalScope(CompanyScope::class)->create([
'name' => 'Warehouse A',
'created_by' => $this->companyA->id,
]);
Warehouse::withoutGlobalScope(CompanyScope::class)->create([
'name' => 'Warehouse B',
'created_by' => $this->companyB->id,
]);
$this->actingAs($this->superadmin);
$warehouses = Warehouse::all();
$this->assertCount(2, $warehouses);
}
public function test_auto_sets_created_by_on_creation(): void
{
$this->actingAs($this->companyA);
$warehouse = Warehouse::create([
'name' => 'Auto Scoped Warehouse',
]);
$this->assertEquals($this->companyA->id, $warehouse->created_by);
}
public function test_staff_auto_sets_creator_company_id(): void
{
$this->actingAs($this->staffA);
$warehouse = Warehouse::create([
'name' => 'Staff Created Warehouse',
]);
$this->assertEquals($this->companyA->id, $warehouse->created_by);
}
public function test_without_company_scope_returns_all(): void
{
Warehouse::withoutGlobalScope(CompanyScope::class)->create([
'name' => 'Warehouse A',
'created_by' => $this->companyA->id,
]);
Warehouse::withoutGlobalScope(CompanyScope::class)->create([
'name' => 'Warehouse B',
'created_by' => $this->companyB->id,
]);
$this->actingAs($this->companyA);
$all = Warehouse::withoutCompanyScope()->get();
$this->assertCount(2, $all);
}
public function test_for_company_returns_specific_company_data(): void
{
Warehouse::withoutGlobalScope(CompanyScope::class)->create([
'name' => 'Warehouse A',
'created_by' => $this->companyA->id,
]);
Warehouse::withoutGlobalScope(CompanyScope::class)->create([
'name' => 'Warehouse B',
'created_by' => $this->companyB->id,
]);
$this->actingAs($this->companyA);
$companyBData = Warehouse::forCompany($this->companyB->id)->get();
$this->assertCount(1, $companyBData);
$this->assertEquals('Warehouse B', $companyBData->first()->name);
}
public function test_settings_scoped_per_company(): void
{
Setting::withoutGlobalScope(CompanyScope::class)->create([
'key' => 'logo',
'value' => 'company_a_logo.png',
'created_by' => $this->companyA->id,
]);
Setting::withoutGlobalScope(CompanyScope::class)->create([
'key' => 'logo',
'value' => 'company_b_logo.png',
'created_by' => $this->companyB->id,
]);
$this->actingAs($this->companyA);
$settings = Setting::where('key', 'logo')->get();
$this->assertCount(1, $settings);
$this->assertEquals('company_a_logo.png', $settings->first()->value);
}
}