80 lines
4.1 KiB
PHP
80 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use App\Services\DemographicEvidence;
|
|
use App\Services\DemographicInstallation;
|
|
use App\Support\IsolatedQaDatabase;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Tests\TestCase;
|
|
|
|
final class DemographicZambeziaTest extends TestCase
|
|
{
|
|
use DatabaseTransactions;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->withoutVite();
|
|
$this->assertTrue(IsolatedQaDatabase::allowsSchemaChanges());
|
|
$this->assertMatchesRegularExpression('/^demography_restore_\d{8}_\d{6}_test$/', DB::connection()->getDatabaseName());
|
|
}
|
|
|
|
private function manager(): User
|
|
{
|
|
$actor = User::factory()->create();
|
|
$actor->forceFill(['two_factor_secret' => Crypt::encrypt('JBSWY3DPEHPK3PXP'), 'two_factor_confirmed_at' => now()])->save();
|
|
$second = User::factory()->create();
|
|
DB::table('portal_superuser_pair')->where('id', 1)->update(['first_user_id' => $actor->id, 'second_user_id' => $second->id]);
|
|
config(['portal.managers' => [$actor->email]]);
|
|
$this->actingAs($actor);
|
|
|
|
return $actor;
|
|
}
|
|
|
|
public function test_new_cohort_requires_current_session_mfa(): void
|
|
{
|
|
$this->manager();
|
|
$this->postJson('/management/demography/install', ['mode' => 'zambezia', 'hash' => str_repeat('0', 64), 'operation_key' => (string) Str::uuid(), 'confirm' => true, 'reason' => 'Synthetic fresh session test.'])->assertForbidden();
|
|
$this->assertSame(793772, DB::table('demographic_observations')->count());
|
|
}
|
|
|
|
public function test_only_zambezia_is_added_and_all_previous_imports_are_preserved(): void
|
|
{
|
|
$actor = $this->manager();
|
|
$this->withSession(['portal.mfa_actor' => $actor->id, 'portal.mfa_at' => time()]);
|
|
$this->assertSame(11, DB::table('demographic_imports')->where('status', 'approved')->count());
|
|
$oldIds = DB::table('demographic_imports')->pluck('id');
|
|
$oldImports = DB::table('demographic_imports')->orderBy('id')->get()->toJson();
|
|
$oldEvents = DB::table('demographic_events')->orderBy('id')->get()->toJson();
|
|
$service = app(DemographicInstallation::class);
|
|
$plan = $service->preview($actor);
|
|
$this->assertSame('zambezia', $plan['mode']);
|
|
$this->assertSame(1, $plan['pending_count']);
|
|
$this->assertCount(1, $plan['files']);
|
|
$this->assertCount(5, $plan['new_districts']);
|
|
$body = ['mode' => 'zambezia', 'hash' => $plan['hash'], 'operation_key' => (string) Str::uuid(), 'confirm' => true, 'reason' => 'Incorporação de Zambezia.xlsx autorizada e cinco distritos;ensaio apenas na cópia restaurada.'];
|
|
$result = $service->apply($actor, $body);
|
|
$this->assertSame(126684, $result['record_count']);
|
|
$this->assertCount(1, $result['imports']);
|
|
$this->assertSame(12, DB::table('demographic_imports')->where('status', 'approved')->count());
|
|
$this->assertSame(920456, DB::table('demographic_observations')->count());
|
|
$this->assertSame($oldImports, DB::table('demographic_imports')->whereIn('id', $oldIds)->orderBy('id')->get()->toJson());
|
|
$this->assertSame($oldEvents, DB::table('demographic_events')->whereIn('import_id', $oldIds)->orderBy('id')->get()->toJson());
|
|
$this->assertTrue($service->apply($actor, $body)['repeated']);
|
|
$this->assertSame(920456, DB::table('demographic_observations')->count());
|
|
$this->assertFalse($service->preview($actor)['ready']);
|
|
$this->assertSame(0, $service->preview($actor)['pending_count']);
|
|
foreach (['Derre', 'Luabo', 'Mocubela', 'Molumbo', 'Mulevala'] as $name) {
|
|
$ids = app(DemographicEvidence::class)->territories()->where('name', $name)->pluck('id')->all();
|
|
$this->assertCount(1, $ids);
|
|
$this->assertSame('available', app(DemographicEvidence::class)->analyse($ids, 2017, 2050)['state']);
|
|
}
|
|
$this->assertDatabaseHas('audit_events', ['source_system' => 'demography-installation', 'action' => 'extend_zambezia', 'correlation_id' => $body['operation_key']]);
|
|
}
|
|
}
|