fix: complete InvesteMoz validation and error corrections

This commit is contained in:
2026-10-29 21:47:33 +02:00
parent 63147312e2
commit daf8623441
718 changed files with 127308 additions and 20959 deletions
+49
View File
@@ -0,0 +1,49 @@
<?php
namespace Tests\Unit;
use App\Support\CheckedFile;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RuntimeException;
class CheckedFileTest extends TestCase
{
public function test_empty_file_is_valid_content_with_a_real_hash_and_zero_size(): void
{
$path = tempnam(sys_get_temp_dir(), 'investmoz-file-');
try {
self::assertSame('', CheckedFile::read($path));
self::assertSame(hash('sha256', ''), CheckedFile::sha256($path));
self::assertSame(0, CheckedFile::size($path));
} finally {
unlink($path);
}
}
public static function operations(): array
{
return [['read'], ['sha256'], ['size']];
}
public function test_integrity_check_reads_again_after_content_changes(): void
{
$path = tempnam(sys_get_temp_dir(), 'investmoz-integrity-');
try {
file_put_contents($path, 'original');
$expected = CheckedFile::sha256($path);
self::assertTrue(CheckedFile::matches($path, $expected));
file_put_contents($path, 'changed');
self::assertFalse(CheckedFile::matches($path, $expected));
} finally {
unlink($path);
}
}
#[DataProvider('operations')]
public function test_missing_file_cannot_be_converted_to_empty_content_or_a_false_hash(string $operation): void
{
$this->expectException(RuntimeException::class);
CheckedFile::$operation(sys_get_temp_dir().'/investmoz-absent-'.bin2hex(random_bytes(12)));
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace Tests\Unit;
use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\Facades\DB;
use PHPUnit\Framework\Attributes\DataProvider;
use RuntimeException;
class IsolatedMigrationGuardTest extends TestCase
{
public static function rejectedTargets(): iterable
{
$migrations = ['2026_10_16_130000_add_investment_zone_editorial_draft', '2026_10_16_140000_add_investment_zone_operational_details', '2026_10_16_150000_create_investor_navigation_preferences', '2026_10_18_160000_add_portal_editorial_content'];
$cases = ['production' => ['environment' => 'production'], 'active-name' => ['database' => 'investemoz'], 'active-port' => ['port' => '5432'], 'remote-host' => ['host' => 'example.invalid'], 'database-url' => ['url' => 'postgresql://example.invalid/test'], 'other-driver' => ['driver' => 'sqlite']];
foreach ($migrations as $migration) {
foreach ($cases as $name => $patch) {
yield $migration.'/'.$name => [$migration, $patch];
}
}
}
#[DataProvider('rejectedTargets')]
public function test_new_qa_allowance_cannot_run_outside_the_isolated_target(string $migration, array $patch): void
{
$environment = $patch['environment'] ?? 'testing';
unset($patch['environment']);
app()->instance('env', $environment);
config(['database.default' => 'pgsql', 'database.connections.pgsql' => [...config('database.connections.pgsql'), 'driver' => 'pgsql', 'host' => '127.0.0.1', 'port' => '55441', 'database' => 'migration_guard_test', 'url' => null, ...$patch],
'investment-zone-profile.authenticated_installation' => false, 'investment-zone-details.authenticated_installation' => false, 'investor-context.authenticated_installation' => false, 'editorial.authenticated_installation' => false]);
if (($patch['driver'] ?? '') === 'sqlite') {
config(['database.default' => 'sqlite']);
}
DB::shouldReceive('selectOne')->never();
DB::shouldReceive('connection')->never();
$this->expectException(RuntimeException::class);
(require database_path('migrations/'.$migration.'.php'))->up();
}
}
+10 -9
View File
@@ -16,26 +16,27 @@ class OpportunityCorridorExplorerTest extends TestCase
{
$first = new Opportunity(['title' => 'Primeira', 'province' => 'Sofala', 'district' => 'Dondo']);
$first->id = 101;
$other = new Opportunity(['title' => 'Outra']); $other->id = 102;
$other = new Opportunity(['title' => 'Outra']);
$other->id = 102;
$query = Mockery::mock(Builder::class);
$query->shouldReceive('get')->once()->andReturn(new Collection([$first, $other]));
$catalogue = Mockery::mock(InvestmentCatalogue::class);
$filters = ['sector' => 'agronegocio', 'view' => 'corridors'];
$catalogue->shouldReceive('query')->with($filters)->once()->andReturn($query);
$catalogue->shouldReceive('context')->with(101)->once()->andReturn(['corridors' => collect([(object)['id'=>'a','name'=>'Corredor A'],(object)['id'=>'b','name'=>'Corredor B']])]);
$catalogue->shouldReceive('context')->with(102)->once()->andReturn(['corridors'=>collect()]);
$first->setAttribute('territorial_summary','Sofala / Dondo');
$other->setAttribute('territorial_summary','Localização não indicada');
$catalogue->shouldReceive('context')->with(101)->once()->andReturn(['corridors' => collect([(object) ['id' => 'a', 'name' => 'Corredor A'], (object) ['id' => 'b', 'name' => 'Corredor B']])]);
$catalogue->shouldReceive('context')->with(102)->once()->andReturn(['corridors' => collect()]);
$first->setAttribute('territorial_summary', 'Sofala / Dondo');
$other->setAttribute('territorial_summary', 'Localização não indicada');
$catalogue->shouldReceive('presentLocation')->with($first)->once()->andReturn($first);
$catalogue->shouldReceive('presentLocation')->with($other)->once()->andReturn($other);
$this->app->instance(InvestmentCatalogue::class, $catalogue);
$groups = app(OpportunityCorridorExplorer::class)->groups($filters);
$this->assertSame(['Corredor A','Corredor B','Outras localizações'], array_column($groups,'name'));
$this->assertSame([101,101,102], array_map(fn($g)=>$g['opportunities'][0]['id'], $groups));
$this->assertSame([1,1,1], array_column($groups,'count'));
$this->assertSame(['Corredor A', 'Corredor B', 'Outras localizações'], array_column($groups, 'name'));
$this->assertSame([101, 101, 102], array_map(fn ($g) => $g['opportunities'][0]['id'], $groups));
$this->assertSame([1, 1, 1], array_column($groups, 'count'));
$this->assertSame(['Sofala / Dondo'], $groups[0]['locations']);
$this->assertSame([], $groups[2]['locations']);
$this->assertSame([], $groups[0]['opportunities'][0]['stages']);
$this->assertSame('Sofala / Dondo',$groups[0]['opportunities'][0]['territorial_summary']);
$this->assertSame('Sofala / Dondo', $groups[0]['opportunities'][0]['territorial_summary']);
}
}
+2 -1
View File
@@ -14,7 +14,8 @@ class PortalContactTest extends TestCase
protected function setUp(): void
{
parent::setUp();
config(['contact_messages.recipient' => 'tempo@example.invalid']);
config(['contact_messages.recipient' => 'tempo@example.invalid',
'portal.profile_access_enabled' => true, 'portal.profile_administration_enabled' => true]);
DB::shouldReceive('connection')->never();
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace Tests\Unit;
use App\Support\CheckedPattern;
use App\Support\StructuredData;
use Illuminate\Support\Collection;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;
class StructuredDataTest extends TestCase
{
public function test_collection_normalisation_preserves_keys_and_object_identity(): void
{
$row = (object) ['id' => 7];
$items = StructuredData::collectionItems(new Collection(['row' => $row]));
self::assertSame(['row' => $row], $items);
self::assertSame($row, $items['row']);
$generator = (static function () use ($row) {
yield 'row' => $row;
})();
self::assertSame(['row' => $row], StructuredData::collectionItems($generator));
}
public function test_invalid_scalar_cannot_be_recovered_as_an_empty_collection(): void
{
$this->expectException(UnexpectedValueException::class);
StructuredData::collectionItems('invalid structured snapshot');
}
public function test_invalid_unicode_fails_without_overwriting_the_source(): void
{
$source = "original\xFF";
try {
CheckedPattern::split('/\s+/u', $source);
self::fail('Malformed text must not be accepted as an empty result.');
} catch (\RuntimeException) {
self::assertSame("original\xFF", $source);
}
}
}