This commit is contained in:
@@ -0,0 +1,260 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\EconomicCluster;
|
||||
use App\Models\InvestmentDossier;
|
||||
use App\Models\Opportunity;
|
||||
use App\Models\Project;
|
||||
use App\Models\Sector;
|
||||
use App\Models\User;
|
||||
use App\Services\InvestmentCatalogue;
|
||||
use App\Services\InvestmentRelations;
|
||||
use App\Services\InvestmentRelationshipRepair;
|
||||
use App\Services\InvestmentTerritorialValidation;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Tests\TestCase;
|
||||
|
||||
class InvestmentRelationshipTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private function fixture(): array
|
||||
{
|
||||
$parent = null;
|
||||
$ids = [];
|
||||
foreach (['country', 'province', 'district', 'admin_post', 'locality', 'site'] as $type) {
|
||||
$id = (string) Str::uuid();
|
||||
DB::table('geo_locations')->insert(['id' => $id, 'code' => $id, 'name' => 'QA '.$type, 'location_type' => $type, 'parent_id' => $parent,
|
||||
'data_classification' => 'public', 'source_system' => 'cartografia-workbook', 'external_id' => '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:'.$id]);
|
||||
$ids[$type] = $id;
|
||||
$parent = $id;
|
||||
}
|
||||
$project = Project::factory()->create(['code' => 'A99', 'name' => 'QA Anchor', 'status' => 'draft', 'data_classification' => 'internal']);
|
||||
DB::table('geo_locations')->where('id', $ids['site'])->update(['source_system' => 'QA documented site', 'external_id' => null]);
|
||||
$dossier = InvestmentDossier::create(['project_id' => $project->id, 'title' => 'QA Anchor dossier', 'source' => 'QA synthetic dossier source',
|
||||
'content' => ['location' => 'QA province, district and site', 'description' => 'Unique documented component needle'], 'publication_status' => 'published', 'published_at' => now()->subMinute(), 'approval_basis' => 'document_owner', 'approval_evidence' => 'Synthetic QA fixture disclosure only']);
|
||||
$cluster = EconomicCluster::create(['code' => 'QA-CLU', 'name' => 'QA Cluster', 'sector' => 'QA', 'status' => 'planned']);
|
||||
$clusterDossier = InvestmentDossier::create(['cluster_id' => $cluster->id, 'title' => 'QA Cluster dossier', 'source' => 'QA synthetic cluster source',
|
||||
'content' => ['anchors' => 'A99; QA documented proposal'], 'publication_status' => 'published', 'published_at' => now()->subMinute(), 'approval_basis' => 'document_owner', 'approval_evidence' => 'Synthetic QA fixture disclosure only']);
|
||||
DB::table('cluster_projects')->insert(['id' => (string) Str::uuid(), 'cluster_id' => $cluster->id, 'project_id' => $project->id, 'role' => 'anchor', 'source_system' => 'upgrade-v4']);
|
||||
DB::table('project_locations')->insert(['id' => (string) Str::uuid(), 'project_id' => $project->id, 'geo_location_id' => $ids['site'], 'role' => 'primary', 'data_classification' => 'public']);
|
||||
DB::table('geo_geometries')->insert(['id' => (string) Str::uuid(), 'geo_location_id' => $ids['site'], 'geometry_type' => 'point', 'data_classification' => 'public',
|
||||
'geom' => DB::raw('ST_SetSRID(ST_MakePoint(32.5,-25.9),4326)'), 'source_system' => 'QA documented coordinates fixture']);
|
||||
$sector = Sector::create(['name' => 'QA sector', 'slug' => 'qa-sector', 'is_active' => true]);
|
||||
$opportunity = Opportunity::create(['sector_id' => $sector->id, 'reference' => 'QA-REL', 'title' => 'QA opportunity', 'slug' => 'qa-rel', 'status' => 'published', 'published_at' => now()->subMinute()]);
|
||||
DB::table('opportunity_projects')->insert(['opportunity_id' => $opportunity->id, 'project_id' => $project->id]);
|
||||
|
||||
return compact('ids', 'project', 'dossier', 'cluster', 'clusterDossier', 'opportunity', 'sector');
|
||||
}
|
||||
|
||||
public function test_project_location_and_dossier_navigation_are_bidirectional_without_changing_project_status(): void
|
||||
{
|
||||
$f = $this->fixture();
|
||||
$this->get('/carteira/'.$f['dossier']->id)->assertInertia(fn (Assert $p) => $p->has('geography.locations', 1)->has('geography.locations.0.path', 4)
|
||||
->where('geography.locations.0.role', 'primary')->where('geography.locations.0.coordinates.0.geometry.coordinates.0', 32.5)->has('opportunities', 1)->has('geography.clusters', 1));
|
||||
$this->get('/oportunidades/qa-rel?from_dossier='.$f['dossier']->id)->assertInertia(fn (Assert $p) => $p->where('dossierOrigin.id', $f['dossier']->id)
|
||||
->where('context.projects.0.dossiers.0.id', $f['dossier']->id)->has('context.projects.0.geography.locations', 1));
|
||||
$this->get('/oportunidades/qa-rel?from_dossier='.$f['clusterDossier']->id)->assertNotFound();
|
||||
$this->assertSame('draft', $f['project']->fresh()->status);
|
||||
}
|
||||
|
||||
public function test_search_and_all_territorial_levels_follow_explicit_project_relations(): void
|
||||
{
|
||||
$f = $this->fixture();
|
||||
$filters = [];
|
||||
foreach (['province', 'district', 'admin_post', 'locality'] as $type) {
|
||||
$filters[$type.'_id'] = $f['ids'][$type];
|
||||
$this->assertSame([$f['opportunity']->id], app(InvestmentCatalogue::class)->query($filters)->pluck('id')->all());
|
||||
$this->get('/carteira?'.http_build_query($filters))->assertInertia(fn (Assert $p) => $p->has('records.data', 2));
|
||||
}
|
||||
$this->assertSame([$f['opportunity']->id], app(InvestmentCatalogue::class)->query(['q' => 'component needle'])->pluck('id')->all());
|
||||
$this->assertSame([$f['opportunity']->id], app(InvestmentCatalogue::class)->query(['geo_location_id' => $f['ids']['province']])->pluck('id')->all());
|
||||
$map = app(InvestmentCatalogue::class)->map($filters);
|
||||
$this->assertSame(1, $map['georeferenced_count']);
|
||||
$this->assertSame('linked_project_site', $map['features'][0]['properties']['location_basis']);
|
||||
}
|
||||
|
||||
public function test_cluster_member_locations_do_not_create_opportunity_project_relations_or_a_cluster_point(): void
|
||||
{
|
||||
$f = $this->fixture();
|
||||
DB::table('opportunity_projects')->delete();
|
||||
DB::table('opportunity_clusters')->insert(['opportunity_id' => $f['opportunity']->id, 'cluster_id' => $f['cluster']->id]);
|
||||
$this->get('/carteira/'.$f['clusterDossier']->id)->assertInertia(fn (Assert $p) => $p->has('geography.locations', 0)
|
||||
->has('geography.members', 1)->where('geography.members.0.validation_status', 'pending')->has('geography.members.0.locations', 1)->has('opportunities', 1));
|
||||
$this->get('/carteira/'.$f['dossier']->id.'?from='.$f['clusterDossier']->id)->assertOk();
|
||||
$this->assertSame([$f['opportunity']->id], app(InvestmentCatalogue::class)->query(['province_id' => $f['ids']['province']])->pluck('id')->all());
|
||||
$this->assertSame([], app(InvestmentCatalogue::class)->context($f['opportunity']->id)['projects']->all());
|
||||
$this->assertSame(0, app(InvestmentCatalogue::class)->map([])['georeferenced_count']);
|
||||
$this->assertDatabaseCount('opportunity_projects', 0);
|
||||
}
|
||||
|
||||
public function test_private_revoked_and_deleted_relationships_are_not_disclosed(): void
|
||||
{
|
||||
$f = $this->fixture();
|
||||
DB::table('project_locations')->update(['data_classification' => 'confidential']);
|
||||
DB::table('cluster_projects')->update(['validation_status' => 'revoked']);
|
||||
$this->assertSame([], app(InvestmentRelations::class)->projectLocations($f['project']->id));
|
||||
$this->assertSame(0, app(InvestmentRelations::class)->memberships()->count());
|
||||
$this->assertSame(0, app(InvestmentCatalogue::class)->map([])['georeferenced_count']);
|
||||
$f['dossier']->update(['publication_status' => 'draft']);
|
||||
$this->get('/carteira/'.$f['dossier']->id)->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_ambiguous_multiple_sites_do_not_choose_an_arbitrary_opportunity_marker(): void
|
||||
{
|
||||
$f = $this->fixture();
|
||||
$id = (string) Str::uuid();
|
||||
DB::table('geo_locations')->insert(['id' => $id, 'code' => $id, 'name' => 'QA second site', 'location_type' => 'site', 'parent_id' => $f['ids']['locality'], 'data_classification' => 'public']);
|
||||
DB::table('project_locations')->insert(['id' => (string) Str::uuid(), 'project_id' => $f['project']->id, 'geo_location_id' => $id, 'role' => 'secondary', 'data_classification' => 'public']);
|
||||
DB::table('geo_geometries')->insert(['id' => (string) Str::uuid(), 'geo_location_id' => $id, 'geometry_type' => 'point', 'data_classification' => 'public', 'geom' => DB::raw('ST_SetSRID(ST_MakePoint(32.6,-25.8),4326)')]);
|
||||
$map = app(InvestmentCatalogue::class)->map([]);
|
||||
$this->assertSame(0, $map['georeferenced_count']);
|
||||
$this->assertCount(1, $map['unlocated']);
|
||||
$this->assertCount(2, app(InvestmentRelations::class)->projectLocations($f['project']->id));
|
||||
}
|
||||
|
||||
public function test_repair_requires_authenticated_management_is_transactional_and_replays_without_duplicates(): void
|
||||
{
|
||||
$f = $this->fixture();
|
||||
$directory = storage_path('framework/testing/relationship-repair-'.Str::uuid());
|
||||
mkdir($directory, 0700, true);
|
||||
config(['investment.repair_directory' => $directory]);
|
||||
$dump = $directory.'/synthetic-qa-backup.dump';
|
||||
file_put_contents($dump, 'synthetic isolated QA proof fixture');
|
||||
$target = ['database' => DB::connection()->getDatabaseName(), 'port' => 55441];
|
||||
file_put_contents($directory.'/backup-proof.json', json_encode(['restore_verified' => true, 'dump_path' => $dump, 'sha256' => hash_file('sha256', $dump), 'target' => $target]));
|
||||
$row = ['project_id' => $f['project']->id, 'code' => 'A99', 'dossier_id' => $f['dossier']->id, 'dossier_version' => 1, 'geo_location_id' => $f['ids']['province'],
|
||||
'role' => 'secondary', 'source_reference' => $f['dossier']->source, 'source_quote' => $f['dossier']->content['location'], 'reason' => 'QA documented province',
|
||||
'expected_project_version' => $f['project']->fresh()->version_no, 'data_classification' => 'public'];
|
||||
file_put_contents($directory.'/preview.json', json_encode(['target' => $target, 'locations' => [$row], 'memberships' => [], 'prepared_opportunities' => [], 'pending' => []]));
|
||||
$hash = hash_file('sha256', $directory.'/preview.json');
|
||||
$this->get('/management/relationship-repair')->assertRedirect('/login');
|
||||
$manager = User::factory()->create();
|
||||
$this->actingAs($manager)->get('/management/relationship-repair')->assertForbidden();
|
||||
config(['portal.managers' => [$manager->email]]);
|
||||
$this->get('/management/relationship-repair')->assertOk();
|
||||
$this->postJson('/management/relationship-repair', ['hash' => str_repeat('0', 64)])->assertConflict();
|
||||
$invalid = [...$row, 'expected_project_version' => $row['expected_project_version'] + 7, 'role' => 'impact'];
|
||||
file_put_contents($directory.'/preview.json', json_encode(['target' => $target, 'locations' => [$row, $invalid], 'memberships' => []]));
|
||||
$this->postJson('/management/relationship-repair', ['hash' => hash_file('sha256', $directory.'/preview.json')])->assertConflict();
|
||||
$this->assertDatabaseCount('project_locations', 1);
|
||||
file_put_contents($directory.'/preview.json', json_encode(['target' => $target, 'locations' => [$row], 'memberships' => [], 'prepared_opportunities' => [], 'pending' => []]));
|
||||
$hash = hash_file('sha256', $directory.'/preview.json');
|
||||
$this->postJson('/management/relationship-repair', ['hash' => $hash])->assertOk()->assertJsonPath('locations_created', 1);
|
||||
$this->postJson('/management/relationship-repair', ['hash' => $hash])->assertOk()->assertJsonPath('replayed', true);
|
||||
$this->assertDatabaseCount('project_locations', 2);
|
||||
$this->assertSame('draft', $f['project']->fresh()->status);
|
||||
$this->assertSame('published', $f['dossier']->fresh()->publication_status);
|
||||
$changed = [...$row, 'role' => 'impact'];
|
||||
file_put_contents($directory.'/preview.json', json_encode(['target' => $target, 'locations' => [$changed], 'memberships' => []]));
|
||||
$this->postJson('/management/relationship-repair', ['hash' => hash_file('sha256', $directory.'/preview.json')])->assertConflict();
|
||||
$this->assertDatabaseCount('project_locations', 2);
|
||||
@unlink($directory.'/preview.json');
|
||||
@unlink($directory.'/backup-proof.json');
|
||||
@unlink($dump);
|
||||
@rmdir($directory);
|
||||
}
|
||||
|
||||
private function repairPlan(array $rows): array
|
||||
{
|
||||
$directory = storage_path('framework/testing/territorial-repair-'.Str::uuid());
|
||||
mkdir($directory, 0700, true);
|
||||
config(['investment.repair_directory' => $directory]);
|
||||
$dump = $directory.'/qa.dump';
|
||||
file_put_contents($dump, 'Synthetic QA fixture; not an active backup');
|
||||
$target = ['database' => DB::connection()->getDatabaseName(), 'port' => 55441];
|
||||
file_put_contents($directory.'/backup-proof.json', json_encode(['restore_verified' => true, 'dump_path' => $dump, 'sha256' => hash_file('sha256', $dump), 'target' => $target]));
|
||||
file_put_contents($directory.'/preview.json', json_encode(['target' => $target, 'locations' => $rows, 'memberships' => [], 'pending' => [], 'prepared_opportunities' => []]));
|
||||
$user = User::factory()->create();
|
||||
config(['portal.managers' => [$user->email]]);
|
||||
$this->actingAs($user);
|
||||
|
||||
return app(InvestmentRelationshipRepair::class)->preview();
|
||||
}
|
||||
|
||||
private function territorialRow(array $f, string $level = 'province', string $role = 'secondary'): array
|
||||
{
|
||||
return ['project_id' => $f['project']->id, 'code' => 'A99', 'dossier_id' => $f['dossier']->id, 'dossier_version' => 1,
|
||||
'geo_location_id' => $f['ids'][$level], 'role' => $role, 'source_reference' => $f['dossier']->source,
|
||||
'source_quote' => $f['dossier']->content['location'], 'reason' => 'Explicit synthetic QA document',
|
||||
'expected_project_version' => $f['project']->fresh()->version_no, 'data_classification' => 'public'];
|
||||
}
|
||||
|
||||
public function test_individual_territories_without_coordinates_apply_sequentially_and_audit_actor_date_and_source(): void
|
||||
{
|
||||
$f = $this->fixture();
|
||||
$preview = $this->repairPlan([$this->territorialRow($f), $this->territorialRow($f, 'district', 'impact')]);
|
||||
$this->assertSame('grounded', $preview['territorial'][0]['status']);
|
||||
$this->assertCount(0, $preview['territorial'][0]['coordinates']);
|
||||
$this->assertCount(1, $preview['territorial'][0]['current']);
|
||||
$this->get('/management/relationship-repair')->assertInertia(fn (Assert $p) => $p->where('territorial.0.status', 'grounded')->where('territorial.1.proposed.role', 'impact'));
|
||||
foreach ([0, 1] as $index) {
|
||||
$fresh = app(InvestmentRelationshipRepair::class)->preview();
|
||||
$request = ['hash' => $fresh['hash'], 'row_id' => $fresh['territorial'][$index]['row_id'], 'validation_hash' => $fresh['territorial'][$index]['validation_hash']];
|
||||
$this->postJson('/management/relationship-repair', $request)->assertOk()->assertJsonPath('locations_created', 1);
|
||||
$this->postJson('/management/relationship-repair', $request)->assertOk()->assertJsonPath('replayed', true);
|
||||
}
|
||||
$this->assertDatabaseCount('project_locations', 3);
|
||||
$history = app(InvestmentRelationshipRepair::class)->preview()['history'];
|
||||
$this->assertCount(2, $history);
|
||||
$this->assertSame(auth()->id(), $history[0]['actor_id']);
|
||||
$this->assertNotEmpty($history[0]['date']);
|
||||
$this->assertSame($f['dossier']->source, $history[0]['data']['sources'][0]['source_reference']);
|
||||
$this->assertSame('draft', $f['project']->fresh()->status);
|
||||
}
|
||||
|
||||
public function test_pending_and_contradictory_rows_are_retained_while_grounded_rows_apply(): void
|
||||
{
|
||||
$f = $this->fixture();
|
||||
$good = $this->territorialRow($f);
|
||||
$pending = [...$this->territorialRow($f, 'district'), 'source_quote' => ''];
|
||||
$bad = [...$this->territorialRow($f, 'locality', 'impact'), 'province_id' => (string) Str::uuid()];
|
||||
$preview = $this->repairPlan([$good, $pending, $bad]);
|
||||
$this->assertSame(['grounded', 'pending', 'contradictory'], array_column($preview['territorial'], 'status'));
|
||||
foreach ([1, 2] as $index) {
|
||||
$this->postJson('/management/relationship-repair', ['hash' => $preview['hash'], 'row_id' => $preview['territorial'][$index]['row_id'], 'validation_hash' => $preview['territorial'][$index]['validation_hash']])->assertConflict();
|
||||
}
|
||||
$this->postJson('/management/relationship-repair', ['hash' => $preview['hash']])->assertOk()->assertJsonPath('locations_created', 1);
|
||||
$this->assertDatabaseCount('project_locations', 2);
|
||||
$this->assertDatabaseMissing('project_locations', ['project_id' => $f['project']->id, 'geo_location_id' => $f['ids']['locality']]);
|
||||
$this->assertSame('contradictory', app(InvestmentRelationshipRepair::class)->preview()['territorial'][2]['status']);
|
||||
}
|
||||
|
||||
public function test_coordinate_inclusion_and_broken_hierarchy_are_checked_without_inventing_boundaries(): void
|
||||
{
|
||||
$f = $this->fixture();
|
||||
$row = $this->territorialRow($f, 'site');
|
||||
$validator = app(InvestmentTerritorialValidation::class);
|
||||
$without = $validator->validate($row);
|
||||
$this->assertSame('grounded', $without['status']);
|
||||
$this->assertSame([], $without['coordinate_checks']);
|
||||
DB::table('geo_geometries')->insert(['id' => (string) Str::uuid(), 'geo_location_id' => $f['ids']['province'], 'geometry_type' => 'polygon', 'data_classification' => 'public',
|
||||
'geom' => DB::raw('ST_MakeEnvelope(32,-26,33,-25,4326)'), 'source_system' => 'Synthetic QA boundary']);
|
||||
$inside = $validator->validate($row);
|
||||
$this->assertSame('grounded', $inside['status']);
|
||||
$this->assertTrue($inside['coordinate_checks'][0]['compatible']);
|
||||
DB::table('geo_geometries')->where('geo_location_id', $f['ids']['site'])->update(['geom' => DB::raw('ST_SetSRID(ST_MakePoint(35,-25.9),4326)')]);
|
||||
$outside = $validator->validate($row);
|
||||
$this->assertSame('contradictory', $outside['status']);
|
||||
$this->assertFalse($outside['coordinate_checks'][0]['compatible']);
|
||||
$this->assertSame('contradictory', $validator->validate([...$row, 'coordinates' => [181, -25]])['status']);
|
||||
DB::table('geo_locations')->where('id', $f['ids']['admin_post'])->update(['deleted_at' => now()]);
|
||||
$broken = $validator->validate($this->territorialRow($f, 'locality'));
|
||||
$this->assertSame('contradictory', $broken['status']);
|
||||
$this->assertStringContainsString('antepassado indisponível', implode(' ', $broken['issues']));
|
||||
}
|
||||
|
||||
public function test_individual_application_rejects_changed_territorial_preview_and_unknown_row(): void
|
||||
{
|
||||
$f = $this->fixture();
|
||||
$preview = $this->repairPlan([$this->territorialRow($f)]);
|
||||
$this->postJson('/management/relationship-repair', ['hash' => $preview['hash'], 'row_id' => str_repeat('a', 64), 'validation_hash' => $preview['territorial'][0]['validation_hash']])->assertUnprocessable();
|
||||
DB::table('geo_locations')->where('id', $f['ids']['province'])->update(['name' => 'Changed QA territory']);
|
||||
$this->postJson('/management/relationship-repair', ['hash' => $preview['hash'], 'row_id' => $preview['territorial'][0]['row_id'], 'validation_hash' => $preview['territorial'][0]['validation_hash']])->assertConflict();
|
||||
$this->assertDatabaseCount('project_locations', 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\InvestmentDossier;
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use App\Services\PartnershipExchange;
|
||||
use App\Services\PartnershipInstallation;
|
||||
use App\Services\PartnershipMatching;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PartnershipExchangeTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private function draft(User $owner, array $extra = []): object
|
||||
{
|
||||
return app(PartnershipExchange::class)->save($owner, array_replace_recursive(['client_key' => (string) Str::uuid(), 'title' => 'QA partnership', 'kind' => 'seeking', 'partnership_type' => 'joint_venture',
|
||||
'offer' => 'QA documented capacity', 'seeking' => 'QA technical contribution', 'payload' => ['summary' => 'QA summary', 'consent' => true, 'mandatory' => ['capacity']], 'origins' => [], 'locations' => []], $extra));
|
||||
}
|
||||
|
||||
private function publish(User $owner): array
|
||||
{
|
||||
$manager = User::factory()->create();
|
||||
config(['portal.managers' => [$manager->email]]);
|
||||
$p = $this->draft($owner);
|
||||
$p = app(PartnershipExchange::class)->transition($owner, $p->id, $p->version, 'submit', 'QA submission');
|
||||
$p = app(PartnershipExchange::class)->transition($manager, $p->id, $p->version, 'publish', 'QA reviewed disclosure');
|
||||
|
||||
return [$p, $manager];
|
||||
}
|
||||
|
||||
private function candidature(User $candidate, object $p): object
|
||||
{
|
||||
return app(PartnershipExchange::class)->interest($candidate, $p->id, ['proposal' => 'QA proposal', 'payload' => ['consent' => true, 'capacity' => 'Declared capacity, not certified']]);
|
||||
}
|
||||
|
||||
public function test_complete_local_flow_from_personal_draft_to_agreement_and_completed_milestone(): void
|
||||
{
|
||||
$owner = User::factory()->create(['email_verified_at' => null]);
|
||||
$candidate = User::factory()->create(['email_verified_at' => null]);
|
||||
[$p, $manager] = $this->publish($owner);
|
||||
$this->getJson('/parcerias/'.$p->id)->assertOk()->assertJsonPath('record.status', 'published');
|
||||
$i = $this->candidature($candidate, $p);
|
||||
$other = $this->candidature(User::factory()->create(), $p);
|
||||
$i = app(PartnershipExchange::class)->decideInterest($owner, $i->id, $i->version, 'shortlist', 'Potential match; unknown data to clarify');
|
||||
$i = app(PartnershipExchange::class)->decideInterest($owner, $i->id, $i->version, 'connect', 'Private negotiation authorized');
|
||||
$room = DB::table('partnership_rooms')->where('interest_id', $i->id)->first();
|
||||
$this->actingAs($owner)->postJson('/parcerias/salas/'.$room->id.'/acordo', ['version' => 1, 'action' => 'record', 'title' => 'QA work plan', 'terms' => 'Parties, objectives, contributions, risks and exit conditions', 'reason' => 'QA instrument recorded'])->assertOk();
|
||||
$this->postJson('/parcerias/salas/'.$room->id.'/acordo', ['version' => 2, 'action' => 'confirm', 'reason' => 'Proponent confirmation'])->assertOk();
|
||||
$this->actingAs($candidate)->postJson('/parcerias/salas/'.$room->id.'/acordo', ['version' => 3, 'action' => 'confirm', 'reason' => 'Candidate confirmation'])->assertOk();
|
||||
$this->assertDatabaseHas('partnership_rooms', ['id' => $room->id, 'status' => 'agreed']);
|
||||
$this->postJson('/parcerias/salas/'.$room->id.'/marcos', ['title' => 'Pilot deliverable', 'responsible_id' => $candidate->id, 'status' => 'pending'])->assertOk();
|
||||
$this->postJson('/parcerias/salas/'.$room->id.'/acordo', ['version' => 4, 'action' => 'start', 'reason' => 'Start pilot'])->assertOk();
|
||||
$this->postJson('/parcerias/salas/'.$room->id.'/acordo', ['version' => 5, 'action' => 'complete', 'reason' => 'Premature completion'])->assertConflict();
|
||||
$milestone = DB::table('partnership_milestones')->where('room_id', $room->id)->first();
|
||||
$this->putJson('/parcerias/salas/'.$room->id.'/marcos/'.$milestone->id, ['version' => 1, 'title' => 'Pilot deliverable', 'responsible_id' => $candidate->id, 'status' => 'completed', 'evidence' => 'QA evidence reference'])->assertOk();
|
||||
$this->postJson('/parcerias/salas/'.$room->id.'/acordo', ['version' => 5, 'action' => 'complete', 'reason' => 'Deliverables complete'])->assertOk();
|
||||
$this->assertDatabaseHas('partnership_rooms', ['id' => $room->id, 'status' => 'closed']);
|
||||
$this->assertDatabaseHas('partnership_interests', ['id' => $other->id, 'status' => 'submitted']);
|
||||
$this->assertDatabaseHas('portal_partnerships', ['id' => $p->id, 'status' => 'published']);
|
||||
$this->assertNotEmpty(DB::table('audit_events')->where('source_system', 'partnership-exchange')->get());
|
||||
$this->assertSame('not_verified', json_decode(DB::table('partnership_rooms')->where('id', $room->id)->value('agreement'), true)['legal_signature_status']);
|
||||
}
|
||||
|
||||
public function test_preserves_legacy_proposal_and_rejects_self_moderation_and_concurrent_updates(): void
|
||||
{
|
||||
$owner = User::factory()->create();
|
||||
$manager = User::factory()->create();
|
||||
config(['portal.managers' => [$owner->email, $manager->email]]);
|
||||
$id = DB::table('portal_partnerships')->insertGetId(['user_id' => $owner->id, 'title' => 'Legacy', 'sector' => 'Original sector', 'province' => 'Original province', 'offer' => 'Original offer', 'seeking' => 'Original request', 'status' => 'pending']);
|
||||
$this->actingAs($owner)->postJson('/parcerias/'.$id.'/estado', ['version' => 1, 'action' => 'publish', 'reason' => 'Self moderation'])->assertForbidden();
|
||||
$this->actingAs($manager)->postJson('/parcerias/'.$id.'/estado', ['version' => 1, 'action' => 'publish', 'reason' => 'Legacy reviewed'])->assertOk();
|
||||
$this->assertDatabaseHas('portal_partnerships', ['id' => $id, 'user_id' => $owner->id, 'sector' => 'Original sector', 'province' => 'Original province', 'offer' => 'Original offer']);
|
||||
$this->postJson('/parcerias/'.$id.'/estado', ['version' => 1, 'action' => 'return', 'reason' => 'Stale'])->assertConflict();
|
||||
}
|
||||
|
||||
public function test_draft_recovery_and_idempotency_do_not_require_organization_or_documents(): void
|
||||
{
|
||||
$owner = User::factory()->create(['email_verified_at' => null]);
|
||||
$input = ['client_key' => (string) Str::uuid(), 'title' => '', 'kind' => 'seeking', 'payload' => [], 'origins' => [], 'locations' => []];
|
||||
$this->actingAs($owner)->postJson('/parcerias/rascunhos', $input)->assertOk();
|
||||
$this->postJson('/parcerias/rascunhos', $input)->assertOk();
|
||||
$this->postJson('/parcerias/rascunhos', [...$input, 'title' => 'Different bytes'])->assertConflict();
|
||||
$this->assertDatabaseCount('portal_partnerships', 1);
|
||||
$p = DB::table('portal_partnerships')->first();
|
||||
$this->get('/parcerias/'.$p->id.'/editar')->assertOk();
|
||||
$this->postJson('/parcerias/'.$p->id.'/estado', ['version' => $p->version, 'action' => 'submit', 'reason' => 'Still incomplete'])->assertUnprocessable();
|
||||
$this->actingAs(User::factory()->create())->get('/parcerias/'.$p->id.'/editar')->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_competing_candidates_and_moderator_cannot_enter_private_room_or_read_other_interests(): void
|
||||
{
|
||||
$owner = User::factory()->create();
|
||||
$candidate = User::factory()->create();
|
||||
[$p, $moderator] = $this->publish($owner);
|
||||
$i = $this->candidature($candidate, $p);
|
||||
$outsider = User::factory()->create();
|
||||
$this->candidature($outsider, $p);
|
||||
$this->actingAs($outsider)->getJson('/parcerias/'.$p->id)->assertOk()->assertJsonCount(1, 'interests');
|
||||
$i = app(PartnershipExchange::class)->decideInterest($owner, $i->id, $i->version, 'shortlist', 'QA shortlist');
|
||||
app(PartnershipExchange::class)->decideInterest($owner, $i->id, $i->version, 'connect', 'QA connect');
|
||||
$room = DB::table('partnership_rooms')->first();
|
||||
$this->getJson('/parcerias/salas/'.$room->id)->assertForbidden();
|
||||
$this->actingAs($moderator)->getJson('/parcerias/salas/'.$room->id)->assertForbidden();
|
||||
$this->getJson('/parcerias/'.$p->id)->assertJsonCount(0, 'interests');
|
||||
$this->actingAs($candidate)->postJson('/parcerias/candidaturas/'.$i->id, ['version' => $i->version + 1, 'action' => 'assess', 'reason' => 'Self assessment', 'assessment' => ['capacity' => ['state' => 'met', 'evidence' => 'Claim']]])->assertForbidden();
|
||||
$key = (string) Str::uuid();
|
||||
$this->postJson('/parcerias/salas/'.$room->id.'/mensagens', ['client_key' => $key, 'body' => 'Private QA message'])->assertOk();
|
||||
$this->postJson('/parcerias/salas/'.$room->id.'/mensagens', ['client_key' => $key, 'body' => 'Private QA message'])->assertOk();
|
||||
$this->postJson('/parcerias/salas/'.$room->id.'/mensagens', ['client_key' => $key, 'body' => 'Changed'])->assertConflict();
|
||||
$this->assertDatabaseCount('partnership_messages', 1);
|
||||
}
|
||||
|
||||
public function test_matching_keeps_unknown_information_separate_from_proven_failure_and_desirable_criteria(): void
|
||||
{
|
||||
$owner = User::factory()->create();
|
||||
[$p] = $this->publish($owner);
|
||||
$i = $this->candidature(User::factory()->create(), $p);
|
||||
$engine = app(PartnershipMatching::class);
|
||||
$unknown = $engine->explain($p, $i);
|
||||
$this->assertSame('unknown', $unknown['eligibility']);
|
||||
$this->assertSame(100.0, $unknown['score_max']);
|
||||
$failed = $engine->explain($p, $i, ['capacity' => ['state' => 'unmet', 'evidence' => 'QA documented inability']]);
|
||||
$this->assertSame('unmet', $failed['eligibility']);
|
||||
$desirable = $engine->explain($p, $i, ['impact' => ['state' => 'unmet', 'evidence' => 'QA reference']]);
|
||||
$this->assertSame('unknown', $desirable['eligibility']);
|
||||
$this->assertFalse($desirable['recommendation_is_approval']);
|
||||
$all = [];
|
||||
foreach (array_keys(config('partnerships.criteria')) as $code) {
|
||||
$all[$code] = ['state' => 'met', 'evidence' => 'Synthetic QA source for '.$code];
|
||||
}
|
||||
$this->assertSame(100.0, $engine->explain($p, $i, $all)['score_min']);
|
||||
}
|
||||
|
||||
public function test_document_sharing_reuses_private_repository_and_scan_controls(): void
|
||||
{
|
||||
Queue::fake();
|
||||
Storage::fake('local');
|
||||
$owner = User::factory()->create();
|
||||
$candidate = User::factory()->create();
|
||||
[$p] = $this->publish($owner);
|
||||
$i = $this->candidature($candidate, $p);
|
||||
$i = app(PartnershipExchange::class)->decideInterest($owner, $i->id, $i->version, 'shortlist', 'QA shortlist');
|
||||
app(PartnershipExchange::class)->decideInterest($owner, $i->id, $i->version, 'connect', 'QA room');
|
||||
$room = DB::table('partnership_rooms')->first();
|
||||
$this->actingAs($owner)->postJson('/parcerias/salas/'.$room->id.'/documentos', ['file' => UploadedFile::fake()->create('qa.pdf', 1, 'application/pdf'), 'shared' => false])->assertOk();
|
||||
$link = DB::table('partnership_documents')->first();
|
||||
$this->actingAs($candidate)->getJson('/parcerias/salas/'.$room->id)->assertJsonCount(0, 'documents');
|
||||
$this->get('/parcerias/salas/'.$room->id.'/documentos/'.$link->id)->assertNotFound();
|
||||
$this->postJson('/parcerias/salas/'.$room->id.'/documentos/'.$link->id.'/partilha', ['shared' => true])->assertForbidden();
|
||||
$this->actingAs($owner)->postJson('/parcerias/salas/'.$room->id.'/documentos/'.$link->id.'/partilha', ['shared' => true])->assertOk();
|
||||
$this->actingAs($candidate);
|
||||
$this->get('/parcerias/salas/'.$room->id.'/documentos/'.$link->id)->assertNotFound();
|
||||
$document = DB::table('documents')->where('id', $link->document_id)->first();
|
||||
DB::table('documents')->where('id', $document->id)->update(['status' => 'active', 'malware_scan_status' => 'clean', 'malware_scanned_sha256' => $document->sha256]);
|
||||
$this->get('/parcerias/salas/'.$room->id.'/documentos/'.$link->id)->assertOk();
|
||||
Storage::disk('local')->put($document->storage_key, 'Changed bytes');
|
||||
$this->get('/parcerias/salas/'.$room->id.'/documentos/'.$link->id)->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_organization_representation_blocks_viewer_writes_and_revoked_authors(): void
|
||||
{
|
||||
$owner = User::factory()->create();
|
||||
$representative = User::factory()->create();
|
||||
$viewer = User::factory()->create();
|
||||
$organization = (string) Str::uuid();
|
||||
DB::table('organizations')->insert(['id' => $organization, 'name' => 'Synthetic QA organization', 'organization_type' => 'private']);
|
||||
foreach ([[$owner, 'manager'], [$representative, 'officer'], [$viewer, 'viewer']] as [$user, $role]) {
|
||||
DB::table('memberships')->insert(['user_id' => $user->id, 'organization_id' => $organization, 'role' => $role]);
|
||||
}
|
||||
$p = $this->draft($owner, ['organization_id' => $organization]);
|
||||
$this->actingAs($viewer)->getJson('/parcerias/'.$p->id)->assertOk()->assertJsonPath('canEdit', false);
|
||||
$this->postJson('/parcerias/'.$p->id.'/estado', ['version' => $p->version, 'action' => 'submit', 'reason' => 'Viewer attempt'])->assertForbidden();
|
||||
$this->actingAs($representative)->get('/parcerias/'.$p->id.'/editar')->assertOk();
|
||||
DB::table('memberships')->where('user_id', $owner->id)->update(['deleted_at' => now()]);
|
||||
$this->actingAs($owner)->get('/parcerias/'.$p->id.'/editar')->assertForbidden();
|
||||
$this->getJson('/parcerias/'.$p->id)->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_hierarchical_filters_and_source_links_use_only_explicit_documented_relations(): void
|
||||
{
|
||||
$parent = null;
|
||||
$ids = [];
|
||||
foreach (['country', 'province', 'district', 'admin_post', 'locality'] as $type) {
|
||||
$id = (string) Str::uuid();
|
||||
DB::table('geo_locations')->insert(['id' => $id, 'code' => $id, 'name' => 'QA '.$type, 'location_type' => $type, 'parent_id' => $parent,
|
||||
'data_classification' => 'public', 'source_system' => 'cartografia-workbook', 'external_id' => '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:'.$id]);
|
||||
$ids[$type] = $id;
|
||||
$parent = $id;
|
||||
}
|
||||
$owner = User::factory()->create();
|
||||
$manager = User::factory()->create();
|
||||
config(['portal.managers' => [$manager->email]]);
|
||||
$project = Project::factory()->create();
|
||||
$dossier = InvestmentDossier::create(['project_id' => $project->id, 'title' => 'QA documented anchor', 'source' => 'Synthetic QA source', 'content' => ['location' => 'QA locality'], 'publication_status' => 'published', 'published_at' => now()->subMinute(), 'approval_basis' => 'document_owner', 'approval_evidence' => 'Synthetic QA fixture disclosure only']);
|
||||
DB::table('project_locations')->insert(['project_id' => $project->id, 'geo_location_id' => $ids['locality'], 'role' => 'primary', 'data_classification' => 'public']);
|
||||
$p = $this->draft($owner, ['origins' => [['type' => 'project', 'target_id' => $project->id]]]);
|
||||
$p = app(PartnershipExchange::class)->transition($owner, $p->id, $p->version, 'submit', 'QA submission');
|
||||
$p = app(PartnershipExchange::class)->transition($manager, $p->id, $p->version, 'publish', 'QA source checked');
|
||||
foreach (['province', 'district', 'admin_post', 'locality'] as $level) {
|
||||
$this->getJson('/api/v1/partnerships?territory_id='.$ids[$level])->assertOk()->assertJsonPath('data.0.id', $p->id)->assertJsonPath('total', 1);
|
||||
}
|
||||
$this->getJson('/api/v1/partnerships/source/project/'.$project->id)->assertOk()->assertJsonPath('items.0.id', $p->id);
|
||||
$this->getJson('/parcerias/'.$p->id)->assertOk()->assertJsonPath('origins.0.href', '/carteira/'.$dossier->id);
|
||||
$this->assertDatabaseCount('partnership_locations', 0);
|
||||
$this->assertDatabaseCount('opportunity_projects', 0);
|
||||
}
|
||||
|
||||
public function test_authenticated_installation_preserves_legacy_rows_and_is_repeatable_in_qa(): void
|
||||
{
|
||||
$migration = require database_path('migrations/2026_10_07_160000_extend_internal_partnership_exchange.php');
|
||||
$migration->down();
|
||||
$manager = User::factory()->create();
|
||||
config(['portal.managers' => [$manager->email]]);
|
||||
$id = DB::table('portal_partnerships')->insertGetId(['user_id' => $manager->id, 'title' => 'Legacy before migration', 'sector' => 'Original', 'province' => 'Original', 'offer' => 'Retained', 'seeking' => 'Retained', 'status' => 'pending']);
|
||||
$dir = storage_path('framework/testing/partnership-install-'.Str::uuid());
|
||||
mkdir($dir, 0700, true);
|
||||
config(['partnerships.proof_directory' => $dir]);
|
||||
$dump = $dir.'/synthetic-unit-fixture.dump';
|
||||
file_put_contents($dump, 'Isolated synthetic unit proof, not active backup');
|
||||
file_put_contents($dir.'/backup-proof.json', json_encode(['restore_verified' => true, 'target' => ['database' => DB::connection()->getDatabaseName(), 'port' => 55441], 'dump_path' => $dump, 'sha256' => hash_file('sha256', $dump)]));
|
||||
$installer = app(PartnershipInstallation::class);
|
||||
file_put_contents($dir.'/qa-evidence.json', json_encode(['passed' => true, 'code_hash' => $installer->fingerprint(), 'database' => DB::connection()->getDatabaseName(), 'port' => 55441]));
|
||||
$hash = $installer->preview()['hash'];
|
||||
$this->actingAs($manager)->post('/management/partnerships-install', ['hash' => str_repeat('0', 64)])->assertConflict();
|
||||
$this->post('/management/partnerships-install', ['hash' => $hash])->assertRedirect();
|
||||
$this->assertDatabaseHas('portal_partnerships', ['id' => $id, 'user_id' => $manager->id, 'title' => 'Legacy before migration', 'status' => 'pending', 'offer' => 'Retained']);
|
||||
$this->post('/management/partnerships-install', ['hash' => $hash])->assertRedirect();
|
||||
$this->assertSame(1, DB::table('audit_events')->where('action', 'partnership_module_installed')->count());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user