User::forceCreate([ 'email' => 'rank-'.uniqid('', true).'@example.com', 'password' => bcrypt('secret'), 'lang' => 'de', ]); it('vergibt Raenge zuerst an Qualifizierte, dann nach Punkten', function () use ($makeIncentiveRankingUser) { $incentive = Incentive::factory()->create(['max_winners' => 2]); $notQualifiedTopPoints = IncentiveParticipant::factory()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'is_qualified' => false, 'total_points' => 5000, ]); $qualifiedMidPoints = IncentiveParticipant::factory()->qualified()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'total_points' => 900, ]); $qualifiedLowPoints = IncentiveParticipant::factory()->qualified()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'total_points' => 400, ]); $zeroPoints = IncentiveParticipant::factory()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'total_points' => 0, 'rank' => 7, ]); IncentiveTracker::updateRanking($incentive); expect($qualifiedMidPoints->refresh()->rank)->toBe(1) ->and($qualifiedLowPoints->refresh()->rank)->toBe(2) ->and($notQualifiedTopPoints->refresh()->rank)->toBe(3) ->and($zeroPoints->refresh()->rank)->toBeNull(); }); it('macht Qualifizierte mit gespeichertem Rang innerhalb max_winners zu Gewinnern', function () use ($makeIncentiveRankingUser) { $incentive = Incentive::factory()->create(['max_winners' => 2]); $notQualifiedTopPoints = IncentiveParticipant::factory()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'is_qualified' => false, 'total_points' => 5000, ]); $qualifiedA = IncentiveParticipant::factory()->qualified()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'total_points' => 900, ]); $qualifiedB = IncentiveParticipant::factory()->qualified()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'total_points' => 400, ]); IncentiveTracker::updateRanking($incentive); expect($qualifiedA->refresh()->isWinner())->toBeTrue() ->and($qualifiedB->refresh()->isWinner())->toBeTrue() ->and($notQualifiedTopPoints->refresh()->isWinner())->toBeFalse(); }); it('sortiert bei Punktgleichstand bestaetigte Teilnehmer vor anonymen', function () use ($makeIncentiveRankingUser) { $incentive = Incentive::factory()->create(); $anonymous = IncentiveParticipant::factory()->unconfirmed()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'total_points' => 500, ]); $confirmed = IncentiveParticipant::factory()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'accepted_terms_at' => now(), 'total_points' => 500, ]); IncentiveTracker::updateRanking($incentive); expect($confirmed->refresh()->rank)->toBe(1) ->and($anonymous->refresh()->rank)->toBe(2); }); it('stimmt mit der Leaderboard-Anzeigereihenfolge ueberein', function () use ($makeIncentiveRankingUser) { $incentive = Incentive::factory()->create(); IncentiveParticipant::factory()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'is_qualified' => false, 'total_points' => 5000, ]); IncentiveParticipant::factory()->qualified()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'total_points' => 900, ]); IncentiveParticipant::factory()->qualified()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'total_points' => 400, ]); IncentiveParticipant::factory()->create([ 'incentive_id' => $incentive->id, 'user_id' => $makeIncentiveRankingUser()->id, 'is_qualified' => false, 'total_points' => 200, ]); IncentiveTracker::updateRanking($incentive); $displayOrder = IncentiveParticipant::query() ->where('incentive_id', $incentive->id) ->orderByIncentiveLeaderboard() ->pluck('rank') ->all(); expect($displayOrder)->toBe([1, 2, 3, 4]); });