From 14cf745a89538f372c16852cd5b0e215a708e563 Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Mon, 21 Jul 2014 10:36:40 +0200 Subject: [PATCH] =?UTF-8?q?Add=20ImportController=20Tests=20and=20fix=20bu?= =?UTF-8?q?gs=20=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia?= =?UTF-8?q?/Controller/Admin/ImportController.php=20=09modifi=C3=A9:=20=20?= =?UTF-8?q?=20=20=20=20=20=20=20core/lib/Thelia/Exception/FileNotFoundExce?= =?UTF-8?q?ption.php=20=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib?= =?UTF-8?q?/Thelia/Tests/Controller/ImportControllerTest.php=20=09modifi?= =?UTF-8?q?=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Tests/Control?= =?UTF-8?q?ler/ImportExportControllerTest.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/Admin/ImportController.php | 19 +- .../Exception/FileNotFoundException.php | 2 +- .../Tests/Controller/ImportControllerTest.php | 190 +++++++++++++++++- .../Controller/ImportExportControllerTest.php | 2 +- 4 files changed, 201 insertions(+), 12 deletions(-) diff --git a/core/lib/Thelia/Controller/Admin/ImportController.php b/core/lib/Thelia/Controller/Admin/ImportController.php index 674b46306..6b718b16d 100644 --- a/core/lib/Thelia/Controller/Admin/ImportController.php +++ b/core/lib/Thelia/Controller/Admin/ImportController.php @@ -24,6 +24,7 @@ use Thelia\Core\Security\AccessManager; use Thelia\Core\Security\Resource\AdminResources; use Thelia\Core\Template\Element\LoopResult; use Thelia\Core\Template\Loop\Import as ImportLoop; +use Thelia\Exception\FileNotFoundException; use Thelia\Form\Exception\FormValidationException; use Thelia\Form\ImportForm; use Thelia\ImportExport\Import\ImportHandler; @@ -160,7 +161,7 @@ class ImportController extends BaseAdminController return $this->importView($id); } - protected function getFileContentInArchive( + public function getFileContentInArchive( AbstractArchiveBuilder $archiveBuilder, FormatterManager $formatterManager, array $types @@ -184,7 +185,7 @@ class ImportController extends BaseAdminController } if ($content === null) { - throw new \ErrorException( + throw new FileNotFoundException( $this->getTranslator()->trans( "Your archive must contain one of these file and doesn't: %files", [ @@ -252,14 +253,14 @@ class ImportController extends BaseAdminController public function checkFileExtension($fileName, $uploadFormat) { - $splitName = explode(".", $fileName); - $ext = ""; - - if (1 < $limit = count($splitName)) { - $ext = "." . $splitName[$limit-1]; - } - if ($uploadFormat === null) { + $splitName = explode(".", $fileName); + $ext = ""; + + if (1 < $limit = count($splitName)) { + $ext = "." . $splitName[$limit-1]; + } + throw new FormValidationException( $this->getTranslator()->trans( "The extension \"%ext\" is not allowed", diff --git a/core/lib/Thelia/Exception/FileNotFoundException.php b/core/lib/Thelia/Exception/FileNotFoundException.php index 23157f80b..7dc13dbd0 100644 --- a/core/lib/Thelia/Exception/FileNotFoundException.php +++ b/core/lib/Thelia/Exception/FileNotFoundException.php @@ -17,7 +17,7 @@ namespace Thelia\Exception; * @package Thelia\Exception * @author Benjamin Perche */ -class FileNotFoundException extends \Exception +class FileNotFoundException extends \ErrorException { } diff --git a/core/lib/Thelia/Tests/Controller/ImportControllerTest.php b/core/lib/Thelia/Tests/Controller/ImportControllerTest.php index 42d278b6a..ff1867d0d 100644 --- a/core/lib/Thelia/Tests/Controller/ImportControllerTest.php +++ b/core/lib/Thelia/Tests/Controller/ImportControllerTest.php @@ -12,6 +12,11 @@ namespace Thelia\Tests\Controller; use Thelia\Controller\Admin\ImportController; +use Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveBuilder; +use Thelia\Core\FileFormat\Archive\ArchiveBuilderManagerTrait; +use Thelia\Core\FileFormat\Formatting\Formatter\XMLFormatter; +use Thelia\Core\FileFormat\Formatting\FormatterManagerTrait; +use Thelia\Core\FileFormat\FormatType; /** * Class ImportControllerTest @@ -20,6 +25,9 @@ use Thelia\Controller\Admin\ImportController; */ class ImportControllerTest extends ImportExportControllerTest { + use FormatterManagerTrait; + use ArchiveBuilderManagerTrait; + /** * @return \Thelia\Controller\BaseController The controller you want to test */ @@ -27,6 +35,186 @@ class ImportControllerTest extends ImportExportControllerTest { return new ImportController(); } - + public function testCheckFileExtension() + { + $this->controller->checkFileExtension("a.zip", "zip"); + } + + /** + * @expectedException \Thelia\Form\Exception\FormValidationException + * @expectedExceptionMessage The extension ".zip" is not allowed + */ + public function testCheckFileExtensionFail() + { + $this->controller->checkFileExtension("a.zip", null); + } + + /** + * @expectedException \Thelia\Form\Exception\FormValidationException + * @expectedExceptionMessage The extension ".bz2" is not allowed + */ + public function testCheckFileExtensionFailMultipleExt() + { + $this->controller->checkFileExtension("a.tar.bz2", null); + } + + /** + * @expectedException \Thelia\Form\Exception\FormValidationException + * @expectedExceptionMessage The extension "" is not allowed + */ + public function testCheckFileExtensionFailNoExt() + { + $this->controller->checkFileExtension("file", null); + } + + public function testGetFileContentInArchive() + { + /** @var ZipArchiveBuilder $archive */ + $archive = $this->getArchiveBuilderManager($this->container)->get("ZIP"); + $formatter = new XMLFormatter(); + + $archive->addFileFromString("foo", $formatter::FILENAME . "." . $formatter->getExtension()); + + $content = $this->controller->getFileContentInArchive( + $archive, + $this->getFormatterManager($this->container), + [$formatter->getHandledType()] + ); + + $this->assertEquals("foo", $content); + } + + /** + * @expectedException \Thelia\Exception\FileNotFoundException + * @expectedExceptionMessage Your archive must contain one of these file and doesn't: + */ + public function testGetFileContentInArchiveFail() + { + /** @var ZipArchiveBuilder $archive */ + $archive = $this->getArchiveBuilderManager($this->container)->get("ZIP"); + $formatter = new XMLFormatter(); + + $archive->addFileFromString("foo", "bar"); + + $this->controller->getFileContentInArchive( + $archive, + $this->getFormatterManager($this->container), + [$formatter->getHandledType()] + ); + } + + public function testRetrieveFormatTools() + { + $handler = $this + ->getMock( + "\\Thelia\\ImportExport\\Import\\ImportHandler", + [], + [ + $this->container + ] + ) + ; + + $handler + ->expects($this->any()) + ->method("getHandledTypes") + ->willReturn(FormatType::UNBOUNDED) + ; + + $tools = $this->controller->retrieveFormatTools( + "foo.xml", + $handler, + $this->getFormatterManager($this->container), + $this->getArchiveBuilderManager($this->container) + ); + + $this->assertArrayHasKey("formatter", $tools); + $this->assertInstanceOf( + "Thelia\\Core\\FileFormat\\Formatting\\AbstractFormatter", + $tools["formatter"] + ); + + $this->assertArrayHasKey("archive_builder", $tools); + $this->assertNull($tools["archive_builder"]); + + $this->assertArrayHasKey("extension", $tools); + $this->assertEquals(".xml", $tools["extension"]); + + $this->assertArrayHasKey("types", $tools); + $this->assertEquals( + FormatType::UNBOUNDED, + $tools["types"] + ); + + $handler = $this + ->getMock( + "\\Thelia\\ImportExport\\Import\\ImportHandler", + [], + [ + $this->container + ] + ) + ; + + $handler + ->expects($this->any()) + ->method("getHandledTypes") + ->willReturn([FormatType::UNBOUNDED]) + ; + + $tools = $this->controller->retrieveFormatTools( + "foo.zip", + $handler, + $this->getFormatterManager($this->container), + $this->getArchiveBuilderManager($this->container) + ); + + $this->assertArrayHasKey("formatter", $tools); + $this->assertNull($tools["formatter"]); + + $this->assertArrayHasKey("archive_builder", $tools); + $this->assertInstanceOf( + "Thelia\\Core\\FileFormat\\Archive\\AbstractArchiveBuilder", + $tools["archive_builder"] + ); + + $this->assertArrayHasKey("extension", $tools); + $this->assertEquals(".zip", $tools["extension"]); + + $this->assertArrayHasKey("types", $tools); + $this->assertEquals( + [FormatType::UNBOUNDED], + $tools["types"] + ); + } + + /** + * @expectedException \Thelia\Form\Exception\FormValidationException + */ + public function testRetrieveFormatToolsFail() + { + $handler = $this + ->getMock( + "\\Thelia\\ImportExport\\Import\\ImportHandler", + [], + [ + $this->container + ] + ) + ; + + $handler + ->expects($this->any()) + ->method("getHandledTypes") + ->willReturn(FormatType::UNBOUNDED) + ; + + $this->controller->retrieveFormatTools( + "foo.csv", + $handler, + $this->getFormatterManager($this->container), + $this->getArchiveBuilderManager($this->container) + ); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Tests/Controller/ImportExportControllerTest.php b/core/lib/Thelia/Tests/Controller/ImportExportControllerTest.php index 5af6ce61d..da126fca7 100644 --- a/core/lib/Thelia/Tests/Controller/ImportExportControllerTest.php +++ b/core/lib/Thelia/Tests/Controller/ImportExportControllerTest.php @@ -49,7 +49,7 @@ abstract class ImportExportControllerTest extends ControllerTestBase ->add(new CSVFormatter()) ; - $container->set("thelia.manager.formatter_manager", $archiveBuilderManager); + $container->set("thelia.manager.formatter_manager", $formatterManager); } } \ No newline at end of file