diff --git a/app/AbsenceCalendar/EmployeeAbsenceCalendar.php b/app/AbsenceCalendar/EmployeeAbsenceCalendar.php
index ca83f06..e811a56 100644
--- a/app/AbsenceCalendar/EmployeeAbsenceCalendar.php
+++ b/app/AbsenceCalendar/EmployeeAbsenceCalendar.php
@@ -29,7 +29,6 @@ public function __construct(User $employee, Location $location, CarbonPeriod $pe
$period->getStartDate()->startOfDay(),
$period->getEndDate()->endOfDay()
);
-
$this->calculateDays();
}
@@ -113,7 +112,6 @@ protected function diffHoursToStartOrEndOfDay(Carbon $date)
$diffInHours = BigDecimal::of($date->startOfDay()->diffInMinutes($this->endDay))
->dividedBy('60', 2, RoundingMode::HALF_EVEN);
}
-
return $diffInHours;
}
diff --git a/app/Actions/AddAbsence.php b/app/Actions/AddAbsence.php
index 1647888..3ec930f 100644
--- a/app/Actions/AddAbsence.php
+++ b/app/Actions/AddAbsence.php
@@ -7,9 +7,9 @@
use App\Models\Location;
use Carbon\CarbonPeriod;
use App\Models\AbsenceType;
-use Illuminate\Support\Arr;
use App\Contracts\AddsAbsences;
use App\Formatter\DateFormatter;
+use Laravel\Jetstream\Jetstream;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
@@ -29,11 +29,12 @@ public function __construct()
$this->dateFormatter = app(DateFormatter::class);
}
- public function add(User $employee, array $data): void
+ public function add(User $user, Location $location, int $managingAbsenceForId, array $data)
{
- Gate::forUser($employee)->authorize('addAbsence', [
+ Gate::forUser($user)->authorize('addAbsence', [
Absence::class,
- $employee->currentLocation
+ $managingAbsenceForId,
+ $location
]);
Validator::make($data, [
@@ -50,7 +51,6 @@ public function add(User $employee, array $data): void
'full_day' => ['required', 'boolean']
])->validateWithBag('addAbsence');
-
$startsAt = $this->dateFormatter->timeStrToCarbon($data['starts_at']);
$endsAt = $this->dateFormatter->timeStrToCarbon($data['ends_at']);
@@ -58,21 +58,22 @@ public function add(User $employee, array $data): void
if (isset($data['full_day']) && $data['full_day']) {
$startsAt = $startsAt->copy()->startOfDay();
$endsAt = $endsAt->copy()->endOfDay();
-
}
+ $addingAbsenceFor = Jetstream::findUserByIdOrFail($managingAbsenceForId);
+
$calculator = new AbsenceCalculator(
new EmployeeAbsenceCalendar(
- $employee,
- $employee->currentLocation,
+ $addingAbsenceFor,
+ $location,
new CarbonPeriod($startsAt, $endsAt)
),
AbsenceType::findOrFail($data['absence_type_id'])
);
- $absence = $employee->absences()->create(
+ $absence = $addingAbsenceFor->absences()->create(
[
- 'location_id' => $employee->currentLocation->id,
+ 'location_id' => $location->id,
'vacation_days' => $calculator->sumVacationDays(),
'paid_hours' => $calculator->sumPaidHours(),
'starts_at' => $startsAt,
@@ -80,10 +81,8 @@ public function add(User $employee, array $data): void
] + $data
);
- $admins = User::all()->filter
- ->hasLocationRole($employee->currentLocation, 'admin');
-
- Mail::to($admins)
- ->send(new NewAbsenceWaitingForApproval($absence, $employee));
+ Mail::to(
+ $location->allUsers()->filter->hasLocationRole($location, 'admin')
+ )->send(new NewAbsenceWaitingForApproval($absence, $addingAbsenceFor));
}
}
diff --git a/app/Actions/AddLocationMember.php b/app/Actions/AddLocationMember.php
index d5ada5a..64c1c80 100644
--- a/app/Actions/AddLocationMember.php
+++ b/app/Actions/AddLocationMember.php
@@ -117,9 +117,9 @@ protected function rules()
}
/**
- * Ensure that the user is not already on the team.
+ * Ensure that the user is not already on the location.
*
- * @param mixed $team
+ * @param mixed $location
* @param string $email
* @return \Closure
*/
@@ -129,7 +129,7 @@ protected function ensureUserIsNotAlreadyInLocation($location, string $email)
$validator->errors()->addIf(
$location->hasUserWithEmail($email),
'email',
- __('This user already belongs to the team.')
+ __('This user already belongs to the location.')
);
};
}
diff --git a/app/Actions/AddTimeTracking.php b/app/Actions/AddTimeTracking.php
index 0c0a183..a08b920 100644
--- a/app/Actions/AddTimeTracking.php
+++ b/app/Actions/AddTimeTracking.php
@@ -88,8 +88,8 @@ protected function ensureDateIsNotBeforeEmploymentDate($employee, $startsAt)
}
}
- protected function ensureGivenTimeIsNotOverlappingWithExisting($employee, $startsAt, $endsAt) {
-
+ protected function ensureGivenTimeIsNotOverlappingWithExisting($employee, $startsAt, $endsAt)
+ {
if ($employee->timeTrackings()->where(function ($query) use ($startsAt, $endsAt) {
$query->whereBetween('starts_at', [$startsAt, $endsAt])
->orWhereBetween('ends_at', [$startsAt, $endsAt])
diff --git a/app/Actions/ApproveAbscence.php b/app/Actions/ApproveAbscence.php
index 85dcc6c..17538c2 100644
--- a/app/Actions/ApproveAbscence.php
+++ b/app/Actions/ApproveAbscence.php
@@ -27,7 +27,7 @@ class ApproveAbscence implements ApprovesAbsence
* @param int $absenceId
* @return void
*/
- public function approve(User $user, $absenceId)
+ public function approve(User $user, Location $location, $absenceId)
{
Validator::make([
'absence_id' => $absenceId
@@ -57,6 +57,7 @@ public function bookVacationDays($absence)
//TODO: distribute absence days between available vacation entitlements
$currentVacationEntitlement = $absence->employee->currentVacationEntitlement();
+
if (!isset($currentVacationEntitlement) || !$currentVacationEntitlement->hasEnoughUnusedVacationDays($absence->vacation_days)) {
throw ValidationException::withMessages([
'error' => [__('Sorry, there is no fitting vacation entitlement for this absence.')],
diff --git a/app/Actions/InviteLocationMember.php b/app/Actions/InviteLocationMember.php
index a2dda1c..98e1f08 100644
--- a/app/Actions/InviteLocationMember.php
+++ b/app/Actions/InviteLocationMember.php
@@ -18,8 +18,6 @@ public function invite($user, $location, string $email, string $role = null)
$this->validate($location, $email, $role);
- // InvitinglocationMember::dispatch($location, $email, $role);
-
$invitation = $location->locationInvitations()->create([
'email' => $email,
'role' => $role,
diff --git a/app/Actions/RemoveAbsence.php b/app/Actions/RemoveAbsence.php
index 16e88f5..c8531db 100644
--- a/app/Actions/RemoveAbsence.php
+++ b/app/Actions/RemoveAbsence.php
@@ -2,20 +2,25 @@
namespace App\Actions;
+use DB;
use App\Models\User;
-use Illuminate\Support\Facades\Gate;
+use App\Models\Location;
use App\Mail\AbsenceRemoved;
use App\Contracts\RemovesAbsence;
+use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Mail;
-use DB;
class RemoveAbsence implements RemovesAbsence
{
- public function remove($user, $removesAbsenceId)
+ public function remove(User $user, Location $location, $removesAbsenceId)
{
- Gate::forUser($user)->authorize('removeAbsence', $user->currentLocation);
+ tap($location->absences()->whereKey($removesAbsenceId)->first(), function ($absence) use ($user, $location) {
- tap($user->currentLocation->absences()->whereKey($removesAbsenceId)->first(), function ($absence) {
+ Gate::forUser($user)->authorize('removeAbsence', [
+ Absence::class,
+ $absence,
+ $location
+ ]);
$absence->delete();
diff --git a/app/Contracts/AddsAbsences.php b/app/Contracts/AddsAbsences.php
index 68598ad..b5303b6 100644
--- a/app/Contracts/AddsAbsences.php
+++ b/app/Contracts/AddsAbsences.php
@@ -10,9 +10,10 @@ interface AddsAbsences
/**
* Add a absence for user and location
*
- * @param User $employee
+ * @param User $user
+ * @param mixed $addingAbsenceForId
* @param array $data
* @return void
*/
- public function add(User $employee, array $data) : void;
+ public function add(User $user, Location $location, int $addingAbsenceForId, array $data);
}
diff --git a/app/Contracts/ApprovesAbsence.php b/app/Contracts/ApprovesAbsence.php
index 66b5c3d..d527b96 100644
--- a/app/Contracts/ApprovesAbsence.php
+++ b/app/Contracts/ApprovesAbsence.php
@@ -2,10 +2,11 @@
namespace App\Contracts;
-use App\Models\Absence;
use App\Models\User;
+use App\Models\Absence;
+use App\Models\Location;
interface ApprovesAbsence
{
- public function approve(User $user, Absence $absence);
+ public function approve(User $user, Location $location, Absence $absence);
}
diff --git a/app/Contracts/RemovesAbsence.php b/app/Contracts/RemovesAbsence.php
index 6ee04ac..223c729 100644
--- a/app/Contracts/RemovesAbsence.php
+++ b/app/Contracts/RemovesAbsence.php
@@ -3,15 +3,17 @@
namespace App\Contracts;
use App\Models\User;
+use App\Models\Location;
interface RemovesAbsence
{
/**
* Remove a absence.
*
- * @param mixed $user
+ * @param User $user
+ * @param Location $location
* @param mixed $removesAbsenceId
* @return void
*/
- public function remove($user, $removesAbsenceId);
+ public function remove(User $user, Location $location, $removesAbsenceId);
}
diff --git a/app/Http/Controllers/AbsenceController.php b/app/Http/Controllers/AbsenceController.php
index 7778063..c8ae9d1 100644
--- a/app/Http/Controllers/AbsenceController.php
+++ b/app/Http/Controllers/AbsenceController.php
@@ -8,8 +8,6 @@ class AbsenceController extends Controller
{
public function __invoke(Request $request)
{
- return view('absences.index', [
- 'employee' => $request->user()
- ]);
+ return view('absences.index');
}
}
diff --git a/app/Http/Livewire/AbsenceManager.php b/app/Http/Livewire/AbsenceManager.php
index 538d2a2..eb7d290 100644
--- a/app/Http/Livewire/AbsenceManager.php
+++ b/app/Http/Livewire/AbsenceManager.php
@@ -10,14 +10,18 @@
use Livewire\WithPagination;
use App\Contracts\AddsAbsences;
use App\Formatter\DateFormatter;
+use Laravel\Jetstream\Jetstream;
use App\Contracts\RemovesAbsence;
use App\Contracts\ApprovesAbsence;
+use Illuminate\Support\Facades\Auth;
use App\AbsenceCalendar\AbsenceCalculator;
use App\AbsenceCalendar\EmployeeAbsenceCalendar;
+use App\Daybreak;
+use App\Traits\HasUser;
class AbsenceManager extends Component
{
- use WithPagination;
+ use WithPagination, HasUser;
public $absenceModal = false;
@@ -29,7 +33,15 @@ class AbsenceManager extends Component
public $minutes;
public $location;
- public $employee;
+
+ /**
+ * The user that is currently having its absence managed.
+ *
+ * @var mixed
+ */
+ public $managingAbsenceForId;
+
+ public $locationMembers;
public $startDate;
public $startHours = 9;
@@ -73,14 +85,16 @@ class AbsenceManager extends Component
public $employeeFilter = [];
- public $employeeOptions;
+ public $employeeSimpleSelectOptions;
+
+ public $employeeMultipleSelectOptions;
public function updated()
{
//TODO set to start of day and end of day if full day is activated...
$calendar = new EmployeeAbsenceCalendar(
- $this->employee,
- $this->employee->currentLocation,
+ $this->user,
+ $this->user->currentLocation,
new CarbonPeriod(
$this->startTimeStr(),
$this->endTimeStr()
@@ -98,8 +112,8 @@ public function refreshAbsenceHours()
{
//TODO set to start of day and end of day if full day is activated...
$calendar = new EmployeeAbsenceCalendar(
- $this->employee,
- $this->employee->currentLocation,
+ $this->user,
+ $this->user->currentLocation,
new CarbonPeriod(
$this->startTimeStr(),
$this->endTimeStr()
@@ -113,20 +127,21 @@ public function refreshAbsenceHours()
$this->paidHours = $calculator->sumPaidHours();
}
- public function mount(User $employee, DateFormatter $dateFormatter)
+ public function mount(DateFormatter $dateFormatter)
{
- $this->employee = $employee;
- $this->absenceTypes = $employee->absenceTypesForLocation($employee->currentLocation);
+ $this->absenceTypes = $this->user->absenceTypesForLocation($this->user->currentLocation);
$this->hours = range(0,23);
$this->minutes = range(0,59);
- $this->employeeOptions = $employee
- ->currentLocation
- ->allUsers()->pluck('name', 'id')
- ->mapToMultipleSelect();
+ $employeeSelectCollection = $this->user->currentLocation->allUsers()->pluck('name', 'id');
+
+ $this->employeeSimpleSelectOptions = $employeeSelectCollection->toArray();
+ $this->employeeMultipleSelectOptions = $employeeSelectCollection->mapToMultipleSelect();
+
+ $this->managingAbsenceForId = (string)$this->user->id;
$this->resetFormFields();
- $this->buildVacationInfoPanel($employee, $dateFormatter);
+ $this->buildVacationInfoPanel($this->user, $dateFormatter);
}
protected function buildVacationInfoPanel($employee, $dateFormatter)
@@ -152,13 +167,6 @@ protected function buildVacationInfoPanel($employee, $dateFormatter)
}
}
- public function switchEmployee()
- {
- $this->employee = $this->emyployee->currentLocation->allUsers()->first(function ($user) {
- return $user->id === (int)$this->employeeIdToBeSwitched;
- });
- }
-
public function openAbsenceModal()
{
$this->absenceModal = true;
@@ -191,17 +199,22 @@ public function addAbsence(AddsAbsences $adder)
{
$this->clearValidation();
- $this->addAbsenceForm['starts_at'] = $this->startTimeStr();
-
- $this->addAbsenceForm['ends_at'] = $this->endTimeStr();
-
- $this->addAbsenceForm['full_day'] = $this->hideTime;
+ $this->addAbsenceForm = array_merge($this->addAbsenceForm, [
+ 'starts_at' => $this->startTimeStr(),
+ 'ends_at' => $this->endTimeStr(),
+ 'full_day' => $this->hideTime
+ ]);
- $adder->add($this->employee, $this->addAbsenceForm);
+ $adder->add(
+ $this->user,
+ $this->user->currentLocation,
+ $this->managingAbsenceForId,
+ $this->addAbsenceForm
+ );
$this->resetFormfields();
- $this->employee = $this->employee->fresh();
+ $this->user = $this->user->fresh();
$this->absenceModal = false;
}
@@ -219,12 +232,13 @@ public function approveAbsence($absenceId, ApprovesAbsence $approver)
{
if (!empty($absenceId)) {
$approver->approve(
- $this->employee,
+ $this->user,
+ $this->user->currentLocation,
$absenceId
);
}
- $this->employee = $this->employee->fresh();
+ $this->user = $this->user->fresh();
}
public function confirmAbsenceRemoval($absenceId)
@@ -237,30 +251,30 @@ public function confirmAbsenceRemoval($absenceId)
public function removeAbsence(RemovesAbsence $remover)
{
$remover->remove(
- $this->employee,
+ $this->user,
+ $this->user->currentLocation,
$this->absenceIdBeingRemoved
);
$this->confirmingAbsenceRemoval = false;
$this->absenceIdBeingRemoved = null;
-
}
public function render()
{
- return view('livewire.absence', [
+ return view('absences.absence-manager', [
'calculatedDays' => $this->calculatedDays,
- 'absences' => $this->employee->currentLocation
+ 'absences' => $this->user->currentLocation
->absences()
->orderBy('status','DESC')
->latest()
->when(
- $this->employee->hasLocationPermission($this->employee->currentLocation, 'filterAbsences'),
+ $this->user->hasLocationPermission($this->user->currentLocation, 'filterAbsences'),
fn($query) => $query->filterEmployees(
collect($this->employeeFilter)->pluck('id')->toArray()
),
- fn($query) => $query->filterEmployees([$this->employee->id])
+ fn($query) => $query->filterEmployees([$this->user->id])
)
->paginate(10)
]);
diff --git a/app/Policies/AbsencePolicy.php b/app/Policies/AbsencePolicy.php
index 2a59398..8f47331 100644
--- a/app/Policies/AbsencePolicy.php
+++ b/app/Policies/AbsencePolicy.php
@@ -3,6 +3,7 @@
namespace App\Policies;
use App\Models\User;
+use App\Models\Absence;
use App\Models\Location;
use Illuminate\Auth\Access\HandlesAuthorization;
@@ -10,18 +11,30 @@ class AbsencePolicy
{
use HandlesAuthorization;
- public function addAbsence(User $user, Location $location)
+ public function manageAbsence(User $user, Location $location)
{
- return ($user->hasLocationPermission($location, 'addAbsence'));
+ return $user->hasLocationPermission($location, 'manageAbsence');
+ }
+
+ public function addAbsence(User $user, $managingAbsenceForId, Location $location)
+ {
+ return $user->isLocationAdmin($location) ||
+ $user->id === (int)$managingAbsenceForId;
+ }
+
+ public function removeAbsence(User $user, Absence $absence, Location $location)
+ {
+ return $user->hasLocationPermission($location, 'manageAbsence') ||
+ $user->id === $absence->user_id;
}
public function approveAbsence(User $user, Location $location)
{
- return ($user->hasLocationPermission($location, 'approveAbsence'));
+ return $user->hasLocationPermission($location, 'approveAbsence');
}
public function filterAbsences(User $user, Location $location)
{
- return ($user->hasLocationPermission($location, 'filterAbsences'));
+ return $user->hasLocationPermission($location, 'filterAbsences');
}
}
diff --git a/app/Policies/LocationPolicy.php b/app/Policies/LocationPolicy.php
index 6b2a75e..74ff962 100644
--- a/app/Policies/LocationPolicy.php
+++ b/app/Policies/LocationPolicy.php
@@ -31,12 +31,12 @@ public function removeAbsence(User $user, Location $location)
*/
public function addLocationMember(User $user, Location $location)
{
- return $user->ownsLocation($location);
+ return $user->hasLocationPermission($location, 'addLocationMember');
}
public function updateLocationMember(User $user, Location $location)
{
- return $user->ownsLocation($location);
+ return $user->hasLocationPermission($location, 'updateLocationMember');
}
/**
@@ -52,25 +52,25 @@ public function view(User $user, Location $location)
}
/**
- * Determine whether the user can view the model.
+ * Determine whether the user can update the model.
*
* @param \App\Models\User $user
- * @param \App\Models\Account $account
+ * @param \App\Models\Location $location
* @return mixed
*/
public function update(User $user, Location $location)
{
- return $user->ownsLocation($location);
+ return $user->hasLocationPermission($location, 'updateLocation');
}
public function removeLocationMember(User $user, Location $location)
{
- return $user->ownsLocation($location);
+ return $user->hasLocationPermission($location, 'removeLocationMember');
}
public function addLocationAbsentType(User $user, Location $location)
{
- return $user->ownsLocation($location);
+ return $user->hasLocationPermission($location, 'addLocationAbsentType');
}
public function addDefaultRestingTime(User $user, Location $location)
diff --git a/app/Providers/JetstreamServiceProvider.php b/app/Providers/JetstreamServiceProvider.php
index 0ab835f..8b086bd 100644
--- a/app/Providers/JetstreamServiceProvider.php
+++ b/app/Providers/JetstreamServiceProvider.php
@@ -37,11 +37,14 @@ public function boot()
*/
protected function configurePermissions()
{
- // Jetstream::defaultApiTokenPermissions(['read']);
Jetstream::role('admin', __('Location administrator'), [
- 'addAbsence',
+ 'updateLocation',
+ 'addLocationMember',
+ 'updateLocationMember',
+ 'removeLocationMember',
+ 'addLocationAbsentType',
+ 'manageAbsence',
'approveAbsence',
- 'removeAbsence',
'filterAbsences',
'addDefaultRestingTime',
'viewAnyTimeTracking',
@@ -52,8 +55,6 @@ protected function configurePermissions()
])->description(__('Location administrators can perform updates on a location.'));
Jetstream::role('employee', __('Employee'), [
- 'addAbsence',
- 'removeAbsence'
- ])->description(__('Employees have can create new working hours.'));
+ ])->description(__('Employees can create new working hours.'));
}
}
diff --git a/app/Traits/HasLocations.php b/app/Traits/HasLocations.php
index d1a64f0..8d1eaaf 100644
--- a/app/Traits/HasLocations.php
+++ b/app/Traits/HasLocations.php
@@ -185,4 +185,9 @@ public function hasLocationPermission($location, string $permission)
(Str::endsWith($permission, ':create') && in_array('*:create', $permissions)) ||
(Str::endsWith($permission, ':update') && in_array('*:update', $permissions));
}
+
+ public function isLocationAdmin($location)
+ {
+ return $this->hasLocationRole($location, 'admin');
+ }
}
diff --git a/app/Traits/HasUser.php b/app/Traits/HasUser.php
new file mode 100644
index 0000000..636cd7e
--- /dev/null
+++ b/app/Traits/HasUser.php
@@ -0,0 +1,18 @@
+ Location::factory(),
+ 'title' => 'Urlaub',
+ 'affect_vacation_times' => true,
+ 'affect_evaluations' => true,
+ 'evaluation_calculation_setting' => 'absent_to_target'
];
}
}
diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php
index 89fe098..a1cc146 100644
--- a/database/factories/UserFactory.php
+++ b/database/factories/UserFactory.php
@@ -35,31 +35,6 @@ public function definition()
];
}
- /**
- * Indicate that the user should be owner of a location.
- *
- * @return $this
- */
- public function withOwnedLocation()
- {
- return $this->has(
- Location::factory()
- ->state(
- function (array $attributes, User $user) {
- if (is_null($user->ownedAccount)) {
- throw new \LogicException("withOwnedAccount should be called before withOwnedLocation");
- }
-
- return [
- 'name' => $user->name.'\'s Location',
- 'owned_by' => $user->id,
- 'account_id' => $user->ownedAccount->id
- ];
- }),
- 'ownedLocations'
- );
- }
-
/**
* Indicate that the user should have own an account.
*
diff --git a/resources/lang/de.json b/resources/lang/de.json
index ef53e88..b85bb5d 100644
--- a/resources/lang/de.json
+++ b/resources/lang/de.json
@@ -2,7 +2,7 @@
"Location administrator": "Standortadministrator",
"Location administrators can perform updates on a location.": "Standortadministratoren können Aktualisierungen an einem Standort durchführen.",
"Employee": "Mitarbeiter",
- "Employees have can create new working hours.": "Mitarbeiter können neue Arbeitszeiten schaffen.",
+ "Employees can create new working hours.": "Mitarbeiter können neue Arbeitszeiten schaffen.",
"Great! You have accepted the invitation to join the :location team.": "Großartig! Sie haben die Einladung zum :location-Team angenommen.",
"Target to zero (to be implemented)": "Ziel auf Null (zu implementieren)",
"The target hours will be set to zero in the evaluation.": "Die Zielstunden werden in der Auswertung auf Null gesetzt.",
@@ -379,5 +379,6 @@
"Pause from x hours": "Pause ab x Stunden",
"Add default resting time": "Automatische Pausenzeit hinzufügen",
"Remove Default Pause Time": "Automatische Pausenzeit löschen",
- "Are you sure you would like to remove the default pause time from this location?": "Sind Sie sicher das Sie die automatische Pausen-Zeit von diesem Standort löschen möchten?"
+ "Are you sure you would like to remove the default pause time from this location?": "Sind Sie sicher das Sie die automatische Pausen-Zeit von diesem Standort löschen möchten?",
+ "Choose Employee": "Mitarbeiter auswählen"
}
diff --git a/resources/views/livewire/absence.blade.php b/resources/views/absences/absence-manager.blade.php
similarity index 96%
rename from resources/views/livewire/absence.blade.php
rename to resources/views/absences/absence-manager.blade.php
index 0903ec5..bb910ad 100644
--- a/resources/views/livewire/absence.blade.php
+++ b/resources/views/absences/absence-manager.blade.php
@@ -103,9 +103,8 @@
-
- @can('addAbsence', [App\Model\Absence::class, $this->employee->currentLocation])
+ @can('addAbsence', [App\Model\Absence::class, $this->managingAbsenceForId, $this->user->currentLocation])
{{ __('Add Absence') }}
@@ -145,13 +144,13 @@
- @can('filterAbsences', [App\Models\Absence::class, $employee->currentLocation])
+ @can('filterAbsences', [App\Models\Absence::class, $this->user->currentLocation])
|
@@ -184,7 +183,7 @@
|
- @if(Gate::check('approveAbsence', [App\Model\Absence::class, $this->employee->currentLocation]) && !$absence->isConfimred())
+ @if(Gate::check('approveAbsence', [App\Model\Absence::class, $this->user->currentLocation]) && !$absence->isConfimred())
{{ __('Approve') }}
@@ -213,9 +212,15 @@
{{ __('Add absence') }}
-
+ @can('manageAbsence', [App\Model\Absence::class, $this->user->currentLocation])
+
+
+
+
+
+ @endcan
diff --git a/resources/views/absences/index.blade.php b/resources/views/absences/index.blade.php
index 232d7ae..8f82309 100644
--- a/resources/views/absences/index.blade.php
+++ b/resources/views/absences/index.blade.php
@@ -7,7 +7,7 @@
- @livewire('absence-manager', [$employee])
+ @livewire('absence-manager')
diff --git a/tests/Feature/AbsenceTest.php b/tests/Feature/AbsenceTest.php
index c70c47e..39e9a2b 100644
--- a/tests/Feature/AbsenceTest.php
+++ b/tests/Feature/AbsenceTest.php
@@ -2,14 +2,15 @@
namespace Tests\Feature;
-use Carbon\Carbon;
use Tests\TestCase;
use App\Models\User;
-use App\Models\Account;
use App\Models\Location;
-use App\Actions\AddAbsence;
+use App\Contracts\AddsAbsences;
+use App\Contracts\AddsVacationEntitlements;
+use App\Contracts\ApprovesAbsence;
+use App\Http\Livewire\AbsenceManager;
use App\Models\AbsenceType;
-use Illuminate\Validation\ValidationException;
+use Livewire\Livewire;
class AbsenceTest extends TestCase
{
@@ -23,70 +24,79 @@ public function setUp(): void
{
parent::setUp();
- $this->user = User::factory()->hasTargetHours([
- "start_date" => Carbon::make('2020-11-16')
- ])->create();
-
- $this->account = Account::forceCreate([
- 'owned_by' => $this->user->id,
- 'name' => "Account"
- ]);
-
- $this->location = Location::forceCreate([
- 'account_id' => $this->account->id,
- 'owned_by' => $this->user->id,
- 'name' => "A Location",
- 'locale' => 'de',
- 'time_zone' => 'Europe/Berlin'
- ]);
-
- $this->user->switchLocation($this->location);
+ $this->user = User::factory([
+ 'date_of_employment' => '2020-11-01 07:47:05',
+ 'current_location_id' => $this->location = Location::factory()->create()
+ ])->withOwnedAccount()->hasTargetHours([
+ "start_date" => '2020-11-01'
+ ])->hasAttached(
+ $this->location, [
+ 'role' => 'admin'
+ ]
+ )->create();
}
public function test_end_date_must_be_after_start_date()
{
- $this->expectException(ValidationException::class);
+ $this->actingAs($this->user);
- $absenceType = AbsenceType::forceCreate([
+ $absenceType = AbsenceType::factory([
'location_id' => $this->location->id,
- 'title' => 'Urlaub',
- 'affect_vacation_times' => true,
- 'affect_evaluations' => true,
- 'evaluation_calculation_setting' => 'absent_to_target'
- ]);
+ ])->create();
$absenceType->users()->sync($this->user);
- $action = new AddAbsence();
-
- $action->add($this->user, [
- 'absence_type_id' => $absenceType->id,
- 'starts_at' => '20.11.2020 14:00',
- 'ends_at' => '20.11.2020 13:30',
- 'full_day' => false
- ]);
+ Livewire::test(AbsenceManager::class)
+ ->set(['addAbsenceForm' => [
+ 'absence_type_id' => $absenceType->id,
+ 'full_day' => false
+ ]])->set([
+ 'startDate' => "20.11.2020"
+ ])->set([
+ 'endDate' => "20.11.2020"
+ ])->set([
+ 'startHours' => 13
+ ])->set([
+ 'startMinutes' => 30
+ ])->set([
+ 'endHours' => 12
+ ])->set([
+ 'endMinutes' => 0
+ ])->call('addAbsence')->assertHasErrors(['ends_at']);
}
public function test_can_create_half_day_vacation()
{
- $absenceType = AbsenceType::forceCreate([
+ $this->actingAs($this->user);
+
+ $absenceType = AbsenceType::factory([
'location_id' => $this->location->id,
'title' => 'Urlaub',
'affect_vacation_times' => true,
'affect_evaluations' => true,
'evaluation_calculation_setting' => 'absent_to_target'
- ]);
+ ])->create();
$absenceType->users()->sync($this->user);
- $action = new AddAbsence();
-
- $action->add($this->user, [
- 'absence_type_id' => $absenceType->id,
- 'starts_at' => '20.11.2020 14:00',
- 'ends_at' => '20.11.2020 16:30',
- 'full_day' => false
- ]);
+ Livewire::test(AbsenceManager::class)
+ ->set(['addAbsenceForm' => [
+ 'absence_type_id' => $absenceType->id,
+ 'full_day' => false
+ ]])->set(['hideTime' => false])
+ ->set([
+ 'startDate' => "20.11.2020"
+ ])->set([
+ 'endDate' => "20.11.2020"
+ ])->set([
+ 'startHours' => 14
+ ])->set([
+ 'startMinutes' => 00
+ ])->set([
+ 'endHours' => 16
+ ])->set([
+ 'endMinutes' => 30
+ ])->call('addAbsence');
$this->assertDatabaseHas('absences',[
'absence_type_id' => $absenceType->id,
@@ -97,6 +107,8 @@ public function test_can_create_half_day_vacation()
public function test_can_create_half_days_vacation()
{
+ $this->actingAs($this->user);
+
$absenceType = AbsenceType::forceCreate([
'location_id' => $this->location->id,
'title' => 'Urlaub',
@@ -107,14 +119,24 @@ public function test_can_create_half_days_vacation()
$absenceType->users()->sync($this->user);
- $action = new AddAbsence();
-
- $action->add($this->user, [
- 'absence_type_id' => $absenceType->id,
- 'starts_at' => '19.11.2020 22:00',
- 'ends_at' => '20.11.2020 02:30',
- 'full_day' => false
- ]);
+ Livewire::test(AbsenceManager::class)
+ ->set(['addAbsenceForm' => [
+ 'absence_type_id' => $absenceType->id,
+ 'full_day' => false
+ ]])->set(['hideTime' => false])
+ ->set([
+ 'startDate' => "19.11.2020"
+ ])->set([
+ 'endDate' => "20.11.2020"
+ ])->set([
+ 'startHours' => 22
+ ])->set([
+ 'startMinutes' => 00
+ ])->set([
+ 'endHours' => 2
+ ])->set([
+ 'endMinutes' => 30
+ ])->call('addAbsence');
$this->assertDatabaseHas('absences',[
'absence_type_id' => $absenceType->id,
@@ -125,6 +147,8 @@ public function test_can_create_half_days_vacation()
public function test_can_create_three_days_illness()
{
+ $this->actingAs($this->user);
+
$absenceType = AbsenceType::forceCreate([
'location_id' => $this->location->id,
'title' => 'Krankheit',
@@ -135,14 +159,24 @@ public function test_can_create_three_days_illness()
$absenceType->users()->sync($this->user);
- $action = new AddAbsence();
-
- $action->add($this->user, [
- 'absence_type_id' => $absenceType->id,
- 'starts_at' => '18.11.2020 22:00',
- 'ends_at' => '20.11.2020 02:30',
- 'full_day' => false
- ]);
+ Livewire::test(AbsenceManager::class)
+ ->set(['addAbsenceForm' => [
+ 'absence_type_id' => $absenceType->id,
+ 'full_day' => false
+ ]])->set(['hideTime' => false])
+ ->set([
+ 'startDate' => "18.11.2020"
+ ])->set([
+ 'endDate' => "20.11.2020"
+ ])->set([
+ 'startHours' => 22
+ ])->set([
+ 'startMinutes' => 00
+ ])->set([
+ 'endHours' => 2
+ ])->set([
+ 'endMinutes' => 30
+ ])->call('addAbsence');
$this->assertDatabaseHas('absences',[
'absence_type_id' => $absenceType->id,
@@ -153,6 +187,8 @@ public function test_can_create_three_days_illness()
public function test_can_create_half_day_illness()
{
+ $this->actingAs($this->user);
+
$absenceType = AbsenceType::forceCreate([
'location_id' => $this->location->id,
'title' => 'Krankheit',
@@ -163,14 +199,24 @@ public function test_can_create_half_day_illness()
$absenceType->users()->sync($this->user);
- $action = new AddAbsence();
-
- $action->add($this->user, [
- 'absence_type_id' => $absenceType->id,
- 'starts_at' => '20.11.2020 14:00',
- 'ends_at' => '20.11.2020 16:30',
- 'full_day' => false
- ]);
+ Livewire::test(AbsenceManager::class)
+ ->set(['addAbsenceForm' => [
+ 'absence_type_id' => $absenceType->id,
+ 'full_day' => false
+ ]])->set(['hideTime' => false])
+ ->set([
+ 'startDate' => "20.11.2020"
+ ])->set([
+ 'endDate' => "20.11.2020"
+ ])->set([
+ 'startHours' => 14
+ ])->set([
+ 'startMinutes' => 00
+ ])->set([
+ 'endHours' => 16
+ ])->set([
+ 'endMinutes' => 30
+ ])->call('addAbsence');
$this->assertDatabaseHas('absences',[
'absence_type_id' => $absenceType->id,
@@ -181,6 +227,10 @@ public function test_can_create_half_day_illness()
public function test_can_approve_two_week_vacation()
{
+ $this->actingAs($this->user);
+
+ $this->travelTo($this->user->date_of_employment);
+
$absenceType = AbsenceType::forceCreate([
'location_id' => $this->location->id,
'title' => 'Urlaub',
@@ -191,9 +241,21 @@ public function test_can_approve_two_week_vacation()
$absenceType->users()->sync($this->user);
- $action = new AddAbsence();
+ $action = app(AddsVacationEntitlements::class);
$action->add($this->user, [
+ 'name' => 'Jahresurlaub',
+ 'starts_at' => '01.01.2020',
+ 'ends_at' => '31.12.2020',
+ 'days' => 30,
+ 'expires' => true,
+ 'transfer_remaining' => 0,
+ 'end_of_transfer_period' => '01.03.2021'
+ ]);
+
+ $action = app(AddsAbsences::class);
+
+ $action->add($this->user, $this->location, $this->user->id, [
'absence_type_id' => $absenceType->id,
'starts_at' => '20.11.2020 14:00',
'ends_at' => '27.11.2020 16:30',
@@ -203,7 +265,23 @@ public function test_can_approve_two_week_vacation()
$this->assertDatabaseHas('absences',[
'absence_type_id' => $absenceType->id,
'paid_hours' => 48,
- 'vacation_days' => 6
+ 'vacation_days' => 6,
+ 'status' => 'pending'
+ ]);
+
+ $action = app(ApprovesAbsence::class);
+
+ $action->approve(
+ $this->user,
+ $this->location,
+ $this->user->absences->first()->id
+ );
+
+ $this->assertDatabaseHas('absences',[
+ 'absence_type_id' => $absenceType->id,
+ 'paid_hours' => 48,
+ 'vacation_days' => 6,
+ 'status' => 'confirmed'
]);
}
}
diff --git a/tests/Feature/CreateLocationTest.php b/tests/Feature/CreateLocationTest.php
index 3052ead..007e8b2 100644
--- a/tests/Feature/CreateLocationTest.php
+++ b/tests/Feature/CreateLocationTest.php
@@ -17,7 +17,6 @@ public function test_locations_can_be_created()
$this->actingAs(
$user = User::factory()
->withOwnedAccount()
- ->withOwnedLocation()
->create()
);
@@ -31,6 +30,5 @@ public function test_locations_can_be_created()
->call('confirmAddLocation');
$this->assertEquals('Test Location', $user->fresh()->ownedLocations()->latest('id')->first()->name);
- $this->assertCount(2, $user->fresh()->ownedLocations);
}
}
diff --git a/tests/Feature/DeleteAbsenceTest.php b/tests/Feature/DeleteAbsenceTest.php
index 298322b..cd30c4f 100644
--- a/tests/Feature/DeleteAbsenceTest.php
+++ b/tests/Feature/DeleteAbsenceTest.php
@@ -5,6 +5,7 @@
use Tests\TestCase;
use App\Models\User;
use Livewire\Livewire;
+use App\Models\Location;
use App\Http\Livewire\AbsenceManager;
class DeleteAbsenceTest extends TestCase
@@ -16,9 +17,16 @@ class DeleteAbsenceTest extends TestCase
*/
public function test_can_delete_absence()
{
- $this->actingAs($user = User::factory()->withOwnedAccount()->withOwnedLocation()->create());
+ $this->actingAs($user = User::factory()->withOwnedAccount()->create());
- $user->switchLocation($user->ownedLocations()->first());
+ $location = Location::factory()->create();
+
+ $location->users()->attach(
+ $user,
+ ['role' => 'admin']
+ );
+
+ $user->switchLocation($location);
$absentType = $user->allLocations()->first()->absentTypes()->create([
'title' => 'Illness',
@@ -33,7 +41,6 @@ public function test_can_delete_absence()
$absentType->users()->sync($user);
-
$absence = $user->absences()->create([
'location_id' => $user->allLocations()->first()->id,
'vacation_days' => 2,
@@ -44,8 +51,6 @@ public function test_can_delete_absence()
'absence_type_id' => 1,
]);
-
- // dd($user->currentLocation);
Livewire::test(AbsenceManager::class, ['employee' => $user])->set([
'absenceIdBeingRemoved' => $absence->id
])->call('removeAbsence');
diff --git a/tests/Feature/DeleteLocationTest.php b/tests/Feature/DeleteLocationTest.php
index 82ce95f..59a4542 100644
--- a/tests/Feature/DeleteLocationTest.php
+++ b/tests/Feature/DeleteLocationTest.php
@@ -18,7 +18,6 @@ public function test_locations_can_be_deleted()
$this->actingAs(
$user = User::factory()
->withOwnedAccount()
- ->withOwnedLocation()
->create()
);
@@ -30,8 +29,7 @@ public function test_locations_can_be_deleted()
['role' => 'test-role']
);
- $component = Livewire::test(
- LocationManager::class, [
+ Livewire::test(LocationManager::class, [
'account' => $user->ownedAccount,
'employee' => $user
])->set([
diff --git a/tests/Feature/EvaluationTest.php b/tests/Feature/EvaluationTest.php
index d7b8d4a..8eb24fb 100644
--- a/tests/Feature/EvaluationTest.php
+++ b/tests/Feature/EvaluationTest.php
@@ -68,7 +68,7 @@ public function test_can_submit_fullday_illness()
*/
$action = app(AddsAbsences::class);
- $action->add($this->user, [
+ $action->add($this->user, $this->location, $this->user->id, [
'absence_type_id' => $absenceType->id,
'starts_at' => '20.11.2020 00:00',
'ends_at' => '20.11.2020 00:00',
@@ -90,6 +90,7 @@ public function test_can_submit_fullday_illness()
$approver->approve(
$this->user,
+ $this->location,
Absence::first()->id
);
@@ -131,11 +132,6 @@ public function test_can_submit_fullday_illness()
);
}
- public function test_can_submit_illness()
- {
- //
- }
-
//Überstunden
//Urlaub
diff --git a/tests/Feature/InviteLocationMemberTest.php b/tests/Feature/InviteLocationMemberTest.php
index 4935199..b2b6d04 100644
--- a/tests/Feature/InviteLocationMemberTest.php
+++ b/tests/Feature/InviteLocationMemberTest.php
@@ -5,11 +5,11 @@
use Tests\TestCase;
use App\Models\User;
use Livewire\Livewire;
+use App\Models\Location;
use App\Mail\LocationInvitation;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Http\Livewire\Locations\LocationMemberManager;
-use Laravel\Jetstream\Http\Livewire\TeamMemberManager;
class InviteLocationMemberTest extends TestCase
{
@@ -20,19 +20,21 @@ public function test_members_can_be_invited_to_location()
Mail::fake();
$this->actingAs(
- $user = User::factory()
- ->withOwnedAccount()
- ->withOwnedLocation()
- ->create()
+ $user = User::factory([
+ 'date_of_employment' => '2020-11-01 07:47:05',
+ 'current_location_id' => $location = Location::factory()->create()
+ ])->withOwnedAccount()->hasTargetHours([
+ 'start_date' => '2020-11-01'
+ ])->hasAttached($location, [
+ 'role' => 'admin'
+ ])->create()
);
- $user->switchLocation($user->ownedLocations()->first());
-
- $component = Livewire::test(LocationMemberManager::class, ['location' => $user->currentLocation])
- ->set('addLocationMemberForm', [
- 'email' => 'test@example.com',
- 'role' => 'admin',
- ])->call('addLocationMember');
+ Livewire::test(LocationMemberManager::class, ['location' => $user->currentLocation])
+ ->set('addLocationMemberForm', [
+ 'email' => 'test@example.com',
+ 'role' => 'admin',
+ ])->call('addLocationMember');
Mail::assertQueued(LocationInvitation::class);
@@ -41,27 +43,31 @@ public function test_members_can_be_invited_to_location()
public function test_member_invitations_can_be_cancelled()
{
+
$this->actingAs(
- $user = User::factory()
- ->withOwnedAccount()
- ->withOwnedLocation()
- ->create()
+ $user = User::factory([
+ 'date_of_employment' => '2020-11-01 07:47:05',
+ ])->withOwnedAccount()->hasTargetHours([
+ 'start_date' => '2020-11-01'
+ ])->hasAttached(Location::factory()->state(function (array $attributes, User $user) {
+ return ['owned_by' => $user->id];
+ }), [
+ 'role' => 'admin'
+ ])->create()
);
- $user->switchLocation($user->ownedLocations()->first());
-
// Add the location member...
- $component = Livewire::test(LocationMemberManager::class, ['location' => $user->currentLocation])
+ $component = Livewire::test(LocationMemberManager::class, ['location' => $user->locations()->first()])
->set('addLocationMemberForm', [
'email' => 'test@example.com',
'role' => 'admin',
])->call('addLocationMember');
- $invitationId = $user->currentLocation->fresh()->locationInvitations->first()->id;
+ $invitationId = $user->locations()->first()->locationInvitations->first()->id;
// Cancel the location invitation...
$component->call('cancelLocationInvitation', $invitationId);
- $this->assertCount(0, $user->currentLocation->fresh()->locationInvitations);
+ $this->assertCount(0, $user->locations()->first()->locationInvitations);
}
}
diff --git a/tests/Feature/LeaveLocationTest.php b/tests/Feature/LeaveLocationTest.php
index 48c3c27..34a3039 100644
--- a/tests/Feature/LeaveLocationTest.php
+++ b/tests/Feature/LeaveLocationTest.php
@@ -5,61 +5,59 @@
use Tests\TestCase;
use App\Models\User;
use Livewire\Livewire;
-use Illuminate\Support\Facades\DB;
-use Illuminate\Support\Facades\Auth;
-use App\Providers\RouteServiceProvider;
-use Illuminate\Auth\AuthenticationException;
+use App\Models\Location;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Http\Livewire\Locations\LocationMemberManager;
-use Laravel\Jetstream\Http\Livewire\TeamMemberManager;
-use Laravel\Jetstream\Http\Livewire\LogoutOtherBrowserSessionsForm;
class LeaveLocationTest extends TestCase
{
use RefreshDatabase;
- public function test_users_can_leave_locatios()
+ public function test_users_can_leave_locations()
{
$user = User::factory()
->withOwnedAccount()
- ->withOwnedLocation()
- ->create();
+ ->create()
+ ->ownedLocations()
+ ->save($location = Location::factory()->make());
- $user->switchLocation(
- $user->ownedLocations->first()
- );
-
- $user->currentLocation->users()->attach(
+ $location->users()->attach(
$otherUser = User::factory()->create(),
['role' => 'admin']
);
+ $this->assertCount(1, $location->fresh()->users);
+
$this->actingAs($otherUser);
- $component = Livewire::test(LocationMemberManager::class, ['location' => $user->currentLocation])
- ->call('leaveLocation');
+ Livewire::test(LocationMemberManager::class, ['location' => $location])
+ ->call('leaveLocation');
- $this->assertCount(0, $user->currentLocation->fresh()->users);
+ $this->assertCount(0, $location->fresh()->users);
}
public function test_location_owners_cant_leave_their_own_location()
{
- $this->actingAs(
- $user = User::factory()
- ->withOwnedAccount()
- ->withOwnedLocation()
- ->create()
- );
- $user->switchLocation(
- $user->ownedLocations->first()
+ $this->actingAs(
+ $user = User::factory([
+ 'date_of_employment' => '2020-11-01 07:47:05',
+ ])->withOwnedAccount()->hasTargetHours([
+ 'start_date' => '2020-11-01'
+ ])->hasAttached($location = Location::factory()->state(
+ function (array $attributes, User $user) {
+ return ['owned_by' => $user->id];
+ }
+ ), [
+ 'role' => 'admin'
+ ])->create()
);
- $component = Livewire::test(LocationMemberManager::class, ['location' => $user->currentLocation])
- ->call('leaveLocation')
- ->assertHasErrors(['location']);
+ Livewire::test(LocationMemberManager::class, ['location' => $user->ownedLocations->first()])
+ ->call('leaveLocation')
+ ->assertHasErrors(['location']);
- $this->assertNotNull($user->currentLocation->fresh());
+ $this->assertNotNull($user->fresh()->ownedLocations);
}
public function test_user_will_be_logged_out_when_location_gets_deleted()
diff --git a/tests/Feature/RemoveLocationMemberTest.php b/tests/Feature/RemoveLocationMemberTest.php
index 2bbaa57..5003303 100644
--- a/tests/Feature/RemoveLocationMemberTest.php
+++ b/tests/Feature/RemoveLocationMemberTest.php
@@ -5,9 +5,9 @@
use Tests\TestCase;
use App\Models\User;
use Livewire\Livewire;
+use App\Models\Location;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Http\Livewire\Locations\LocationMemberManager;
-use Laravel\Jetstream\Http\Livewire\TeamMemberManager;
class RemoveLocationMemberTest extends TestCase
{
@@ -15,44 +15,44 @@ class RemoveLocationMemberTest extends TestCase
public function test_location_members_can_be_removed_from_locations()
{
- $this->actingAs(
- $user = User::factory()
- ->withOwnedAccount()
- ->withOwnedLocation()
- ->create()
- );
+ $user = User::factory()
+ ->withOwnedAccount()
+ ->create();
- $user->switchLocation($user->ownedLocations()->first());
+ $location = $user->ownedLocations()->save($location = Location::factory()->make());
- $user->currentLocation->users()->attach(
- $otherUser = User::factory()->create(), ['role' => 'admin']
+ $location->users()->attach(
+ $otherUser = User::factory()->create(),
+ ['role' => 'admin']
);
- $component = Livewire::test(LocationMemberManager::class, ['location' => $user->currentLocation])
- ->set('locationMemberIdBeingRemoved', $otherUser->id)
- ->call('removeLocationMember');
+ $this->actingAs($user);
- $this->assertCount(0, $user->currentLocation->fresh()->users);
+ $component = Livewire::test(LocationMemberManager::class, ['location' => $location])
+ ->set('locationMemberIdBeingRemoved', $otherUser->id)
+ ->call('removeLocationMember');
+
+ $this->assertCount(0, $location->fresh()->users);
}
- public function test_only_location_owner_can_remove_location_members()
+ public function test_only_location_admin_can_remove_location_members()
{
$user = User::factory()
->withOwnedAccount()
- ->withOwnedLocation()
- ->create();
-
- $user->switchLocation($user->ownedLocations()->first());
+ ->create()
+ ->ownedLocations()
+ ->save($location = Location::factory()->make());
- $user->currentLocation->users()->attach(
- $otherUser = User::factory()->create(), ['role' => 'admin']
+ $location->users()->attach(
+ $otherUser = User::factory()->create(),
+ ['role' => 'employee']
);
$this->actingAs($otherUser);
- $component = Livewire::test(LocationMemberManager::class, ['location' => $user->currentLocation])
- ->set('locationMemberIdBeingRemoved', $user->id)
- ->call('removeLocationMember')
- ->assertStatus(403);
+ $component = Livewire::test(LocationMemberManager::class, ['location' => $location])
+ ->set('locationMemberIdBeingRemoved', $user->id)
+ ->call('removeLocationMember')
+ ->assertStatus(403);
}
}
diff --git a/tests/Feature/UpdateLocationMemberRoleTest.php b/tests/Feature/UpdateLocationMemberRoleTest.php
index 93c7da5..cced677 100644
--- a/tests/Feature/UpdateLocationMemberRoleTest.php
+++ b/tests/Feature/UpdateLocationMemberRoleTest.php
@@ -5,6 +5,7 @@
use Tests\TestCase;
use App\Models\User;
use Livewire\Livewire;
+use App\Models\Location;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Http\Livewire\Locations\LocationMemberManager;
@@ -17,50 +18,52 @@ public function test_location_member_roles_can_be_updated()
$this->actingAs(
$user = User::factory()
->withOwnedAccount()
- ->withOwnedLocation()
->create()
);
- $user->switchLocation($user->ownedLocations()->first());
+ $location = $user->ownedLocations()
+ ->save(Location::factory()->make());
- $user->currentLocation->users()->attach(
- $otherUser = User::factory()->create(), ['role' => 'admin']
+ $location->users()->attach(
+ $otherUser = User::factory()->create(),
+ ['role' => 'admin']
);
- $component = Livewire::test(LocationMemberManager::class, ['location' => $user->currentLocation])
- ->set('managingRoleFor', $otherUser)
- ->set('currentRole', 'employee')
- ->call('updateRole');
+ $component = Livewire::test(LocationMemberManager::class, ['location' => $location->fresh()])
+ ->set('managingRoleFor', $otherUser)
+ ->set('currentRole', 'employee')
+ ->call('updateRole');
$this->assertTrue($otherUser->fresh()->hasLocationRole(
- $user->currentLocation->fresh(),
+ $location,
'employee'
));
}
- public function test_only_location_owner_can_update_location_owner_roles()
+ public function test_only_location_admin_can_update_location_owner_roles()
{
$user = User::factory()
->withOwnedAccount()
- ->withOwnedLocation()
->create();
- $user->switchLocation($user->ownedLocations()->first());
+ $location = $user->ownedLocations()
+ ->save(Location::factory()->make());
- $user->currentLocation->users()->attach(
- $otherUser = User::factory()->create(), ['role' => 'admin']
+ $location->users()->attach(
+ $otherUser = User::factory()->create(),
+ ['role' => 'employee']
);
$this->actingAs($otherUser);
- $component = Livewire::test(LocationMemberManager::class, ['location' => $user->currentLocation])
+ $component = Livewire::test(LocationMemberManager::class, ['location' => $location->fresh()])
->set('managingRoleFor', $otherUser)
- ->set('currentRole', 'editor')
+ ->set('currentRole', 'admin')
->call('updateRole')
->assertStatus(403);
- $this->assertTrue($otherUser->fresh()->hasLocationRole(
- $user->currentLocation->fresh(), 'admin'
- ));
+ $this->assertTrue(
+ $otherUser->fresh()->hasLocationRole($location, 'employee')
+ );
}
}
diff --git a/tests/Feature/UpdateLocationNameTest.php b/tests/Feature/UpdateLocationNameTest.php
index d3bb420..daac3a5 100644
--- a/tests/Feature/UpdateLocationNameTest.php
+++ b/tests/Feature/UpdateLocationNameTest.php
@@ -5,6 +5,7 @@
use Tests\TestCase;
use App\Models\User;
use Livewire\Livewire;
+use App\Models\Location;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Http\Livewire\Locations\UpdateLocationNameForm;
@@ -14,22 +15,24 @@ class UpdateLocationNameTest extends TestCase
public function test_location_names_can_be_updated()
{
- $this->actingAs(
- $user = User::factory()
- ->withOwnedAccount()
- ->withOwnedLocation()
- ->create()
- );
+ $user = User::factory()
+ ->withOwnedAccount()
+ ->create();
+
+ $location = $user->ownedLocations()
+ ->save($location = Location::factory()->make());
- $user->switchLocation(
- $user->ownedLocations->first()
+ $location->users()->attach(
+ $otherUser = User::factory()->create(),
+ ['role' => 'admin']
);
- Livewire::test(UpdateLocationNameForm::class, ['location' => $user->currentLocation])
- ->set(['state' => ['name' => 'Test Location']])
- ->call('updateLocationName');
+ $this->actingAs($user);
+
+ Livewire::test(UpdateLocationNameForm::class, ['location' => $location->fresh()])
+ ->set(['state' => ['name' => 'Test Location']])
+ ->call('updateLocationName');
- $this->assertCount(1, $user->fresh()->ownedLocations);
- $this->assertEquals('Test Location', $user->currentLocation->fresh()->name);
+ $this->assertEquals('Test Location', $location->fresh()->name);
}
}
|