Skip to content

Commit a8b5621

Browse files
author
Erik Porsche
committed
adding more tests
1 parent db877a9 commit a8b5621

File tree

4 files changed

+188
-14
lines changed

4 files changed

+188
-14
lines changed

app/Http/Livewire/TimeTracking/TimeTrackingManager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ public function updateTimeTracking($index)
262262

263263
public function updateTimeTrackingForm($timeTracking)
264264
{
265+
$this->managingTimeTrackingForId = $timeTracking->user_id;
266+
265267
$this->timeTrackingForm = array_merge_when(array_merge($this->timeTrackingForm,[
266268
'date' => app(DateFormatter::class)->formatDateForView($timeTracking->starts_at),
267269
'start_hour' => $timeTracking->starts_at->hour,

app/Policies/TimeTrackingPolicy.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public function updateTimeTracking(User $user, $managingTimeTrackingForId, Locat
1717
$user->id === (int)$managingTimeTrackingForId;
1818
}
1919

20-
2120
public function addTimeTracking(User $user, $managingTimeTrackingForId, Location $location)
2221
{
2322
return $user->isLocationAdmin($location) ||

tests/Feature/AddTimeTrackingForOtherUserTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@
22

33
namespace Tests\Feature;
44

5-
use Carbon\Carbon;
65
use Tests\TestCase;
76
use App\Models\User;
87
use Livewire\Livewire;
98
use App\Models\Location;
10-
use Illuminate\Foundation\Testing\WithFaker;
11-
use Illuminate\Foundation\Testing\RefreshDatabase;
129
use App\Http\Livewire\TimeTracking\TimeTrackingManager;
1310

1411
class AddTimeTrackingForOtherUserTest extends TestCase
1512
{
16-
/**
17-
* A basic feature test example.
18-
*
19-
* @return void
20-
*/
2113
public function test_can_create_time_for_other_user()
2214
{
2315
$user = User::factory([
@@ -65,11 +57,6 @@ public function test_can_create_time_for_other_user()
6557
]);
6658
}
6759

68-
/**
69-
* A basic feature test example.
70-
*
71-
* @return void
72-
*/
7360
public function test_employee_cannot_create_time_for_other_user()
7461
{
7562
$user = User::factory([
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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

Comments
 (0)