Complete grouped collaborative editorial queues and native review coverage
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-14 11:15:09 +02:00
parent f40924f1be
commit a4541298c0
36 changed files with 2691 additions and 89 deletions
+100
View File
@@ -0,0 +1,100 @@
<?php
namespace Tests\Feature;
use App\Models\EconomicCluster;
use App\Models\User;
use App\Services\ClusterCompetitiveness;
use App\Services\PortalEditorialNativeNotices;
use App\Services\PortalEditorialWorkflow;
use App\Services\TerritorialRegistry;
use App\Support\IsolatedQaDatabase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Tests\Support\AuthenticatesQaActors;
use Tests\TestCase;
final class EditorialNativeNoticeTest extends TestCase
{
use AuthenticatesQaActors,DatabaseTransactions;
private User $author;
private User $reviewer;
protected function setUp(): void
{
parent::setUp();
$this->assertTrue(IsolatedQaDatabase::allowsSchemaChanges());
$this->withoutVite();
[$this->author,$this->reviewer] = User::factory()->count(2)->create()->all();
config(['portal.managers' => [$this->author->email, $this->reviewer->email], 'portal.profile_access_enabled' => false, 'editorial_workflow.enabled' => true, 'editorial_workflow.cluster_competitiveness' => true, 'editorial_workflow.territorial_versions' => true, 'territorial.enabled' => true, 'territorial.editors' => [$this->author->email], 'territorial.reviewers' => [$this->reviewer->email], 'territorial.publishers' => [$this->reviewer->email]]);
$this->actingAsWithMfa($this->reviewer);
$this->actingAsWithMfa($this->author);
}
public function test_cluster_events_use_the_common_inbox_and_recheck_current_scope_and_event_integrity(): void
{
$cluster = EconomicCluster::create(['code' => 'NOTICE-'.Str::random(12), 'name' => 'Synthetic notice cluster', 'sector' => 'Energy', 'status' => 'planned']);
$draft = ['version' => 1, 'clusterId' => $cluster->id, 'sector' => 'Energy', 'region' => 'Synthetic territory', 'forces' => []];
foreach (ClusterCompetitiveness::FORCES as $force) {
$draft['forces'][$force] = ['pressure' => 'medium', ...array_fill_keys(ClusterCompetitiveness::FIELDS, 'Synthetic evidence and limitations')];
}
$service = app(ClusterCompetitiveness::class);
$service->change($cluster, $this->author->id, 0, 'saved', ['draft' => $draft]);
$this->assertDatabaseMissing('portal_editorial_notifications', ['user_id' => $this->reviewer->id, 'action' => 'saved']);
$state = $service->change($cluster, $this->author->id, 1, 'submitted', ['reason' => 'Synthetic review requested']);
$notice = DB::table('portal_editorial_notifications')->where('user_id', $this->reviewer->id)->where('action', 'submitted')->first();
$this->assertNotNull($notice);
$this->assertTrue(app(PortalEditorialWorkflow::class)->visibleNotice($this->reviewer, $notice));
app(PortalEditorialNativeNotices::class)->emit($this->author, $notice->native_event_id, [$this->reviewer->id]);
$this->assertSame(1, DB::table('portal_editorial_notifications')->where('native_event_id', $notice->native_event_id)->where('user_id', $this->reviewer->id)->count());
$present = app(PortalEditorialNativeNotices::class)->present($notice);
$this->assertStringContainsString($cluster->id.'/edit?notice_version=2', $present['href']);
$this->assertSame('Competitividade — versão 2', $present['reference']);
$this->actingAsWithMfa($this->reviewer)->postJson('/management/editorial/notices/'.$notice->id.'/read')->assertOk();
config(['portal.managers' => [$this->author->email]]);
$this->assertFalse(app(PortalEditorialWorkflow::class)->visibleNotice($this->reviewer, $notice));
$this->postJson('/management/editorial/notices/'.$notice->id.'/read')->assertNotFound();
config(['portal.managers' => [$this->author->email, $this->reviewer->email]]);
DB::table('portal_editorial_notifications')->where('id', $notice->id)->update(['content_hash' => str_repeat('0', 64)]);
$tampered = DB::table('portal_editorial_notifications')->where('id', $notice->id)->first();
$this->assertFalse(app(PortalEditorialWorkflow::class)->visibleNotice($this->reviewer, $tampered));
}
public function test_territory_events_open_the_correct_record_without_publishing_or_granting_access(): void
{
$source = (string) Str::uuid();
DB::table('geo_sources')->insert(['id' => $source, 'name' => 'Synthetic source', 'reference' => 'Synthetic source reference', 'checksum' => str_repeat('f', 64), 'source_version' => 'qa', 'collected_at' => today(), 'editor_id' => $this->author->id]);
$registry = app(TerritorialRegistry::class);
$id = $registry->save($this->author, ['name' => 'Synthetic country', 'code' => 'NOTICE-'.Str::random(12), 'location_type' => 'country', 'source_id' => $source, 'valid_from' => '2020-01-01', 'reason' => 'Synthetic territorial preparation']);
$baseline = app(PortalEditorialNativeNotices::class)->pending($this->reviewer);
$registry->transitionEditorial($this->author, $id, ['action' => 'pending_review', 'version_no' => 1, 'reason' => 'Synthetic territorial review requested']);
$notice = DB::table('portal_editorial_notifications')->where('user_id', $this->reviewer->id)->where('action', 'pending_review')->first();
$this->assertNotNull($notice);
$this->assertTrue(app(PortalEditorialWorkflow::class)->visibleNotice($this->reviewer, $notice));
$this->assertStringContainsString('territory='.$id, app(PortalEditorialNativeNotices::class)->present($notice)['href']);
$this->actingAsWithMfa($this->reviewer)->get('/management/territorial/editorial?territory='.$id.'&notice_version=2')->assertOk();
$this->assertFalse($registry->published()->where('g.id', $id)->exists());
$registry->transitionEditorial($this->reviewer, $id, ['action' => 'in_review', 'version_no' => 2, 'reason' => 'Synthetic reviewer starts review']);
$this->assertSame($baseline['territories'], app(PortalEditorialNativeNotices::class)->pending($this->reviewer)['territories']);
$registry->transitionEditorial($this->reviewer, $id, ['action' => 'archived', 'version_no' => 3, 'reason' => 'Synthetic proposal archived']);
$this->assertSame($baseline['territories'] - 1, app(PortalEditorialNativeNotices::class)->pending($this->reviewer)['territories']);
$this->assertNotEmpty(app(PortalEditorialWorkflow::class)->notices($this->author));
}
public function test_email_designation_is_explicit_revocable_and_does_not_grant_content_management(): void
{
$outsider = User::factory()->create();
$workflow = app(PortalEditorialWorkflow::class);
$this->assertFalse($workflow->legalReviewer($outsider));
config(['editorial_workflow.legal_reviewer_emails' => [strtolower($outsider->email)]]);
$this->assertTrue($workflow->legalReviewer($outsider));
$this->assertFalse($outsider->can('manage-portal'));
$outsider->forceFill(['email_verified_at' => null])->save();
$this->assertFalse($workflow->legalReviewer($outsider));
config(['editorial_workflow.legal_reviewer_emails' => []]);
$this->assertFalse($workflow->legalReviewer($this->reviewer));
}
}
@@ -0,0 +1,207 @@
<?php
namespace Tests\Feature;
use App\Models\EconomicCluster;
use App\Models\Project;
use App\Models\User;
use App\Models\ValueChain;
use App\Models\ValueChainLink;
use App\Services\PortalEditorialSubjects;
use App\Services\PortalEditorialWorkflow;
use App\Support\IsolatedQaDatabase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tests\Support\AuthenticatesQaActors;
use Tests\TestCase;
final class EditorialStructuralSubjectTest extends TestCase
{
use AuthenticatesQaActors,DatabaseTransactions;
private User $author;
private User $reviewer;
protected function setUp(): void
{
parent::setUp();
$this->assertTrue(IsolatedQaDatabase::allowsSchemaChanges());
$this->withoutVite();
[$this->author,$this->reviewer] = User::factory()->count(2)->create()->all();
config(['portal.managers' => [$this->author->email, $this->reviewer->email], 'portal.profile_access_enabled' => false, 'editorial_workflow.enabled' => true, 'editorial_workflow.structural_subjects' => true]);
$this->actingAsWithMfa($this->author);
}
private function move(\stdClass $edition, string $action, ?User $actor = null): \stdClass
{
$actor ??= $action === 'submit' ? $this->author : $this->reviewer;
$this->actingAsWithMfa($actor);
return app(PortalEditorialWorkflow::class)->transition($actor, $edition->subject_key, $edition->id, ['action' => $action, 'version' => $edition->version, 'reason' => 'Synthetic subject review decision'], false, false, $edition->subject_kind);
}
private function approve(\stdClass $edition): \stdClass
{
foreach (['submit', 'start_review', 'approve'] as $action) {
$edition = $this->move($edition, $action);
}
return $edition;
}
public function test_a_structure_without_a_fiche_keeps_its_public_fields_until_explicit_publication(): void
{
$record = Project::create(['code' => 'SUB-'.Str::random(10), 'name' => 'Original public name', 'status' => 'active', 'data_classification' => 'public']);
$before = $record->fresh()->getAttributes();
$dossiersBefore = DB::table('investment_dossiers')->count();
$edition = app(PortalEditorialWorkflow::class)->saveSubject($this->author, 'projects', $record->id, 1, ['name' => 'Proposed private name', 'budget_amount' => '999999999999999999.99', 'budget_currency' => 'MZN']);
$this->assertEquals($before, $record->fresh()->getAttributes());
$this->assertSame(2, $edition->revision_no);
$edition = $this->approve($edition);
$this->assertEquals($before, $record->fresh()->getAttributes());
$edition = $this->move($edition, 'publish');
$this->assertSame('Proposed private name', $record->fresh()->name);
$this->assertSame('999999999999999999.99', $record->fresh()->getRawOriginal('budget_amount'));
$this->assertDatabaseCount('investment_dossiers', $dossiersBefore);
$this->assertSame('published', $edition->state);
}
public function test_relation_edits_do_not_invalidate_or_replace_the_current_public_association_before_publication(): void
{
$project = Project::create(['code' => 'REL-'.Str::random(10), 'name' => 'Synthetic project', 'status' => 'active']);
$cluster = EconomicCluster::create(['code' => 'REL-'.Str::random(10), 'name' => 'Synthetic cluster', 'sector' => 'Energy', 'status' => 'active']);
$id = (string) Str::uuid();
DB::table('cluster_projects')->insert(['id' => $id, 'cluster_id' => $cluster->id, 'project_id' => $project->id, 'role' => 'anchor', 'association_type' => 'potential', 'source_reference' => 'Synthetic identified source', 'evidence_reference' => 'Synthetic page 2', 'valid_from' => '2020-01-01', 'validation_status' => 'validated', 'editor_id' => $this->author->id, 'validator_id' => $this->reviewer->id, 'version_no' => 1, 'data_classification' => 'public']);
$before = DB::table('cluster_projects')->find($id);
$edition = app(PortalEditorialWorkflow::class)->saveSubject($this->author, 'cluster-project', $id, 1, ['role' => 'supporting']);
$edition = $this->move($edition, 'submit');
$edition = $this->move($edition, 'start_review');
try {
$this->move($edition, 'approve', $this->author);
$this->fail('Contributor approval must be rejected');
} catch (HttpException $e) {
$this->assertSame(403, $e->getStatusCode());
}
$edition = $this->move($edition, 'approve');
$this->assertEquals($before, DB::table('cluster_projects')->find($id));
$this->move($edition, 'publish');
$this->assertSame('supporting', DB::table('cluster_projects')->find($id)->role);
$this->assertSame('validated', DB::table('cluster_projects')->find($id)->validation_status);
}
public function test_new_value_chain_is_private_until_published_and_archiving_keeps_its_history(): void
{
$cluster = EconomicCluster::create(['code' => 'CHAIN-'.Str::random(10), 'name' => 'Synthetic cluster', 'sector' => 'Energy', 'status' => 'active']);
$chain = ValueChain::create(['cluster_id' => $cluster->id, 'code' => 'QA-'.Str::random(8), 'name' => 'Private new chain', 'stage' => 'production', 'deleted_at' => now()]);
$edition = app(PortalEditorialWorkflow::class)->saveSubject($this->author, 'value-chains', $chain->id, 0, [], true);
$this->assertNull(ValueChain::find($chain->id));
$edition = $this->approve($edition);
$this->assertNull(ValueChain::find($chain->id));
$edition = $this->move($edition, 'publish');
$this->assertNotNull(ValueChain::find($chain->id));
$edition = $this->move($edition, 'archive');
$this->assertNull(ValueChain::find($chain->id));
$this->assertNotNull(app(PortalEditorialWorkflow::class)->latestSubject('value-chains', $chain->id));
}
public function test_existing_editor_routes_and_private_subject_endpoints_preserve_context_and_permissions(): void
{
$record = Project::create(['code' => 'HTTP-'.Str::random(10), 'name' => 'Original HTTP name', 'status' => 'active']);
$this->put('/management/projects/'.$record->id, ['editorial_version' => 1, 'code' => $record->code, 'name' => 'Private HTTP name', 'status' => 'active'])->assertRedirect()->assertSessionHasNoErrors();
$this->assertSame('Original HTTP name', $record->fresh()->name);
$base = '/management/editorial/subjects/projects/'.$record->id;
$this->getJson($base)->assertOk()->assertHeader('Cache-Control', 'no-store, private')->assertJsonPath('edition.payload.resource._subject.fields.name', 'Private HTTP name');
$this->getJson('/management/editorial/subjects')->assertOk();
$this->getJson($base.'/choices?field=primary_territory_id')->assertOk();
$this->getJson($base.'/choices?field=password')->assertNotFound();
$this->postJson($base.'/drafts', ['expected_revision' => 2, 'fields' => ['data_classification' => 'public']])->assertUnprocessable();
$this->postJson($base.'/drafts', ['expected_revision' => 2, 'fields' => ['name' => 'Deliberate revised name']])->assertCreated();
$this->getJson($base)->assertJsonPath('edition.payload.resource._subject.fields.name', 'Deliberate revised name');
$outsider = User::factory()->create();
$this->actingAsWithMfa($outsider)->getJson($base)->assertForbidden();
$this->postJson($base.'/drafts', ['expected_revision' => 3, 'fields' => ['name' => 'Forbidden']])->assertForbidden();
$this->assertSame('Original HTTP name', $record->fresh()->name);
}
public function test_withdrawal_of_an_existing_chain_link_is_a_separate_reviewed_publication(): void
{
$cluster = EconomicCluster::create(['code' => 'WITHDRAW-'.Str::random(10), 'name' => 'Synthetic cluster', 'sector' => 'Energy', 'status' => 'active']);
$a = ValueChain::create(['cluster_id' => $cluster->id, 'code' => 'A-'.Str::random(5), 'name' => 'Origin', 'stage' => 'production', 'data_classification' => 'public']);
$b = ValueChain::create(['cluster_id' => $cluster->id, 'code' => 'B-'.Str::random(5), 'name' => 'Destination', 'stage' => 'processing', 'data_classification' => 'public']);
$link = ValueChainLink::create(['from_value_chain_id' => $a->id, 'to_value_chain_id' => $b->id, 'link_type' => 'supplies', 'data_classification' => 'public']);
$this->delete('/management/clusters/'.$cluster->id.'/chain-links/'.$link->id)->assertRedirect('/management/editorial/subjects/value-chain-links/'.$link->id);
$this->assertNotNull(ValueChainLink::find($link->id));
$edition = app(PortalEditorialWorkflow::class)->latestSubject('value-chain-links', $link->id);
$this->assertTrue(app(PortalEditorialWorkflow::class)->payload($edition)['resource']['_subject']['withdraw']);
$edition = $this->approve($edition);
$this->assertNotNull(ValueChainLink::find($link->id));
$this->move($edition, 'publish');
$this->assertNull(ValueChainLink::find($link->id));
$this->getJson('/management/editorial/subjects/value-chain-links/'.$link->id)->assertOk();
}
public function test_new_chain_form_creates_a_private_proposal_in_the_existing_model(): void
{
$cluster = EconomicCluster::create(['code' => 'FORM-'.Str::random(10), 'name' => 'Synthetic cluster', 'sector' => 'Energy', 'status' => 'active']);
$code = 'NEW-'.Str::random(10);
$this->post('/management/clusters/'.$cluster->id.'/chains', ['code' => $code, 'name' => 'Private new stage', 'stage' => 'production'])->assertRedirect()->assertSessionHasNoErrors();
$chain = ValueChain::withTrashed()->where('code', $code)->firstOrFail();
$this->assertNull(ValueChain::find($chain->id));
$edition = app(PortalEditorialWorkflow::class)->latestSubject('value-chains', $chain->id);
$this->assertSame('draft', $edition->state);
$this->getJson('/management/editorial/subjects/value-chains/'.$chain->id)->assertOk()->assertJsonPath('edition.payload.resource._subject.new_record', true);
}
public function test_duplicate_relation_publication_is_refused_and_keeps_both_original_links(): void
{
$cluster = EconomicCluster::create(['code' => 'DUP-'.Str::random(10), 'name' => 'Synthetic cluster', 'sector' => 'Energy', 'status' => 'active']);
$chains = collect(['A', 'B', 'C'])->map(fn ($code) => ValueChain::create(['cluster_id' => $cluster->id, 'code' => $code, 'name' => $code, 'stage' => 'production']));
$existing = ValueChainLink::create(['from_value_chain_id' => $chains[0]->id, 'to_value_chain_id' => $chains[1]->id, 'link_type' => 'supplies']);
$edited = ValueChainLink::create(['from_value_chain_id' => $chains[0]->id, 'to_value_chain_id' => $chains[2]->id, 'link_type' => 'supplies']);
$edition = app(PortalEditorialWorkflow::class)->saveSubject($this->author, 'value-chain-links', $edited->id, 1, ['to_value_chain_id' => $chains[1]->id]);
$edition = $this->approve($edition);
try {
$this->move($edition, 'publish');
$this->fail('Duplicate must be refused before writing');
} catch (HttpException $e) {
$this->assertSame(409, $e->getStatusCode());
}
$this->assertSame($chains[2]->id, $edited->fresh()->to_value_chain_id);
$this->assertNotNull($existing->fresh());
}
public function test_partial_edits_validate_against_the_preserved_dates_and_currency(): void
{
$record = Project::create(['code' => 'DATES-'.Str::random(10), 'name' => 'Original', 'status' => 'active', 'start_date' => '2026-05-01']);
$base = '/management/editorial/subjects/projects/'.$record->id.'/drafts';
$this->postJson($base, ['expected_revision' => 1, 'fields' => ['end_date' => '2026-04-01']])->assertUnprocessable();
$this->postJson($base, ['expected_revision' => 1, 'fields' => ['budget_amount' => '2000.25']])->assertUnprocessable();
$this->assertSame(0, DB::table('portal_editorial_editions')->where('subject_key', $record->id)->count());
$this->assertSame('Original', $record->fresh()->name);
}
public function test_context_changes_and_disabled_workflow_do_not_allow_legacy_bypass(): void
{
$record = Project::create(['code' => 'STALE-'.Str::random(10), 'name' => 'Original', 'status' => 'active']);
$edition = app(PortalEditorialWorkflow::class)->saveSubject($this->author, 'projects', $record->id, 1, ['name' => 'Private']);
$edition = $this->approve($edition);
$record->forceFill(['description' => 'Changed contextual evidence'])->save();
try {
$this->move($edition, 'publish');
$this->fail('Context changed');
} catch (HttpException $e) {
$this->assertSame(409, $e->getStatusCode());
}
$this->assertSame('Original', $record->fresh()->name);
config(['editorial_workflow.structural_subjects' => false]);
try {
app(PortalEditorialSubjects::class)->writes('projects', $record->id);
$this->fail('Legacy bypass');
} catch (HttpException $e) {
$this->assertSame(503, $e->getStatusCode());
}
}
}
@@ -0,0 +1,163 @@
<?php
namespace Tests\Feature;
use App\Models\InvestmentZone;
use App\Models\User;
use App\Services\InvestmentMapIndicators;
use App\Services\InvestmentZoneOperationalDetails;
use App\Services\InvestmentZoneOperationalProfile;
use App\Services\InvestmentZones;
use App\Services\PortalEditorialNativeNotices;
use App\Services\PortalEditorialWorkflow;
use App\Support\IsolatedQaDatabase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tests\Support\AuthenticatesQaActors;
use Tests\TestCase;
final class ZoneStatisticsEditorialTest extends TestCase
{
use AuthenticatesQaActors,DatabaseTransactions;
private User $author;
private User $reviewer;
private InvestmentZone $zone;
protected function setUp(): void
{
parent::setUp();
$this->assertTrue(IsolatedQaDatabase::allowsSchemaChanges());
$this->assertTrue(Schema::hasColumn('investment_zones', 'editorial_draft'));
$this->assertTrue(Schema::hasColumn('investment_zones', 'operational_details'));
$this->withoutVite();
[$this->author,$this->reviewer] = User::factory()->count(2)->create()->all();
config(['portal.managers' => [$this->author->email, $this->reviewer->email], 'portal.profile_access_enabled' => false, 'editorial_workflow.enabled' => true, 'editorial_workflow.zone_review' => true]);
$this->actingAsWithMfa($this->reviewer);
$this->actingAsWithMfa($this->author);
$country = (string) Str::uuid();
DB::table('geo_locations')->insert(['id' => $country, 'code' => 'ZONE-'.Str::random(12), 'name' => 'Synthetic reviewed country', 'location_type' => 'country', 'data_classification' => 'public', 'source_system' => 'cartografia-workbook', 'external_id' => '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:'.$country]);
$province = (string) Str::uuid();
DB::table('geo_locations')->insert(['id' => $province, 'parent_id' => $country, 'code' => 'ZONE-'.Str::random(12), 'name' => 'Synthetic reviewed province', 'location_type' => 'province', 'data_classification' => 'public', 'source_system' => 'cartografia-workbook', 'external_id' => '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:'.$province]);
// Previously published synthetic baseline; this is not an approval of a real source.
$this->zone = InvestmentZone::create(['code' => 'ZONE-'.Str::random(10), 'name' => 'Original public zone', 'description' => 'Synthetic informational description', 'category' => 'industrial_park', 'physical_category' => 'industrial_park', 'legal_regime' => 'pending', 'primary_territory_id' => $province, 'territory_version' => 1, 'version_no' => 1, 'publication_status' => 'published', 'validation_status' => 'validated', 'editor_id' => $this->author->id, 'reviewer_id' => $this->author->id, 'published_at' => now(),
'source_evidence' => [['title' => 'Synthetic institutional report', 'url' => 'https://example.test/institutional', 'source_type' => 'institutional', 'supports' => 'Synthetic evidence of the identity and location only']]]);
}
private function profile(): InvestmentZoneOperationalProfile
{
return app(InvestmentZoneOperationalProfile::class);
}
private function prepare(): void
{
$this->actingAsWithMfa($this->author);
$fields = ['version_no' => $this->profile()->workingVersion($this->zone->fresh()), 'name' => 'Private revised zone', 'operational_details' => ['schema_version' => 1, 'integral' => ['schema_version' => 1, 'observations' => [['id' => (string) Str::uuid(), 'label' => 'Synthetic investment observation', 'knowledge' => 'documented', 'disclosure' => true, 'source_reference' => 'Synthetic report page 2', 'source_date' => '2026-01-15', 'scope' => 'This synthetic zone only', 'indicator' => 'investment', 'measure_kind' => 'observed', 'value' => '9007199254740.25', 'unit' => 'MZN', 'period_start' => '2025-01-01', 'period_end' => '2025-12-31', 'population' => 'Declared establishments', 'methodology' => 'Synthetic documentary record']]]]];
app(InvestmentZones::class)->save($this->zone->id, $fields, $this->author->id);
}
private function decision(string $action, ?User $actor = null): array
{
$actor ??= $action === 'submit' ? $this->author : $this->reviewer;
$this->actingAsWithMfa($actor);
return $this->profile()->review($this->zone->id, $actor, $this->profile()->workingVersion($this->zone->fresh()), $action, 'Synthetic review decision with source');
}
private function approve(): void
{
foreach (['submit', 'start_review', 'approve'] as $action) {
$this->decision($action);
}
}
public function test_indicator_preparation_and_approval_preserve_the_public_projection_until_explicit_publication(): void
{
$before = Arr::except($this->zone->fresh()->getAttributes(), ['editorial_draft']);
$this->prepare();
$this->assertSame($before, Arr::except($this->zone->fresh()->getAttributes(), ['editorial_draft']));
$this->assertSame([], app(InvestmentZoneOperationalDetails::class)->publicData($this->zone->fresh()));
$this->approve();
$this->assertSame($before, Arr::except($this->zone->fresh()->getAttributes(), ['editorial_draft']));
$this->actingAsWithMfa($this->reviewer);
$saved = app(InvestmentZones::class)->publish($this->zone->id, $this->profile()->workingVersion($this->zone->fresh()), $this->reviewer->id);
$this->assertSame('Private revised zone', $saved->name);
$rows = app(InvestmentMapIndicators::class)->forZones(collect([$saved]))['records'];
$this->assertCount(1, $rows);
$this->assertSame('9007199254740.25', $rows[0]['value']);
$this->assertSame('observed', $rows[0]['measure_kind']);
$this->assertSame('This synthetic zone only', $rows[0]['scope']);
$this->assertSame('Synthetic report page 2', $rows[0]['source_reference']);
$this->assertNull($this->profile()->draft($saved));
$this->assertNotEmpty(app(PortalEditorialWorkflow::class)->notices($this->author));
}
public function test_editing_after_approval_requires_a_new_decision_and_direct_publish_cannot_bypass_it(): void
{
$this->prepare();
$this->approve();
$this->actingAsWithMfa($this->author);
app(InvestmentZones::class)->save($this->zone->id, ['version_no' => $this->profile()->workingVersion($this->zone->fresh()), 'description' => 'Deliberate later change'], $this->author->id);
$this->assertSame('draft', $this->profile()->management($this->zone->fresh())['editorial_workflow']['state']);
try {
app(InvestmentZones::class)->publish($this->zone->id, $this->profile()->workingVersion($this->zone->fresh()), $this->author->id);
$this->fail('Approval must be renewed');
} catch (HttpException $e) {
$this->assertSame(409, $e->getStatusCode());
}
$this->assertSame('Original public zone', $this->zone->fresh()->name);
}
public function test_public_suspension_preserves_the_private_review_and_approved_exact_content(): void
{
$this->prepare();
$this->approve();
$this->actingAsWithMfa($this->reviewer);
$saved = app(InvestmentZones::class)->suspend($this->zone->id, $this->profile()->workingVersion($this->zone->fresh()), $this->reviewer->id);
$working = $this->profile()->management($saved);
$this->assertSame('approved', $working['editorial_workflow']['state']);
$this->assertTrue($working['editorial_workflow']['fresh']);
$this->assertSame('suspended', $saved->publication_status);
app(InvestmentZones::class)->publish($saved->id, $working['version_no'], $this->reviewer->id);
$this->assertSame('Private revised zone', $saved->fresh()->name);
}
public function test_history_recovers_the_exact_private_observation_without_overwriting_current_content(): void
{
$this->prepare();
$version = $this->profile()->workingVersion($this->zone->fresh());
$url = '/api/v2/investment/admin/zones/'.$this->zone->id.'/revisions/'.$version;
$before = $this->zone->fresh()->getAttributes();
$this->getJson($url)->assertOk()->assertJsonPath('fields.name', 'Private revised zone')->assertJsonPath('observations.0.value', '9007199254740.25')->assertJsonMissingPath('fields.editorial_draft')->assertJsonMissingPath('private_ciphertext');
$this->assertSame($before, $this->zone->fresh()->getAttributes());
$this->getJson(str_replace($this->zone->id, (string) Str::uuid(), $url))->assertNotFound();
$this->actingAsWithMfa(User::factory()->create())->getJson($url)->assertForbidden();
}
public function test_http_decisions_require_scope_fresh_mfa_and_preserve_source_and_history(): void
{
$this->prepare();
$version = $this->profile()->workingVersion($this->zone->fresh());
$url = '/api/v2/investment/admin/zones/'.$this->zone->id.'/editorial/submit';
$this->withSession(['portal.mfa_at' => time() - 1000])->postJson($url, ['version_no' => $version, 'reason' => 'Synthetic decision reason'])->assertForbidden();
$this->actingAsWithMfa($this->author)->postJson($url, ['version_no' => $version, 'reason' => 'Synthetic decision reason'])->assertOk()->assertJsonPath('editorial_workflow.state', 'submitted');
$this->assertGreaterThan(1, app(PortalEditorialNativeNotices::class)->pending($this->reviewer)['zones'] + 1);
$outsider = User::factory()->create();
$this->actingAsWithMfa($outsider)->postJson($url, ['version_no' => $version, 'reason' => 'Synthetic decision reason'])->assertForbidden();
config(['editorial_workflow.zone_review' => false]);
$this->actingAsWithMfa($this->author);
try {
app(InvestmentZones::class)->save($this->zone->id, ['version_no' => $version, 'name' => 'Forbidden legacy overwrite'], $this->author->id);
$this->fail('Disabling must preserve workflow');
} catch (HttpException $e) {
$this->assertSame(503, $e->getStatusCode());
}
$this->assertSame('Original public zone', $this->zone->fresh()->name);
}
}
@@ -0,0 +1,55 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { createRequire } from 'node:module';
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import ts from 'typescript';
const require = createRequire(import.meta.url);
let states = [], formData;
const mockReact = { ...React, useState: initial => [states.length ? states.shift() : initial, () => {}], useEffect() {} };
function load(file, appended = '') {
const code = ts.transpileModule(fs.readFileSync(new URL('../../resources/js/pages/' + file, import.meta.url), 'utf8') + appended, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.ReactJSX } }).outputText;
const module = { exports: {} };
new Function('require', 'module', 'exports', code)(name => {
if (name === 'react') return mockReact;
if (name === 'react/jsx-runtime') return require(name);
if (name === '@inertiajs/react') return { useForm: data => { formData = data; return {data, errors: {}, processing: false, setData() {}, post() {}}; } };
if (name === './shared') return { Field: ({label, children}) => React.createElement('label', null, label, children), button: '', control: '', input: '', card: '' };
return {};
}, module, module.exports);
return module.exports;
}
const { ZoneReview, ZoneHistory } = load('investment/zone.tsx', '\nexport { ZoneReview, ZoneHistory };');
const SubjectForm = load('management/editorial-subject-form.tsx').default;
const zone = { id: 'synthetic-zone', version_no: 8, editorial_workflow: { state: 'in_review', content_version: 3, fresh: true, can_approve: true } };
const renderReview = (patch = {}, dirty = false) => { states = ['Documentary review reason']; return renderToStaticMarkup(React.createElement(ZoneReview, {zone: {...zone, editorial_workflow: {...zone.editorial_workflow, ...patch}}, busy: false, dirty, action() {}})); };
test('zone review separates approval from publication and hides unauthorised approval', () => {
const html = renderReview();
assert.match(html, /Aprovar esta edição/);
assert.doesNotMatch(html, />Publicar/);
assert.doesNotMatch(renderReview({can_approve: false}), /Aprovar esta edição/);
});
test('changed sources and unsaved edits disable decisions without hiding the history', () => {
for (const html of [renderReview({fresh: false}), renderReview({}, true)]) assert.match(html, /disabled=""[^>]*>Aprovar esta edição/);
assert.match(renderReview({fresh: false}), /fonte ou anexos mudaram/);
});
test('history displays exact observation and source without applying it', () => {
states = [{version_no: 4, fields: {name: 'Historical zone', description: 'Preserved description'}, observations: [{label: 'Investment', value: '9007199254740.25', unit: 'MZN', scope: 'Synthetic zone', source_reference: 'Report page 2'}]}, '', false];
const html = renderToStaticMarkup(React.createElement(ZoneHistory, {id: 'synthetic-zone', revisions: [{version_no: 4, action: 'profile_prepared', created_at: '2026-11-14'}]}));
assert.match(html, /9007199254740.25/);
assert.match(html, /Report page 2/);
assert.match(html, /preserva a versão actual/);
assert.doesNotMatch(html, /Restaurar|Publicar/);
});
test('structural form preserves revision and selected values and opens progressively', () => {
const props = {base: '/management/editorial/subjects/projects/synthetic', revision: 7, fields: [{name: 'name', label: 'Designação', type: 'text', options: []}], initial: {name: 'Deliberate edit'}};
states = [false];
assert.doesNotMatch(renderToStaticMarkup(React.createElement(SubjectForm, props)), /textarea/);
states = [true];
const html = renderToStaticMarkup(React.createElement(SubjectForm, props));
assert.equal(formData.expected_revision, 7);
assert.equal(formData.fields.name, 'Deliberate edit');
assert.match(html, /Deliberate edit/);
assert.match(html, /publicação expressa da decisão/);
});
+2 -1
View File
@@ -24,6 +24,7 @@ const code = ts.transpileModule(source, {
const module = { exports: {} };
new Function('require', 'module', 'exports', code)(
(name) => {
if (name === './editorial-subject-form') return { default: () => null };
if (name === '../portfolio/shared')
return { labels: { description: 'Visão e proposta de valor' } };
if (name === '@inertiajs/react')
@@ -130,7 +131,7 @@ test('approved edition offers explicit publication; stale files and historical v
const stale = render({
edition: { ...props.edition, state: 'approved', fresh: false },
});
assert.match(stale, /Os anexos mudaram/);
assert.match(stale, /O conteúdo, contexto ou anexos mudaram/);
assert.doesNotMatch(stale, /value="publish"/);
const staleReview = render({
edition: { ...props.edition, state: 'in_review', fresh: false },