From c4d22b8c793039563d835c0c5fd8fdcdc1510b14 Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Thu, 3 Jul 2014 14:51:20 +0200 Subject: [PATCH] =?UTF-8?q?Finish=20Tar=20archive=20builder=20=09modifi?= =?UTF-8?q?=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Core/FileForm?= =?UTF-8?q?at/Archive/ArchiveBuilder/TarArchiveBuilder.php=20=09modifi?= =?UTF-8?q?=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Tests/FileFor?= =?UTF-8?q?mat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php=20=09nouve?= =?UTF-8?q?au=20fichier:=20core/lib/Thelia/Tests/FileFormat/Archive/Archiv?= =?UTF-8?q?eBuilder/TestResources/bad=5Fformatted.tar=20=09nouveau=20fichi?= =?UTF-8?q?er:=20core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/T?= =?UTF-8?q?estResources/well=5Fformatted.tar=20=09modifi=C3=A9:=20=20=20?= =?UTF-8?q?=20=20=20=20=20=20core/lib/Thelia/Tests/FileFormat/Archive/Arch?= =?UTF-8?q?iveBuilder/ZipArchiveBuilderTest.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArchiveBuilder/TarArchiveBuilder.php | 37 ++++- .../ArchiveBuilder/TarArchiveBuilderTest.php | 135 +++++++++++++++++- .../TestResources/bad_formatted.tar | 2 + .../TestResources/well_formatted.tar | Bin 0 -> 9216 bytes .../ArchiveBuilder/ZipArchiveBuilderTest.php | 4 +- 5 files changed, 171 insertions(+), 7 deletions(-) create mode 100644 core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.tar create mode 100644 core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.tar diff --git a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php index b9ccb6a6a..08cd483e9 100644 --- a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php +++ b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php @@ -13,9 +13,10 @@ namespace Thelia\Core\FileFormat\Archive\ArchiveBuilder; use Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder; use Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception\TarArchiveException; -use Thelia\Core\FileFormat\Archive\ArchiveBuilderInterface; +use Thelia\Core\HttpFoundation\Response; use Thelia\Core\Thelia; use Thelia\Core\Translation\Translator; +use Thelia\Exception\FileNotReadableException; use Thelia\Log\Tlog; use Thelia\Tools\FileDownload\FileDownloaderInterface; @@ -68,7 +69,7 @@ class TarArchiveBuilder extends AbstractArchiveBuilder $this->compression = $compressionType; } - /** + public function __destruct() { if ($this->tar instanceof \PharData) { @@ -76,7 +77,7 @@ class TarArchiveBuilder extends AbstractArchiveBuilder unlink($this->cacheFile); } } - }*/ + } /** * @param string $filePath It is the path to access the file. @@ -118,6 +119,11 @@ class TarArchiveBuilder extends AbstractArchiveBuilder $this->tar->addFile($filePath, $name); + /** + * And clear the download temp file + */ + unlink($fileDownloadCache); + return $this; } @@ -257,9 +263,32 @@ class TarArchiveBuilder extends AbstractArchiveBuilder */ public function buildArchiveResponse() { - $this->tar->setMetadata("comment", "Generated by Thelia v" . Thelia::THELIA_VERSION); + $this->tar->setMetadata("Generated by Thelia v" . Thelia::THELIA_VERSION); + if (!is_file($this->cacheFile)) { + $this->throwFileNotFound($this->cacheFile); + } + if (!is_readable($this->cacheFile)) { + throw new FileNotReadableException( + $this->translator->trans( + "The file %file is not readable", + [ + "%file" => $this->cacheFile + ] + ) + ); + } + + $content = file_get_contents($this->cacheFile); + + return new Response( + $content, + 200, + [ + "Content-Type" => $this->getMimeType(), + ] + ); } /** diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php index 6c3bc61a7..323300714 100644 --- a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php +++ b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php @@ -132,7 +132,6 @@ class TarArchiveBuilderTest extends \PHPUnit_Framework_TestCase ); } - public function testDeleteFile() { $this->tar->addFileFromString( @@ -149,4 +148,138 @@ class TarArchiveBuilderTest extends \PHPUnit_Framework_TestCase $this->tar->hasFile("bar") ); } + + public function testLoadValidArchive() + { + $tar = TarArchiveBuilder::loadArchive( + __DIR__ . DS . "TestResources/well_formatted.tar", + "dev" + ); + + $this->assertInstanceOf( + get_class($this->tar), + $tar + ); + + $this->assertTrue( + $tar->hasFile("LICENSE.txt") + ); + } + + /** + * @expectedException \Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception\TarArchiveException + */ + public function testLoadInvalidArchive() + { + $tar = TarArchiveBuilder::loadArchive( + __DIR__ . DS . "TestResources/bad_formatted.tar", + "dev" + ); + } + + public function testFormatDirectoryPath() + { + $this->assertEquals( + "foo/", + $this->tar->formatDirectoryPath("foo") + ); + + $this->assertEquals( + "foo/", + $this->tar->formatDirectoryPath("/foo") + ); + + $this->assertEquals( + "foo/", + $this->tar->formatDirectoryPath("foo/") + ); + + $this->assertEquals( + "foo/", + $this->tar->formatDirectoryPath("/foo/") + ); + + $this->assertEquals( + "foo/bar/", + $this->tar->formatDirectoryPath("foo/bar") + ); + + $this->assertEquals( + "foo/bar/", + $this->tar->formatDirectoryPath("/foo/bar") + ); + + $this->assertEquals( + "foo/bar/", + $this->tar->formatDirectoryPath("/foo//bar/") + ); + + $this->assertEquals( + "foo/bar/", + $this->tar->formatDirectoryPath("/foo/bar/") + ); + + $this->assertEquals( + "foo/bar/baz/", + $this->tar->formatDirectoryPath("foo/bar/baz") + ); + + $this->assertEquals( + "foo/bar/baz/", + $this->tar->formatDirectoryPath("//foo/bar///baz/") + ); + } + + public function testFormatFilePath() + { + $this->assertEquals( + "foo", + $this->tar->formatFilePath("foo") + ); + + $this->assertEquals( + "foo", + $this->tar->formatFilePath("/foo") + ); + + $this->assertEquals( + "foo", + $this->tar->formatFilePath("foo/") + ); + + $this->assertEquals( + "foo", + $this->tar->formatFilePath("/foo/") + ); + + $this->assertEquals( + "foo/bar", + $this->tar->formatFilePath("foo/bar") + ); + + $this->assertEquals( + "foo/bar", + $this->tar->formatFilePath("/foo/bar") + ); + + $this->assertEquals( + "foo/bar", + $this->tar->formatFilePath("/foo//bar/") + ); + + $this->assertEquals( + "foo/bar", + $this->tar->formatFilePath("/foo/bar/") + ); + + $this->assertEquals( + "foo/bar/baz", + $this->tar->formatFilePath("foo/bar/baz") + ); + + $this->assertEquals( + "foo/bar/baz", + $this->tar->formatFilePath("//foo/bar///baz/") + ); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.tar b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.tar new file mode 100644 index 000000000..3bd1f0e29 --- /dev/null +++ b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.tar @@ -0,0 +1,2 @@ +foo +bar diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.tar b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.tar new file mode 100644 index 0000000000000000000000000000000000000000..3bff92c8e4bd7b74d49959707e36ec89faab6335 GIT binary patch literal 9216 zcmeHMU2ogS70ok$#f4sK1F9m&j)N|W0&dnxz0jscoTTXEh#X1WP^3B}Evvu2=iHg0 zNXbr8EVkW;1PE-4oICg9+;cxPyLo;6?(TZhJT%XKtAEq!^z!mTo=vA`uU?+@f8xpX z?DEycxt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#`kH z=)`ppyvU|L^PdWatg&xqW9HiqD#3jIBB`;j875m#Z|{K2P9)5918vTHr>2+2nDV~9 zdX&(EOW=wBiXbRrz!3-f268y%i1D)59Qiar)le|U1_|X3wWbI>5k^9m7KISE#m4WM z`b!S1M?J4_C!!-Ve=31Nz~L*3g0v4jRn;l9oXW zlfQzV3=~?zs7Swbgh?TZlS0VUueR932KMNRz()Npw#MYpb00(3t%B5pV?oSVqL&-yb|njbq7ejyF4T(^sFX##v0i9OO?Nz$a{Ma((G1dgO10qz@PX!Km~k& zc~5=yft*7z;#Uau3UutR0g$*pEA2M+0yWpQbQ3A}i#Ph&nT1gMH2j>%9Vs2s;@Sh9 z9t#<_G#E9xY(>)4o7`c{3Jf0WW1wEYriQKwwnz9zfCn;*8KU3;tOig{xu)Bai&4n1 zv-AB!iehQl*N;H4n*+kH5K@|+Mk7xn?&}|m-4O(9Oi?03{P&vDd3boFLb88T$J?iI6&)9ogKa(ZP5O@8uc5Rf zassertEquals( "foo", @@ -102,7 +102,7 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase ); } - public function testGetDirectoryPath() + public function testFormatDirectoryPath() { $this->assertEquals( "/foo/",