Update Activity UpdateTest

This commit is contained in:
philipp lang 2024-09-22 00:06:03 +02:00
parent b8d389bdb9
commit 4d74e53fc2
1 changed files with 169 additions and 137 deletions

View File

@ -3,18 +3,17 @@
namespace Tests\Feature\Activity;
use App\Activity;
use App\Activity\Actions\ActivityUpdateAction;
use App\Member\Member;
use App\Member\Membership;
use App\Subactivity;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class UpdateTest extends TestCase
{
use DatabaseTransactions;
covers(ActivityUpdateAction::class);
public function testItCannotUpdateAnActivityFromNami(): void
{
uses(DatabaseTransactions::class);
it('testItCannotUpdateAnActivityFromNami', function () {
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->create();
@ -25,10 +24,9 @@ class UpdateTest extends TestCase
]);
$response->assertSessionHasErrors(['nami_id' => 'Aktivität ist in NaMi. Update des Namens nicht möglich.']);
}
});
public function testItCanUpdateSubactivitiesOfNamiActivity(): void
{
it('testItCanUpdateSubactivitiesOfNamiActivity', function () {
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->create();
$subactivity = Subactivity::factory()->create();
@ -40,10 +38,9 @@ class UpdateTest extends TestCase
]);
$this->assertDatabaseHas('activity_subactivity', ['activity_id' => $activity->id, 'subactivity_id' => $subactivity->id]);
}
});
public function testItCannotRemoveANamiSubactivityFromANamiActivity(): void
{
it('testItCannotRemoveANamiSubactivityFromANamiActivity', function () {
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->has(Subactivity::factory()->inNami(69))->create();
@ -54,10 +51,9 @@ class UpdateTest extends TestCase
]);
$response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht entfernt werden.']);
}
});
public function testItCannotAddANamiSubactivityToANamiActivity(): void
{
it('testItCannotAddANamiSubactivityToANamiActivity', function () {
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->create();
$subactivity = Subactivity::factory()->inNami(60)->create();
@ -69,10 +65,21 @@ class UpdateTest extends TestCase
]);
$response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht hinzugefügt werden.']);
}
});
public function testItCannotRemoveANamiSubactivityFromANamiActivityAndSetAnother(): void
{
it('test it cannot set subactivity to a string', function () {
$this->login()->loginNami();
$activity = Activity::factory()->create();
$this->patch(route('activity.update', ['activity' => $activity]), [
'name' => 'abc',
'is_filterable' => false,
'subactivities' => ['AAA'],
])->assertSessionHasErrors('subactivities.0');
});
it('testItCannotRemoveANamiSubactivityFromANamiActivityAndSetAnother', function () {
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->has(Subactivity::factory()->inNami(69))->create();
$otherSubactivity = Subactivity::factory()->create();
@ -84,10 +91,9 @@ class UpdateTest extends TestCase
]);
$response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht entfernt werden.']);
}
});
public function testNameIsRequired(): void
{
it('testNameIsRequired', function () {
$this->login()->loginNami();
$activity = Activity::factory()->create();
@ -98,10 +104,9 @@ class UpdateTest extends TestCase
$response->assertSessionHasErrors(['name' => 'Name ist erforderlich.']);
$response->assertSessionHasErrors(['subactivities' => 'Untergliederungen muss vorhanden sein.']);
}
});
public function testItUpdatesName(): void
{
it('testItUpdatesName', function () {
$this->login()->loginNami();
$activity = Activity::factory()->name('UUU')->create();
@ -113,10 +118,9 @@ class UpdateTest extends TestCase
$response->assertRedirect('/activity');
$this->assertDatabaseHas('activities', ['name' => 'Lorem', 'is_filterable' => true]);
}
});
public function testItSetsSubactivities(): void
{
it('testItSetsSubactivities', function () {
$this->login()->loginNami();
$activity = Activity::factory()->create();
$subactivity = Subactivity::factory()->create();
@ -128,10 +132,9 @@ class UpdateTest extends TestCase
]);
$this->assertDatabaseHas('activity_subactivity', ['activity_id' => $activity->id, 'subactivity_id' => $subactivity->id]);
}
});
public function testItCannotSetNamiId(): void
{
it('testItCannotSetNamiId', function () {
$this->login()->loginNami();
$activity = Activity::factory()->create();
@ -143,10 +146,9 @@ class UpdateTest extends TestCase
]);
$this->assertDatabaseHas('activities', ['nami_id' => null]);
}
});
public function testItUnsetsSubactivities(): void
{
it('testItUnsetsSubactivities', function () {
$this->login()->loginNami();
$activity = Activity::factory()
->hasAttached(Subactivity::factory())
@ -159,22 +161,52 @@ class UpdateTest extends TestCase
]);
$this->assertDatabaseEmpty('activity_subactivity');
}
});
public function testItCannotSetSubactivityIfItStillHasMembers(): void
{
it('testItCannotSetSubactivityIfItStillHasMembers', function () {
$this->login()->loginNami();
$activity = Activity::factory()->create();
$subactivity = Subactivity::factory()->hasAttached($activity)->create();
$newSubactivity = Subactivity::factory()->create();
Member::factory()->defaults()->has(Membership::factory()->for($activity)->for($subactivity))->create();
$activity = Activity::factory()
->hasAttached(Subactivity::factory())
->create();
Member::factory()->defaults()->has(Membership::factory()->for($activity)->for($activity->subactivities->first()))->create();
$response = $this->patch(route('activity.update', ['activity' => $activity]), [
'name' => 'abc',
'is_filterable' => false,
'subactivities' => [$newSubactivity->id],
'subactivities' => [],
]);
$response->assertSessionHasErrors(['subactivities' => 'Untergliederung hat noch Mitglieder.']);
}
}
});
it('test it succeeds when membership is not of removing subactivity', function () {
$this->login()->loginNami();
$activity = Activity::factory()
->hasAttached(Subactivity::factory())
->create();
Member::factory()->defaults()->has(Membership::factory()->for($activity)->for(Subactivity::factory()))->create();
$response = $this->patch(route('activity.update', ['activity' => $activity]), [
'name' => 'abc',
'is_filterable' => false,
'subactivities' => [],
]);
$response->assertSessionDoesntHaveErrors();
});
it('test it succeeds when membership is not of removing activity', function () {
$this->login()->loginNami();
$activity = Activity::factory()
->hasAttached(Subactivity::factory())
->create();
Member::factory()->defaults()->has(Membership::factory()->for(Activity::factory())->for($activity->subactivities->first()))->create();
$response = $this->patch(route('activity.update', ['activity' => $activity]), [
'name' => 'abc',
'is_filterable' => false,
'subactivities' => [],
]);
$response->assertSessionDoesntHaveErrors();
});