Skip to content

Commit

Permalink
Add tests for updating evaluation grid row templates and rows
Browse files Browse the repository at this point in the history
  • Loading branch information
carlobeltrame committed Aug 4, 2024
1 parent 6e5decd commit 7dcc843
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/EvaluationGridTemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down
5 changes: 3 additions & 2 deletions database/factories/EvaluationGridTemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [
Expand All @@ -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],
],
],
];
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/Feature/EvaluationGrid/ReadEvaluationGridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCaseWithBasicData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 7dcc843

Please sign in to comment.