From 22aa303311f8c7d2dd5181174953a43ebf6d0070 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 4 Apr 2025 22:53:37 +0200 Subject: [PATCH] Add updater for bank account --- database/factories/Member/MemberFactory.php | 5 + tests/Feature/Member/UpdateTest.php | 446 ++++++++---------- .../RequestFactories/MemberRequestFactory.php | 22 +- .../MemberStoreRequestFactory.php | 45 +- .../MemberUpdateRequestFactory.php | 11 + 5 files changed, 244 insertions(+), 285 deletions(-) create mode 100644 tests/RequestFactories/MemberUpdateRequestFactory.php diff --git a/database/factories/Member/MemberFactory.php b/database/factories/Member/MemberFactory.php index 4b7054c7..c23805b3 100644 --- a/database/factories/Member/MemberFactory.php +++ b/database/factories/Member/MemberFactory.php @@ -93,6 +93,11 @@ class MemberFactory extends Factory return $this->state(['nami_id' => $namiId]); } + public function notInNami(): self + { + return $this->state(['nami_id' => null]); + } + public function withBankAccount(BankAccountFactory $factory): self { return $this->afterCreating(function ($member) use ($factory) { diff --git a/tests/Feature/Member/UpdateTest.php b/tests/Feature/Member/UpdateTest.php index b1289e66..4752af2c 100644 --- a/tests/Feature/Member/UpdateTest.php +++ b/tests/Feature/Member/UpdateTest.php @@ -6,269 +6,239 @@ use App\Actions\PullMemberAction; use App\Actions\PullMembershipsAction; use App\Activity; use App\Confession; -use App\Country; -use App\Fee; use App\Group; use App\Member\Actions\NamiPutMemberAction; use App\Member\Member; -use App\Nationality; -use App\Payment\Subscription; use App\Subactivity; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Http; -use Tests\TestCase; +use Tests\RequestFactories\MemberUpdateRequestFactory; use Zoomyboy\LaravelNami\Fakes\MemberFake; -class UpdateTest extends TestCase +uses(DatabaseTransactions::class); + +function singleMemberUrl(int $gruppierungId, int $memberId): string { - use DatabaseTransactions; + return "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$gruppierungId}/{$memberId}"; +} - public function testItRedirectsToMemberOverview(): void - { - $this->withoutExceptionHandling()->login()->loginNami(); - $member = $this->member(); - $this->fakeRequest(); - NamiPutMemberAction::allowToRun(); +beforeEach(function () { + Confession::factory()->create(['is_null' => true]); +}); - $response = $this - ->from("/member/{$member->id}") - ->patch("/member/{$member->id}", array_merge($member->getAttributes(), ['has_nami' => true, 'bank_account' => []])); +function fakeRequest(): void +{ + Http::fake(function ($request) { + if ($request->url() === singleMemberUrl(10, 135) && 'GET' === $request->method()) { + return Http::response('{ "success": true, "data": {"missingkey": "missingvalue", "kontoverbindung": {"a": "b"} } }', 200); + } - $response->assertRedirect('/member'); - NamiPutMemberAction::spy()->shouldHaveReceived('handle')->withArgs( - fn (Member $memberParam, ?Activity $activityParam, ?Subactivity $subactivityParam) => $memberParam->is($member) - && null === $activityParam - && null === $subactivityParam - )->once(); - } + if ($request->url() === singleMemberUrl(10, 135) && 'PUT' === $request->method() && 43 === $request['version']) { + return Http::response('{ "success": false, "message": "Update nicht möglich. Der Datensatz wurde zwischenzeitlich verändert." }', 200); + } - public function testItChecksVersion(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $member = $this->member(); - $member->update(['version' => 43]); - $this->fakeRequest(); + if ($request->url() === singleMemberUrl(10, 135) && 'PUT' === $request->method()) { + return Http::response('{ "success": true, "data": { "version": 44 } }', 200); + } + }); +} - $response = $this - ->from("/member/{$member->id}") - ->patch("/member/{$member->id}", array_merge($member->getAttributes(), ['has_nami' => true, 'firstname' => '::firstname::', 'bank_account' => []])); +function factory() +{ + return Member::factory() + ->defaults() + ->for(Group::factory()->state(['nami_id' => 10])) + ->state(['nami_id' => 135]); +} - $response->assertRedirect("/member/{$member->id}/edit?conflict=1"); - } +it('calls put action', function () { + $this->withoutExceptionHandling()->login()->loginNami(); + $member = factory()->create(); + fakeRequest(); + NamiPutMemberAction::allowToRun(); - public function testItUpdatesPhoneNumber(): void - { - $this->withoutExceptionHandling()->login()->loginNami(); - $member = $this->member(); - $this->fakeRequest(); - NamiPutMemberAction::allowToRun(); + $this->patch("/member/{$member->id}", MemberUpdateRequestFactory::new()->create()); - $this->patch("/member/{$member->id}", array_merge($member->getAttributes(), [ - 'main_phone' => '02103 4455129', - 'fax' => '02103 4455130', - 'children_phone' => '02103 4455130', - 'has_nami' => true, - 'bank_account' => [] - ])); + NamiPutMemberAction::spy()->shouldHaveReceived('handle')->withArgs( + fn (Member $memberParam, ?Activity $activityParam, ?Subactivity $subactivityParam) => $memberParam->is($member) + && null === $activityParam + && null === $subactivityParam + )->once(); +}); - $this->assertDatabaseHas('members', [ - 'main_phone' => '+49 2103 4455129', - 'fax' => '+49 2103 4455130', - 'children_phone' => '+49 2103 4455130', - ]); - } +it('redirects to member overview', function () { + $this->withoutExceptionHandling()->login()->loginNami(); + $member = factory()->create(); + fakeRequest(); + NamiPutMemberAction::allowToRun(); - public function testItUpdatesBankAccount(): void - { - $this->withoutExceptionHandling()->login()->loginNami(); - $member = $this->member(); - $this->fakeRequest(); - NamiPutMemberAction::allowToRun(); + $this->patch("/member/{$member->id}", MemberUpdateRequestFactory::new()->create()) + ->assertRedirect('/member'); +}); - $this->patch("/member/{$member->id}", array_merge($member->getAttributes(), [ - 'has_nami' => true, - 'bank_account' => [ - 'iban' => 'DE1122', - 'bic' => 'SOLSDE', - 'person' => 'Max' - ] - ])); +it('testItChecksVersion', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $member = tap(factory()->create(), fn ($member) => $member->update(['version' => 43])); + fakeRequest(); - $this->assertDatabaseHas('bank_accounts', [ + $this->patch("/member/{$member->id}", array_merge($member->getAttributes(), MemberUpdateRequestFactory::new()->create())) + ->assertRedirect("/member/{$member->id}/edit?conflict=1"); +}); + +it('testItUpdatesPhoneNumber', function () { + $this->withoutExceptionHandling()->login()->loginNami(); + $member = factory()->create(); + fakeRequest(); + NamiPutMemberAction::allowToRun(); + + $this->patch("/member/{$member->id}", MemberUpdateRequestFactory::new()->create([ + 'main_phone' => '02103 4455129', + 'fax' => '02103 4455130', + 'children_phone' => '02103 4455130', + ])); + + $this->assertDatabaseHas('members', [ + 'main_phone' => '+49 2103 4455129', + 'fax' => '+49 2103 4455130', + 'children_phone' => '+49 2103 4455130', + ]); +}); + +it('testItUpdatesBankAccount', function () { + $this->withoutExceptionHandling()->login()->loginNami(); + $member = factory()->create(); + fakeRequest(); + NamiPutMemberAction::allowToRun(); + + $this->patch("/member/{$member->id}", MemberUpdateRequestFactory::new()->create([ + 'bank_account' => [ 'iban' => 'DE1122', 'bic' => 'SOLSDE', - 'person' => 'Max', - 'member_id' => $member->id, - ]); - } + 'person' => 'Max' + ] + ])); - public function testItUpdatesWiederverwendenFlag(): void - { - $this->withoutExceptionHandling()->login()->loginNami(); - $member = $this->member(); - $this->fakeRequest(); - NamiPutMemberAction::allowToRun(); + $this->assertDatabaseHas('bank_accounts', [ + 'iban' => 'DE1122', + 'bic' => 'SOLSDE', + 'person' => 'Max', + 'member_id' => $member->id, + ]); +}); - $this->patch("/member/{$member->id}", array_merge($member->getAttributes(), [ - 'keepdata' => true, - 'has_nami' => true, +it('testItUpdatesWiederverwendenFlag', function () { + $this->withoutExceptionHandling()->login()->loginNami(); + $member = factory()->create(); + fakeRequest(); + NamiPutMemberAction::allowToRun(); + + $this->patch("/member/{$member->id}", MemberUpdateRequestFactory::new()->create([ + 'keepdata' => true, + ])); + + $this->assertDatabaseHas('members', [ + 'keepdata' => true, + ]); +}); + +it('testItSetsLocationToNull', function () { + $this->withoutExceptionHandling()->login()->loginNami(); + $member = factory()->notInNami()->create(['location' => 'Hilden']); + fakeRequest(); + NamiPutMemberAction::allowToRun(); + + $this->patch("/member/{$member->id}", MemberUpdateRequestFactory::new()->noNami()->create([ + 'location' => null, + 'bank_account' => [] + ])); + + $this->assertDatabaseHas('members', [ + 'location' => null, + ]); +}); + +it('testItUpdatesContact', function () { + $this->withoutExceptionHandling()->login()->loginNami(); + $member = factory()->notInNami()->create(); + fakeRequest(); + + $this->patch("/member/{$member->id}", MemberUpdateRequestFactory::new()->noNami()->create([ + 'other_country' => 'englisch', + 'bank_account' => [] + ])); + + $this->assertEquals('englisch', $member->fresh()->other_country); +}); + +it('testItCreatesMemberWithFirstActivityId', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $member = factory()->new()->defaults()->create(); + app(MemberFake::class)->stores($member->group->nami_id, 103); + $activity = Activity::factory()->inNami(89)->create(); + $subactivity = Subactivity::factory()->inNami(90)->create(); + Confession::factory()->create(['is_null' => true]); + PullMemberAction::shouldRun(); + PullMembershipsAction::shouldRun(); + + $this->patch("/member/{$member->id}", MemberUpdateRequestFactory::new()->create([ + 'first_activity_id' => $activity->id, + 'first_subactivity_id' => $subactivity->id, + ]))->assertSessionHasNoErrors(); + + app(MemberFake::class)->assertStored($member->group->nami_id, [ + 'ersteTaetigkeitId' => 89, + 'ersteUntergliederungId' => 90, + ]); +}); + +it('testItRequiresFirstActivityId', function () { + $this->login()->loginNami(); + $member = factory()->new()->defaults()->create(); + app(MemberFake::class)->stores($member->group->nami_id, 103); + Confession::factory()->create(['is_null' => true]); + PullMemberAction::shouldRun(); + PullMembershipsAction::shouldRun(); + + $this->patch("/member/{$member->id}", MemberUpdateRequestFactory::new()->create([ + 'first_activity_id' => null, + 'first_subactivity_id' => null, + ]))->assertSessionHasErrors([ + 'first_activity_id' => 'Erste Tätigkeit ist erforderlich.', + 'first_subactivity_id' => 'Erste Untergliederung ist erforderlich.', + ]); +}); + +it('testItUpdatesCriminalRecord', function () { + $this->withoutExceptionHandling()->login()->loginNami(); + $member = factory()->notInNami()->create(); + fakeRequest(); + + $this + ->patch("/member/{$member->id}", MemberUpdateRequestFactory::new()->noNami()->create([ + 'ps_at' => '2021-02-01', + 'more_ps_at' => '2021-02-02', + 'has_svk' => true, + 'has_vk' => true, + 'efz' => '2021-02-03', + 'without_education_at' => '2021-02-04', + 'without_efz_at' => '2021-02-05', + 'recertified_at' => '2021-02-08', + 'has_nami' => false, + 'multiply_pv' => true, + 'multiply_more_pv' => true, + 'salutation' => 'Doktor', 'bank_account' => [] ])); - $this->assertDatabaseHas('members', [ - 'keepdata' => true, - ]); - } - - public function testItSetsLocationToNull(): void - { - $this->withoutExceptionHandling()->login()->loginNami(); - $member = $this->member(['location' => 'Hilden', 'nami_id' => null]); - $this->fakeRequest(); - NamiPutMemberAction::allowToRun(); - - $this->patch("/member/{$member->id}", array_merge($member->getAttributes(), [ - 'location' => null, - 'bank_account' => [] - ])); - - $this->assertDatabaseHas('members', [ - 'location' => null, - ]); - } - - public function testItUpdatesContact(): void - { - $this->withoutExceptionHandling()->login()->loginNami(); - $member = $this->member(['nami_id' => null]); - $this->fakeRequest(); - - $response = $this - ->from("/member/{$member->id}") - ->patch("/member/{$member->id}", array_merge($member->getAttributes(), [ - 'other_country' => 'englisch', - 'bank_account' => [] - ])); - - $this->assertEquals('englisch', $member->fresh()->other_country); - } - - public function testItCreatesMemberWithFirstActivityId(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $member = Member::factory()->defaults()->create(); - app(MemberFake::class)->stores($member->group->nami_id, 103); - $activity = Activity::factory()->inNami(89)->create(); - $subactivity = Subactivity::factory()->inNami(90)->create(); - Confession::factory()->create(['is_null' => true]); - PullMemberAction::shouldRun(); - PullMembershipsAction::shouldRun(); - - $this->patch("/member/{$member->id}", [ - ...$member->getAttributes(), - 'has_nami' => true, - 'first_activity_id' => $activity->id, - 'first_subactivity_id' => $subactivity->id, - 'bank_account' => [] - ])->assertSessionHasNoErrors(); - - app(MemberFake::class)->assertStored($member->group->nami_id, [ - 'ersteTaetigkeitId' => 89, - 'ersteUntergliederungId' => 90, - ]); - } - - public function testItRequiresFirstActivityId(): void - { - $this->login()->loginNami(); - $member = Member::factory()->defaults()->create(); - app(MemberFake::class)->stores($member->group->nami_id, 103); - Confession::factory()->create(['is_null' => true]); - PullMemberAction::shouldRun(); - PullMembershipsAction::shouldRun(); - - $this->patch("/member/{$member->id}", [ - ...$member->getAttributes(), - 'has_nami' => true, - 'first_activity_id' => null, - 'first_subactivity_id' => null, - 'bank_account' => [] - ])->assertSessionHasErrors([ - 'first_activity_id' => 'Erste Tätigkeit ist erforderlich.', - 'first_subactivity_id' => 'Erste Untergliederung ist erforderlich.', - ]); - } - - public function testItUpdatesCriminalRecord(): void - { - $this->withoutExceptionHandling()->login()->loginNami(); - $member = $this->member(['nami_id' => null]); - $this->fakeRequest(); - - $response = $this - ->from("/member/{$member->id}") - ->patch("/member/{$member->id}", array_merge($member->getAttributes(), [ - 'ps_at' => '2021-02-01', - 'more_ps_at' => '2021-02-02', - 'has_svk' => true, - 'has_vk' => true, - 'efz' => '2021-02-03', - 'without_education_at' => '2021-02-04', - 'without_efz_at' => '2021-02-05', - 'recertified_at' => '2021-02-08', - 'has_nami' => false, - 'multiply_pv' => true, - 'multiply_more_pv' => true, - 'salutation' => 'Doktor', - 'bank_account' => [] - ])); - - $this->assertEquals('2021-02-01', $member->fresh()->ps_at->format('Y-m-d')); - $this->assertEquals('2021-02-02', $member->fresh()->more_ps_at->format('Y-m-d')); - $this->assertTrue($member->fresh()->has_svk); - $this->assertTrue($member->fresh()->has_vk); - $this->assertTrue($member->fresh()->multiply_pv); - $this->assertTrue($member->fresh()->multiply_more_pv); - $this->assertEquals('2021-02-03', $member->fresh()->efz->format('Y-m-d')); - $this->assertEquals('2021-02-04', $member->fresh()->without_education_at->format('Y-m-d')); - $this->assertEquals('2021-02-05', $member->fresh()->without_efz_at->format('Y-m-d')); - $this->assertEquals('2021-02-08', $member->fresh()->recertified_at->format('Y-m-d')); - $this->assertEquals('Doktor', $member->fresh()->salutation); - } - - /** - * @param array $overwrites - */ - private function member(array $overwrites = []): Member - { - return Member::factory() - ->for(Group::factory()->state(['nami_id' => 10])) - ->for(Confession::factory()) - ->for(Nationality::factory()) - ->for(Subscription::factory()->forFee()) - ->for(Country::factory()) - ->create(['nami_id' => 135, ...$overwrites]); - } - - private function fakeRequest(): void - { - Http::fake(function ($request) { - if ($request->url() === $this->singleMemberUrl(10, 135) && 'GET' === $request->method()) { - return Http::response('{ "success": true, "data": {"missingkey": "missingvalue", "kontoverbindung": {"a": "b"} } }', 200); - } - - if ($request->url() === $this->singleMemberUrl(10, 135) && 'PUT' === $request->method() && 43 === $request['version']) { - return Http::response('{ "success": false, "message": "Update nicht möglich. Der Datensatz wurde zwischenzeitlich verändert." }', 200); - } - - if ($request->url() === $this->singleMemberUrl(10, 135) && 'PUT' === $request->method()) { - return Http::response('{ "success": true, "data": { "version": 44 } }', 200); - } - }); - } - - private function singleMemberUrl(int $gruppierungId, int $memberId): string - { - return "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$gruppierungId}/{$memberId}"; - } -} + $this->assertEquals('2021-02-01', $member->fresh()->ps_at->format('Y-m-d')); + $this->assertEquals('2021-02-02', $member->fresh()->more_ps_at->format('Y-m-d')); + $this->assertTrue($member->fresh()->has_svk); + $this->assertTrue($member->fresh()->has_vk); + $this->assertTrue($member->fresh()->multiply_pv); + $this->assertTrue($member->fresh()->multiply_more_pv); + $this->assertEquals('2021-02-03', $member->fresh()->efz->format('Y-m-d')); + $this->assertEquals('2021-02-04', $member->fresh()->without_education_at->format('Y-m-d')); + $this->assertEquals('2021-02-05', $member->fresh()->without_efz_at->format('Y-m-d')); + $this->assertEquals('2021-02-08', $member->fresh()->recertified_at->format('Y-m-d')); + $this->assertEquals('Doktor', $member->fresh()->salutation); +}); diff --git a/tests/RequestFactories/MemberRequestFactory.php b/tests/RequestFactories/MemberRequestFactory.php index 1f4658f4..690da844 100644 --- a/tests/RequestFactories/MemberRequestFactory.php +++ b/tests/RequestFactories/MemberRequestFactory.php @@ -2,6 +2,9 @@ namespace Tests\RequestFactories; +use App\Country; +use App\Nationality; +use App\Payment\Subscription; use Worksome\RequestFactories\RequestFactory; class MemberRequestFactory extends RequestFactory @@ -11,14 +14,18 @@ class MemberRequestFactory extends RequestFactory */ public function definition(): array { + $country = Country::factory()->create(); + $nationality = Nationality::factory()->create(); + $subscription = Subscription::factory()->forFee()->create(); + return [ 'address' => 'Bavert 50', 'birthday' => '2013-02-19', - 'children_phone' => '+49 123 44444', + 'children_phone' => '+49 176 70512778', 'efz' => '', 'email' => '', 'email_parents' => 'osloot@aol.com', - 'fax' => '+49 666', + 'fax' => '+49 212 4732223', 'firstname' => 'Joe', 'further_address' => '', 'has_nami' => true, @@ -28,8 +35,8 @@ class MemberRequestFactory extends RequestFactory 'lastname' => 'Muster', 'letter_address' => '', 'location' => 'Solingen', - 'main_phone' => '+49 212 2334322', - 'mobile_phone' => '+49 157 53180451', + 'main_phone' => '+49 212 337056', + 'mobile_phone' => '+49 176 70512774', 'more_ps_at' => '', 'multiply_more_pv' => false, 'multiply_pv' => false, @@ -40,6 +47,13 @@ class MemberRequestFactory extends RequestFactory 'without_efz_at' => '', 'work_phone' => '', 'zip' => '42719', + 'bank_account' => [ + 'iban' => '', + 'bic' => '', + ], + 'country_id' => $country->id, + 'nationality_id' => $nationality->id, + 'subscription_id' => $subscription->id, ]; } } diff --git a/tests/RequestFactories/MemberStoreRequestFactory.php b/tests/RequestFactories/MemberStoreRequestFactory.php index 0a6c803f..103a613b 100644 --- a/tests/RequestFactories/MemberStoreRequestFactory.php +++ b/tests/RequestFactories/MemberStoreRequestFactory.php @@ -3,58 +3,17 @@ namespace Tests\RequestFactories; use App\Activity; -use App\Country; -use App\Nationality; -use App\Payment\Subscription; use App\Subactivity; -use Worksome\RequestFactories\RequestFactory; -class MemberStoreRequestFactory extends RequestFactory +class MemberStoreRequestFactory extends MemberRequestFactory { public function definition(): array { - $country = Country::factory()->create(); - $nationality = Nationality::factory()->create(); - $subscription = Subscription::factory()->forFee()->create(); $activity = Activity::factory()->inNami(89)->create(); $subactivity = Subactivity::factory()->inNami(90)->create(); return [ - 'bank_account' => [ - 'iban' => '', - 'bic' => '', - ], - 'address' => 'Bavert 50', - 'birthday' => '2013-02-19', - 'children_phone' => '+49 176 70512778', - 'efz' => '', - 'email' => '', - 'email_parents' => 'osloot@aol.com', - 'fax' => '+49 212 4732223', - 'firstname' => 'Joe', - 'further_address' => '', - 'has_nami' => true, - 'has_svk' => false, - 'has_vk' => false, - 'joined_at' => '2022-08-12', - 'lastname' => 'Muster', - 'letter_address' => '', - 'location' => 'Solingen', - 'main_phone' => '+49 212 337056', - 'mobile_phone' => '+49 176 70512774', - 'more_ps_at' => '', - 'multiply_more_pv' => false, - 'multiply_pv' => false, - 'other_country' => '', - 'ps_at' => '', - 'send_newspaper' => true, - 'without_education_at' => '', - 'without_efz_at' => '', - 'work_phone' => '', - 'zip' => '42719', - 'country_id' => $country->id, - 'nationality_id' => $nationality->id, - 'subscription_id' => $subscription->id, + ...parent::definition(), 'first_activity_id' => $activity->id, 'first_subactivity_id' => $subactivity->id, ]; diff --git a/tests/RequestFactories/MemberUpdateRequestFactory.php b/tests/RequestFactories/MemberUpdateRequestFactory.php new file mode 100644 index 00000000..c3e310b0 --- /dev/null +++ b/tests/RequestFactories/MemberUpdateRequestFactory.php @@ -0,0 +1,11 @@ +state(['has_nami' => false]); + } +}