create([ 'email' => uniqid('ing_', true).'@test.example', 'password' => bcrypt('password'), ]); $user->forceFill([ 'admin' => $adminLevel, 'confirmed' => true, 'active' => true, 'wizard' => 100, 'blocked' => false, ])->save(); return $user->fresh(); } function ingredientOrderSupplier(string $name): Supplier { return Supplier::factory()->create([ 'name' => $name, 'active' => true, ]); } test('ingredient form shows suppliers, tax rates and delivery time templates', function () { $supplier = ingredientOrderSupplier('Aktiv-Lieferant'); TaxRate::query()->create(['name' => 'Standard', 'percent' => 19, 'active' => true, 'pos' => 0]); DeliveryTime::query()->create(['label' => '3–5 Werktage', 'days' => 5, 'active' => true, 'pos' => 0]); DeliveryTime::query()->create(['label' => 'Inaktiv-Vorlage', 'active' => false, 'pos' => 1]); $this->actingAs(ingredientOrderUser(7), 'user'); $this->get(route('admin_product_ingredient_edit', 'new')) ->assertSuccessful() ->assertSee('supplier_ids') ->assertSee('tax_rate_id') ->assertSee('Aktiv-Lieferant') ->assertSee('3–5 Werktage') ->assertSee('data-days="5"', false) ->assertDontSee('Inaktiv-Vorlage'); }); test('ingredient can be stored with tax rate, delivery time and suppliers', function () { $taxRate = TaxRate::query()->create(['name' => 'Standard', 'percent' => 19, 'active' => true, 'pos' => 0]); $supplierA = ingredientOrderSupplier('Lieferant A'); $supplierB = ingredientOrderSupplier('Lieferant B'); $this->actingAs(ingredientOrderUser(7), 'user'); $this->post(route('admin_product_ingredient_store'), [ 'id' => 'new', 'name' => 'Sheabutter', 'active' => '1', 'tax_rate_id' => (string) $taxRate->id, 'delivery_time' => '3–5 Werktage', 'delivery_time_days' => '5', 'supplier_ids' => [(string) $supplierA->id, (string) $supplierB->id], ])->assertRedirect(route('admin_product_ingredients')); $ingredient = Ingredient::query()->where('name', 'Sheabutter')->firstOrFail(); expect($ingredient->tax_rate_id)->toBe($taxRate->id) ->and($ingredient->delivery_time)->toBe('3–5 Werktage') ->and($ingredient->delivery_time_days)->toBe(5) ->and($ingredient->suppliers()->pluck('suppliers.id')->all()) ->toEqualCanonicalizing([$supplierA->id, $supplierB->id]); }); test('ingredient suppliers are synced on update', function () { $supplierA = ingredientOrderSupplier('Lieferant A'); $supplierB = ingredientOrderSupplier('Lieferant B'); $ingredient = Ingredient::query()->create([ 'name' => 'Lavendelöl', 'active' => true, ]); $ingredient->suppliers()->sync([$supplierA->id]); $this->actingAs(ingredientOrderUser(7), 'user'); $this->post(route('admin_product_ingredient_store'), [ 'id' => (string) $ingredient->id, 'name' => 'Lavendelöl', 'active' => '1', 'supplier_ids' => [(string) $supplierB->id], ])->assertRedirect(route('admin_product_ingredients')); expect($ingredient->fresh()->suppliers()->pluck('suppliers.id')->all()) ->toEqualCanonicalizing([$supplierB->id]); }); test('ingredient delivery time days must be an integer', function () { $this->actingAs(ingredientOrderUser(7), 'user'); $this->post(route('admin_product_ingredient_store'), [ 'id' => 'new', 'name' => 'Falsche-Tage', 'active' => '1', 'delivery_time_days' => 'viele', ])->assertSessionHasErrors('delivery_time_days'); }); test('ingredient tax rate must exist', function () { $this->actingAs(ingredientOrderUser(7), 'user'); $this->post(route('admin_product_ingredient_store'), [ 'id' => 'new', 'name' => 'Falscher-Steuersatz', 'active' => '1', 'tax_rate_id' => '999999', ])->assertSessionHasErrors('tax_rate_id'); }); test('ingredient name is required', function () { $this->actingAs(ingredientOrderUser(7), 'user'); $this->post(route('admin_product_ingredient_store'), [ 'id' => 'new', 'name' => '', 'active' => '1', ])->assertSessionHasErrors('name'); });