From e7f42a72f3d381b5798cb687ffa8807307e95480 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Wed, 31 Aug 2022 00:33:36 +0200 Subject: [PATCH] Add initialize action --- .../RedirectIfNotInitializedMiddleware.php | 4 +- app/Initialize/Actions/InitializeAction.php | 44 ++++++++++++ .../Actions/InitializeFormAction.php | 17 +++++ app/Initialize/InitializeController.php | 22 ------ ...2022_02_19_230152_create_nami_settings.php | 4 +- ...5_01_185012_create_nami_settings_group.php | 2 +- routes/web.php | 6 +- tests/Feature/Initialize/InitializeTest.php | 70 +++++++++++++++---- 8 files changed, 128 insertions(+), 41 deletions(-) create mode 100644 app/Initialize/Actions/InitializeAction.php create mode 100644 app/Initialize/Actions/InitializeFormAction.php delete mode 100644 app/Initialize/InitializeController.php diff --git a/app/Http/Middleware/RedirectIfNotInitializedMiddleware.php b/app/Http/Middleware/RedirectIfNotInitializedMiddleware.php index e695ff36..637bf0ac 100644 --- a/app/Http/Middleware/RedirectIfNotInitializedMiddleware.php +++ b/app/Http/Middleware/RedirectIfNotInitializedMiddleware.php @@ -8,7 +8,7 @@ use Closure; class RedirectIfNotInitializedMiddleware { - public array $dontRedirect = ['initialize.index', 'initialize.store']; + public array $dontRedirect = ['initialize.form', 'initialize.store']; /** * Handle an incoming request. @@ -28,7 +28,7 @@ class RedirectIfNotInitializedMiddleware } if (!$this->initialized()) { - return redirect()->route('initialize.index'); + return redirect()->route('initialize.form'); } return $next($request); diff --git a/app/Initialize/Actions/InitializeAction.php b/app/Initialize/Actions/InitializeAction.php new file mode 100644 index 00000000..27a6e31c --- /dev/null +++ b/app/Initialize/Actions/InitializeAction.php @@ -0,0 +1,44 @@ + 'required|numeric', + 'password' => 'required|string', + 'group_id' => 'required|numeric', + ]; + } + + public function asController(ActionRequest $request, NamiSettings $settings): RedirectResponse + { + $settings->mglnr = $request->input('mglnr'); + $settings->password = $request->input('password'); + $settings->default_group_id = $request->input('group_id'); + $settings->save(); + + $this->handle( + (int) $request->input('mglnr', 0), + (string) $request->input('password', ''), + (int) $request->input('group_id', 0) + ); + + return redirect()->route('home'); + } +} diff --git a/app/Initialize/Actions/InitializeFormAction.php b/app/Initialize/Actions/InitializeFormAction.php new file mode 100644 index 00000000..198c8b84 --- /dev/null +++ b/app/Initialize/Actions/InitializeFormAction.php @@ -0,0 +1,17 @@ +route('home'); - } -} diff --git a/database/settings/2022_02_19_230152_create_nami_settings.php b/database/settings/2022_02_19_230152_create_nami_settings.php index f63393cf..21c1a978 100644 --- a/database/settings/2022_02_19_230152_create_nami_settings.php +++ b/database/settings/2022_02_19_230152_create_nami_settings.php @@ -6,7 +6,7 @@ class CreateNamiSettings extends SettingsMigration { public function up(): void { - $this->migrator->add('nami.mglnr', (int) env('NAMI_MGLNR')); - $this->migrator->add('nami.password', env('NAMI_PASSWORD')); + $this->migrator->add('nami.mglnr', 0); + $this->migrator->add('nami.password', ''); } } diff --git a/database/settings/2022_05_01_185012_create_nami_settings_group.php b/database/settings/2022_05_01_185012_create_nami_settings_group.php index c3dfb027..df8dde86 100644 --- a/database/settings/2022_05_01_185012_create_nami_settings_group.php +++ b/database/settings/2022_05_01_185012_create_nami_settings_group.php @@ -6,6 +6,6 @@ class CreateNamiSettingsGroup extends SettingsMigration { public function up(): void { - $this->migrator->add('nami.default_group_id', (int) env('NAMI_GROUP')); + $this->migrator->add('nami.default_group_id', 0); } } diff --git a/routes/web.php b/routes/web.php index b4627593..3295ff7e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,7 +3,8 @@ use App\Contribution\ContributionController; use App\Course\Controllers\CourseController; use App\Http\Controllers\HomeController; -use App\Initialize\InitializeController; +use App\Initialize\Actions\InitializeAction; +use App\Initialize\Actions\InitializeFormAction; use App\Member\Controllers\MemberResyncController; use App\Member\MemberConfirmController; use App\Member\MemberController; @@ -22,7 +23,8 @@ Route::group(['namespace' => 'App\\Http\\Controllers'], function (): void { Route::group(['middleware' => 'auth:web'], function (): void { Route::get('/', HomeController::class)->name('home'); - Route::resource('initialize', InitializeController::class); + Route::get('/initialize', InitializeFormAction::class)->name('initialize.form'); + Route::post('/initialize', InitializeAction::class)->name('initialize.store'); Route::resource('member', MemberController::class); Route::resource('member.payment', PaymentController::class); Route::resource('allpayment', AllpaymentController::class); diff --git a/tests/Feature/Initialize/InitializeTest.php b/tests/Feature/Initialize/InitializeTest.php index 86ed01c6..02816f08 100644 --- a/tests/Feature/Initialize/InitializeTest.php +++ b/tests/Feature/Initialize/InitializeTest.php @@ -8,13 +8,17 @@ use App\Country; use App\Course\Models\Course; use App\Gender; use App\Group; +use App\Initialize\Actions\InitializeAction; +use App\Initialize\InitializeJob; use App\Member\Member; use App\Nationality; use App\Setting\GeneralSettings; +use App\Setting\NamiSettings; use App\Subactivity; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Queue; use Tests\TestCase; use Zoomyboy\LaravelNami\Backend\FakeBackend; use Zoomyboy\LaravelNami\Fakes\GroupFake; @@ -27,7 +31,7 @@ class InitializeTest extends TestCase public function initializeProvider(callable $callback = null): void { app(GroupFake::class) - ->fetches(null, [1000 => ['name' => '::group::']]) + ->fetches(null, [1000 => ['name' => 'testgroup']]) ->fetches(1000, []); $backend = app(FakeBackend::class) ->addSearch(123, ['entries_vorname' => '::firstname::', 'entries_nachname' => '::lastname::', 'entries_gruppierungId' => 1000]) @@ -56,7 +60,7 @@ class InitializeTest extends TestCase 'strasse' => '::street', 'plz' => '12346', 'ort' => '::location::', - 'gruppierung' => '::group::', + 'gruppierung' => 'testgroup', 'version' => 40, ]); } else { @@ -68,12 +72,54 @@ class InitializeTest extends TestCase ]); } + 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); + } + public function testItInitializesAll(): void { $this->withoutExceptionHandling()->login()->loginNami(); $this->initializeProvider(); - $this->post('/initialize'); + InitializeJob::dispatch(); $this->assertDatabaseHas('regions', [ 'name' => 'nrw', @@ -107,7 +153,7 @@ class InitializeTest extends TestCase 'name' => '1a', 'nami_id' => 506, ]); - $this->assertDatabaseHas('groups', ['nami_id' => 1000, 'name' => '::group::']); + $this->assertDatabaseHas('groups', ['nami_id' => 1000, 'name' => 'testgroup']); $this->assertDatabaseHas('members', [ 'nami_id' => 411, 'gender_id' => Gender::nami(303)->id, @@ -148,7 +194,7 @@ class InitializeTest extends TestCase }); $this->login(); - $this->post('/initialize'); + InitializeJob::dispatch(); $this->assertDatabaseHas('course_members', [ 'member_id' => Member::where('firstname', '::firstname::')->firstOrFail()->id, @@ -190,7 +236,7 @@ class InitializeTest extends TestCase [ 'aktivVon' => '2021-08-22 00:00:00', 'aktivBis' => '', - 'gruppierung' => '::group::', + 'gruppierung' => 'testgroup', 'id' => 1077, 'taetigkeit' => '€ leiter (305)', 'untergliederung' => 'wö', @@ -202,7 +248,7 @@ class InitializeTest extends TestCase 'subactivity_id' => Subactivity::where('nami_id', 306)->firstOrFail()->id, 'nami_id' => 1077, 'from' => '2021-08-22 00:00:00', - 'group_id' => Group::where('name', '::group::')->firstOrFail()->id, + 'group_id' => Group::where('name', 'testgroup')->firstOrFail()->id, ]); }, ], @@ -349,7 +395,7 @@ class InitializeTest extends TestCase 'memberships' => [array_merge([ 'aktivVon' => '2021-08-22 00:00:00', 'aktivBis' => '', - 'gruppierung' => '::group::', + 'gruppierung' => 'testgroup', 'id' => 1077, 'taetigkeit' => 'leiter (305)', 'untergliederung' => 'wö', @@ -359,7 +405,7 @@ class InitializeTest extends TestCase $backendCallback($backend); }); - $this->post('/initialize'); + InitializeJob::dispatch(); $dbcheck($this); } @@ -372,7 +418,7 @@ class InitializeTest extends TestCase }); $this->login(); - $this->post('/initialize'); + InitializeJob::dispatch(); $this->assertDatabaseCount('members', 0); } @@ -409,7 +455,7 @@ class InitializeTest extends TestCase }); $this->login(); - $this->post('/initialize'); + InitializeJob::dispatch(); $this->assertDatabaseCount('members', $num); } @@ -452,7 +498,7 @@ class InitializeTest extends TestCase 'plz' => '12346', 'ort' => '::location::', 'version' => 40, - 'gruppierung' => '::group::', + 'gruppierung' => 'testgroup', ], $overwrites); } }