adrema/tests/Feature/Initialize/InitializeTest.php

505 lines
18 KiB
PHP
Raw Normal View History

2021-11-17 22:32:52 +01:00
<?php
namespace Tests\Feature\Initialize;
use App\Activity;
use App\Console\Commands\NamiInitializeCommand;
2021-11-17 22:32:52 +01:00
use App\Country;
2021-11-19 22:58:27 +01:00
use App\Course\Models\Course;
2021-11-17 22:32:52 +01:00
use App\Gender;
use App\Group;
2022-08-31 00:33:36 +02:00
use App\Initialize\Actions\InitializeAction;
use App\Initialize\InitializeJob;
2021-11-18 01:54:27 +01:00
use App\Member\Member;
2021-11-17 22:32:52 +01:00
use App\Nationality;
2022-01-03 01:17:24 +01:00
use App\Setting\GeneralSettings;
2022-08-31 00:33:36 +02:00
use App\Setting\NamiSettings;
use App\Subactivity;
2022-02-12 01:08:56 +01:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2022-01-03 01:17:24 +01:00
use Illuminate\Support\Facades\Artisan;
2021-11-17 22:32:52 +01:00
use Illuminate\Support\Facades\Http;
2022-08-31 00:33:36 +02:00
use Illuminate\Support\Facades\Queue;
2021-11-17 22:32:52 +01:00
use Tests\TestCase;
use Zoomyboy\LaravelNami\Backend\FakeBackend;
2022-02-19 18:06:07 +01:00
use Zoomyboy\LaravelNami\Fakes\GroupFake;
use Zoomyboy\LaravelNami\Fakes\SearchFake;
2021-11-17 22:32:52 +01:00
class InitializeTest extends TestCase
{
2022-02-12 01:08:56 +01:00
use DatabaseTransactions;
2021-11-17 22:32:52 +01:00
2021-11-17 23:41:04 +01:00
public function initializeProvider(callable $callback = null): void
2021-11-17 22:32:52 +01:00
{
2022-03-15 12:03:00 +01:00
app(GroupFake::class)
2022-08-31 00:33:36 +02:00
->fetches(null, [1000 => ['name' => 'testgroup']])
2022-03-15 12:03:00 +01:00
->fetches(1000, []);
2021-11-17 23:41:04 +01:00
$backend = app(FakeBackend::class)
2021-11-17 22:32:52 +01:00
->addSearch(123, ['entries_vorname' => '::firstname::', 'entries_nachname' => '::lastname::', 'entries_gruppierungId' => 1000])
->fakeNationalities([['name' => 'deutsch', 'id' => 291]])
->fakeFees(1000, [['name' => 'Family', 'id' => 300]])
->fakeConfessions([['name' => 'Konf', 'id' => 301]])
->fakeCountries([['name' => 'Germany', 'id' => 302]])
->fakeGenders([['name' => 'Male', 'id' => 303]])
->fakeRegions([['name' => 'nrw', 'id' => 304]])
2021-11-18 01:54:27 +01:00
->fakeCourses([['name' => '1a', 'id' => 506]])
->fakeActivities(1000, [['name' => '€ leiter', 'id' => 305]]);
2021-11-17 23:41:04 +01:00
if (!$callback) {
$backend->fakeMember([
2021-11-17 22:32:52 +01:00
'vorname' => '::firstname::',
'nachname' => '::lastname::',
'beitragsartId' => 300,
'geburtsDatum' => '2014-07-11 00:00:00',
'gruppierungId' => 1000,
'id' => 411,
'eintrittsdatum' => '2020-11-17 00:00:00',
'geschlechtId' => 303,
'landId' => 302,
'staatsangehoerigkeitId' => 291,
'zeitschriftenversand' => true,
'strasse' => '::street',
'plz' => '12346',
2021-11-17 22:32:52 +01:00
'ort' => '::location::',
2022-08-31 00:33:36 +02:00
'gruppierung' => 'testgroup',
2021-11-17 22:32:52 +01:00
'version' => 40,
]);
2021-11-17 23:41:04 +01:00
} else {
$callback($backend);
}
$backend->fakeSubactivities([
2022-03-11 20:19:17 +01:00
305 => [['name' => 'wö', 'id' => 306]],
2021-11-17 23:41:04 +01:00
]);
}
2022-08-31 00:33:36 +02:00
public function testItSetsSettingsBeforeRunningInitializer(): void
{
$this->withoutExceptionHandling()->login();
InitializeAction::partialMock()->shouldReceive('handle')->with(12345, 'secret', 185)->once()->andReturn(true);
$response = $this->post('/initialize', [
'group_id' => 185,
'password' => 'secret',
'mglnr' => 12345,
]);
$response->assertRedirect('/');
$settings = app(NamiSettings::class);
$this->assertEquals(12345, $settings->mglnr);
$this->assertEquals('secret', $settings->password);
$this->assertEquals(185, $settings->default_group_id);
}
public function testItValidatesSetupInfo(): void
{
$this->login();
InitializeAction::partialMock()->shouldReceive('handle')->never();
$response = $this->post('/initialize', [
'group_id' => null,
'password' => null,
'mglnr' => null,
]);
$this->assertErrors(['password' => 'Passwort ist erforderlich.'], $response);
}
public function testItFiresJobWhenRunningInitializer(): void
{
Queue::fake();
$this->withoutExceptionHandling()->login();
app(InitializeAction::class)->handle(12345, 'secret', 185);
Queue::assertPushed(InitializeJob::class);
}
2021-11-18 01:54:27 +01:00
public function testItInitializesAll(): void
2021-11-17 23:41:04 +01:00
{
2022-02-19 18:06:07 +01:00
$this->withoutExceptionHandling()->login()->loginNami();
2021-11-17 23:41:04 +01:00
$this->initializeProvider();
2021-11-17 22:32:52 +01:00
2022-08-31 00:33:36 +02:00
InitializeJob::dispatch();
2021-11-17 22:32:52 +01:00
$this->assertDatabaseHas('regions', [
'name' => 'nrw',
2022-03-11 20:19:17 +01:00
'nami_id' => 304,
2021-11-17 22:32:52 +01:00
]);
$this->assertDatabaseHas('genders', [
'name' => 'Male',
2022-03-11 20:19:17 +01:00
'nami_id' => 303,
2021-11-17 22:32:52 +01:00
]);
$this->assertDatabaseHas('nationalities', [
'name' => 'deutsch',
2022-03-11 20:19:17 +01:00
'nami_id' => 291,
2021-11-17 22:32:52 +01:00
]);
$this->assertDatabaseHas('fees', [
'name' => 'Family',
2022-03-11 20:19:17 +01:00
'nami_id' => 300,
2021-11-17 22:32:52 +01:00
]);
$this->assertDatabaseHas('confessions', [
'name' => 'Konf',
2022-03-11 20:19:17 +01:00
'nami_id' => 301,
2021-11-17 22:32:52 +01:00
]);
$this->assertDatabaseHas('countries', [
'name' => 'Germany',
2022-03-11 20:19:17 +01:00
'nami_id' => 302,
2021-11-17 22:32:52 +01:00
]);
$this->assertDatabaseHas('activities', [
'name' => '€ Leiter',
2022-03-11 20:19:17 +01:00
'nami_id' => 305,
2021-11-17 22:32:52 +01:00
]);
2021-11-18 01:54:27 +01:00
$this->assertDatabaseHas('courses', [
'name' => '1a',
2022-03-11 20:19:17 +01:00
'nami_id' => 506,
2021-11-18 01:54:27 +01:00
]);
2022-08-31 00:33:36 +02:00
$this->assertDatabaseHas('groups', ['nami_id' => 1000, 'name' => 'testgroup']);
2021-11-17 22:32:52 +01:00
$this->assertDatabaseHas('members', [
'nami_id' => 411,
'gender_id' => Gender::nami(303)->id,
'country_id' => Country::nami(302)->id,
'nationality_id' => Nationality::nami(291)->id,
'send_newspaper' => 1,
'address' => '::street',
'zip' => '12346',
2021-11-17 22:32:52 +01:00
'location' => '::location::',
'version' => 40,
]);
$this->assertEquals([306], Activity::where('nami_id', 305)->firstOrFail()->subactivities()->pluck('nami_id')->toArray());
2021-11-17 22:32:52 +01:00
2022-03-15 12:03:00 +01:00
Http::assertSentCount(17);
2021-11-18 01:54:27 +01:00
}
2022-01-03 01:17:24 +01:00
public function testItInitializesFromCommandLine(): void
{
2022-02-19 18:06:07 +01:00
$this->withoutExceptionHandling()->loginNami();
2022-01-03 01:17:24 +01:00
$this->initializeProvider();
GeneralSettings::fake(['allowed_nami_accounts' => [123]]);
Artisan::call(NamiInitializeCommand::class);
2022-01-03 01:17:24 +01:00
$this->assertDatabaseHas('regions', [
'name' => 'nrw',
2022-03-11 20:19:17 +01:00
'nami_id' => 304,
2022-01-03 01:17:24 +01:00
]);
}
2021-11-18 01:54:27 +01:00
public function testSyncCoursesOfMember(): void
{
2022-02-19 18:06:07 +01:00
$this->withoutExceptionHandling()->login()->loginNami();
2022-03-11 20:19:17 +01:00
$this->initializeProvider(function ($backend) {
2021-11-18 01:54:27 +01:00
$backend->fakeMembers([
2022-03-11 20:19:17 +01:00
$this->member(['courses' => [['bausteinId' => 506, 'id' => 788, 'veranstalter' => 'KJA', 'vstgName' => 'eventname', 'vstgTag' => '2021-11-12 00:00:00']]]),
2021-11-18 01:54:27 +01:00
]);
});
2021-11-23 01:02:18 +01:00
$this->login();
2021-11-18 01:54:27 +01:00
2022-08-31 00:33:36 +02:00
InitializeJob::dispatch();
2021-11-18 01:54:27 +01:00
2021-11-20 00:48:42 +01:00
$this->assertDatabaseHas('course_members', [
2021-11-18 01:54:27 +01:00
'member_id' => Member::where('firstname', '::firstname::')->firstOrFail()->id,
'course_id' => Course::where('name', '1a')->firstOrFail()->id,
'event_name' => 'eventname',
'completed_at' => '2021-11-12',
'organizer' => 'KJA',
'nami_id' => 788,
]);
2021-11-17 22:32:52 +01:00
}
2021-11-17 23:41:04 +01:00
2022-02-12 01:16:37 +01:00
public function membershipDataProvider(): array
{
return [
2022-01-03 10:33:48 +01:00
'dont_fetch_activity_from_group' => [
[
'gruppierung' => '::newgroup:: 22',
'id' => 1077,
'taetigkeit' => '::newtaetigkeit:: (9001)',
],
2022-03-11 20:19:17 +01:00
function ($db) {
2022-01-03 10:33:48 +01:00
$db->assertDatabaseCount('memberships', 0);
},
2022-03-11 20:19:17 +01:00
function ($backend) {
2022-01-03 10:33:48 +01:00
return $backend->fakeSingleMembership(116, 1077, [
'aktivVon' => '2021-08-22 00:00:00',
'aktivBis' => '',
'gruppierungId' => 9056,
'gruppierung' => '::newgroup::',
'id' => 1077,
'taetigkeit' => '::newtaetigkeit::',
'taetigkeitId' => 4000,
'untergliederungId' => 306,
])
->fakeActivities(9056, []);
2022-03-11 20:19:17 +01:00
},
2022-01-03 10:33:48 +01:00
],
'normal' => [
[
'aktivVon' => '2021-08-22 00:00:00',
'aktivBis' => '',
2022-08-31 00:33:36 +02:00
'gruppierung' => 'testgroup',
2022-01-03 10:33:48 +01:00
'id' => 1077,
'taetigkeit' => '€ leiter (305)',
'untergliederung' => 'wö',
],
2022-03-11 20:19:17 +01:00
function ($db) {
2022-01-03 10:33:48 +01:00
$db->assertDatabaseHas('memberships', [
'member_id' => Member::where('firstname', '::firstname::')->firstOrFail()->id,
'activity_id' => Activity::where('nami_id', 305)->firstOrFail()->id,
'subactivity_id' => Subactivity::where('nami_id', 306)->firstOrFail()->id,
'nami_id' => 1077,
'from' => '2021-08-22 00:00:00',
2022-08-31 00:33:36 +02:00
'group_id' => Group::where('name', 'testgroup')->firstOrFail()->id,
2022-01-03 10:33:48 +01:00
]);
},
],
2022-01-03 01:45:33 +01:00
'fetch_subactivity_from_group' => [
[
'gruppierung' => '::newgroup:: 22',
'id' => 1077,
'untergliederung' => '::newuntertaetigkeit::',
],
2022-03-11 20:19:17 +01:00
function ($db) {
2022-01-03 01:45:33 +01:00
$db->assertDatabaseHas('subactivities', ['name' => 'wö2', 'nami_id' => 3007]);
$db->assertDatabaseHas('activity_subactivity', [
'activity_id' => Activity::where('nami_id', 305)->firstOrFail()->id,
'subactivity_id' => Subactivity::where('nami_id', 3007)->firstOrFail()->id,
]);
$db->assertDatabaseHas('memberships', [
'activity_id' => Activity::where('nami_id', 305)->firstOrFail()->id,
'subactivity_id' => Subactivity::where('nami_id', 3007)->firstOrFail()->id,
'group_id' => Group::where('nami_id', 9056)->firstOrFail()->id,
'nami_id' => 1077,
]);
},
2022-03-11 20:19:17 +01:00
function ($backend) {
2022-01-03 01:45:33 +01:00
return $backend->fakeSingleMembership(116, 1077, [
'aktivVon' => '2021-08-22 00:00:00',
'aktivBis' => '',
'gruppierungId' => 9056,
'gruppierung' => '::newgroup::',
'id' => 1077,
'taetigkeitId' => 305, // default
'taetigkeit' => 'wö',
'untergliederung' => '::newuntertaetigkeit::',
'untergliederungId' => 3007,
])
->fakeActivities(9056, [['name' => '€ leiter', 'id' => 305]])
->fakeSubactivities([
305 => [
['name' => 'wö', 'id' => 306],
['name' => 'wö2', 'id' => 3007],
2022-03-11 20:19:17 +01:00
],
2022-01-03 01:45:33 +01:00
]);
2022-03-11 20:19:17 +01:00
},
2022-01-03 01:45:33 +01:00
],
'fetch_activity_from_group' => [
[
'gruppierung' => '::newgroup:: 22',
'id' => 1077,
'taetigkeit' => '::newtaetigkeit:: (9001)',
],
2022-03-11 20:19:17 +01:00
function ($db) {
$db->assertDatabaseHas('activities', ['name' => '::newtaetigkeit::', 'nami_id' => 4000]);
$db->assertDatabaseHas('groups', ['name' => '::newgroup::', 'nami_id' => 9056]);
$db->assertDatabaseHas('activity_subactivity', [
'activity_id' => Activity::where('nami_id', 4000)->firstOrFail()->id,
'subactivity_id' => Subactivity::where('nami_id', 306)->firstOrFail()->id,
]);
$db->assertDatabaseHas('memberships', [
'activity_id' => Activity::where('nami_id', 4000)->firstOrFail()->id,
'group_id' => Group::where('nami_id', 9056)->firstOrFail()->id,
'nami_id' => 1077,
]);
},
2022-03-11 20:19:17 +01:00
function ($backend) {
return $backend->fakeSingleMembership(116, 1077, [
'aktivVon' => '2021-08-22 00:00:00',
'aktivBis' => '',
'gruppierungId' => 9056,
'gruppierung' => '::newgroup::',
'id' => 1077,
'taetigkeit' => '::newtaetigkeit::',
'taetigkeitId' => 4000,
'untergliederungId' => 306,
])
->fakeActivities(9056, [['name' => '::newtaetigkeit::', 'id' => 4000]])
->fakeSubactivities([
2022-03-11 20:19:17 +01:00
4000 => [['name' => 'wö', 'id' => 306]],
]);
2022-03-11 20:19:17 +01:00
},
],
'new_group' => [
[
'gruppierung' => '::new group:: 5555',
],
2022-03-11 20:19:17 +01:00
function ($db) {
$db->assertDatabaseHas('groups', ['name' => '::new group::', 'nami_id' => 5555]);
},
2022-03-11 20:19:17 +01:00
function ($backend) {
2022-01-03 10:33:48 +01:00
return $backend->fakeSingleMembership(116, 1077, [
'aktivVon' => '2021-08-22 00:00:00',
'aktivBis' => '',
'gruppierungId' => 5555,
'gruppierung' => '::new group::',
'id' => 1077,
'taetigkeitId' => 305,
'untergliederungId' => 306,
])
->fakeActivities(5555, [['name' => 'mitglied', 'id' => 310]])
->fakeSubactivities([
2022-03-11 20:19:17 +01:00
310 => [['name' => 'wö', 'id' => 306]],
2022-01-03 10:33:48 +01:00
]);
2022-03-11 20:19:17 +01:00
},
],
'no_subactivity' => [
[
'untergliederung' => '',
],
2022-03-11 20:19:17 +01:00
function ($db) {
$db->assertDatabaseHas('memberships', ['subactivity_id' => null]);
},
],
2022-01-03 10:33:48 +01:00
'wrong_dates' => [
[
'aktivVon' => '1014-04-01 00:00:00',
],
2022-03-11 20:19:17 +01:00
function ($db) {
2022-01-03 10:33:48 +01:00
$db->assertDatabaseHas('memberships', [
'from' => '1014-04-01 00:00:00',
]);
},
],
'not_inactive' => [
[
'aktivBis' => '2021-08-25 00:00:00',
],
2022-03-11 20:19:17 +01:00
function ($db) {
$db->assertDatabaseCount('memberships', 0);
},
],
];
}
/**
* @dataProvider membershipDataProvider
*/
public function testSyncMembershipsOfMember(array $membership, callable $dbcheck, ?callable $backendCallback = null): void
{
if (!$backendCallback) {
2022-03-11 20:19:17 +01:00
$backendCallback = function ($backend) { return $backend; };
}
2022-02-19 18:06:07 +01:00
$this->withoutExceptionHandling()->login()->loginNami();
2022-03-11 20:19:17 +01:00
$this->initializeProvider(function ($backend) use ($membership, $backendCallback) {
$backend->fakeMembers([
$this->member([
'memberships' => [array_merge([
'aktivVon' => '2021-08-22 00:00:00',
'aktivBis' => '',
2022-08-31 00:33:36 +02:00
'gruppierung' => 'testgroup',
'id' => 1077,
'taetigkeit' => 'leiter (305)',
'untergliederung' => 'wö',
], $membership)],
2022-03-11 20:19:17 +01:00
]),
]);
$backendCallback($backend);
});
2022-08-31 00:33:36 +02:00
InitializeJob::dispatch();
$dbcheck($this);
}
public function testItDoesntGetMembersWithNoJoinedAtDate(): void
{
2022-02-19 18:06:07 +01:00
$this->withoutExceptionHandling()->login()->loginNami();
2022-03-11 20:19:17 +01:00
$this->initializeProvider(function ($backend) {
$backend->fakeMembers([$this->member(['eintrittsdatum' => null])]);
});
2021-11-23 01:02:18 +01:00
$this->login();
2022-08-31 00:33:36 +02:00
InitializeJob::dispatch();
$this->assertDatabaseCount('members', 0);
}
2021-11-17 23:41:04 +01:00
/**
* @return array<int, array<int, int>>
*/
public function pageProvider(): array
{
return [
[99],
[100],
[101],
[199],
[200],
[201],
];
}
/**
* @dataProvider pageProvider
*/
public function testItInitializesPages(int $num): void
2021-11-17 23:41:04 +01:00
{
2022-02-19 18:06:07 +01:00
$this->withoutExceptionHandling()->login()->loginNami();
2022-03-11 20:19:17 +01:00
$this->initializeProvider(function ($backend) use ($num) {
2021-11-17 23:41:04 +01:00
$members = collect([]);
foreach (range(1, $num) as $i) {
$members->push($this->member(['id' => $i]));
2021-11-17 23:41:04 +01:00
}
$backend->fakeMembers($members->toArray());
});
2021-11-23 01:02:18 +01:00
$this->login();
2021-11-17 23:41:04 +01:00
2022-08-31 00:33:36 +02:00
InitializeJob::dispatch();
2021-11-17 23:41:04 +01:00
$this->assertDatabaseCount('members', $num);
}
public function testRenderErrorInConsoleWhenUsingArtisan(): void
{
$this->withoutExceptionHandling()->login()->loginNami();
2022-03-11 20:19:17 +01:00
$this->initializeProvider(function ($backend) {
app(SearchFake::class)->fetchFails($page = 1, $start = 0, 'search error');
});
$this->login();
$command = $this->artisan(NamiInitializeCommand::class);
$command->assertFailed();
$command->expectsOutput('response: {"success":false,"message":"search error"}');
$command->expectsOutput('Search failed');
}
/**
* @param array<string, mixed> $overwrites
2022-03-11 20:19:17 +01:00
*
* @return array<string, mixed>
*/
private function member(array $overwrites): array
{
return array_merge([
'vorname' => '::firstname::',
'nachname' => '::lastname::',
'beitragsartId' => 300,
'geburtsDatum' => '2014-07-11 00:00:00',
'gruppierungId' => 1000,
'geschlechtId' => 303,
'id' => 116,
'eintrittsdatum' => '2020-11-17 00:00:00',
'landId' => 302,
'staatsangehoerigkeitId' => 291,
'zeitschriftenversand' => true,
'strasse' => '::street',
'plz' => '12346',
'ort' => '::location::',
'version' => 40,
2022-08-31 00:33:36 +02:00
'gruppierung' => 'testgroup',
], $overwrites);
}
2021-11-17 22:32:52 +01:00
}