Commit 9c69ab2f authored by Eric Pommateau's avatar Eric Pommateau

Merge branch '631-des-pes-retour-peuvent-disparaitre' into 'master'

Resolve "Des pes retour peuvent disparaitre"

Closes #631

See merge request libriciel/pole-plate-formes/s2low/s2low!196
parents 70328b29 1c06e12f
Pipeline #10038 failed with stages
# 4.2.3
## Correction
- Ajouter un rollback du changement d'état de la transaction lorsqu'un fichier PES ne peut être déplacé #631
# 4.2.2 - 2020-06-11
## Evolution
......
......@@ -206,6 +206,7 @@ class HeliosAnalyseFichierRecu {
$root_name= "validation_error";
}
$this->heliosTransactionsSQL->begin();
switch($root_name){
case 'pes_acquit': $this->traitementAck($basename,$xml); break;
case 'pes_nonacquit': $this->traitementNack($basename,$xml); break;
......@@ -213,10 +214,22 @@ class HeliosAnalyseFichierRecu {
case 'validation_error' : $this->traitementErreur($basename,$xml); break;
default: throw new Exception("$basename : Type PES retour inconnu : $root_name (fichier ignor)");
}
if (! rename($file_path,$helios_response_root."/".$basename)){
throw new Exception(" Le fichier $file_path n'a pas pu tre dplac !");
$renameSuccess = false;
try{
$renameSuccess = rename($file_path, $helios_response_root . "/" . $basename);
} catch (Exception $e) {
$this->s2lowLogger->error(
"Erreur lors du rename de $file_path en $helios_response_root / $basename : ".$e->getMessage()
);
}
if (!$renameSuccess){
$this->s2lowLogger->error("Traitement de $file_path annul : dplacement impossible");
$this->heliosTransactionsSQL->rollback();
}
else{
$this->heliosTransactionsSQL->commit();
}
}
/**
......@@ -237,8 +250,9 @@ class HeliosAnalyseFichierRecu {
HeliosTransactionsSQL::ERREUR,
$message
);
$this->s2lowLogger->info($message);
$this->heliosTransactionsSQL->setAcquitFilename($helios_transaction_id, $basename);
$this->s2lowLogger->info($message);
}
/**
......@@ -254,6 +268,7 @@ class HeliosAnalyseFichierRecu {
if (count($xml->ACQUIT) == 0){
$message = "Transaction $helios_transaction_id acceptee";
$this->heliosTransactionsSQL->updateStatus($helios_transaction_id, HeliosTransactionsSQL::ACQUITTER, $message);
} else {
$message = "Transaction $helios_transaction_id : information disponible";
$this->heliosTransactionsSQL->updateStatus($helios_transaction_id, HeliosTransactionsSQL::INFORMATION_DISPONIBLE, $message);
......@@ -340,10 +355,10 @@ class HeliosAnalyseFichierRecu {
$this->s2lowLogger->info("Transaction trouv : helios_transaction_id=$helios_transaction_id");
$message = "Transaction $helios_transaction_id refuse";
$this->heliosTransactionsSQL->updateStatus($helios_transaction_id, HeliosTransactionsSQL::REFUSER, $message);
$this->heliosTransactionsSQL->updateStatus($helios_transaction_id, HeliosTransactionsSQL::REFUSER, $message,true);
$this->heliosTransactionsSQL->setAcquitFilename($helios_transaction_id, $basename);
$this->s2lowLogger->info($message);
$this->heliosTransactionsSQL->setAcquitFilename($helios_transaction_id, $basename);
}
/**
......
......@@ -51,6 +51,16 @@ class HeliosTransactionsSQL extends SQL {
return $id;
}
public function begin(){
$this->queryOne("BEGIN;");
}
public function commit(){
$this->queryOne("COMMIT;");
}
public function rollback(){
$this->query("ROLLBACK;");
}
public function getLastStatusInfo($id){
$sql = "SELECT * FROM helios_transactions_workflow WHERE transaction_id=? ORDER BY date DESC,id DESC LIMIT 1";
return $this->queryOne($sql,$id);
......
......@@ -48,5 +48,56 @@ class ActesUpdateClassificationSQLTest extends S2lowTestCase {
$this->actesUpdateClassificationSQL->updateClassification("123456789",file_get_contents(__DIR__."/../fixtures/test-archive-MISILCL/TACT--SPREF0011-000000000-20170721-4.xml"));
}
public function testRollback(){
$heliosTransactionSQL = $this->getObjectInstancier()->get(HeliosTransactionsSQL::class);
$transaction_id = $heliosTransactionSQL->create("toto",
"xxx",
8,
1,
42,
"123");
$heliosTransactionSQL->updateStatus($transaction_id, HeliosTransactionsSQL::TRANSMIS, "test");
$heliosTransactionSQL->begin();
$heliosTransactionSQL->updateStatus($transaction_id, HeliosTransactionsSQL::INFORMATION_DISPONIBLE, "test");
$this->assertEquals(
HeliosTransactionsSQL::INFORMATION_DISPONIBLE,
$heliosTransactionSQL->getLatestStatusId($transaction_id)
);
$heliosTransactionSQL->rollback();
$this->assertEquals(
HeliosTransactionsSQL::TRANSMIS,
$heliosTransactionSQL->getLatestStatusId($transaction_id)
);
}
public function testCommit(){
$heliosTransactionSQL = $this->getObjectInstancier()->get(HeliosTransactionsSQL::class);
$transaction_id = $heliosTransactionSQL->create("toto",
"xxx",
8,
1,
42,
"123");
$heliosTransactionSQL->updateStatus($transaction_id, HeliosTransactionsSQL::TRANSMIS, "test");
$heliosTransactionSQL->begin();
$heliosTransactionSQL->updateStatus($transaction_id, HeliosTransactionsSQL::INFORMATION_DISPONIBLE, "test");
$this->assertEquals(
HeliosTransactionsSQL::INFORMATION_DISPONIBLE,
$heliosTransactionSQL->getLatestStatusId($transaction_id)
);
$heliosTransactionSQL->commit();
$this->assertEquals(
HeliosTransactionsSQL::INFORMATION_DISPONIBLE,
$heliosTransactionSQL->getLatestStatusId($transaction_id)
);
$heliosTransactionSQL->rollback();
$this->assertEquals(
HeliosTransactionsSQL::INFORMATION_DISPONIBLE,
$heliosTransactionSQL->getLatestStatusId($transaction_id)
);
}
}
\ No newline at end of file
......@@ -11,13 +11,13 @@ class HeliosAnalyseFichierRecuTest extends S2lowTestCase {
private $heliosTransactionSQL;
public function setUp() : void {
parent::setUp();
org\bovigo\vfs\vfsStream::setup('test');
$testStreamUrl = org\bovigo\vfs\vfsStream::url('test');
$this->helios_ftp_response_tmp_local_path = $testStreamUrl."/helios_ftp_response_tmp_local_path/";
$this->helios_response_root = $testStreamUrl."/helios_response_root/";
$this->helios_responses_error_path = $testStreamUrl."/helios_responses_error_path/";
$this->helios_ocre = $testStreamUrl."/helios_ocre/";
parent::setUp();
$this->baseDirectory=sys_get_temp_dir()."/".uniqid("phpunit");
mkdir($this->baseDirectory);
$this->helios_ftp_response_tmp_local_path = $this->baseDirectory."/helios_ftp_response_tmp_local_path/";
$this->helios_response_root = $this->baseDirectory."/helios_response_root/";
$this->helios_responses_error_path = $this->baseDirectory."/helios_responses_error_path/";
$this->helios_ocre = $this->baseDirectory."/helios_ocre/";
mkdir($this->helios_ftp_response_tmp_local_path);
mkdir($this->helios_response_root);
mkdir($this->helios_responses_error_path);
......@@ -25,15 +25,31 @@ class HeliosAnalyseFichierRecuTest extends S2lowTestCase {
$this->heliosTransactionSQL = new HeliosTransactionsSQL($this->getSQLQuery());
}
public function testAnalyseVide(){
public function tearDown(): void
{
parent::tearDown(); // TODO: Change the autogenerated stub
foreach ([$this->helios_ftp_response_tmp_local_path,
$this->helios_response_root,
$this->helios_responses_error_path,
$this->helios_ocre] as $dirname){
array_map('unlink', glob("$dirname/*.*"));
rmdir($dirname);
}
}
public function testAnalyseVide(){
$this->analyse();
$this->assertEquals("Aucun fichier analyser",$this->getLogRecords()[1]['message']);
}
private function analyse(){
private function analyse($response_root = null){
if(is_null($response_root)){
$response_root=$this->helios_response_root;
}
$this->getHeliosAnalyseFichierReponse()->analyse(
$this->helios_ftp_response_tmp_local_path,
$this->helios_response_root,
$response_root,
$this->helios_responses_error_path,
$this->helios_ocre
);
......@@ -55,8 +71,8 @@ class HeliosAnalyseFichierRecuTest extends S2lowTestCase {
$this->analysePesRetour(__DIR__."/fixtures/pes_retour.xml");
//print_r($this->getLogRecords());
$this->assertEquals(
'Traitement de vfs://test/helios_ftp_response_tmp_local_path/pes_retour.xml',
$this->assertRegExp(
'#Traitement de /tmp/phpunit.*/helios_ftp_response_tmp_local_path/pes_retour.xml#i',
$this->getLogRecords()[2]['message']
);
......@@ -66,10 +82,24 @@ class HeliosAnalyseFichierRecuTest extends S2lowTestCase {
$this->assertEquals("12345678900035", $info['siret']);
}
private function analysePesRetour($file_path){
public function testAnalysePesRetourErreurRename(){
$authoritySireSQL = new AuthoritySiretSQL($this->getSQLQuery());
$authoritySireSQL->add(1,"12345678900035");
$this->analysePesRetour(__DIR__."/fixtures/pes_retour.xml","/Rep/qui/existe/pas");
//print_r($this->getLogRecords()[4]['formatted']);
$this->assertRegExp(
'#Traitement de /tmp/phpunit.*/helios_ftp_response_tmp_local_path/pes_retour.xml annul : dplacement impossible#i',
$this->getLogRecords()[5]['formatted']
);
$this->assertFalse(file_exists($this->helios_response_root."/pes_retour.xml"));
$this->assertTrue(file_exists($this->helios_ftp_response_tmp_local_path."/pes_retour.xml"));
}
private function analysePesRetour($file_path,$response_root=null){
$filename = basename($file_path);
copy($file_path, $this->helios_ftp_response_tmp_local_path."/$filename");
$this->analyse();
$this->analyse($response_root);
}
public function testAnalysePesRetourNonAbonne(){
......@@ -117,8 +147,8 @@ class HeliosAnalyseFichierRecuTest extends S2lowTestCase {
$this->analyse();
$this->assertTrue(file_exists($this->helios_ocre."/".$filename));
$logs_records = $this->getLogRecords();
$this->assertEquals(
"Traitement de vfs://test/helios_ftp_response_tmp_local_path/toto.ocre",
$this->assertRegExp(
"#Traitement de /tmp/phpunit.*/helios_ftp_response_tmp_local_path/toto.ocre#i",
$logs_records[2]['message']
);
}
......@@ -150,8 +180,34 @@ class HeliosAnalyseFichierRecuTest extends S2lowTestCase {
"Transaction $transaction_id : information disponible",
$logs_records[5]['message']
);
$heliosTransactionSQL = $this->getObjectInstancier()->get(HeliosTransactionsSQL::class);
$this->assertEquals(HeliosTransactionsSQL::INFORMATION_DISPONIBLE,
$heliosTransactionSQL->getLatestStatusId($transaction_id)
);
}
public function testPesAcquitErreurRename(){
$transaction_id = $this->createPESAller();
$filename = "pes_acquit.xml";
file_put_contents(
$this->helios_ftp_response_tmp_local_path."/$filename",
file_get_contents(__DIR__."/fixtures/pes_acquit.xml")
);
$this->analyse("/repertoire/Non/Existant");
$logs_records = $this->getLogRecords();
$this->assertRegExp(
"#Traitement.*/helios_ftp_response_tmp_local_path/pes_acquit.xml annul : dplacement impossible#i",
$logs_records[7]['message']
);
$this->assertFalse(file_exists($this->helios_response_root."/pes_acquit.xml"));
$this->assertTrue(file_exists($this->helios_ftp_response_tmp_local_path."/pes_acquit.xml"));
$heliosTransactionSQL = $this->getObjectInstancier()->get(HeliosTransactionsSQL::class);
$this->assertEquals(HeliosTransactionsSQL::TRANSMIS,
$heliosTransactionSQL->getLatestStatusId($transaction_id)
);
}
private function createPESAller(){
$heliosTransactionSQL = new HeliosTransactionsSQL($this->getSQLQuery());
$transaction_id = $heliosTransactionSQL->create("toto","xxx",8,1,42,"123");
......@@ -208,7 +264,10 @@ class HeliosAnalyseFichierRecuTest extends S2lowTestCase {
public function testPesAcquitDeuxPESUnTransmis(){
$transaction_id_1 = $this->createPESAller();
$transaction_id_2 = $this->createPESAller();
$this->heliosTransactionSQL->updateStatus($transaction_id_1,HeliosTransactionsSQL::ACQUITTER,"test");
$this->heliosTransactionSQL->updateStatus(
$transaction_id_1,
HeliosTransactionsSQL::ACQUITTER,
"test");
$this->recupPESAcquit("Transaction {$transaction_id_2} : information disponible");
}
......@@ -246,9 +305,36 @@ class HeliosAnalyseFichierRecuTest extends S2lowTestCase {
"Transaction $transaction_id : erreur retourne par Helios",
$logs_records[4]['message']
);
}
/**
* @throws Exception
*/
public function testMalformedAcquitErreurRename(){
$transaction_id = $this->createPESAller();
$filename = "pes_acquit_not_valid.xml";
file_put_contents(
$this->helios_ftp_response_tmp_local_path."/$filename",
file_get_contents(__DIR__."/fixtures/pes_acquit_not_valid.xml")
);
}
$this->getHeliosAnalyseFichierReponse()->analyseOneFile(
$this->helios_ftp_response_tmp_local_path."/$filename",
"/Un/Rep/Inexistant",
$this->helios_ocre,
HELIOS_XSD_PATH
);
$logs_records = $this->getLogRecords();
$this->assertRegExp(
"#Trai.*/helios_ftp_response_tmp_local_path//pes_acquit_not_valid.xml annul : dplacement impossible#i",
$logs_records[6]['message']
);
$heliosTransactionSQL = $this->getObjectInstancier()->get(HeliosTransactionsSQL::class);
$this->assertEquals(HeliosTransactionsSQL::TRANSMIS,
$heliosTransactionSQL->getLatestStatusId($transaction_id)
);
}
public function testMalformaedAcquitWithoutCodCol(){
$transaction_id = $this->createPESAller();
......@@ -271,4 +357,34 @@ class HeliosAnalyseFichierRecuTest extends S2lowTestCase {
}
public function testBadRenamePesAcquit(){
$heliosTransactionSQL = $this->getObjectInstancier()->get(HeliosTransactionsSQL::class);
$transaction_id = $heliosTransactionSQL->create("toto",
"xxx",
8,
1,
42,
"123");
$info = array(
'nom_fic' => "pescg291201703030412001",
'cod_col' => 400,
'cod_bud' => 01,
'id_post' => "086016",
);
$heliosTransactionSQL->setInfoFromPESAller($transaction_id, $info);
$heliosTransactionSQL->updateStatus($transaction_id, HeliosTransactionsSQL::TRANSMIS, "test");
$filename = "pes_acquit.xml";
file_put_contents(
$this->helios_ftp_response_tmp_local_path."/$filename",
file_get_contents(__DIR__."/fixtures/pes_acquit.xml")
);
$this->analyse("Nim/Por/Te/Quoi");
$this->assertEquals(HeliosTransactionsSQL::TRANSMIS,
$heliosTransactionSQL->getLatestStatusId($transaction_id)
);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment