diff --git a/tests/Unit/SisterCatastoServiceTest.php b/tests/Unit/SisterCatastoServiceTest.php new file mode 100644 index 0000000..25c1611 --- /dev/null +++ b/tests/Unit/SisterCatastoServiceTest.php @@ -0,0 +1,180 @@ +truncate(); + + // Crea la configurazione globale + FeCassettoServiceConfig::query()->create([ + 'password_script' => 'fallback_script', + 'base_url' => 'https://thenetworksolution.it/FattureCorrispettivi/ScaricaFatture/CassettoFiscale.php', + 'username' => 'fallback_user', + 'password' => 'fallback_pass', + 'pin' => '12345', + ]); + + $service = new SisterCatastoService(); + $reflector = new \ReflectionClass(SisterCatastoService::class); + $method = $reflector->getMethod('resolveCredentials'); + $method->setAccessible(true); + + $creds = $method->invoke($service, 1); + + $this->assertEquals('fallback_script', $creds['passwordScript']); + $this->assertEquals('https://thenetworksolution.it/FattureCorrispettivi/ScaricaFatture/CassettoFiscale.php', $creds['base_url']); + $this->assertNotEmpty($creds['app_url']); + } + + public function test_it_resolves_credentials_fallback_to_admin_id_4() + { + FeCassettoServiceConfig::query()->truncate(); + Amministratore::query()->where('id', 4)->delete(); + + // Crea amministratore ID 4 + $admin = Amministratore::factory()->create([ + 'id' => 4, + 'fe_cassetto_password_script' => 'admin_4_script_key', + ]); + + $service = new SisterCatastoService(); + $reflector = new \ReflectionClass(SisterCatastoService::class); + $method = $reflector->getMethod('resolveCredentials'); + $method->setAccessible(true); + + $creds = $method->invoke($service, 1); + + $this->assertEquals('admin_4_script_key', $creds['passwordScript']); + } + + public function test_it_calculates_correct_isolated_storage_paths() + { + $admin = Amministratore::factory()->create([ + 'codice_amministratore' => 'HWFGITXK', + ]); + + $stabile = Stabile::factory()->create([ + 'codice_stabile' => '0021', + 'denominazione' => 'Milizie 3', + 'amministratore_id' => $admin->id, + ]); + + $service = new SisterCatastoService(); + $path = $service->getStoragePath($stabile->id); + + $this->assertEquals('AMMINISTRATORE_HWFGITXK/STABILE_0021_Milizie_3/catasto', $path); + } + + public function test_it_skips_api_call_when_cache_is_valid() + { + $admin = Amministratore::factory()->create([ + 'codice_amministratore' => 'HWFGITXK', + ]); + + $stabile = Stabile::factory()->create([ + 'codice_stabile' => '0021', + 'amministratore_id' => $admin->id, + ]); + + $service = new SisterCatastoService(); + $dirPath = $service->getStoragePath($stabile->id); + $localFilename = "visura_F_12_34_sub5.pdf"; + + Storage::disk('public')->put("{$dirPath}/{$localFilename}", "Fake PDF Content"); + + // Effettua la richiesta con cache=24. Dovrebbe ritornare cached = true senza chiamare HTTP + $res = $service->acquistaEPrelevaVisura( + $stabile->id, + 'F', + 'ROMA Territorio-RM', + 'H501#ROMA#0#0', + '12', + '34', + '5', + null, + null, + 'PDF', + 'completa', + 24 + ); + + $this->assertTrue($res['cached']); + $this->assertEquals("{$dirPath}/{$localFilename}", $res['file_path']); + } + + public function test_it_extracts_zip_archives_for_omoimmobili() + { + Http::fake([ + '*/mashVisuraI.php' => Http::response([ + 'url' => 'https://thenetworksolution.it/sister/acquistate/multiple_visure.zip' + ], 200), + '*/acquistate/multiple_visure.zip' => Http::response($this->generateFakeZipContent(), 200) + ]); + + $admin = Amministratore::factory()->create([ + 'codice_amministratore' => 'HWFGITXK', + ]); + + $stabile = Stabile::factory()->create([ + 'codice_stabile' => '0021', + 'amministratore_id' => $admin->id, + ]); + + $service = new SisterCatastoService(); + $dirPath = $service->getStoragePath($stabile->id); + $res = $service->acquistaEPrelevaVisura( + $stabile->id, + 'F', + 'ROMA Territorio-RM', + 'H501#ROMA#0#0', + '12', + '34', + '5', + null, + null, + 'PDF', + 'completa', + 0 // Disabilita cache per forzare chiamata http fake + ); + + $this->assertTrue($res['zip_extracted']); + $this->assertTrue(Storage::disk('public')->exists("{$dirPath}/visura1.pdf")); + $this->assertTrue(Storage::disk('public')->exists("{$dirPath}/visura2.pdf")); + } + + protected function generateFakeZipContent(): string + { + $tempZipPath = tempnam(sys_get_temp_dir(), 'test_zip_'); + $zip = new ZipArchive(); + if ($zip->open($tempZipPath, ZipArchive::CREATE) === true) { + $zip->addFromString('visura1.pdf', 'Content of visura 1'); + $zip->addFromString('visura2.pdf', 'Content of visura 2'); + $zip->close(); + } + $content = file_get_contents($tempZipPath); + unlink($tempZipPath); + return $content; + } +}