From a60c5987abf9ef8748011eb8566b4a387b878eb3 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sat, 19 Aug 2023 14:13:34 +0200 Subject: [PATCH] Worklog: create log entry on successfull creation --- src/Controllers/Admin/UserWorkLogController.php | 14 ++++++++++++-- .../Admin/UserWorkLogControllerTest.php | 4 +++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Controllers/Admin/UserWorkLogController.php b/src/Controllers/Admin/UserWorkLogController.php index 4289e332..69453bb0 100644 --- a/src/Controllers/Admin/UserWorkLogController.php +++ b/src/Controllers/Admin/UserWorkLogController.php @@ -64,9 +64,9 @@ class UserWorkLogController extends BaseController $user = $this->user->findOrFail($userId); $data = $this->validate($request, [ - 'work_date' => 'required|date:Y-m-d', + 'work_date' => 'required|date:Y-m-d', 'work_hours' => 'float|min:0', - 'comment' => 'required|max:200', + 'comment' => 'required|max:200', ]); if (isset($worklogId)) { @@ -85,6 +85,16 @@ class UserWorkLogController extends BaseController $worklog->comment = $data['comment']; $worklog->save(); + $this->log->info( + 'Added worklog for {name} ({id}) at {time} about {hours}h: {text}', + [ + 'name' => $user->name, + 'id' => $user->id, + 'time' => $worklog->worked_at, + 'hours' => $worklog->hours, + 'text' => $worklog->comment, + ] + ); $this->addNotification(isset($worklogId) ? 'worklog.edit.success' : 'worklog.add.success'); return $this->redirect->to('/users?action=view&user_id=' . $userId); diff --git a/tests/Unit/Controllers/Admin/UserWorkLogControllerTest.php b/tests/Unit/Controllers/Admin/UserWorkLogControllerTest.php index 92bec537..13682685 100644 --- a/tests/Unit/Controllers/Admin/UserWorkLogControllerTest.php +++ b/tests/Unit/Controllers/Admin/UserWorkLogControllerTest.php @@ -109,7 +109,7 @@ class UserWorkLogControllerTest extends ControllerTest /** * @covers \Engelsystem\Controllers\Admin\UserWorkLogController::saveWorklog */ - public function testSaveWorklogWithUnkownUserIdThrows(): void + public function testSaveWorklogWithUnknownUserIdThrows(): void { $request = $this->request->withAttribute('user_id', 1234)->withParsedBody([]); $this->expectException(ModelNotFoundException::class); @@ -147,6 +147,8 @@ class UserWorkLogControllerTest extends ControllerTest $this->controller->saveWorklog($request); $this->assertHasNotification('worklog.add.success'); + $this->assertTrue($this->log->hasInfoThatContains('Added worklog for')); + $this->assertEquals(1, $this->user->worklogs->count()); $new_worklog = $this->user->worklogs[0]; $this->assertEquals($this->user->id, $new_worklog->user->id);