188 lines
5.6 KiB
PHP
188 lines
5.6 KiB
PHP
<?php
|
|
|
|
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('zeigt alle Teilnehmer mit Aktivitaet in der User-Rangliste (kein Limit)', 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,
|
|
]);
|
|
}
|
|
|
|
// Teilnehmer ohne Punkte soll nicht erscheinen
|
|
IncentiveParticipant::factory()->create([
|
|
'incentive_id' => $incentive->id,
|
|
'user_id' => $makeUser()->id,
|
|
'total_points' => 0,
|
|
'qualified_partners' => 0,
|
|
'qualified_abos' => 0,
|
|
]);
|
|
|
|
$ranking = IncentiveParticipant::where('incentive_id', $incentive->id)
|
|
->withRankingActivity()
|
|
->orderByIncentiveLeaderboard()
|
|
->get();
|
|
|
|
expect($ranking)->toHaveCount(35);
|
|
});
|
|
|
|
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);
|
|
});
|