Files
Toiminnanohjaus/tests/Feature/Modules/Catalog/CatalogTest.php
T
2026-11-26 16:01:59 +02:00

150 lines
6.5 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->actingAs(User::factory()->management()->create());
$this->unit = fn (string $name) => ServiceUnitModel::query()->where('name', $name)->sole();
});
it('has the starting catalog from the specification', function () {
$tree = app(CatalogQueries::class)->tree();
$media = $tree[0];
expect(array_map(fn ($node) => $node->serviceUnit->name, $tree))->toBe([
'Mediatuotanto', 'Markkinointi', 'Painotuotanto', 'Ylläpitopalvelut', 'Verkkosivut', 'Ohjelmistot', 'Kuluttajapalvelut', 'Kaikille yhteiset',
])
->and(array_map(fn ($node) => $node->service->name, $media->services))->toBe(['Videotuotanto', 'Valokuvaus', 'Äänituotanto'])
->and(array_map(fn ($workType) => $workType->name, $media->services[0]->workTypes))->toBe(['videokuvaus', 'kuvausassistentti', 'editointi', 'animointi', '3D-mallinnus'])
->and(end($tree)->serviceUnit->isShared)->toBeTrue()
->and(WorkTypeModel::query()->count())->toBe(29);
});
it('offers the work types of the chosen units and the shared ones, but not inactive ones', function () {
$marketing = ($this->unit)('Markkinointi');
WorkTypeModel::query()->where('name', 'kampanjatyö')->update(['is_active' => false]);
$nodes = app(CatalogQueries::class)->activeWorkTypesFor([$marketing->id]);
expect(array_map(fn ($node) => $node->serviceUnit->name, $nodes))->toBe(['Markkinointi', 'Kaikille yhteiset'])
->and(array_map(fn ($workType) => $workType->name, $nodes[0]->workTypes))->toBe(['graafinen työ', 'julkaisutyö'])
->and(array_map(fn ($unit) => $unit->name, app(CatalogQueries::class)->activeServiceUnits()))->not->toContain('Kaikille yhteiset');
});
it('shows the catalog page to admins and management only', function (string $role, int $status) {
$this->actingAs(User::factory()->{$role}()->create())->get(route('catalog.index'))->assertStatus($status);
})->with([
'admin' => ['admin', 200],
'management' => ['management', 200],
'employee' => ['employee', 403],
'intern' => ['intern', 403],
'subcontractor' => ['subcontractor', 403],
]);
it('shows the menu link only to those who may manage the catalog', function () {
$this->get(route('customers.index'))->assertSee('Pohjatiedot');
$this->actingAs(User::factory()->create())->get(route('customers.index'))->assertDontSee('Pohjatiedot');
});
it('adds a service unit, a service and a work type', function () {
Livewire::test('catalog::service-unit-form')
->set('form.name', 'Tapahtumat')
->call('save')
->assertHasNoErrors()
->assertRedirect(route('catalog.index'));
$unit = ($this->unit)('Tapahtumat');
Livewire::test('catalog::service-form', ['serviceUnit' => $unit->id])
->set('form.name', 'Messut')
->call('save')
->assertHasNoErrors();
$service = ServiceModel::query()->where('name', 'Messut')->sole();
Livewire::test('catalog::work-type-form', ['serviceUnit' => $unit->id])
->set('form.name', 'osastosuunnittelu')
->set('form.service_id', (string) $service->id)
->call('save')
->assertHasNoErrors();
expect(WorkTypeModel::query()->where('name', 'osastosuunnittelu')->sole()->service_id)->toBe($service->id);
});
it('shows a taken name as a Finnish field error', function () {
Livewire::test('catalog::service-unit-form')
->set('form.name', 'markkinointi')
->call('save')
->assertHasErrors(['form.name'])
->assertSee('Samalla tasolla on jo tämänniminen rivi.');
});
it('moves and deactivates rows on the catalog page', function () {
$marketing = ($this->unit)('Markkinointi');
Livewire::test('catalog::index')
->call('move', 'service_unit', $marketing->id, 'up')
->assertSee('Markkinointi siirretty ylöspäin.')
->call('move', 'service_unit', $marketing->id, 'up')
->assertSee('Markkinointi on jo ensimmäisenä.')
->call('toggleActive', 'service_unit', $marketing->id)
->assertSee('Markkinointi poistettu käytöstä.')
->set('showInactive', false)
->assertDontSee('graafinen työ');
expect($marketing->fresh()->sort_order)->toBe(1)
->and($marketing->fresh()->is_active)->toBeFalse();
});
it('returns 404 for unknown items and does not add services to the shared unit', function () {
$shared = ($this->unit)('Kaikille yhteiset');
$this->get(route('catalog.service-units.edit', 999))->assertNotFound();
$this->get(route('catalog.work-types.edit', 999))->assertNotFound();
$this->get(route('catalog.services.create', $shared->id))->assertNotFound();
});
it('does not let the edited item be changed in the browser', function () {
Livewire::test('catalog::work-type-form', ['serviceUnit' => ($this->unit)('Markkinointi')->id])
->set('workTypeId', 1);
})->throws(CannotUpdateLockedPropertyException::class);
it('turns stock and machine features on for a service unit, but not for the shared unit', function () {
$print = ($this->unit)('Painotuotanto');
$shared = ($this->unit)('Kaikille yhteiset');
Livewire::test('catalog::service-unit-form', ['serviceUnit' => $print->id])
->assertSee(__('catalog.service_units.uses_stock'))
->set('usesStock', true)
->call('save')
->assertHasNoErrors();
expect($print->fresh()->uses_stock)->toBeTrue()
->and(app(CatalogQueries::class)->stockServiceUnitIds())->toBe([$print->id]);
Livewire::test('catalog::index')->assertSee(__('catalog.service_units.uses_stock_on'));
Livewire::test('catalog::service-unit-form', ['serviceUnit' => $shared->id])
->assertDontSee(__('catalog.service_units.uses_stock'))
->set('usesStock', true)
->call('save');
expect($shared->fresh()->uses_stock)->toBeFalse();
Livewire::test('catalog::service-unit-form', ['serviceUnit' => $print->id])
->assertSet('usesStock', true)
->set('usesStock', false)
->call('save');
expect(app(CatalogQueries::class)->stockServiceUnitIds())->toBe([]);
});