create([ 'email' => uniqid('det_', true).'@test.example', 'password' => bcrypt('password'), ]); $user->forceFill([ 'admin' => $adminLevel, 'confirmed' => true, 'active' => true, 'wizard' => 100, 'blocked' => false, ])->save(); return $user->fresh(); } test('supplier show displays assigned ingredients and packaging items', function () { $supplier = Supplier::factory()->create(['name' => 'Detail-Lieferant']); $ingredient = Ingredient::query()->create(['name' => 'Sheabutter', 'active' => true]); $supplier->ingredients()->attach($ingredient->id); $item = PackagingItem::factory()->create(['name' => 'Braunglas 50ml', 'supplier_id' => $supplier->id]); $this->actingAs(supplierDetailsUser(7), 'user'); $this->get(route('admin.inventory.suppliers.show', $supplier)) ->assertSuccessful() ->assertSee('Detail-Lieferant') ->assertSee('Sheabutter') ->assertSee('Braunglas 50ml') ->assertSee('Zugeordnete INCIs') ->assertSee('Zugeordnete Verpackungsartikel'); }); test('ingredient can be attached to supplier from details', function () { $supplier = Supplier::factory()->create(); $ingredient = Ingredient::query()->create(['name' => 'Lavendelöl', 'active' => true]); $this->actingAs(supplierDetailsUser(7), 'user'); $this->post(route('admin.inventory.suppliers.ingredients.attach', $supplier), [ 'ingredient_id' => $ingredient->id, ])->assertSuccessful()->assertSee('Lavendelöl'); expect($supplier->ingredients()->pluck('ingredients.id')->all())->toContain($ingredient->id); }); test('ingredient can be detached from supplier', function () { $supplier = Supplier::factory()->create(); $ingredient = Ingredient::query()->create(['name' => 'Jojobaöl', 'active' => true]); $supplier->ingredients()->attach($ingredient->id); $this->actingAs(supplierDetailsUser(7), 'user'); $this->delete(route('admin.inventory.suppliers.ingredients.detach', [$supplier, $ingredient])) ->assertSuccessful(); expect($supplier->ingredients()->count())->toBe(0); }); test('packaging item can be attached to supplier from details', function () { $supplier = Supplier::factory()->create(); $item = PackagingItem::factory()->create(['name' => 'Pumpspender', 'supplier_id' => null]); $this->actingAs(supplierDetailsUser(7), 'user'); $this->post(route('admin.inventory.suppliers.packaging-items.attach', $supplier), [ 'packaging_item_id' => $item->id, ])->assertSuccessful()->assertSee('Pumpspender'); expect($item->fresh()->supplier_id)->toBe($supplier->id); }); test('packaging item can be detached from supplier', function () { $supplier = Supplier::factory()->create(); $item = PackagingItem::factory()->create(['name' => 'Faltschachtel', 'supplier_id' => $supplier->id]); $this->actingAs(supplierDetailsUser(7), 'user'); $this->delete(route('admin.inventory.suppliers.packaging-items.detach', [$supplier, $item])) ->assertSuccessful(); expect($item->fresh()->supplier_id)->toBeNull(); }); test('attach ingredient validates ingredient exists', function () { $supplier = Supplier::factory()->create(); $this->actingAs(supplierDetailsUser(7), 'user'); $this->post(route('admin.inventory.suppliers.ingredients.attach', $supplier), [ 'ingredient_id' => 999999, ])->assertSessionHasErrors('ingredient_id'); }); test('supplier details require admin level', function () { $supplier = Supplier::factory()->create(); $this->actingAs(supplierDetailsUser(1), 'user'); $this->get(route('admin.inventory.suppliers.show', $supplier)) ->assertRedirect(); });