Commit 2ef153c2 authored by Korko's avatar Korko
Browse files

Fix DearSanta. Email was sent to the santa himself, not his'

parent 06abe940
Pipeline #91112 passed with stage
in 5 minutes and 4 seconds
......@@ -81,15 +81,18 @@ class RandomFormController extends Controller
$santa = $participants[$santaIdx];
$target = $participants[$targetIdx];
$this->sendMessage($request, $santa, $target);
// Santa of that santa
$superSanta = $participants[array_search($santaIdx, $hat)];
$this->sendMessage($request, $santa, $target, $superSanta);
}
}
protected function sendMessage(Request $request, array $santa, array $target)
protected function sendMessage(Request $request, array $santa, array $target, array $superSanta)
{
$dearSantaLink = null;
if ($request->input('dearsanta')) {
$dearSantaLink = $this->getDearSantaLink($santa, $request->input('dearsanta-expiration'));
$dearSantaLink = $this->getDearSantaLink($superSanta, $request->input('dearsanta-expiration'));
}
if (!empty($santa['email'])) {
......
......@@ -26,13 +26,34 @@ class RequestDearSantaTest extends RequestCase
NoCaptcha::shouldReceive('verifyResponse')->andReturn(true);
NoCaptcha::makePartial(); // We don't want to mock the display
// Participants can only select one person, all the others will be excluded
$participants = [
[
'name' => 'toto',
'email' => 'test@test.com',
'target' => 1
],
[
'name' => 'tata',
'email' => 'test2@test.com',
'target' => 2
],
[
'name' => 'tutu',
'email' => 'test3@test.com',
'target' => 0
],
];
// Initiate DearSanta
$content = $this->ajaxPost('/', [
'g-recaptcha-response' => 'mocked',
'name' => ['toto', 'tata', 'tutu'],
'email' => ['test@test.com', 'test2@test.com', 'test3@test.com'],
'name' => array_column($participants, 'name'),
'email' => array_column($participants, 'email'),
'phone' => ['', '', ''],
'exclusions' => [['2']],
'exclusions' => array_map(function ($id) use ($participants) {
return array_values(array_map('strval', array_diff(array_keys($participants), [$id], [$participants[$id]['target']])));
}, array_keys($participants)),
'title' => 'test mail title',
'contentMail' => 'test mail {SANTA} => {TARGET}',
'contentSMS' => '',
......@@ -43,14 +64,15 @@ class RequestDearSantaTest extends RequestCase
// For security issues, the key is only sent by mail and never stored
// So fetch it from the mail
$link = null;
Mail::assertSent(\Korko\SecretSanta\Mail\TargetDrawn::class, function ($mail) use (&$link) {
$link = $mail->dearSantaLink;
$links = [];
Mail::assertSent(\Korko\SecretSanta\Mail\TargetDrawn::class, function ($mail) use (&$links) {
$links[] = $mail->dearSantaLink;
return true;
});
$this->assertNotNull($link);
$this->assertEquals(count($participants), count($links));
foreach ($links as $id => $link) {
$path = parse_url($link, PHP_URL_PATH);
$key = parse_url($link, PHP_URL_FRAGMENT);
......@@ -67,6 +89,12 @@ class RequestDearSantaTest extends RequestCase
], 200);
$this->assertEquals(['message' => 'Envoyé avec succès !'], $content);
Mail::assertSent(\Korko\SecretSanta\Mail\DearSanta::class);
Mail::assertSent(\Korko\SecretSanta\Mail\DearSanta::class, function ($mail) use ($id, $participants) {
$santaId = array_search($id, array_column($participants, 'target'));
$santa = $participants[$santaId];
return $mail->hasTo($santa['email']);
});
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment