Add ImportController Tests and fix bugs
modifié: core/lib/Thelia/Controller/Admin/ImportController.php modifié: core/lib/Thelia/Exception/FileNotFoundException.php modifié: core/lib/Thelia/Tests/Controller/ImportControllerTest.php modifié: core/lib/Thelia/Tests/Controller/ImportExportControllerTest.php
This commit is contained in:
@@ -24,6 +24,7 @@ use Thelia\Core\Security\AccessManager;
|
|||||||
use Thelia\Core\Security\Resource\AdminResources;
|
use Thelia\Core\Security\Resource\AdminResources;
|
||||||
use Thelia\Core\Template\Element\LoopResult;
|
use Thelia\Core\Template\Element\LoopResult;
|
||||||
use Thelia\Core\Template\Loop\Import as ImportLoop;
|
use Thelia\Core\Template\Loop\Import as ImportLoop;
|
||||||
|
use Thelia\Exception\FileNotFoundException;
|
||||||
use Thelia\Form\Exception\FormValidationException;
|
use Thelia\Form\Exception\FormValidationException;
|
||||||
use Thelia\Form\ImportForm;
|
use Thelia\Form\ImportForm;
|
||||||
use Thelia\ImportExport\Import\ImportHandler;
|
use Thelia\ImportExport\Import\ImportHandler;
|
||||||
@@ -160,7 +161,7 @@ class ImportController extends BaseAdminController
|
|||||||
return $this->importView($id);
|
return $this->importView($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getFileContentInArchive(
|
public function getFileContentInArchive(
|
||||||
AbstractArchiveBuilder $archiveBuilder,
|
AbstractArchiveBuilder $archiveBuilder,
|
||||||
FormatterManager $formatterManager,
|
FormatterManager $formatterManager,
|
||||||
array $types
|
array $types
|
||||||
@@ -184,7 +185,7 @@ class ImportController extends BaseAdminController
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($content === null) {
|
if ($content === null) {
|
||||||
throw new \ErrorException(
|
throw new FileNotFoundException(
|
||||||
$this->getTranslator()->trans(
|
$this->getTranslator()->trans(
|
||||||
"Your archive must contain one of these file and doesn't: %files",
|
"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)
|
public function checkFileExtension($fileName, $uploadFormat)
|
||||||
{
|
{
|
||||||
$splitName = explode(".", $fileName);
|
|
||||||
$ext = "";
|
|
||||||
|
|
||||||
if (1 < $limit = count($splitName)) {
|
|
||||||
$ext = "." . $splitName[$limit-1];
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($uploadFormat === null) {
|
if ($uploadFormat === null) {
|
||||||
|
$splitName = explode(".", $fileName);
|
||||||
|
$ext = "";
|
||||||
|
|
||||||
|
if (1 < $limit = count($splitName)) {
|
||||||
|
$ext = "." . $splitName[$limit-1];
|
||||||
|
}
|
||||||
|
|
||||||
throw new FormValidationException(
|
throw new FormValidationException(
|
||||||
$this->getTranslator()->trans(
|
$this->getTranslator()->trans(
|
||||||
"The extension \"%ext\" is not allowed",
|
"The extension \"%ext\" is not allowed",
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace Thelia\Exception;
|
|||||||
* @package Thelia\Exception
|
* @package Thelia\Exception
|
||||||
* @author Benjamin Perche <bperche@openstudio.fr>
|
* @author Benjamin Perche <bperche@openstudio.fr>
|
||||||
*/
|
*/
|
||||||
class FileNotFoundException extends \Exception
|
class FileNotFoundException extends \ErrorException
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,11 @@
|
|||||||
|
|
||||||
namespace Thelia\Tests\Controller;
|
namespace Thelia\Tests\Controller;
|
||||||
use Thelia\Controller\Admin\ImportController;
|
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
|
* Class ImportControllerTest
|
||||||
@@ -20,6 +25,9 @@ use Thelia\Controller\Admin\ImportController;
|
|||||||
*/
|
*/
|
||||||
class ImportControllerTest extends ImportExportControllerTest
|
class ImportControllerTest extends ImportExportControllerTest
|
||||||
{
|
{
|
||||||
|
use FormatterManagerTrait;
|
||||||
|
use ArchiveBuilderManagerTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Thelia\Controller\BaseController The controller you want to test
|
* @return \Thelia\Controller\BaseController The controller you want to test
|
||||||
*/
|
*/
|
||||||
@@ -27,6 +35,186 @@ class ImportControllerTest extends ImportExportControllerTest
|
|||||||
{
|
{
|
||||||
return new ImportController();
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,7 @@ abstract class ImportExportControllerTest extends ControllerTestBase
|
|||||||
->add(new CSVFormatter())
|
->add(new CSVFormatter())
|
||||||
;
|
;
|
||||||
|
|
||||||
$container->set("thelia.manager.formatter_manager", $archiveBuilderManager);
|
$container->set("thelia.manager.formatter_manager", $formatterManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user