10.April 2026

This commit is contained in:
Kevin Adametz 2026-04-10 17:15:27 +02:00
parent a00c42e770
commit f58c709945
208 changed files with 19280 additions and 2914 deletions

View file

@ -0,0 +1,316 @@
<?php
/**
* Validiert IncentiveTracker::trackSalesVolume für Umsatz aus einer weiteren Abo-Bestellung
* (gleicher Kunde/shopping_user_id wie beim Neuabo). Rechnung und UserSalesVolume entstehen
* über Payment::paymentStatusPaidAction / InvoiceRepository::createAndSalesVolume (Payone-API),
* nicht über user:make_abo_order.
*/
use App\Models\Country;
use App\Models\Incentive;
use App\Models\IncentiveNewAbo;
use App\Models\IncentiveParticipant;
use App\Models\IncentivePointsLog;
use App\Models\Shipping;
use App\Models\ShippingCountry;
use App\Models\ShoppingOrder;
use App\Models\ShoppingUser;
use App\Models\UserAbo;
use App\Models\UserAboOrder;
use App\Models\UserSalesVolume;
use App\Models\UserShop;
use App\Services\Incentive\IncentiveTracker;
use App\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
Carbon::setTestNow(Carbon::parse('2026-05-15 10:00:00'));
});
afterEach(function () {
Carbon::setTestNow();
});
it('addiert Umsatzpunkte beim Berater wenn UserSalesVolume zur Abo-Verlängerung gehört (trackSalesVolume Pfad Neuabo)', function () {
$country = Country::create([
'code' => 'DE',
'phone' => '49',
'en' => 'Germany',
'de' => 'Deutschland',
'es' => 'Alemania',
'fr' => 'Allemagne',
'it' => 'Germania',
'ru' => 'Германия',
]);
$shipping = Shipping::create([
'name' => 'Standard',
'active' => true,
]);
$shippingCountry = ShippingCountry::create([
'shipping_id' => $shipping->id,
'country_id' => $country->id,
]);
$shopOwner = User::forceCreate([
'email' => 'shop-owner-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userShop = UserShop::create([
'user_id' => $shopOwner->id,
'name' => 'TS'.substr(uniqid('', true), 0, 8),
'slug' => 'ts-'.uniqid(),
'active' => true,
]);
$consultant = User::forceCreate([
'email' => 'consultant-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$customer = User::forceCreate([
'email' => 'customer-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$shoppingUser = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'cust@example.com',
]);
$userAbo = UserAbo::create([
'user_id' => null,
'member_id' => $consultant->id,
'shopping_user_id' => $shoppingUser->id,
'is_for' => 'ot',
'email' => 'abo@example.com',
'payone_userid' => 999001,
'clearingtype' => 'cc',
'active' => true,
'status' => 2,
'abo_interval' => 1,
]);
$incentive = Incentive::factory()->create([
'qualification_start' => '2026-04-01',
'qualification_end' => '2026-07-31',
'calculation_end' => '2026-08-31',
'status' => 1,
]);
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $consultant->id,
]);
IncentiveNewAbo::create([
'participant_id' => $participant->id,
'user_abo_id' => $userAbo->id,
'activated_at' => Carbon::now()->subMonth(),
]);
$shoppingOrder = ShoppingOrder::create([
'shopping_user_id' => $shoppingUser->id,
'auth_user_id' => $customer->id,
'country_id' => $shippingCountry->id,
'user_shop_id' => $userShop->id,
'payment_for' => 6,
'points' => 75,
'is_abo' => true,
'paid' => true,
'txaction' => 'paid',
'mode' => 'test',
'total' => 100,
'subtotal' => 90,
]);
UserAboOrder::create([
'user_abo_id' => $userAbo->id,
'shopping_order_id' => $shoppingOrder->id,
'status' => 2,
'paid' => true,
]);
$userSalesVolume = UserSalesVolume::create([
'user_id' => $customer->id,
'shopping_order_id' => $shoppingOrder->id,
'month' => 5,
'year' => 2026,
'date' => '15.05.2026',
'points' => 75,
'total_net' => 90,
'status_points' => 1,
'status' => 2,
'message' => 'Abo Verlängerung Test',
]);
IncentiveTracker::trackSalesVolume($userSalesVolume);
$participant->refresh();
expect(
IncentivePointsLog::where('participant_id', $participant->id)
->where('type', 'abo')
->where('user_sales_volume_id', $userSalesVolume->id)
->where('points_accumulated', 75)
->exists()
)->toBeTrue()
->and($participant->total_points)->toBeGreaterThan(0);
});
it('addiert Umsatzpunkte bei Verlaengerung auch wenn shopping_order einen replizierten ShoppingUser hat', function () {
$country = Country::create([
'code' => 'CH',
'phone' => '41',
'en' => 'Switzerland',
'de' => 'Schweiz',
'es' => 'Suiza',
'fr' => 'Suisse',
'it' => 'Svizzera',
'ru' => 'Швейцария',
]);
$shipping = Shipping::create([
'name' => 'Std-CH',
'active' => true,
]);
$shippingCountry = ShippingCountry::create([
'shipping_id' => $shipping->id,
'country_id' => $country->id,
]);
$shopOwner = User::forceCreate([
'email' => 'so-ch-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userShop = UserShop::create([
'user_id' => $shopOwner->id,
'name' => 'TSCH'.substr(uniqid('', true), 0, 6),
'slug' => 'tsch-'.uniqid(),
'active' => true,
]);
$consultant = User::forceCreate([
'email' => 'consultant-ch-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$customer = User::forceCreate([
'email' => 'customer-ch-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$shoppingUserStamm = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'cust-ch-'.uniqid('', true).'@example.com',
]);
$shoppingUserReplica = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => $shoppingUserStamm->billing_email,
]);
$userAbo = UserAbo::create([
'user_id' => null,
'member_id' => $consultant->id,
'shopping_user_id' => $shoppingUserStamm->id,
'is_for' => 'ot',
'email' => $shoppingUserStamm->billing_email,
'payone_userid' => 999101,
'clearingtype' => 'cc',
'active' => true,
'status' => 2,
'abo_interval' => 1,
]);
$incentive = Incentive::factory()->create([
'qualification_start' => '2026-04-01',
'qualification_end' => '2026-07-31',
'calculation_end' => '2026-08-31',
'status' => 1,
]);
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $consultant->id,
]);
IncentiveNewAbo::create([
'participant_id' => $participant->id,
'user_abo_id' => $userAbo->id,
'activated_at' => Carbon::now()->subMonth(),
]);
$shoppingOrder = ShoppingOrder::create([
'shopping_user_id' => $shoppingUserReplica->id,
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'country_id' => $shippingCountry->id,
'user_shop_id' => $userShop->id,
'payment_for' => 6,
'points' => 40,
'is_abo' => true,
'paid' => true,
'txaction' => 'paid',
'mode' => 'test',
'total' => 100,
'subtotal' => 90,
]);
UserAboOrder::create([
'user_abo_id' => $userAbo->id,
'shopping_order_id' => $shoppingOrder->id,
'status' => 2,
'paid' => true,
]);
expect($shoppingOrder->shopping_user_id)->not->toBe($userAbo->shopping_user_id);
$userSalesVolume = UserSalesVolume::create([
'user_id' => $customer->id,
'shopping_order_id' => $shoppingOrder->id,
'month' => 5,
'year' => 2026,
'date' => '15.05.2026',
'points' => 40,
'total_net' => 90,
'status_points' => 1,
'status' => 2,
'message' => 'Abo Verlängerung Replikat',
]);
IncentiveTracker::trackSalesVolume($userSalesVolume);
$participant->refresh();
expect(
IncentivePointsLog::where('participant_id', $participant->id)
->where('type', 'abo')
->where('user_sales_volume_id', $userSalesVolume->id)
->where('points_accumulated', 40)
->exists()
)->toBeTrue();
});

View file

@ -0,0 +1,120 @@
<?php
use App\Models\Country;
use App\Models\Incentive;
use App\Models\IncentiveNewAbo;
use App\Models\IncentiveParticipant;
use App\Models\Shipping;
use App\Models\ShippingCountry;
use App\Models\ShoppingOrder;
use App\Models\ShoppingUser;
use App\Models\UserAbo;
use App\Models\UserAboOrder;
use App\Models\UserShop;
use App\Services\Incentive\IncentiveTracker;
use App\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
Carbon::setTestNow(Carbon::parse('2026-05-15 10:00:00'));
});
afterEach(function () {
Carbon::setTestNow();
});
it('trackAboActivated erfasst Berater-Eigenabo (is_for me) wie ein Kundenabo', function () {
$country = Country::create([
'code' => 'DE',
'phone' => '49',
'en' => 'Germany',
'de' => 'Deutschland',
'es' => 'Alemania',
'fr' => 'Allemagne',
'it' => 'Germania',
'ru' => 'Германия',
]);
$shipping = Shipping::create(['name' => 'S', 'active' => true]);
$shippingCountry = ShippingCountry::create(['shipping_id' => $shipping->id, 'country_id' => $country->id]);
$consultant = User::forceCreate([
'email' => 'co-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userShop = UserShop::create([
'user_id' => $consultant->id,
'name' => 'TS'.substr(uniqid('', true), 0, 6),
'slug' => 'ts-'.uniqid(),
'active' => true,
]);
$shoppingUser = ShoppingUser::create([
'auth_user_id' => $consultant->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'me-'.uniqid('', true).'@example.com',
'is_from' => 'wizard',
]);
$userAbo = UserAbo::create([
'user_id' => $consultant->id,
'member_id' => null,
'shopping_user_id' => $shoppingUser->id,
'is_for' => 'me',
'email' => $shoppingUser->billing_email,
'payone_userid' => 999002,
'clearingtype' => 'cc',
'active' => true,
'status' => 2,
'abo_interval' => 1,
]);
$incentive = Incentive::factory()->create([
'qualification_start' => '2026-04-01',
'qualification_end' => '2026-07-31',
'calculation_end' => '2026-08-31',
'status' => 1,
]);
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $consultant->id,
]);
$shoppingOrder = ShoppingOrder::create([
'shopping_user_id' => $shoppingUser->id,
'auth_user_id' => $consultant->id,
'country_id' => $shippingCountry->id,
'user_shop_id' => $userShop->id,
'payment_for' => 6,
'points' => 50,
'is_abo' => true,
'paid' => true,
'txaction' => 'paid',
'mode' => 'test',
'total' => 100,
'subtotal' => 90,
]);
UserAboOrder::create([
'user_abo_id' => $userAbo->id,
'shopping_order_id' => $shoppingOrder->id,
'status' => 2,
'paid' => true,
]);
IncentiveTracker::trackAboActivated($shoppingOrder->fresh());
expect(
IncentiveNewAbo::where('participant_id', $participant->id)
->where('user_abo_id', $userAbo->id)
->exists()
)->toBeTrue();
});

View file

@ -0,0 +1,110 @@
<?php
use App\Models\Country;
use App\Models\Incentive;
use App\Models\IncentiveNewAbo;
use App\Models\IncentiveParticipant;
use App\Models\IncentivePointsLog;
use App\Models\ShoppingUser;
use App\Models\UserAbo;
use App\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Artisan;
uses(RefreshDatabase::class);
beforeEach(function () {
Carbon::setTestNow(Carbon::parse('2026-05-15 10:00:00'));
});
afterEach(function () {
Carbon::setTestNow();
});
it('zieht Eigenabo vor Qualifikationsbeginn bei incentive:calculate ohne skip-repair nach', function () {
$country = Country::create([
'code' => 'DE',
'phone' => '49',
'en' => 'Germany',
'de' => 'Deutschland',
'es' => 'Alemania',
'fr' => 'Allemagne',
'it' => 'Germania',
'ru' => 'Германия',
]);
$consultant = User::forceCreate([
'email' => 'co-calc-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$shoppingUser = ShoppingUser::create([
'auth_user_id' => $consultant->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'me-'.uniqid('', true).'@example.com',
'is_from' => 'wizard',
]);
$userAbo = UserAbo::create([
'user_id' => $consultant->id,
'member_id' => null,
'shopping_user_id' => $shoppingUser->id,
'is_for' => 'me',
'email' => $shoppingUser->billing_email,
'payone_userid' => 999002,
'clearingtype' => 'cc',
'active' => true,
'status' => 2,
'abo_interval' => 1,
]);
$userAbo->forceFill([
'created_at' => Carbon::parse('2026-01-15 12:00:00'),
'updated_at' => Carbon::parse('2026-01-15 12:00:00'),
])->save();
$incentive = Incentive::factory()->create([
'qualification_start' => '2026-04-01',
'qualification_end' => '2026-07-31',
'calculation_end' => '2026-08-31',
'status' => 1,
'points_abo_onetime' => 400,
]);
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $consultant->id,
]);
$exitCode = Artisan::call('incentive:calculate', [
'incentive_id' => (string) $incentive->id,
]);
expect($exitCode)->toBe(0);
$participant->refresh();
expect(
IncentiveNewAbo::query()
->where('participant_id', $participant->id)
->where('user_abo_id', $userAbo->id)
->exists()
)->toBeTrue();
$log = IncentivePointsLog::query()
->where('participant_id', $participant->id)
->where('type', 'abo')
->where('source_id', $userAbo->id)
->first();
expect($log)->not->toBeNull()
->and($log->month)->toBe(4)
->and($log->year)->toBe(2026)
->and($log->points_onetime)->toBe(400);
expect($participant->total_points)->toBe(400);
});

View file

@ -0,0 +1,188 @@
<?php
/**
* Detailansicht: Akkumulierte Abo-Punkte muessen dem richtigen Neuabo zugeordnet werden,
* auch wenn die Verlaengerungsbestellung einen anderen shopping_user_id hat (Replikat).
*/
use App\Http\Controllers\Admin\IncentiveController;
use App\Models\Country;
use App\Models\Incentive;
use App\Models\IncentiveNewAbo;
use App\Models\IncentiveParticipant;
use App\Models\IncentivePointsLog;
use App\Models\Shipping;
use App\Models\ShippingCountry;
use App\Models\ShoppingOrder;
use App\Models\ShoppingUser;
use App\Models\UserAbo;
use App\Models\UserAboOrder;
use App\Models\UserSalesVolume;
use App\Models\UserShop;
use App\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
Carbon::setTestNow(Carbon::parse('2026-05-15 12:00:00'));
});
afterEach(function () {
Carbon::setTestNow();
});
it('ordnet akkumulierte Abo-Punkte dem Neuabo ueber user_abo_id zu (Replikat-ShoppingUser)', function () {
$country = Country::create([
'code' => 'DE',
'phone' => '49',
'en' => 'Germany',
'de' => 'Deutschland',
'es' => 'Alemania',
'fr' => 'Allemagne',
'it' => 'Germania',
'ru' => 'Германия',
]);
$shipping = Shipping::create([
'name' => 'Standard',
'active' => true,
]);
$shippingCountry = ShippingCountry::create([
'shipping_id' => $shipping->id,
'country_id' => $country->id,
]);
$shopOwner = User::forceCreate([
'email' => 'shop-owner-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userShop = UserShop::create([
'user_id' => $shopOwner->id,
'name' => 'TS'.substr(uniqid('', true), 0, 8),
'slug' => 'ts-'.uniqid(),
'active' => true,
]);
$consultant = User::forceCreate([
'email' => 'consultant-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$customer = User::forceCreate([
'email' => 'customer-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$shoppingUserStamm = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'cust-detail-'.uniqid('', true).'@example.com',
]);
$shoppingUserReplica = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => $shoppingUserStamm->billing_email,
]);
$userAbo = UserAbo::create([
'user_id' => null,
'member_id' => $consultant->id,
'shopping_user_id' => $shoppingUserStamm->id,
'is_for' => 'ot',
'email' => $shoppingUserStamm->billing_email,
'payone_userid' => 999201,
'clearingtype' => 'cc',
'active' => true,
'status' => 2,
'abo_interval' => 1,
]);
$incentive = Incentive::factory()->create([
'qualification_start' => '2026-04-01',
'qualification_end' => '2026-07-31',
'calculation_end' => '2026-08-31',
'status' => 1,
'points_abo_onetime' => 100,
]);
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $consultant->id,
]);
$newAbo = IncentiveNewAbo::create([
'participant_id' => $participant->id,
'user_abo_id' => $userAbo->id,
'activated_at' => Carbon::parse('2026-04-10'),
]);
$shoppingOrder = ShoppingOrder::create([
'shopping_user_id' => $shoppingUserReplica->id,
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'country_id' => $shippingCountry->id,
'user_shop_id' => $userShop->id,
'payment_for' => 6,
'points' => 42,
'is_abo' => true,
'paid' => true,
'txaction' => 'paid',
'mode' => 'test',
'total' => 100,
'subtotal' => 90,
]);
UserAboOrder::create([
'user_abo_id' => $userAbo->id,
'shopping_order_id' => $shoppingOrder->id,
'status' => 2,
'paid' => true,
]);
$userSalesVolume = UserSalesVolume::create([
'user_id' => $customer->id,
'shopping_order_id' => $shoppingOrder->id,
'month' => 5,
'year' => 2026,
'date' => '15.05.2026',
'points' => 42,
'total_net' => 90,
'status_points' => 1,
'status' => 2,
'message' => 'Verlängerung',
]);
IncentivePointsLog::create([
'participant_id' => $participant->id,
'type' => 'abo',
'source_type' => UserSalesVolume::class,
'source_id' => $userSalesVolume->id,
'source_label' => 'SV Test',
'month' => 5,
'year' => 2026,
'points_onetime' => 0,
'points_accumulated' => 42,
'user_sales_volume_id' => $userSalesVolume->id,
'incentive_new_abo_id' => $newAbo->id,
'is_storno' => false,
]);
$data = IncentiveController::buildParticipantDetailData($participant->fresh(['incentive', 'user']));
$aboRow = $data['abo_sources']->first();
expect($aboRow)->not->toBeNull()
->and($aboRow['id'])->toBe($newAbo->id)
->and(array_sum($aboRow['monthly']))->toBe(42);
});

View file

@ -0,0 +1,181 @@
<?php
use App\Http\Controllers\User\IncentiveController;
use App\Models\Incentive;
use App\Models\IncentiveParticipant;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('sortiert Teilnehmer mit Rang 1,2,… und ohne Rang zuletzt', function () {
$incentive = Incentive::factory()->create();
$makeUser = fn () => User::forceCreate([
'email' => 't-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userNoRank = $makeUser();
$userRank2 = $makeUser();
$userRank1 = $makeUser();
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $userNoRank->id,
'rank' => null,
]);
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $userRank2->id,
'rank' => 2,
]);
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $userRank1->id,
'rank' => 1,
]);
$orderedUserIds = IncentiveParticipant::query()
->where('incentive_id', $incentive->id)
->orderByIncentiveLeaderboard()
->pluck('user_id')
->all();
expect($orderedUserIds)->toBe([$userRank1->id, $userRank2->id, $userNoRank->id]);
});
it('sortiert qualifizierte Teilnehmer vor nicht qualifizierten, unabhängig von Punkten und Rang', function () {
$incentive = Incentive::factory()->create();
$makeUser = fn () => User::forceCreate([
'email' => 't-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userNotQualified = $makeUser();
$userQualified = $makeUser();
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $userNotQualified->id,
'is_qualified' => false,
'total_points' => 10_000,
'rank' => 1,
]);
IncentiveParticipant::factory()->qualified()->create([
'incentive_id' => $incentive->id,
'user_id' => $userQualified->id,
'total_points' => 500,
'rank' => 2,
]);
$orderedUserIds = IncentiveParticipant::query()
->where('incentive_id', $incentive->id)
->orderByIncentiveLeaderboard()
->pluck('user_id')
->all();
expect($orderedUserIds)->toBe([$userQualified->id, $userNotQualified->id]);
});
it('sortiert bei Punktgleichstand Teilnehmer mit Klarnamen (bestaetigte Teilnahme) vor anonymen', function () {
$incentive = Incentive::factory()->create();
$makeUser = fn () => User::forceCreate([
'email' => 't-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$anonymous = IncentiveParticipant::factory()->unconfirmed()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeUser()->id,
'total_points' => 500,
'rank' => 5,
]);
$confirmed = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeUser()->id,
'accepted_terms_at' => now(),
'total_points' => 500,
'rank' => 5,
]);
$orderedUserIds = IncentiveParticipant::query()
->where('incentive_id', $incentive->id)
->orderByIncentiveLeaderboard()
->pluck('user_id')
->all();
expect($orderedUserIds)->toBe([$confirmed->user_id, $anonymous->user_id]);
});
it('begrenzt die User-Rangliste auf 30 Plaetze (Gewinner-Zone bleibt max_winners)', function () {
$incentive = Incentive::factory()->create(['max_winners' => 20]);
$makeUser = fn () => User::forceCreate([
'email' => 't-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
foreach (range(1, 35) as $i) {
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeUser()->id,
'total_points' => 1000 - $i,
]);
}
$ranking = IncentiveParticipant::where('incentive_id', $incentive->id)
->withRankingActivity()
->orderByIncentiveLeaderboard()
->limit(IncentiveController::USER_RANKING_DISPLAY_LIMIT)
->get();
expect($ranking)->toHaveCount(30);
});
it('blendet in der User-Ranglogik Teilnehmer ohne Partner, Abo und Punkte aus', function () {
$incentive = Incentive::factory()->create();
$makeUser = fn () => User::forceCreate([
'email' => 't-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeUser()->id,
'qualified_partners' => 0,
'qualified_abos' => 0,
'total_points' => 0,
]);
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeUser()->id,
'qualified_partners' => 0,
'qualified_abos' => 0,
'total_points' => 100,
]);
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeUser()->id,
'qualified_partners' => 1,
'qualified_abos' => 0,
'total_points' => 0,
]);
$ids = IncentiveParticipant::query()
->where('incentive_id', $incentive->id)
->withRankingActivity()
->orderBy('id')
->pluck('id')
->all();
expect($ids)->toHaveCount(2);
});

View file

@ -0,0 +1,177 @@
<?php
use App\Models\Country;
use App\Models\Incentive;
use App\Models\IncentiveNewPartner;
use App\Models\IncentiveParticipant;
use App\Models\Product;
use App\Models\Shipping;
use App\Models\ShippingCountry;
use App\Models\ShoppingOrder;
use App\Models\ShoppingOrderItem;
use App\Models\ShoppingUser;
use App\Models\UserShop;
use App\Services\Incentive\IncentiveTracker;
use App\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
beforeEach(function () {
Carbon::setTestNow(Carbon::parse('2026-05-15 12:00:00'));
});
afterEach(function () {
Carbon::setTestNow();
});
function createWizardRegistrationOrderWithProducts(User $registrant, User $shopOwner, array $products): ShoppingOrder
{
$country = Country::create([
'code' => 'DE',
'phone' => '49',
'en' => 'Germany',
'de' => 'Deutschland',
'es' => 'Alemania',
'fr' => 'Allemagne',
'it' => 'Germania',
'ru' => 'Германия',
]);
$shipping = Shipping::create(['name' => 'S', 'active' => true]);
$shippingCountry = ShippingCountry::create(['shipping_id' => $shipping->id, 'country_id' => $country->id]);
$userShop = UserShop::create([
'user_id' => $shopOwner->id,
'name' => 'T'.substr(uniqid('', true), 0, 6),
'slug' => 't-'.uniqid(),
'active' => true,
]);
$shoppingUser = ShoppingUser::create([
'auth_user_id' => $registrant->id,
'member_id' => $shopOwner->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'su-'.uniqid('', true).'@example.com',
'is_from' => 'wizard',
]);
$order = ShoppingOrder::create([
'shopping_user_id' => $shoppingUser->id,
'auth_user_id' => $registrant->id,
'country_id' => $shippingCountry->id,
'user_shop_id' => $userShop->id,
'payment_for' => 1,
'paid' => true,
'txaction' => 'paid',
'mode' => 'test',
'total' => 100,
'subtotal' => 90,
]);
foreach ($products as $product) {
ShoppingOrderItem::create([
'shopping_order_id' => $order->id,
'product_id' => $product->id,
'qty' => 1,
'price' => 50,
]);
}
return $order->fresh(['shopping_order_items.product']);
}
function createProductForTest(bool $membershipOnly): Product
{
$id = DB::table('products')->insertGetId([
'name' => 'P '.uniqid(),
'title' => 'T',
'is_membership_only' => $membershipOnly,
'active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
return Product::query()->findOrFail($id);
}
it('trackNewPartner legt keinen Neupartner an bei reiner Mitgliedschaft ohne Starterpaket', function () {
$sponsor = User::forceCreate([
'email' => 'sp-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$newPartner = User::forceCreate([
'email' => 'np-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
'm_sponsor' => $sponsor->id,
]);
$membershipProduct = createProductForTest(membershipOnly: true);
$order = createWizardRegistrationOrderWithProducts($newPartner, $sponsor, [$membershipProduct]);
expect($order->qualifiesForIncentiveTrackedPartner())->toBeFalse();
$incentive = Incentive::factory()->create([
'qualification_start' => '2026-04-01',
'qualification_end' => '2026-07-31',
'calculation_end' => '2026-08-31',
'status' => 1,
]);
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $sponsor->id,
]);
IncentiveTracker::trackNewPartner($order);
expect(
IncentiveNewPartner::where('user_id', $newPartner->id)->exists()
)->toBeFalse();
});
it('trackNewPartner legt Neupartner an wenn Registrierung ein Starterpaket enthält', function () {
$sponsor = User::forceCreate([
'email' => 'sp-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$newPartner = User::forceCreate([
'email' => 'np-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
'm_sponsor' => $sponsor->id,
]);
$starterProduct = createProductForTest(membershipOnly: false);
$order = createWizardRegistrationOrderWithProducts($newPartner, $sponsor, [$starterProduct]);
expect($order->qualifiesForIncentiveTrackedPartner())->toBeTrue();
$incentive = Incentive::factory()->create([
'qualification_start' => '2026-04-01',
'qualification_end' => '2026-07-31',
'calculation_end' => '2026-08-31',
'status' => 1,
]);
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $sponsor->id,
]);
IncentiveTracker::trackNewPartner($order);
expect(
IncentiveNewPartner::where('user_id', $newPartner->id)->exists()
)->toBeTrue();
});

View file

@ -0,0 +1,37 @@
<?php
use App\Models\Incentive;
use App\Models\IncentiveParticipant;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('markiert Teilnehmer ohne Teilnahmebestaetigung als nicht oeffentlich (hasAcceptedTerms)', function () {
$incentive = Incentive::factory()->create();
$userA = User::forceCreate([
'email' => 'a-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userB = User::forceCreate([
'email' => 'b-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$anonymous = IncentiveParticipant::factory()->unconfirmed()->create([
'incentive_id' => $incentive->id,
'user_id' => $userA->id,
]);
$confirmed = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $userB->id,
'accepted_terms_at' => now(),
]);
expect($anonymous->hasAcceptedTerms())->toBeFalse()
->and($confirmed->hasAcceptedTerms())->toBeTrue();
});

View file

@ -0,0 +1,90 @@
<?php
use App\Http\Controllers\User\IncentiveController;
use App\Models\Incentive;
use App\Models\IncentiveParticipant;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('collectGalleryImages returns an array of image paths excluding the hero image', function () {
$incentive = Incentive::factory()->active()->create([
'image' => 'nikki-beach.jpg',
]);
$controller = new IncentiveController;
$method = new ReflectionMethod($controller, 'collectGalleryImages');
$method->setAccessible(true);
$result = $method->invoke($controller, $incentive);
expect($result)->toBeArray();
expect($result)->not->toContain('img/incentive/nikki-beach.jpg');
foreach ($result as $path) {
expect($path)->toStartWith('img/incentive/');
}
});
it('collectGalleryImages returns empty array when directory does not exist', function () {
$incentive = Incentive::factory()->active()->create([
'image' => 'nonexistent-hero.jpg',
]);
$controller = new IncentiveController;
$method = new ReflectionMethod($controller, 'collectGalleryImages');
$method->setAccessible(true);
$result = $method->invoke($controller, $incentive);
expect($result)->toBeArray();
});
it('collectGalleryImages returns sorted results', function () {
$incentive = Incentive::factory()->active()->create([
'image' => 'nikki-beach.jpg',
]);
$controller = new IncentiveController;
$method = new ReflectionMethod($controller, 'collectGalleryImages');
$method->setAccessible(true);
$result = $method->invoke($controller, $incentive);
$sorted = $result;
sort($sorted);
expect($result)->toBe($sorted);
});
it('teaser view receives galleryImages and participant data', function () {
$incentive = Incentive::factory()->active()->create();
$user = User::forceCreate([
'email' => 'teaser-' . uniqid('', true) . '@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
'active' => 1,
]);
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $user->id,
'accepted_terms_at' => now(),
]);
$this->actingAs($user);
$controller = new IncentiveController;
$response = $controller->teaser($incentive->slug);
$data = $response->getData();
expect($data)->toHaveKeys(['incentive', 'participant', 'galleryImages'])
->and($data['incentive']->id)->toBe($incentive->id)
->and($data['participant']->id)->toBe($participant->id)
->and($data['galleryImages'])->toBeArray();
});