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 ...@@ -81,15 +81,18 @@ class RandomFormController extends Controller
$santa = $participants[$santaIdx]; $santa = $participants[$santaIdx];
$target = $participants[$targetIdx]; $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; $dearSantaLink = null;
if ($request->input('dearsanta')) { if ($request->input('dearsanta')) {
$dearSantaLink = $this->getDearSantaLink($santa, $request->input('dearsanta-expiration')); $dearSantaLink = $this->getDearSantaLink($superSanta, $request->input('dearsanta-expiration'));
} }
if (!empty($santa['email'])) { if (!empty($santa['email'])) {
......
...@@ -26,13 +26,34 @@ class RequestDearSantaTest extends RequestCase ...@@ -26,13 +26,34 @@ class RequestDearSantaTest extends RequestCase
NoCaptcha::shouldReceive('verifyResponse')->andReturn(true); NoCaptcha::shouldReceive('verifyResponse')->andReturn(true);
NoCaptcha::makePartial(); // We don't want to mock the display 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 // Initiate DearSanta
$content = $this->ajaxPost('/', [ $content = $this->ajaxPost('/', [
'g-recaptcha-response' => 'mocked', 'g-recaptcha-response' => 'mocked',
'name' => ['toto', 'tata', 'tutu'], 'name' => array_column($participants, 'name'),
'email' => ['test@test.com', 'test2@test.com', 'test3@test.com'], 'email' => array_column($participants, 'email'),
'phone' => ['', '', ''], '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', 'title' => 'test mail title',
'contentMail' => 'test mail {SANTA} => {TARGET}', 'contentMail' => 'test mail {SANTA} => {TARGET}',
'contentSMS' => '', 'contentSMS' => '',
...@@ -43,30 +64,37 @@ class RequestDearSantaTest extends RequestCase ...@@ -43,30 +64,37 @@ class RequestDearSantaTest extends RequestCase
// For security issues, the key is only sent by mail and never stored // For security issues, the key is only sent by mail and never stored
// So fetch it from the mail // So fetch it from the mail
$link = null; $links = [];
Mail::assertSent(\Korko\SecretSanta\Mail\TargetDrawn::class, function ($mail) use (&$link) { Mail::assertSent(\Korko\SecretSanta\Mail\TargetDrawn::class, function ($mail) use (&$links) {
$link = $mail->dearSantaLink; $links[] = $mail->dearSantaLink;
return true; return true;
}); });
$this->assertNotNull($link); $this->assertEquals(count($participants), count($links));
$path = parse_url($link, PHP_URL_PATH); foreach ($links as $id => $link) {
$key = parse_url($link, PHP_URL_FRAGMENT); $path = parse_url($link, PHP_URL_PATH);
$key = parse_url($link, PHP_URL_FRAGMENT);
// Get the form page (just to check http code) // Get the form page (just to check http code)
$response = $this->get($path); $response = $this->get($path);
$this->assertEquals(200, $response->status(), $response->__toString()); $this->assertEquals(200, $response->status(), $response->__toString());
// Try to contact santa // Try to contact santa
$content = $this->ajaxPost($path, [ $content = $this->ajaxPost($path, [
'g-recaptcha-response' => 'mocked', 'g-recaptcha-response' => 'mocked',
'key' => $key, 'key' => $key,
'title' => 'test dearsanta mail title', 'title' => 'test dearsanta mail title',
'content' => 'test dearsanta mail content', 'content' => 'test dearsanta mail content',
], 200); ], 200);
$this->assertEquals(['message' => 'Envoyé avec succès !'], $content); $this->assertEquals(['message' => 'Envoyé avec succès !'], $content);
Mail::assertSent(\Korko\SecretSanta\Mail\DearSanta::class, function ($mail) use ($id, $participants) {
$santaId = array_search($id, array_column($participants, 'target'));
$santa = $participants[$santaId];
Mail::assertSent(\Korko\SecretSanta\Mail\DearSanta::class); 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