|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use Tests\TestCase; |
| 6 | +use App\Models\User; |
| 7 | +use Livewire\Livewire; |
| 8 | +use App\Models\Location; |
| 9 | +use App\Http\Livewire\TimeTracking\TimeTrackingManager; |
| 10 | + |
| 11 | +class ChangeTimeTrackingForOtherUserTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * A basic feature test example. |
| 15 | + * |
| 16 | + * @return void |
| 17 | + */ |
| 18 | + public function test_can_change_time_for_other_user() |
| 19 | + { |
| 20 | + $user = User::factory([ |
| 21 | + 'date_of_employment' => '2020-11-01 07:47:05', |
| 22 | + 'current_location_id' => $location = Location::factory()->create() |
| 23 | + ])->withOwnedAccount()->hasTargetHours([ |
| 24 | + 'start_date' => '2020-11-01' |
| 25 | + ])->hasAttached($location, [ |
| 26 | + 'role' => 'admin' |
| 27 | + ])->create(); |
| 28 | + |
| 29 | + $location->users()->attach( |
| 30 | + $otherUser = User::factory([ |
| 31 | + 'current_location_id' => $location->id, |
| 32 | + 'date_of_employment' => '2020-11-01 07:47:05', |
| 33 | + ])->create(), |
| 34 | + ['role' => 'admin'] |
| 35 | + ); |
| 36 | + |
| 37 | + $timeTracking = $user->timeTrackings()->create([ |
| 38 | + 'location_id' => $location->id, |
| 39 | + 'starts_at' => '2020-11-01 08:00:00', |
| 40 | + 'ends_at' => '2020-11-01 17:00:00' |
| 41 | + ]); |
| 42 | + |
| 43 | + $this->actingAs($otherUser); |
| 44 | + |
| 45 | + Livewire::test(TimeTrackingManager::class)->set([ |
| 46 | + 'managingTimeTrackingForId' => $user->id, |
| 47 | + 'timeTrackingIdBeingUpdated' => $timeTracking->id, |
| 48 | + 'timeTrackingForm' => [ |
| 49 | + 'description' => 'testing', |
| 50 | + 'date' => '01.11.2020', |
| 51 | + 'start_hour' => 9, |
| 52 | + 'start_minute' => 0, |
| 53 | + 'end_hour' => 17, |
| 54 | + 'end_minute' => 0 |
| 55 | + ], |
| 56 | + 'pauseTimeForm' => [ |
| 57 | + [ |
| 58 | + 'start_hour' => 12, |
| 59 | + 'start_minute' => 00, |
| 60 | + 'end_hour' => 12, |
| 61 | + 'end_minute' => 30 |
| 62 | + ] |
| 63 | + ] |
| 64 | + ])->call('confirmUpdateTimeTracking'); |
| 65 | + |
| 66 | + $this->assertDatabaseHas('time_trackings', [ |
| 67 | + 'user_id' => $user->id, |
| 68 | + 'starts_at' => '2020-11-01 09:00:00' |
| 69 | + ]); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * A basic feature test example. |
| 74 | + * |
| 75 | + * @return void |
| 76 | + */ |
| 77 | + public function test_employee_cannot_change_time_for_other_user() |
| 78 | + { |
| 79 | + $user = User::factory([ |
| 80 | + 'date_of_employment' => '2020-11-01 07:47:05', |
| 81 | + 'current_location_id' => $location = Location::factory()->create() |
| 82 | + ])->withOwnedAccount()->hasTargetHours([ |
| 83 | + 'start_date' => '2020-11-01' |
| 84 | + ])->hasAttached($location, [ |
| 85 | + 'role' => 'admin' |
| 86 | + ])->create(); |
| 87 | + |
| 88 | + $location->users()->attach( |
| 89 | + $otherUser = User::factory([ |
| 90 | + 'current_location_id' => $location->id, |
| 91 | + 'date_of_employment' => '2020-11-01 07:47:05', |
| 92 | + ])->create(), |
| 93 | + ['role' => 'employee'] |
| 94 | + ); |
| 95 | + |
| 96 | + $timeTracking = $user->timeTrackings()->create([ |
| 97 | + 'location_id' => $location->id, |
| 98 | + 'starts_at' => '2020-11-01 08:00:00', |
| 99 | + 'ends_at' => '2020-11-01 17:00:00' |
| 100 | + ]); |
| 101 | + |
| 102 | + $this->actingAs($otherUser); |
| 103 | + |
| 104 | + Livewire::test(TimeTrackingManager::class)->set([ |
| 105 | + 'managingTimeTrackingForId' => $user->id, |
| 106 | + 'timeTrackingIdBeingUpdated' => $timeTracking->id, |
| 107 | + 'timeTrackingForm' => [ |
| 108 | + 'description' => 'testing', |
| 109 | + 'date' => '01.11.2020', |
| 110 | + 'start_hour' => 9, |
| 111 | + 'start_minute' => 0, |
| 112 | + 'end_hour' => 17, |
| 113 | + 'end_minute' => 0 |
| 114 | + ], |
| 115 | + 'pauseTimeForm' => [ |
| 116 | + [ |
| 117 | + 'start_hour' => 12, |
| 118 | + 'start_minute' => 00, |
| 119 | + 'end_hour' => 12, |
| 120 | + 'end_minute' => 30 |
| 121 | + ] |
| 122 | + ] |
| 123 | + ])->call('confirmUpdateTimeTracking')->assertStatus(403); |
| 124 | + |
| 125 | + $this->assertDatabaseHas('time_trackings', [ |
| 126 | + 'user_id' => $user->id, |
| 127 | + 'starts_at' => '2020-11-01 08:00:00' |
| 128 | + ]); |
| 129 | + } |
| 130 | + |
| 131 | + public function test_admin_cannot_create_time_for_other_location_user() |
| 132 | + { |
| 133 | + $user = User::factory([ |
| 134 | + 'date_of_employment' => '2020-11-01 07:47:05', |
| 135 | + 'current_location_id' => $location = Location::factory()->create() |
| 136 | + ])->withOwnedAccount()->hasTargetHours([ |
| 137 | + 'start_date' => '2020-11-01' |
| 138 | + ])->hasAttached($location, [ |
| 139 | + 'role' => 'employee' |
| 140 | + ])->create(); |
| 141 | + |
| 142 | + $otherUser = User::factory([ |
| 143 | + 'date_of_employment' => '2020-11-01 07:47:05', |
| 144 | + 'current_location_id' => $otherLocation = Location::factory()->create() |
| 145 | + ])->withOwnedAccount()->hasTargetHours([ |
| 146 | + 'start_date' => '2020-11-01' |
| 147 | + ])->hasAttached($otherLocation, [ |
| 148 | + 'role' => 'employee' |
| 149 | + ])->create(); |
| 150 | + |
| 151 | + $timeTracking = $user->timeTrackings()->create([ |
| 152 | + 'location_id' => $location->id, |
| 153 | + 'starts_at' => '2020-11-01 08:00:00', |
| 154 | + 'ends_at' => '2020-11-01 17:00:00' |
| 155 | + ]); |
| 156 | + |
| 157 | + $this->actingAs($otherUser); |
| 158 | + |
| 159 | + Livewire::test(TimeTrackingManager::class)->set([ |
| 160 | + 'managingTimeTrackingForId' => $user->id, |
| 161 | + 'timeTrackingIdBeingUpdated' => $timeTracking->id, |
| 162 | + 'timeTrackingForm' => [ |
| 163 | + 'description' => 'testing', |
| 164 | + 'date' => '01.11.2020', |
| 165 | + 'start_hour' => 9, |
| 166 | + 'start_minute' => 0, |
| 167 | + 'end_hour' => 17, |
| 168 | + 'end_minute' => 0 |
| 169 | + ], |
| 170 | + 'pauseTimeForm' => [ |
| 171 | + [ |
| 172 | + 'start_hour' => 12, |
| 173 | + 'start_minute' => 00, |
| 174 | + 'end_hour' => 12, |
| 175 | + 'end_minute' => 30 |
| 176 | + ] |
| 177 | + ] |
| 178 | + ])->call('confirmUpdateTimeTracking')->assertStatus(403); |
| 179 | + |
| 180 | + |
| 181 | + $this->assertDatabaseHas('time_trackings', [ |
| 182 | + 'user_id' => $user->id, |
| 183 | + 'starts_at' => '2020-11-01 08:00:00' |
| 184 | + ]); |
| 185 | + } |
| 186 | +} |
0 commit comments