|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Checkins; |
| 4 | + |
| 5 | +use App\Events\CheckoutableCheckedIn; |
| 6 | +use App\Models\Accessory; |
| 7 | +use App\Models\User; |
| 8 | +use App\Notifications\CheckinAccessoryNotification; |
| 9 | +use Illuminate\Support\Facades\Event; |
| 10 | +use Illuminate\Support\Facades\Notification; |
| 11 | +use Tests\Support\InteractsWithSettings; |
| 12 | +use Tests\TestCase; |
| 13 | + |
| 14 | +class AccessoryCheckinTest extends TestCase |
| 15 | +{ |
| 16 | + use InteractsWithSettings; |
| 17 | + |
| 18 | + public function testCheckingInAccessoryRequiresCorrectPermission() |
| 19 | + { |
| 20 | + $accessory = Accessory::factory()->checkedOutToUser()->create(); |
| 21 | + |
| 22 | + $this->actingAs(User::factory()->create()) |
| 23 | + ->post(route('accessories.checkin.store', $accessory->users->first()->pivot->id)) |
| 24 | + ->assertForbidden(); |
| 25 | + } |
| 26 | + |
| 27 | + public function testAccessoryCanBeCheckedIn() |
| 28 | + { |
| 29 | + Event::fake([CheckoutableCheckedIn::class]); |
| 30 | + |
| 31 | + $user = User::factory()->create(); |
| 32 | + $accessory = Accessory::factory()->checkedOutToUser($user)->create(); |
| 33 | + |
| 34 | + $this->assertTrue($accessory->users->contains($user)); |
| 35 | + |
| 36 | + $this->actingAs(User::factory()->checkinAccessories()->create()) |
| 37 | + ->post(route('accessories.checkin.store', $accessory->users->first()->pivot->id)); |
| 38 | + |
| 39 | + $this->assertFalse($accessory->fresh()->users->contains($user)); |
| 40 | + |
| 41 | + Event::assertDispatched(CheckoutableCheckedIn::class, 1); |
| 42 | + } |
| 43 | + |
| 44 | + public function testEmailSentToUserIfSettingEnabled() |
| 45 | + { |
| 46 | + Notification::fake(); |
| 47 | + |
| 48 | + $user = User::factory()->create(); |
| 49 | + $accessory = Accessory::factory()->checkedOutToUser($user)->create(); |
| 50 | + |
| 51 | + $accessory->category->update(['checkin_email' => true]); |
| 52 | + |
| 53 | + event(new CheckoutableCheckedIn( |
| 54 | + $accessory, |
| 55 | + $user, |
| 56 | + User::factory()->checkinAccessories()->create(), |
| 57 | + '', |
| 58 | + )); |
| 59 | + |
| 60 | + Notification::assertSentTo( |
| 61 | + [$user], |
| 62 | + function (CheckinAccessoryNotification $notification, $channels) { |
| 63 | + return in_array('mail', $channels); |
| 64 | + }, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public function testEmailNotSentToUserIfSettingDisabled() |
| 69 | + { |
| 70 | + Notification::fake(); |
| 71 | + |
| 72 | + $user = User::factory()->create(); |
| 73 | + $accessory = Accessory::factory()->checkedOutToUser($user)->create(); |
| 74 | + |
| 75 | + $accessory->category->update(['checkin_email' => false]); |
| 76 | + |
| 77 | + event(new CheckoutableCheckedIn( |
| 78 | + $accessory, |
| 79 | + $user, |
| 80 | + User::factory()->checkinAccessories()->create(), |
| 81 | + '', |
| 82 | + )); |
| 83 | + |
| 84 | + Notification::assertNotSentTo( |
| 85 | + [$user], |
| 86 | + function (CheckinAccessoryNotification $notification, $channels) { |
| 87 | + return in_array('mail', $channels); |
| 88 | + }, |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
0 commit comments