Add initialize action

This commit is contained in:
philipp lang 2022-08-31 00:33:36 +02:00
parent 6abe55e4a8
commit e7f42a72f3
8 changed files with 128 additions and 41 deletions

View File

@ -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);

View File

@ -0,0 +1,44 @@
<?php
namespace App\Initialize\Actions;
use App\Initialize\InitializeJob;
use App\Setting\NamiSettings;
use Illuminate\Http\RedirectResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class InitializeAction
{
use AsAction;
public function handle(int $mglnr, string $password, int $groupId)
{
InitializeJob::dispatch();
}
public function rules(): array
{
return [
'mglnr' => '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');
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Initialize\Actions;
use Inertia\Inertia;
use Inertia\Response;
use Lorisleiva\Actions\Concerns\AsAction;
class InitializeFormAction
{
use AsAction;
public function asController(): Response
{
return Inertia::render('Initialize/VIndex');
}
}

View File

@ -1,22 +0,0 @@
<?php
namespace App\Initialize;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;
class InitializeController extends Controller
{
public function index(): Response
{
return \Inertia::render('Initialize/VIndex');
}
public function store(): RedirectResponse
{
InitializeJob::dispatch();
return redirect()->route('home');
}
}

View File

@ -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', '');
}
}

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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);
}
}