|
4 | 4 |
|
5 | 5 | use App\Mail\CheckoutAssetMail; |
6 | 6 | use App\Models\Asset; |
| 7 | +use App\Models\Location; |
7 | 8 | use App\Models\User; |
8 | 9 | use Illuminate\Support\Facades\Mail; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
9 | 11 | use PHPUnit\Framework\ExpectationFailedException; |
10 | 12 | use Tests\TestCase; |
11 | 13 |
|
@@ -89,4 +91,63 @@ public function testHandleMissingModelBeingIncluded() |
89 | 91 | $this->fail('Asset checkout email was sent when the entire checkout failed.'); |
90 | 92 | } |
91 | 93 | } |
| 94 | + |
| 95 | + public static function checkoutTargets() |
| 96 | + { |
| 97 | + yield 'Checkout to user' => [ |
| 98 | + function () { |
| 99 | + return [ |
| 100 | + 'type' => 'user', |
| 101 | + 'target' => User::factory()->forCompany()->create(), |
| 102 | + ]; |
| 103 | + } |
| 104 | + ]; |
| 105 | + |
| 106 | + yield 'Checkout to asset' => [ |
| 107 | + function () { |
| 108 | + return [ |
| 109 | + 'type' => 'asset', |
| 110 | + 'target' => Asset::factory()->forCompany()->create(), |
| 111 | + ]; |
| 112 | + } |
| 113 | + ]; |
| 114 | + |
| 115 | + yield 'Checkout to location' => [ |
| 116 | + function () { |
| 117 | + return [ |
| 118 | + 'type' => 'location', |
| 119 | + 'target' => Location::factory()->forCompany()->create(), |
| 120 | + ]; |
| 121 | + } |
| 122 | + ]; |
| 123 | + } |
| 124 | + |
| 125 | + #[DataProvider('checkoutTargets')] |
| 126 | + public function test_prevents_checkouts_of_checked_out_items($data) |
| 127 | + { |
| 128 | + ['type' => $type, 'target' => $target] = $data(); |
| 129 | + |
| 130 | + $asset = Asset::factory()->create(); |
| 131 | + $checkedOutAsset = Asset::factory()->assignedToUser()->create(); |
| 132 | + $existingUserId = $checkedOutAsset->assigned_to; |
| 133 | + |
| 134 | + $response = $this->actingAs(User::factory()->superuser()->create()) |
| 135 | + ->post(route('hardware.bulkcheckout.store'), [ |
| 136 | + 'selected_assets' => [ |
| 137 | + $asset->id, |
| 138 | + $checkedOutAsset->id, |
| 139 | + ], |
| 140 | + 'checkout_to_type' => $type, |
| 141 | + "assigned_$type" => $target->id, |
| 142 | + ]); |
| 143 | + |
| 144 | + $this->assertEquals( |
| 145 | + $existingUserId, |
| 146 | + $checkedOutAsset->fresh()->assigned_to, |
| 147 | + 'Asset was checked out when it should have been prevented.' |
| 148 | + ); |
| 149 | + |
| 150 | + // ensure redirected back |
| 151 | + $response->assertRedirectToRoute('hardware.bulkcheckout.show'); |
| 152 | + } |
92 | 153 | } |
0 commit comments