Files
Investidor-V2/tests/Feature/EducationalGuidesTest.php
T

63 lines
3.5 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Sector;
use App\Services\LegacyContent;
use App\Services\SectorInvestment;
use App\Support\IsolatedQaDatabase;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Tests\TestCase;
class EducationalGuidesTest extends TestCase
{
use DatabaseTransactions;
public function createApplication(): Application
{
$app = parent::createApplication();
if (! IsolatedQaDatabase::allowsSchemaChanges()) {
throw new \LogicException('Isolated QA required');
}
return $app;
}
public function test_guide_uses_public_edition_and_hides_unpublished_or_unscanned_sources(): void
{
Storage::fake('local');
config(['portal.legacy_content_qa_enabled' => true]);
$identity = config('legacy_content.articles.turismo');
$body = '<p>Conteúdo sintético para conferir a apresentação do guia e preservar o texto da edição consultada.</p>';
$bytes = json_encode(['format' => 'investmoz-legacy-article-v1', 'source_url' => $identity['source_url'], 'content_html' => $body,
'title' => 'Guia sintético', 'author' => 'Autor QA', 'legacy_id' => 171990, 'legacy_category' => 'Turismo',
'published_at' => '2026-06-16', 'modified_at' => '2026-06-16'], JSON_THROW_ON_ERROR);
$id = (string) Str::uuid();
Storage::disk('local')->put('guides/'.$id, $bytes);
DB::table('documents')->insert(['id' => $id, 'external_id' => $identity['external_id'], 'source_system' => 'synthetic-test',
'file_name' => 'guide.json', 'media_type' => 'application/json', 'storage_key' => 'guides/'.$id, 'size_bytes' => strlen($bytes),
'sha256' => hash('sha256', $bytes), 'malware_scanned_sha256' => hash('sha256', $bytes), 'malware_scan_status' => 'clean',
'status' => 'active', 'rights_status' => 'authorized', 'data_classification' => 'public']);
$resource = DB::table('portal_resources')->insertGetId(['document_id' => $id, 'title' => 'Guia sintético', 'category' => 'education',
'topic' => 'turismo', 'published' => true, 'rights_status' => 'authorized', 'source_url' => $identity['source_url'], 'source_reference' => 'Synthetic isolated QA fixture', 'metadata_version' => 1]);
$sector = Sector::where('slug', 'turismo')->firstOrFail();
$guides = app(SectorInvestment::class)->guides($sector);
$this->assertSame(strip_tags($body), $guides[0]['summary']);
$this->assertSame('Autor QA', $guides[0]['author']);
$this->assertSame('/turismo', $guides[0]['href']);
$this->assertSame('/oportunidades?sector=turismo', $guides[0]['opportunities_href']);
$this->assertSame('/investimentos/a-partir-de/turismo', app(LegacyContent::class)->article('turismo')['start_href']);
DB::table('opportunities')->where('sector_id', $sector->id)->update(['status' => 'draft']);
$this->assertFalse(app(SectorInvestment::class)->guides($sector)[0]['has_opportunities']);
DB::table('portal_resources')->where('id', $resource)->update(['published' => false]);
$this->assertSame([], app(SectorInvestment::class)->guides($sector));
DB::table('portal_resources')->where('id', $resource)->update(['published' => true]);
DB::table('documents')->where('id', $id)->update(['malware_scan_status' => 'pending']);
$this->assertSame([], app(SectorInvestment::class)->guides($sector));
}
}