Skip to content

Commit

Permalink
Add test of device delete
Browse files Browse the repository at this point in the history
  • Loading branch information
edwh committed Jan 4, 2022
1 parent d8841c1 commit 94d6bd8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
43 changes: 23 additions & 20 deletions app/Http/Controllers/DeviceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,27 +503,30 @@ public function delete(Request $request, $id)
$user = Auth::user();

$device = Device::find($id);
$eventId = $device->event;
$is_attending = EventsUsers::where('event', $device->event)->where('user', Auth::id())->first();
$is_attending = is_object($is_attending) && $is_attending->status == 1;

if (Fixometer::hasRole($user, 'Administrator') ||
Fixometer::userHasEditPartyPermission($eventId, $user->id) ||
$is_attending
) {
$device->delete();

if ($request->ajax()) {
$event = Party::find($eventId);
$stats = $event->getEventStats();

return response()->json([
'success' => true,
'stats' => $stats,
]);
}

return redirect('/party/view/'.$eventId)->with('success', 'Device has been deleted!');
if ($device) {
$eventId = $device->event;
$is_attending = EventsUsers::where('event', $device->event)->where('user', Auth::id())->first();
$is_attending = is_object($is_attending) && $is_attending->status == 1;

if (Fixometer::hasRole($user, 'Administrator') ||
Fixometer::userHasEditPartyPermission($eventId, $user->id) ||
$is_attending
) {
$device->delete();

if ($request->ajax()) {
$event = Party::find($eventId);
$stats = $event->getEventStats();

return response()->json([
'success' => true,
'stats' => $stats,
]);
}

return redirect('/party/view/'.$eventId)->with('success', 'Device has been deleted!');
}
}

if ($request->ajax()) {
Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/Devices/EditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,17 @@ public function testEdit()
$atts['quantity'] = 2;
$rsp = $this->post('/device/edit/' . $iddevices, $atts);
self::assertEquals('Device updated!', $rsp['success']);

# Delete the device.
$rsp = $this->get('/device/delete/' . $iddevices, [
'HTTP_X-Requested-With' => 'XMLHttpRequest'
]);
self::assertTrue($rsp['success']);

# Delete again - should fail.
$rsp = $this->get('/device/delete/' . $iddevices, [
'HTTP_X-Requested-With' => 'XMLHttpRequest'
]);
self::assertFalse($rsp['success']);
}
}

0 comments on commit 94d6bd8

Please sign in to comment.