-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from lewislarsen/add_unsubscribe_link_to_emails
Feature: item email unsubscribe link
- Loading branch information
Showing
13 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Item; | ||
use App\Models\User; | ||
use Illuminate\Http\RedirectResponse; | ||
|
||
class ItemEmailUnsubscribeController extends Controller | ||
{ | ||
public function __invoke(Item $item, User $user): RedirectResponse | ||
{ | ||
if (!$user->isSubscribedToItem($item)) { | ||
return redirect()->route('home'); | ||
} | ||
|
||
$user->toggleVoteSubscription($item->id, Item::class); | ||
|
||
return redirect()->route('items.show', $item->getAttributeValue('slug')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
return [ | ||
'link-expired.title' => 'Link expired', | ||
'link-expired' => 'The link you clicked has expired or otherwise has become invalid. Please try again.', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@section('title', trans('errors.link-expired.title')) | ||
|
||
<x-app> | ||
<p>{{ trans('errors.link-expired') }}</p> | ||
</x-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/Feature/Controllers/ItemEmailUnsubscribeControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
use App\Models\Item; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\URL; | ||
|
||
it('can unsubscribe a user from an item', function () { | ||
|
||
$user = User::factory()->create(); | ||
$item = Item::factory()->create(); | ||
|
||
DB::table('votes')->insert([ | ||
'user_id' => $user->id, | ||
'model_id' => $item->id, | ||
'model_type' => Item::class, | ||
'subscribed' => true, | ||
]); | ||
|
||
$this->assertTrue($user->isSubscribedToItem($item)); | ||
|
||
$response = $this->get(URL::signedRoute('items.email-unsubscribe', [ | ||
'item' => $item, | ||
'user' => $user, | ||
])); | ||
|
||
$this->assertFalse($user->isSubscribedToItem($item)); | ||
|
||
$this->assertDatabaseHas('votes', [ | ||
'user_id' => $user->id, | ||
'model_id' => $item->id, | ||
'model_type' => Item::class, | ||
'subscribed' => false, | ||
]); | ||
|
||
$response->assertRedirect(route('items.show', $item->getAttributeValue('slug'))); | ||
$this->assertGuest(); | ||
}); | ||
|
||
it('does not resubscribe a user who clicks the link whilst unsubscribed', function () { | ||
|
||
$user = User::factory()->create(); | ||
$item = Item::factory()->create(); | ||
|
||
DB::table('votes')->insert([ | ||
'user_id' => $user->id, | ||
'model_id' => $item->id, | ||
'model_type' => Item::class, | ||
'subscribed' => false, | ||
]); | ||
|
||
$this->assertFalse($user->isSubscribedToItem($item)); | ||
|
||
$response = $this->get(URL::signedRoute('items.email-unsubscribe', [ | ||
'item' => $item, | ||
'user' => $user, | ||
])); | ||
|
||
$this->assertFalse($user->isSubscribedToItem($item)); | ||
$response->assertRedirect(route('home')); | ||
$this->assertGuest(); | ||
}); | ||
|
||
it('returns a 403 if the signed link has been modified', function () { | ||
|
||
$user = User::factory()->create(); | ||
$item = Item::factory()->create(); | ||
|
||
$response = $this->get(URL::signedRoute('items.email-unsubscribe', [ | ||
'item' => $item, | ||
'user' => $user, | ||
]) . '&foo=bar'); | ||
|
||
$response->assertStatus(403); | ||
}); | ||
|
||
it('returns a 404 if the item does not exist', function () { | ||
|
||
$user = User::factory()->create(); | ||
$item = Item::factory()->create(); | ||
|
||
$response = $this->get(URL::signedRoute('items.email-unsubscribe', [ | ||
'item' => 999, | ||
'user' => $user, | ||
])); | ||
|
||
$response->assertStatus(404); | ||
}); | ||
|
||
it('returns a 404 if the user does not exist', function () { | ||
|
||
$user = User::factory()->create(); | ||
$item = Item::factory()->create(); | ||
|
||
$response = $this->get(URL::signedRoute('items.email-unsubscribe', [ | ||
'item' => $item, | ||
'user' => 999, | ||
])); | ||
|
||
$response->assertStatus(404); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters