From 206c680010d3d3768c24133616256c27250296d0 Mon Sep 17 00:00:00 2001 From: JamesRezo Date: Tue, 1 Feb 2022 19:22:15 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20R=C3=A9soud=20#4417=20:=20on=20ne=20peut?= =?UTF-8?q?=20pas=20vider=20un=20fichier=20d'archive.=20Le=20plugin=20r?= =?UTF-8?q?=C3=A2le=20et=20=C3=A9met=20une=20nouvelle=20erreur=20en=20cas?= =?UTF-8?q?=20de=20tentative.=20Il=20est=20pratiquement=20impossible=20de?= =?UTF-8?q?=20trouver=20un=20tar=20vide=20dans=20la=20nature.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lang/archiviste_fr.php | 1 + paquet.xml | 4 +- phpunit.xml.dist | 2 +- src/AbstractArchiver.php | 1 + src/SpipArchiver.php | 7 ++++ src/TarArchive.php | 4 -- tests/AbstractArchiverTest.php | 5 --- tests/SpipArchiverTest.php | 66 +++++++++++++++------------------ tests/bootstrap.php | 8 ++++ var/tmp/tar/empty.tar | Bin 1024 -> 0 bytes 10 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 tests/bootstrap.php delete mode 100644 var/tmp/tar/empty.tar diff --git a/lang/archiviste_fr.php b/lang/archiviste_fr.php index d1fd578..b5f0d6d 100644 --- a/lang/archiviste_fr.php +++ b/lang/archiviste_fr.php @@ -8,4 +8,5 @@ $GLOBALS[$GLOBALS['idx_lang']] = [ 'fichier_lecture_seule' => 'Fichier en lecture seule', 'destination_inaccessible' => 'Desination inaccessible', 'fichier_deja_existant' => 'Le fichier existe déjà', + 'tentative_de_vidage_du_fichier' => 'Impossible de vider compl&eagrace;tement vider un fichier d\'archive', ]; diff --git a/paquet.xml b/paquet.xml index 5acc609..4efa6dc 100644 --- a/paquet.xml +++ b/paquet.xml @@ -1,8 +1,8 @@ 'destination_inaccessible', 6 => 'fichier_deja_existant', 7 => 'fichier_inaccessible_en_lecture', + 8 => 'tentative_de_vidage_du_fichier', ]; /** diff --git a/src/SpipArchiver.php b/src/SpipArchiver.php index b54566c..4b7b45d 100644 --- a/src/SpipArchiver.php +++ b/src/SpipArchiver.php @@ -117,6 +117,13 @@ class SpipArchiver extends AbstractArchiver implements ArchiverInterface $archive = $this->getArchive(); if ($archive) { if (1 === $archive->open($this->fichier_archive, 'retrait')) { + // Vérifier qu'on ne cherche pas à vider l'archive + $reste = count($this->informer()); + if ($reste === count($fichiers)) { + $this->setErreur(8); + + return false; + } $retour = $archive->remove($fichiers); $archive->close(); } diff --git a/src/TarArchive.php b/src/TarArchive.php index 602d568..6572f7c 100644 --- a/src/TarArchive.php +++ b/src/TarArchive.php @@ -26,10 +26,6 @@ class TarArchive implements ArchiveInterface 2 ); - if ('' === $this->tar->getFilename()) { - return 0; - } - return 1; } diff --git a/tests/AbstractArchiverTest.php b/tests/AbstractArchiverTest.php index b5a2464..541f61d 100644 --- a/tests/AbstractArchiverTest.php +++ b/tests/AbstractArchiverTest.php @@ -5,11 +5,6 @@ namespace Spip\Archiver\Tests; use PHPUnit\Framework\TestCase; use Spip\Archiver\AbstractArchiver; -// si on lance les tests depuis tests/ -if (function_exists('include_spip')) { - include_spip('inc/archives'); -} - /** * @covers \Spip\Archiver\AbstractArchiver * diff --git a/tests/SpipArchiverTest.php b/tests/SpipArchiverTest.php index fbf9fbe..256c962 100644 --- a/tests/SpipArchiverTest.php +++ b/tests/SpipArchiverTest.php @@ -5,11 +5,6 @@ namespace Spip\Archiver\Tests; use PHPUnit\Framework\TestCase; use Spip\Archiver\SpipArchiver; -// si on lance les tests depuis tests/ -if (function_exists('include_spip')) { - include_spip('inc/archives'); -} - /** * @covers \Spip\Archiver\AbstractArchiver * @covers \Spip\Archiver\SpipArchiver @@ -28,24 +23,25 @@ class SpipArchiverTest extends TestCase @file_put_contents(__DIR__ . '/../var/tmp/directory/test.txt', 'contenu de test'); - $test_retirer_zip = new \ZipArchive(); - $test_retirer_zip->open(__DIR__ . '/../var/tmp/retirer.zip', \ZipArchive::CREATE); - $test_retirer_zip->addFromString('test.txt', 'contenu de test'); - $test_retirer_zip->close(); - @mkdir(__DIR__ . '/../var/tmp/tar/directory'); + $test_retirer_zip = new \ZipArchive(); + $test_retirer_zip->open(__DIR__ . '/../var/tmp/retirer.zip', \ZipArchive::CREATE); + $test_retirer_zip->addFromString('test.txt', 'contenu de test'); + $test_retirer_zip->addFromString('sub_directory/test2.txt', 'contenu de test2'); + $test_retirer_zip->close(); + @mkdir(__DIR__ . '/../var/tmp/tar/directory'); @file_put_contents(__DIR__ . '/../var/tmp/tar/directory/test.txt', 'contenu de test'); - @mkdir(__DIR__ . '/../var/tmp/tar/directory/sub_directory'); + @mkdir(__DIR__ . '/../var/tmp/tar/directory/sub_directory'); @file_put_contents(__DIR__ . '/../var/tmp/tar/directory/sub_directory/test2.txt', 'contenu de test2'); - $test_retirer_tar = new \Spip\Archiver\TarArchive(); - $test_retirer_tar->open(__DIR__ . '/../var/tmp/tar/retirer.tar', 'creation'); - $test_retirer_tar->compress(__DIR__ . '/../var/tmp/tar/directory', [ - __DIR__ . '/../var/tmp/tar/directory/test.txt', - __DIR__ . '/../var/tmp/tar/directory/sub_directory/test2.txt', - ]); - @unlink(__DIR__ . '/../var/tmp/tar/directory/test.txt'); + $test_retirer_tar = new \Spip\Archiver\TarArchive(); + $test_retirer_tar->open(__DIR__ . '/../var/tmp/tar/retirer.tar', 'creation'); + $test_retirer_tar->compress(__DIR__ . '/../var/tmp/tar/directory', [ + __DIR__ . '/../var/tmp/tar/directory/test.txt', + __DIR__ . '/../var/tmp/tar/directory/sub_directory/test2.txt', + ]); + @unlink(__DIR__ . '/../var/tmp/tar/directory/test.txt'); @unlink(__DIR__ . '/../var/tmp/tar/directory/sub_directory/test2.txt'); - @rmdir(__DIR__ . '/../var/tmp/tar/directory/sub_directory'); - @rmdir(__DIR__ . '/../var/tmp/tar/directory'); + @rmdir(__DIR__ . '/../var/tmp/tar/directory/sub_directory'); + @rmdir(__DIR__ . '/../var/tmp/tar/directory'); } public static function tearDownAfterClass(): void @@ -126,15 +122,6 @@ class SpipArchiverTest extends TestCase __DIR__ . '/../var/tmp/tar/test.tar', '', ], - 'empty-tar' => [ - 0, - [ - 'proprietes' => [], - 'fichiers' => [], - ], - __DIR__ . '/../var/tmp/tar/empty.tar', - 'tar', - ], 'tgz' => [ 0, [ @@ -181,7 +168,7 @@ class SpipArchiverTest extends TestCase [], __DIR__ . '/../var/tmp/test.txt', ], - 'destination-not-exists' => [ + 'destination-not-exists' => [ false, 5, __DIR__ . '/../var/tmp/test.zip', @@ -261,11 +248,11 @@ class SpipArchiverTest extends TestCase null, ], 'tar' => [ - true, - 0, - __DIR__ . '/../var/tmp/tar/emballer.tar', - '', - [__DIR__ . '/../var/tmp/directory/test.txt'], + true, + 0, + __DIR__ . '/../var/tmp/tar/emballer.tar', + '', + [__DIR__ . '/../var/tmp/directory/test.txt'], __DIR__ . '/../var/tmp/directory', ], ]; @@ -288,7 +275,7 @@ class SpipArchiverTest extends TestCase $this->assertTrue(file_exists($file), 'compressed file exists'); } - public function dataRetirer() + public function dataRetirer() { return [ 'not-exists' => [ @@ -305,6 +292,13 @@ class SpipArchiverTest extends TestCase '', ['test.txt'], ], + 'vider' => [ + false, + 8, + __DIR__ . '/../var/tmp/retirer.zip', + '', + ['test.txt', 'sub_directory/test2.txt'], + ], 'tar' => [ true, 0, diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..f8eecaa --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,8 @@ +