diff --git a/app/Http/Controllers/EvaluationGridTemplateController.php b/app/Http/Controllers/EvaluationGridTemplateController.php index 0f5e9eba..21b2abde 100644 --- a/app/Http/Controllers/EvaluationGridTemplateController.php +++ b/app/Http/Controllers/EvaluationGridTemplateController.php @@ -85,7 +85,7 @@ public function update(EvaluationGridTemplateUpdateRequest $request, Course $cou $currentRowTemplates = $evaluationGridTemplate->evaluationGridRowTemplates; $specifiedRowTemplates = $data['row_templates']->whereNotNull('id')->unique('id'); - $newRowTemplates = collect($data['row_templates'])->whereNull('id'); + $newRowTemplates = $data['row_templates']->where('id', null); $deletableRowTemplates = $currentRowTemplates->whereNotIn('id', $specifiedRowTemplates->pluck('id')); $existingRowTemplates = $specifiedRowTemplates->whereIn('id', $currentRowTemplates->pluck('id')); diff --git a/database/factories/EvaluationGridTemplateFactory.php b/database/factories/EvaluationGridTemplateFactory.php index f5c9b53c..d3591b2c 100644 --- a/database/factories/EvaluationGridTemplateFactory.php +++ b/database/factories/EvaluationGridTemplateFactory.php @@ -65,9 +65,10 @@ public function withRequirements($count = null) { }); } - public function withRowTemplates() { + public function withRowTemplates($count = null) { + if (!$count) $count = $this->faker->biasedNumberBetween(4, 10); return $this->has( - EvaluationGridRowTemplate::factory()->withOrdering()->count($this->faker->biasedNumberBetween(4, 10)), + EvaluationGridRowTemplate::factory()->withOrdering()->count($count), 'evaluationGridRowTemplates' ); } diff --git a/tests/Feature/Admin/EvaluationGridTemplate/CreateEvaluationGridTemplateTest.php b/tests/Feature/Admin/EvaluationGridTemplate/CreateEvaluationGridTemplateTest.php index ffe49deb..f9c6bb2f 100644 --- a/tests/Feature/Admin/EvaluationGridTemplate/CreateEvaluationGridTemplateTest.php +++ b/tests/Feature/Admin/EvaluationGridTemplate/CreateEvaluationGridTemplateTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Admin\EvaluationGridTemplate; use App\Models\Course; +use App\Models\EvaluationGridRow; use App\Models\EvaluationGridRowTemplate; use App\Models\EvaluationGridTemplate; use Illuminate\Testing\TestResponse; @@ -500,7 +501,7 @@ public function test_shouldValidateNewEvaluationGridRowTemplateData_invalidOrder $this->assertEquals('Sortierreihenfolge muss eine Zahl sein.', $exception->validator->errors()->first()); } - public function test_shouldValidateNewEvaluationGridRowTemplateData_reassignsOrderDuringUpdate() { + public function test_shouldProcessNewEvaluationGridRowTemplateData_reassignsOrder() { // given $payload = $this->payload; $payload['row_templates'][0]['order'] = 3; @@ -520,4 +521,19 @@ public function test_shouldValidateNewEvaluationGridRowTemplateData_reassignsOrd $this->assertEquals(2, EvaluationGridRowTemplate::where(['criterion' => 'Test criterion 3'])->first()->order); $this->assertEquals(3, EvaluationGridRowTemplate::where(['criterion' => 'Test criterion 5'])->first()->order); } + + public function test_shouldProcessNewEvaluationGridRowTemplateData_createsNewRowTemplates() { + // given + $numRowTemplates = EvaluationGridRowTemplate::all()->count(); + $numRows = EvaluationGridRow::all()->count(); + + // when + $response = $this->post('/course/' . $this->courseId . '/admin/evaluation_grids', $this->payload); + + // then + $response->assertStatus(302); + $response->assertRedirect('/course/' . $this->courseId . '/admin/evaluation_grids'); + $this->assertEquals($numRowTemplates + 3, EvaluationGridRowTemplate::all()->count(), 'should have created 3 new evaluation grid row templates'); + $this->assertEquals($numRows, EvaluationGridRow::all()->count(), 'should not have created any new evaluation grid rows'); + } } diff --git a/tests/Feature/Admin/EvaluationGridTemplate/ReadEvaluationGridTemplateTest.php b/tests/Feature/Admin/EvaluationGridTemplate/ReadEvaluationGridTemplateTest.php index 0595153a..a2bb2b17 100644 --- a/tests/Feature/Admin/EvaluationGridTemplate/ReadEvaluationGridTemplateTest.php +++ b/tests/Feature/Admin/EvaluationGridTemplate/ReadEvaluationGridTemplateTest.php @@ -65,7 +65,7 @@ public function test_shouldNotDisplayEvaluationGrid_fromOtherCourseOfSameUser() public function test_shouldNotDisplayEvaluationGrid_fromOtherUser() { // given $otherCourseId = $this->createCourse('Zweiter Kurs', '', false); - $otherEvaluationGridTemplateId = $this->createEvaluationGridTemplate('Fremdes Beurteilungsraster', $otherCourseId); + $otherEvaluationGridTemplateId = $this->createEvaluationGridTemplate('Fremdes Beurteilungsraster', null, $otherCourseId); // when $response = $this->get('/course/' . $otherCourseId . '/admin/evaluation_grids/' . $otherEvaluationGridTemplateId); diff --git a/tests/Feature/Admin/EvaluationGridTemplate/UpdateEvaluationGridTemplateTest.php b/tests/Feature/Admin/EvaluationGridTemplate/UpdateEvaluationGridTemplateTest.php index 3d404366..9205b99f 100644 --- a/tests/Feature/Admin/EvaluationGridTemplate/UpdateEvaluationGridTemplateTest.php +++ b/tests/Feature/Admin/EvaluationGridTemplate/UpdateEvaluationGridTemplateTest.php @@ -3,6 +3,8 @@ namespace Tests\Feature\Admin\EvaluationGridTemplate; use App\Models\Course; +use App\Models\EvaluationGrid; +use App\Models\EvaluationGridRow; use App\Models\EvaluationGridRowTemplate; use App\Models\EvaluationGridTemplate; use Illuminate\Testing\TestResponse; @@ -14,12 +16,14 @@ class UpdateEvaluationGridTemplateTest extends TestCaseWithBasicData { private $course; private $payload; private $evaluationGridTemplateId; + private $rowTemplateIds; public function setUp(): void { parent::setUp(); $this->course = Course::find($this->courseId); - $this->evaluationGridTemplateId = $this->createEvaluationGridTemplate('Unternehmungsplanung'); + $this->evaluationGridTemplateId = $this->createEvaluationGridTemplate('Unternehmungsplanung', 3); + $this->rowTemplateIds = EvaluationGridTemplate::find($this->evaluationGridTemplateId)->evaluationGridRowTemplates()->pluck('id'); $requirement = $this->createRequirement(); $this->payload = [ @@ -31,16 +35,19 @@ public function setUp(): void { 'criterion' => 'Routenwahl', 'control_type' => 'heading', 'order' => 1, + 'id' => $this->rowTemplateIds[0], ], [ 'criterion' => 'Stufengerecht für Wolfsstufe', 'control_type' => 'radiobuttons', 'order' => 2, + 'id' => $this->rowTemplateIds[1], ], [ 'criterion' => 'Höhepunkt eingebaut', 'control_type' => 'checkbox', 'order' => 3, + 'id' => $this->rowTemplateIds[2], ], ], ]; @@ -479,4 +486,102 @@ public function test_shouldValidateNewEvaluationGridRowTemplateData_reassignsOrd $this->assertEquals(2, EvaluationGridRowTemplate::where(['criterion' => 'Test criterion 3'])->first()->order); $this->assertEquals(3, EvaluationGridRowTemplate::where(['criterion' => 'Test criterion 5'])->first()->order); } + + public function test_shouldProcessNewEvaluationGridRowTemplateData_updatesExistingRowTemplates_keepingExistingRows() { + // given + $this->createEvaluationGrid($this->evaluationGridTemplateId); + $numRowTemplates = EvaluationGridRowTemplate::all()->count(); + $numRows = EvaluationGridRow::all()->count(); + $rowIds = EvaluationGridRowTemplate::whereIn('id', $this->rowTemplateIds)->get()->flatMap->evaluation_grid_rows->map->id; + + // when + $response = $this->post('/course/' . $this->courseId . '/admin/evaluation_grids/' . $this->evaluationGridTemplateId, $this->payload); + + // then + $response->assertStatus(302); + $response->assertRedirect('/course/' . $this->courseId . '/admin/evaluation_grids'); + $this->assertEquals($numRowTemplates, EvaluationGridRowTemplate::all()->count(), 'should have kept the 3 evaluation grid row templates'); + foreach($this->rowTemplateIds as $index => $id) { + $this->assertTrue(EvaluationGridRowTemplate::where(['id' => $id])->exists()); + $rowTemplate = EvaluationGridRowTemplate::find($id); + $this->assertEquals($this->payload['row_templates'][$index]['criterion'], $rowTemplate->criterion); + $this->assertEquals($this->payload['row_templates'][$index]['control_type'], $rowTemplate->control_type); + $this->assertEquals($index + 1, $rowTemplate->order); + } + $this->assertEquals($numRows, EvaluationGridRow::all()->count(), 'should have kept the 3 evaluation grid rows'); + foreach($rowIds as $id) { + $this->assertTrue(EvaluationGridRow::where(['id' => $id])->exists()); + } + } + + public function test_shouldProcessNewEvaluationGridRowTemplateData_deletesExistingRowTemplates_deletesExistingRows() { + // given + $this->createEvaluationGrid($this->evaluationGridTemplateId); + $numRowTemplates = EvaluationGridRowTemplate::all()->count(); + $numRows = EvaluationGridRow::all()->count(); + $rowIds = EvaluationGridRowTemplate::whereIn('id', $this->rowTemplateIds)->get()->flatMap->evaluation_grid_rows->map->id; + $payload = $this->payload; + unset($payload['row_templates'][0]['id']); + unset($payload['row_templates'][1]); + unset($payload['row_templates'][2]); + + // when + $response = $this->post('/course/' . $this->courseId . '/admin/evaluation_grids/' . $this->evaluationGridTemplateId, $payload); + + // then + $response->assertStatus(302); + $response->assertRedirect('/course/' . $this->courseId . '/admin/evaluation_grids'); + $this->assertEquals($numRowTemplates - 2, EvaluationGridRowTemplate::all()->count(), 'should have deleted 3 and created 1 evaluation grid row template'); + foreach($this->rowTemplateIds as $id) { + $this->assertFalse(EvaluationGridRowTemplate::where(['id' => $id])->exists()); + } + $this->assertEquals($numRows - 2, EvaluationGridRow::all()->count(), 'should have deleted 3 and created 1 evaluation grid row'); + foreach($rowIds as $id) { + $this->assertFalse(EvaluationGridRow::where(['id' => $id])->exists()); + } + } + + public function test_shouldProcessNewEvaluationGridRowTemplateData_createsNewRowTemplates_creatingNewRows() { + // given + $this->createEvaluationGrid($this->evaluationGridTemplateId); + $numRowTemplates = EvaluationGridRowTemplate::all()->count(); + $numRows = EvaluationGridRow::all()->count(); + $rowIds = EvaluationGridRowTemplate::whereIn('id', $this->rowTemplateIds)->get()->flatMap->evaluation_grid_rows->map->id; + $payload = $this->payload; + $payload['row_templates'][] = [ + 'criterion' => 'Neues Kriterium', + 'control_type' => 'radiobuttons', + 'order' => 0, + ]; + + // when + $response = $this->post('/course/' . $this->courseId . '/admin/evaluation_grids/' . $this->evaluationGridTemplateId, $payload); + + // then + $response->assertStatus(302); + $response->assertRedirect('/course/' . $this->courseId . '/admin/evaluation_grids'); + $this->assertEquals($numRowTemplates + 1, EvaluationGridRowTemplate::all()->count(), 'should have added 1 new evaluation grid row template'); + foreach($this->rowTemplateIds as $index => $id) { + $this->assertTrue(EvaluationGridRowTemplate::where(['id' => $id])->exists()); + $rowTemplate = EvaluationGridRowTemplate::find($id); + $this->assertEquals($payload['row_templates'][$index]['criterion'], $rowTemplate->criterion); + $this->assertEquals($payload['row_templates'][$index]['control_type'], $rowTemplate->control_type); + $this->assertEquals($index + 2, $rowTemplate->order); + } + + $newRowTemplate = EvaluationGridRowTemplate::find(EvaluationGridRowTemplate::max('id')); + $this->assertEquals('Neues Kriterium', $newRowTemplate->criterion); + $this->assertEquals('radiobuttons', $newRowTemplate->control_type); + $this->assertEquals(1, $newRowTemplate->order); + + $this->assertEquals($numRows + 1, EvaluationGridRow::all()->count(), 'should have added 1 new evaluation grid row'); + foreach($rowIds as $id) { + $this->assertTrue(EvaluationGridRow::where(['id' => $id])->exists()); + } + + $newRow = EvaluationGridRow::find(EvaluationGridRow::max('id')); + $this->assertEquals($newRowTemplate->id, $newRow->evaluation_grid_row_template_id); + $this->assertNull($newRow->value); + $this->assertNull($newRow->notes); + } } diff --git a/tests/Feature/EvaluationGrid/ReadEvaluationGridTest.php b/tests/Feature/EvaluationGrid/ReadEvaluationGridTest.php index 233634c9..5394c362 100644 --- a/tests/Feature/EvaluationGrid/ReadEvaluationGridTest.php +++ b/tests/Feature/EvaluationGrid/ReadEvaluationGridTest.php @@ -75,7 +75,7 @@ public function test_shouldNotDisplayEvaluationGrid_fromOtherCourse() { $this->createBlock('Block 1', '1.1', '01.01.2019', null, $otherCourseId); $otherUserId = $this->createUser(['name' => 'Lindo'])->id; Course::find($otherCourseId)->users()->attach($otherUserId); - $otherEvaluationGridTemplateId = $this->createEvaluationGridTemplate('Unternehmungsplanung', $otherCourseId); + $otherEvaluationGridTemplateId = $this->createEvaluationGridTemplate('Unternehmungsplanung', null, $otherCourseId); $otherEvaluationGridId = $this->createEvaluationGrid($otherEvaluationGridTemplateId); // when diff --git a/tests/TestCaseWithBasicData.php b/tests/TestCaseWithBasicData.php index e7e9da9f..a5a6f099 100644 --- a/tests/TestCaseWithBasicData.php +++ b/tests/TestCaseWithBasicData.php @@ -45,13 +45,13 @@ protected function createObservationAssignment($name = "Test Auftrag", $particip return $observation_assignment->id; } - protected function createEvaluationGridTemplate($name = 'Unternehmungsplanung', $courseId = null) { + protected function createEvaluationGridTemplate($name = 'Unternehmungsplanung', $numRowTemplates = null, $courseId = null) { $course = Course::find($courseId === null ? $this->courseId : $courseId); return EvaluationGridTemplate::factory() ->state(['name' => $name, 'course_id' => $course->id]) ->withBlocks(1) ->withRequirements(1) - ->withRowTemplates() + ->withRowTemplates($numRowTemplates) ->create() ->id; }