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,178 +3,210 @@
namespace Tests\Feature\Activity; namespace Tests\Feature\Activity;
use App\Activity; use App\Activity;
use App\Activity\Actions\ActivityUpdateAction;
use App\Member\Member; use App\Member\Member;
use App\Member\Membership; use App\Member\Membership;
use App\Subactivity; use App\Subactivity;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class UpdateTest extends TestCase covers(ActivityUpdateAction::class);
{
use DatabaseTransactions;
public function testItCannotUpdateAnActivityFromNami(): void uses(DatabaseTransactions::class);
{
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->create();
$response = $this->patch(route('activity.update', ['activity' => $activity]), [ it('testItCannotUpdateAnActivityFromNami', function () {
'name' => 'Lorem', $this->login()->loginNami();
'is_filterable' => false, $activity = Activity::factory()->inNami(67)->name('abc')->create();
'subactivities' => [],
]);
$response->assertSessionHasErrors(['nami_id' => 'Aktivität ist in NaMi. Update des Namens nicht möglich.']); $response = $this->patch(route('activity.update', ['activity' => $activity]), [
} 'name' => 'Lorem',
'is_filterable' => false,
'subactivities' => [],
]);
public function testItCanUpdateSubactivitiesOfNamiActivity(): void $response->assertSessionHasErrors(['nami_id' => 'Aktivität ist in NaMi. Update des Namens nicht möglich.']);
{ });
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->create();
$subactivity = Subactivity::factory()->create();
$this->patch(route('activity.update', ['activity' => $activity]), [ it('testItCanUpdateSubactivitiesOfNamiActivity', function () {
'name' => 'abc', $this->login()->loginNami();
'is_filterable' => false, $activity = Activity::factory()->inNami(67)->name('abc')->create();
'subactivities' => [$subactivity->id], $subactivity = Subactivity::factory()->create();
]);
$this->assertDatabaseHas('activity_subactivity', ['activity_id' => $activity->id, 'subactivity_id' => $subactivity->id]); $this->patch(route('activity.update', ['activity' => $activity]), [
} 'name' => 'abc',
'is_filterable' => false,
'subactivities' => [$subactivity->id],
]);
public function testItCannotRemoveANamiSubactivityFromANamiActivity(): void $this->assertDatabaseHas('activity_subactivity', ['activity_id' => $activity->id, 'subactivity_id' => $subactivity->id]);
{ });
$this->login()->loginNami();
$activity = Activity::factory()->inNami(67)->name('abc')->has(Subactivity::factory()->inNami(69))->create();
$response = $this->patch(route('activity.update', ['activity' => $activity]), [ it('testItCannotRemoveANamiSubactivityFromANamiActivity', function () {
'name' => 'abc', $this->login()->loginNami();
'is_filterable' => false, $activity = Activity::factory()->inNami(67)->name('abc')->has(Subactivity::factory()->inNami(69))->create();
'subactivities' => [],
]);
$response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht entfernt werden.']); $response = $this->patch(route('activity.update', ['activity' => $activity]), [
} 'name' => 'abc',
'is_filterable' => false,
'subactivities' => [],
]);
public function testItCannotAddANamiSubactivityToANamiActivity(): void $response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht entfernt werden.']);
{ });
$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]), [ it('testItCannotAddANamiSubactivityToANamiActivity', function () {
'name' => 'abc', $this->login()->loginNami();
'is_filterable' => false, $activity = Activity::factory()->inNami(67)->name('abc')->create();
'subactivities' => [$subactivity->id], $subactivity = Subactivity::factory()->inNami(60)->create();
]);
$response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht hinzugefügt werden.']); $response = $this->patch(route('activity.update', ['activity' => $activity]), [
} 'name' => 'abc',
'is_filterable' => false,
'subactivities' => [$subactivity->id],
]);
public function testItCannotRemoveANamiSubactivityFromANamiActivityAndSetAnother(): void $response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht hinzugefügt werden.']);
{ });
$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]), [ it('test it cannot set subactivity to a string', function () {
'name' => 'abc', $this->login()->loginNami();
'is_filterable' => false, $activity = Activity::factory()->create();
'subactivities' => [$otherSubactivity->id],
]);
$response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht entfernt werden.']); $this->patch(route('activity.update', ['activity' => $activity]), [
} 'name' => 'abc',
'is_filterable' => false,
'subactivities' => ['AAA'],
])->assertSessionHasErrors('subactivities.0');
});
public function testNameIsRequired(): void
{
$this->login()->loginNami();
$activity = Activity::factory()->create();
$response = $this->patch(route('activity.update', ['activity' => $activity]), [ it('testItCannotRemoveANamiSubactivityFromANamiActivityAndSetAnother', function () {
'name' => '', $this->login()->loginNami();
'is_filterable' => true, $activity = Activity::factory()->inNami(67)->name('abc')->has(Subactivity::factory()->inNami(69))->create();
]); $otherSubactivity = Subactivity::factory()->create();
$response->assertSessionHasErrors(['name' => 'Name ist erforderlich.']); $response = $this->patch(route('activity.update', ['activity' => $activity]), [
$response->assertSessionHasErrors(['subactivities' => 'Untergliederungen muss vorhanden sein.']); 'name' => 'abc',
} 'is_filterable' => false,
'subactivities' => [$otherSubactivity->id],
]);
public function testItUpdatesName(): void $response->assertSessionHasErrors(['nami_id' => 'Untertätigkeit kann nicht entfernt werden.']);
{ });
$this->login()->loginNami();
$activity = Activity::factory()->name('UUU')->create();
$response = $this->patch(route('activity.update', ['activity' => $activity]), [ it('testNameIsRequired', function () {
'name' => 'Lorem', $this->login()->loginNami();
'is_filterable' => true, $activity = Activity::factory()->create();
'subactivities' => [],
]);
$response->assertRedirect('/activity'); $response = $this->patch(route('activity.update', ['activity' => $activity]), [
$this->assertDatabaseHas('activities', ['name' => 'Lorem', 'is_filterable' => true]); 'name' => '',
} 'is_filterable' => true,
]);
public function testItSetsSubactivities(): void $response->assertSessionHasErrors(['name' => 'Name ist erforderlich.']);
{ $response->assertSessionHasErrors(['subactivities' => 'Untergliederungen muss vorhanden sein.']);
$this->login()->loginNami(); });
$activity = Activity::factory()->create();
$subactivity = Subactivity::factory()->create();
$this->patch(route('activity.update', ['activity' => $activity]), [ it('testItUpdatesName', function () {
'name' => 'Lorem', $this->login()->loginNami();
'is_filterable' => false, $activity = Activity::factory()->name('UUU')->create();
'subactivities' => [$subactivity->id],
]);
$this->assertDatabaseHas('activity_subactivity', ['activity_id' => $activity->id, 'subactivity_id' => $subactivity->id]); $response = $this->patch(route('activity.update', ['activity' => $activity]), [
} 'name' => 'Lorem',
'is_filterable' => true,
'subactivities' => [],
]);
public function testItCannotSetNamiId(): void $response->assertRedirect('/activity');
{ $this->assertDatabaseHas('activities', ['name' => 'Lorem', 'is_filterable' => true]);
$this->login()->loginNami(); });
$activity = Activity::factory()->create();
$this->patch(route('activity.update', ['activity' => $activity]), [ it('testItSetsSubactivities', function () {
'name' => 'Lorem', $this->login()->loginNami();
'nami_id' => 66, $activity = Activity::factory()->create();
'is_filterable' => false, $subactivity = Subactivity::factory()->create();
'subactivities' => [],
]);
$this->assertDatabaseHas('activities', ['nami_id' => null]); $this->patch(route('activity.update', ['activity' => $activity]), [
} 'name' => 'Lorem',
'is_filterable' => false,
'subactivities' => [$subactivity->id],
]);
public function testItUnsetsSubactivities(): void $this->assertDatabaseHas('activity_subactivity', ['activity_id' => $activity->id, 'subactivity_id' => $subactivity->id]);
{ });
$this->login()->loginNami();
$activity = Activity::factory()
->hasAttached(Subactivity::factory())
->create();
$this->patch(route('activity.update', ['activity' => $activity]), [ it('testItCannotSetNamiId', function () {
'name' => 'Lorem', $this->login()->loginNami();
'is_filterable' => false, $activity = Activity::factory()->create();
'subactivities' => [],
]);
$this->assertDatabaseEmpty('activity_subactivity'); $this->patch(route('activity.update', ['activity' => $activity]), [
} 'name' => 'Lorem',
'nami_id' => 66,
'is_filterable' => false,
'subactivities' => [],
]);
public function testItCannotSetSubactivityIfItStillHasMembers(): void $this->assertDatabaseHas('activities', ['nami_id' => null]);
{ });
$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();
$response = $this->patch(route('activity.update', ['activity' => $activity]), [ it('testItUnsetsSubactivities', function () {
'name' => 'abc', $this->login()->loginNami();
'is_filterable' => false, $activity = Activity::factory()
'subactivities' => [$newSubactivity->id], ->hasAttached(Subactivity::factory())
]); ->create();
$response->assertSessionHasErrors(['subactivities' => 'Untergliederung hat noch Mitglieder.']); $this->patch(route('activity.update', ['activity' => $activity]), [
} 'name' => 'Lorem',
} 'is_filterable' => false,
'subactivities' => [],
]);
$this->assertDatabaseEmpty('activity_subactivity');
});
it('testItCannotSetSubactivityIfItStillHasMembers', function () {
$this->login()->loginNami();
$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' => [],
]);
$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();
});