d2d.emegrendeles.hu/tests/Feature/ProfitCenterProductSearchTest.php
E98Developer 07481f75ce ADD EV3-464 profitcenter egyszerűsített, csak olvasható termékkereső
Filament ProductResource (/admin/products): lista keresővel és szűrőkkel,
csak olvasható adatlap. Profitcenternél szerver oldalon csak a saját,
rendelhető beszállítók termékei látszanak; mindenkinél csak a beszállító
jelenleg érvényes árlistáján szereplők (Product::scopeInActivePriceList).
ProfitCenterProductSearch feature flag mögött, menüpont mindkét nav barban.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-19 21:06:51 +02:00

223 lines
7.8 KiB
PHP

<?php
use App\Filament\Resources\Products\Pages\ListProducts;
use App\Filament\Resources\Products\Pages\ViewProduct;
use App\Filament\Resources\Products\ProductResource;
use App\Models\FeatureFlag;
use App\Models\PriceList;
use App\Models\ProfitCenter;
use App\Models\Product;
use App\Models\Role;
use App\Models\Supplier;
use App\Models\User;
use App\Services\FeatureFlagRegistrar;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
/**
* EV3-464: csak olvasható termékkereső a profitcenternek.
*/
function enableProductSearchFlag(): void
{
FeatureFlag::create([
'name' => ProductResource::FEATURE,
'label' => 'Profitcenter termékkereső',
'enabled' => true,
'stages' => null,
'roles' => null,
]);
app(FeatureFlagRegistrar::class)->registerAll();
}
function userWithRole(string $role): User
{
$user = User::factory()->create();
$user->addRole(Role::firstOrCreate(['name' => $role], ['display_name' => $role]));
return $user;
}
/**
* Profitcenter-felhasználó a megadott (rendelhető) beszállítókkal.
*
* @param array<int, Supplier> $suppliers
*/
function profitCenterUserWith(array $suppliers): User
{
$user = userWithRole('profit-center');
$profitCenter = ProfitCenter::factory()->create();
$profitCenter->users()->attach($user);
$profitCenter->suppliers()->attach(collect($suppliers)->pluck('id')->all());
return $user;
}
function priceListWith(Supplier $supplier, string $available, array $prices): PriceList
{
$priceList = PriceList::create(['supplier_id' => $supplier->id, 'available' => $available, 'status' => 'active']);
foreach ($prices as $productId => $price) {
DB::table('price_list_prices')->insert([
'price_list_id' => $priceList->id,
'product_id' => $productId,
'price' => $price,
]);
}
return $priceList;
}
function product(Supplier $supplier, string $name): Product
{
return Product::create(['name' => $name, 'supplier_id' => $supplier->id, 'status' => 'active']);
}
test('a termékkereső csak a flag mellett, profitcenter/root/developer szerepkörnek érhető el', function () {
$profitCenter = userWithRole('profit-center');
$admin = userWithRole('admin');
$this->actingAs($profitCenter);
expect(ProductResource::canViewAny())->toBeFalse(); // flag még nincs
enableProductSearchFlag();
expect(ProductResource::canViewAny())->toBeTrue();
expect(ProductResource::canCreate())->toBeFalse();
$this->actingAs($admin);
expect(ProductResource::canViewAny())->toBeFalse(); // admin később kapja meg
$this->actingAs(userWithRole('developer'));
expect(ProductResource::canViewAny())->toBeTrue();
});
test('a profitcenter csak a saját, rendelhető beszállítóinak termékeit látja', function () {
enableProductSearchFlag();
$own = Supplier::factory()->create();
$notOrderable = Supplier::factory()->create(['canSeeInOrder' => 0]);
$foreign = Supplier::factory()->create();
$ownProduct = product($own, 'Saját termék');
$hiddenProduct = product($notOrderable, 'Nem rendelhető beszállító termék');
$foreignProduct = product($foreign, 'Idegen termék');
foreach ([$own, $notOrderable, $foreign] as $supplier) {
priceListWith($supplier, Carbon::yesterday()->toDateString(), [
Product::where('supplier_id', $supplier->id)->first()->id => 100,
]);
}
$this->actingAs(profitCenterUserWith([$own, $notOrderable]));
Livewire::test(ListProducts::class)
->assertCanSeeTableRecords([$ownProduct])
->assertCanNotSeeTableRecords([$hiddenProduct, $foreignProduct]);
});
test('csak a beszállító jelenleg érvényes árlistáján szereplő termék látszik, az árral együtt', function () {
enableProductSearchFlag();
$supplier = Supplier::factory()->create();
$onlyOld = product($supplier, 'Csak régi árlistán');
$current = product($supplier, 'Érvényes árlistán');
$onlyFuture = product($supplier, 'Csak jövőbeli árlistán');
$unlisted = product($supplier, 'Árlista nélkül');
priceListWith($supplier, Carbon::today()->subDays(30)->toDateString(), [$onlyOld->id => 10, $current->id => 20]);
priceListWith($supplier, Carbon::today()->toDateString(), [$current->id => 25]);
priceListWith($supplier, Carbon::today()->addDays(7)->toDateString(), [$onlyFuture->id => 30]);
$this->actingAs(profitCenterUserWith([$supplier]));
Livewire::test(ListProducts::class)
->assertCanSeeTableRecords([$current])
->assertCanNotSeeTableRecords([$onlyOld, $onlyFuture, $unlisted]);
expect(ProductResource::getEloquentQuery()->findOrFail($current->id)->current_price)->toEqual(25);
});
test('a törölt árlista nem számít érvényesnek', function () {
enableProductSearchFlag();
$supplier = Supplier::factory()->create();
$fallback = product($supplier, 'Korábbi listán');
$deleted = product($supplier, 'Törölt listán');
priceListWith($supplier, Carbon::today()->subDays(10)->toDateString(), [$fallback->id => 10]);
$deletedList = priceListWith($supplier, Carbon::yesterday()->toDateString(), [$deleted->id => 20]);
DB::table('price_lists')->where('id', $deletedList->id)->update(['deleted_at' => now()]);
$this->actingAs(profitCenterUserWith([$supplier]));
Livewire::test(ListProducts::class)
->assertCanSeeTableRecords([$fallback])
->assertCanNotSeeTableRecords([$deleted]);
});
test('a szóközzel tagolt keresés minden szóra illeszt', function () {
enableProductSearchFlag();
$supplier = Supplier::factory()->create();
$match = product($supplier, 'Csemege szalámi 1 kg');
$other = product($supplier, 'Téliszalámi');
priceListWith($supplier, Carbon::yesterday()->toDateString(), [$match->id => 100, $other->id => 100]);
$this->actingAs(profitCenterUserWith([$supplier]));
Livewire::test(ListProducts::class)
->searchTable('csemege szalámi')
->assertCanSeeTableRecords([$match])
->assertCanNotSeeTableRecords([$other]);
});
test('profitcenter nélküli felhasználó nem lát terméket', function () {
enableProductSearchFlag();
$supplier = Supplier::factory()->create();
$product = product($supplier, 'Valami');
priceListWith($supplier, Carbon::yesterday()->toDateString(), [$product->id => 100]);
$this->actingAs(userWithRole('profit-center'));
Livewire::test(ListProducts::class)->assertCanNotSeeTableRecords([$product]);
});
test('az adatlap renderel, idegen termék adatlapja nem érhető el, szerkesztő oldal nincs', function () {
enableProductSearchFlag();
$own = Supplier::factory()->create();
$foreign = Supplier::factory()->create();
$ownProduct = product($own, 'Saját termék');
$foreignProduct = product($foreign, 'Idegen termék');
priceListWith($own, Carbon::yesterday()->toDateString(), [$ownProduct->id => 1234.5]);
priceListWith($foreign, Carbon::yesterday()->toDateString(), [$foreignProduct->id => 10]);
$this->actingAs(profitCenterUserWith([$own]));
Livewire::test(ViewProduct::class, ['record' => $ownProduct->getRouteKey()])
->assertSuccessful()
->assertSee('Saját termék')
->assertSee('1 234,50 Ft');
expect(fn () => Livewire::test(ViewProduct::class, ['record' => $foreignProduct->getRouteKey()]))
->toThrow(ModelNotFoundException::class);
expect(array_keys(ProductResource::getPages()))->toBe(['index', 'view']);
});
test('flag nélkül a lista oldal tiltott', function () {
$supplier = Supplier::factory()->create();
$this->actingAs(profitCenterUserWith([$supplier]));
$this->get(ProductResource::getUrl())->assertForbidden();
});