adrema/tests/Feature/Activity/UpdateTest.php

162 lines
5.5 KiB
PHP
Raw Normal View History

2023-02-15 01:36:59 +01:00
<?php
namespace Tests\Feature\Activity;
use App\Activity;
use App\Subactivity;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class UpdateTest extends TestCase
{
use DatabaseTransactions;
public function testItCannotUpdateAnActivityFromNami(): void
{
$this->login()->loginNami();
2023-02-17 01:50:44 +01:00
$activity = Activity::factory()->inNami(67)->name('abc')->create();
2023-02-15 01:36:59 +01:00
$response = $this->patch(route('activity.update', ['activity' => $activity]), [
'name' => 'Lorem',
2023-03-04 12:09:25 +01:00
'is_filterable' => false,
2023-02-15 01:36:59 +01:00
'subactivities' => [],
]);
2023-02-17 01:50:44 +01:00
$response->assertSessionHasErrors(['nami_id' => 'Aktivität ist in NaMi. Update des Namens nicht möglich.']);
2023-02-15 01:36:59 +01:00
}
2023-02-17 01:50:44 +01:00
public function testItCanUpdateSubactivitiesOfNamiActivity(): void
2023-02-15 01:36:59 +01:00
{
2023-02-17 01:50:44 +01:00
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->create();
$subactivity = Subactivity::factory()->create();
2023-02-15 01:36:59 +01:00
2023-02-17 01:50:44 +01:00
$this->patch(route('activity.update', ['activity' => $activity]), [
'name' => 'abc',
2023-03-04 12:09:25 +01:00
'is_filterable' => false,
2023-02-17 01:50:44 +01:00
'subactivities' => [$subactivity->id],
]);
$this->assertDatabaseHas('activity_subactivity', ['activity_id' => $activity->id, 'subactivity_id' => $subactivity->id]);
}
public function testItCannotRemoveANamiSubactivityFromANamiActivity(): void
{
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->has(Subactivity::factory()->inNami(69))->create();
$response = $this->patch(route('activity.update', ['activity' => $activity]), [
'name' => 'abc',
2023-03-04 12:09:25 +01:00
'is_filterable' => false,
2023-02-15 01:36:59 +01:00
'subactivities' => [],
]);
2023-02-17 01:50:44 +01:00
$response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht entfernt werden.']);
}
public function testItCannotAddANamiSubactivityToANamiActivity(): void
{
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->create();
$subactivity = Subactivity::factory()->inNami(60)->create();
$response = $this->patch(route('activity.update', ['activity' => $activity]), [
'name' => 'abc',
2023-03-04 12:09:25 +01:00
'is_filterable' => false,
2023-02-17 01:50:44 +01:00
'subactivities' => [$subactivity->id],
2023-02-15 01:36:59 +01:00
]);
2023-02-17 01:50:44 +01:00
$response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht hinzugefügt werden.']);
}
public function testItCannotRemoveANamiSubactivityFromANamiActivityAndSetAnother(): void
{
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->has(Subactivity::factory()->inNami(69))->create();
$otherSubactivity = Subactivity::factory()->create();
$response = $this->patch(route('activity.update', ['activity' => $activity]), [
'name' => 'abc',
2023-03-04 12:09:25 +01:00
'is_filterable' => false,
2023-02-17 01:50:44 +01:00
'subactivities' => [$otherSubactivity->id],
]);
$response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht entfernt werden.']);
2023-02-15 01:36:59 +01:00
}
public function testNameIsRequired(): void
{
$this->login()->loginNami();
2023-02-17 01:50:44 +01:00
$activity = Activity::factory()->create();
2023-02-15 01:36:59 +01:00
2023-02-17 01:50:44 +01:00
$response = $this->patch(route('activity.update', ['activity' => $activity]), [
'name' => '',
2023-03-04 12:09:25 +01:00
'is_filterable' => true,
2023-02-15 01:36:59 +01:00
]);
2023-02-17 01:50:44 +01:00
$response->assertSessionHasErrors(['name' => 'Name ist erforderlich.']);
$response->assertSessionHasErrors(['subactivities' => 'Untergliederungen muss vorhanden sein.']);
2023-02-15 01:36:59 +01:00
}
2023-02-17 01:50:44 +01:00
public function testItUpdatesName(): void
2023-02-15 01:36:59 +01:00
{
2023-02-17 01:50:44 +01:00
$this->login()->loginNami();
$activity = Activity::factory()->name('UUU')->create();
2023-02-15 01:36:59 +01:00
2023-02-17 01:50:44 +01:00
$response = $this->patch(route('activity.update', ['activity' => $activity]), [
2023-02-15 01:36:59 +01:00
'name' => 'Lorem',
2023-03-04 12:09:25 +01:00
'is_filterable' => true,
2023-02-15 01:36:59 +01:00
'subactivities' => [],
]);
2023-02-17 01:50:44 +01:00
$response->assertRedirect('/activity');
2023-03-04 12:09:25 +01:00
$this->assertDatabaseHas('activities', ['name' => 'Lorem', 'is_filterable' => true]);
2023-02-15 01:36:59 +01:00
}
2023-02-17 01:50:44 +01:00
public function testItSetsSubactivities(): void
2023-02-15 01:36:59 +01:00
{
2023-02-17 01:50:44 +01:00
$this->login()->loginNami();
$activity = Activity::factory()->create();
2023-02-15 01:36:59 +01:00
$subactivity = Subactivity::factory()->create();
2023-02-17 01:50:44 +01:00
$this->patch(route('activity.update', ['activity' => $activity]), [
2023-02-15 01:36:59 +01:00
'name' => 'Lorem',
2023-03-04 12:09:25 +01:00
'is_filterable' => false,
2023-02-15 01:36:59 +01:00
'subactivities' => [$subactivity->id],
]);
2023-02-17 01:50:44 +01:00
$this->assertDatabaseHas('activity_subactivity', ['activity_id' => $activity->id, 'subactivity_id' => $subactivity->id]);
}
public function testItCannotSetNamiId(): void
{
$this->login()->loginNami();
$activity = Activity::factory()->create();
$this->patch(route('activity.update', ['activity' => $activity]), [
'name' => 'Lorem',
'nami_id' => 66,
2023-03-04 12:09:25 +01:00
'is_filterable' => false,
2023-02-17 01:50:44 +01:00
'subactivities' => [],
]);
$this->assertDatabaseHas('activities', ['nami_id' => null]);
}
public function testItUnsetsSubactivities(): void
{
$this->login()->loginNami();
$activity = Activity::factory()
->hasAttached(Subactivity::factory())
->create();
$this->patch(route('activity.update', ['activity' => $activity]), [
'name' => 'Lorem',
2023-03-04 12:09:25 +01:00
'is_filterable' => false,
2023-02-17 01:50:44 +01:00
'subactivities' => [],
2023-02-15 01:36:59 +01:00
]);
2023-02-17 01:50:44 +01:00
$this->assertDatabaseEmpty('activity_subactivity');
2023-02-15 01:36:59 +01:00
}
}