示例#1
0
 /**
  * Validate uploaded files action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function execute()
 {
     $data = $this->getRequest()->getPostValue();
     /** @var \Magento\Framework\View\Result\Layout $resultLayout */
     $resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
     /** @var $resultBlock ImportResultBlock */
     $resultBlock = $resultLayout->getLayout()->getBlock('import.frame.result');
     if ($data) {
         // common actions
         $resultBlock->addAction('show', 'import_validation_container');
         /** @var $import \Magento\ImportExport\Model\Import */
         $import = $this->getImport()->setData($data);
         try {
             $source = ImportAdapter::findAdapterFor($import->uploadSource(), $this->_objectManager->create('Magento\\Framework\\Filesystem')->getDirectoryWrite(DirectoryList::ROOT), $data[$import::FIELD_FIELD_SEPARATOR]);
             $this->processValidationResult($import->validateSource($source), $resultBlock);
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             $resultBlock->addError($e->getMessage());
         } catch (\Exception $e) {
             $resultBlock->addError(__('Sorry, but the data is invalid or the file is not uploaded.'));
         }
         return $resultLayout;
     } elseif ($this->getRequest()->isPost() && empty($_FILES)) {
         $resultBlock->addError(__('The file was not uploaded.'));
         return $resultLayout;
     }
     $this->messageManager->addError(__('Sorry, but the data is invalid or the file is not uploaded.'));
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     $resultRedirect->setPath('adminhtml/*/index');
     return $resultRedirect;
 }
示例#2
0
 /**
  * Validate uploaded files action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function executeInternal()
 {
     $data = $this->getRequest()->getPostValue();
     /** @var \Magento\Framework\View\Result\Layout $resultLayout */
     $resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
     /** @var $resultBlock ImportResultBlock */
     $resultBlock = $resultLayout->getLayout()->getBlock('import.frame.result');
     if ($data) {
         // common actions
         $resultBlock->addAction('show', 'import_validation_container');
         /** @var $import \Magento\ImportExport\Model\Import */
         $import = $this->_objectManager->create('Magento\\ImportExport\\Model\\Import')->setData($data);
         $source = ImportAdapter::findAdapterFor($import->uploadSource(), $this->_objectManager->create('Magento\\Framework\\Filesystem')->getDirectoryWrite(DirectoryList::ROOT), $data[$import::FIELD_FIELD_SEPARATOR]);
         $validationResult = $import->validateSource($source);
         if (!$import->getProcessedRowsCount()) {
             if (!$import->getErrorAggregator()->getErrorsCount()) {
                 $resultBlock->addError(__('This file is empty. Please try another one.'));
             } else {
                 foreach ($import->getErrorAggregator()->getAllErrors() as $error) {
                     $resultBlock->addError($error->getErrorMessage(), false);
                 }
             }
         } else {
             $errorAggregator = $import->getErrorAggregator();
             if (!$validationResult) {
                 $resultBlock->addError(__('Data validation is failed. Please fix errors and re-upload the file..'));
                 $this->addErrorMessages($resultBlock, $errorAggregator);
             } else {
                 if ($import->isImportAllowed()) {
                     $resultBlock->addSuccess(__('File is valid! To start import process press "Import" button'), true);
                 } else {
                     $resultBlock->addError(__('The file is valid, but we can\'t import it for some reason.'), false);
                 }
             }
             $resultBlock->addNotice(__('Checked rows: %1, checked entities: %2, invalid rows: %3, total errors: %4', $import->getProcessedRowsCount(), $import->getProcessedEntitiesCount(), $errorAggregator->getInvalidRowsCount(), $errorAggregator->getErrorsCount()));
         }
         return $resultLayout;
     } elseif ($this->getRequest()->isPost() && empty($_FILES)) {
         $resultBlock->addError(__('The file was not uploaded.'));
         return $resultLayout;
     }
     $this->messageManager->addError(__('Sorry, but the data is invalid or the file is not uploaded.'));
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     $resultRedirect->setPath('adminhtml/*/index');
     return $resultRedirect;
 }
示例#3
0
 /**
  * Validate uploaded files action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function execute()
 {
     $data = $this->getRequest()->getPostValue();
     /** @var \Magento\Framework\View\Result\Layout $resultLayout */
     $resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
     /** @var $resultBlock \Magento\ImportExport\Block\Adminhtml\Import\Frame\Result */
     $resultBlock = $resultLayout->getLayout()->getBlock('import.frame.result');
     if ($data) {
         // common actions
         $resultBlock->addAction('show', 'import_validation_container');
         try {
             /** @var $import \Magento\ImportExport\Model\Import */
             $import = $this->_objectManager->create('Magento\\ImportExport\\Model\\Import')->setData($data);
             $source = ImportAdapter::findAdapterFor($import->uploadSource(), $this->_objectManager->create('Magento\\Framework\\Filesystem')->getDirectoryWrite(DirectoryList::ROOT), $data[$import::FIELD_FIELD_SEPARATOR]);
             $validationResult = $import->validateSource($source);
             if (!$import->getProcessedRowsCount()) {
                 $resultBlock->addError(__('File does not contain data. Please upload another one'));
             } else {
                 if (!$validationResult) {
                     $this->processValidationError($import, $resultBlock);
                 } else {
                     if ($import->isImportAllowed()) {
                         $resultBlock->addSuccess(__('File is valid! To start import process press "Import" button'), true);
                     } else {
                         $resultBlock->addError(__('File is valid, but import is not possible'), false);
                     }
                 }
                 $resultBlock->addNotice($import->getNotices());
                 $resultBlock->addNotice(__('Checked rows: %1, checked entities: %2, invalid rows: %3, total errors: %4', $import->getProcessedRowsCount(), $import->getProcessedEntitiesCount(), $import->getInvalidRowsCount(), $import->getErrorsCount()));
             }
         } catch (\Exception $e) {
             $resultBlock->addNotice(__('Please fix errors and re-upload file.'))->addError($e->getMessage());
         }
         return $resultLayout;
     } elseif ($this->getRequest()->isPost() && empty($_FILES)) {
         $resultBlock->addError(__('File was not uploaded'));
         return $resultLayout;
     }
     $this->messageManager->addError(__('Data is invalid or file is not uploaded'));
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     $resultRedirect->setPath('adminhtml/*/index');
     return $resultRedirect;
 }
示例#4
0
 /**
  * Validate uploaded files action
  *
  * @return void
  */
 public function execute()
 {
     $data = $this->getRequest()->getPost();
     if ($data) {
         $this->_view->loadLayout(false);
         /** @var $resultBlock \Magento\ImportExport\Block\Adminhtml\Import\Frame\Result */
         $resultBlock = $this->_view->getLayout()->getBlock('import.frame.result');
         // common actions
         $resultBlock->addAction('show', 'import_validation_container')->addAction('clear', array(\Magento\ImportExport\Model\Import::FIELD_NAME_SOURCE_FILE, \Magento\ImportExport\Model\Import::FIELD_NAME_IMG_ARCHIVE_FILE));
         try {
             /** @var $import \Magento\ImportExport\Model\Import */
             $import = $this->_objectManager->create('Magento\\ImportExport\\Model\\Import')->setData($data);
             $source = \Magento\ImportExport\Model\Import\Adapter::findAdapterFor($import->uploadSource(), $this->_objectManager->create('Magento\\Framework\\App\\Filesystem')->getDirectoryWrite(\Magento\Framework\App\Filesystem::ROOT_DIR));
             $validationResult = $import->validateSource($source);
             if (!$import->getProcessedRowsCount()) {
                 $resultBlock->addError(__('File does not contain data. Please upload another one'));
             } else {
                 if (!$validationResult) {
                     $this->_processValidationError($import, $resultBlock);
                 } else {
                     if ($import->isImportAllowed()) {
                         $resultBlock->addSuccess(__('File is valid! To start import process press "Import" button'), true);
                     } else {
                         $resultBlock->addError(__('File is valid, but import is not possible'), false);
                     }
                 }
                 $resultBlock->addNotice($import->getNotices());
                 $resultBlock->addNotice(__('Checked rows: %1, checked entities: %2, invalid rows: %3, total errors: %4', $import->getProcessedRowsCount(), $import->getProcessedEntitiesCount(), $import->getInvalidRowsCount(), $import->getErrorsCount()));
             }
         } catch (\Exception $e) {
             $resultBlock->addNotice(__('Please fix errors and re-upload file.'))->addError($e->getMessage());
         }
         $this->_view->renderLayout();
     } elseif ($this->getRequest()->isPost() && empty($_FILES)) {
         $this->_view->loadLayout(false);
         $resultBlock = $this->_view->getLayout()->getBlock('import.frame.result');
         $resultBlock->addError(__('File was not uploaded'));
         $this->_view->renderLayout();
     } else {
         $this->messageManager->addError(__('Data is invalid or file is not uploaded'));
         $this->_redirect('adminhtml/*/index');
     }
 }
示例#5
0
 /**
  * Returns source adapter object.
  *
  * @param string $sourceFile Full path to source file
  * @return \Magento\ImportExport\Model\Import\AbstractSource
  */
 protected function _getSourceAdapter($sourceFile)
 {
     return \Magento\ImportExport\Model\Import\Adapter::findAdapterFor($sourceFile, $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT), $this->getData(self::FIELD_FIELD_SEPARATOR));
 }
示例#6
0
 /**
  * @param string $behavior
  * @param string $sourceFile
  * @param array $dataBefore
  * @param array $dataAfter
  * @param array $errors
  *
  * @magentoDataFixture Magento/Customer/_files/import_export/customers_for_address_import.php
  * @magentoAppIsolation enabled
  *
  * @dataProvider importDataDataProvider
  */
 public function testImportData($behavior, $sourceFile, array $dataBefore, array $dataAfter, array $errors = array())
 {
     \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea(\Magento\Framework\App\Area::AREA_FRONTEND);
     // set entity adapter parameters
     $this->_entityAdapter->setParameters(array('behavior' => $behavior));
     /** @var \Magento\Framework\App\Filesystem $filesystem */
     $filesystem = $this->_objectManager->create('Magento\\Framework\\App\\Filesystem');
     $rootDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::ROOT_DIR);
     // set fixture CSV file
     $result = $this->_entityAdapter->setSource(\Magento\ImportExport\Model\Import\Adapter::findAdapterFor($sourceFile, $rootDirectory))->isDataValid();
     if ($errors) {
         $this->assertFalse($result);
     } else {
         $this->assertTrue($result);
     }
     // assert validation errors
     // can't use error codes because entity adapter gathers only error messages from aggregated adapters
     $actualErrors = array_values($this->_entityAdapter->getErrorMessages());
     $this->assertEquals($errors, $actualErrors);
     // assert data before import
     $this->_assertCustomerData($dataBefore);
     // import data
     $this->_entityAdapter->importData();
     // assert data after import
     $this->_assertCustomerData($dataAfter);
 }
 /**
  * @param string $behavior
  * @param string $sourceFile
  * @param array $dataBefore
  * @param array $dataAfter
  * @param array $errors
  *
  * @magentoDataFixture Magento/Customer/_files/import_export/customers_for_address_import.php
  * @magentoAppIsolation enabled
  *
  * @dataProvider importDataDataProvider
  */
 public function testImportData($behavior, $sourceFile, array $dataBefore, array $dataAfter, array $errors = [])
 {
     \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea(\Magento\Framework\App\Area::AREA_FRONTEND);
     // set entity adapter parameters
     $this->_entityAdapter->setParameters(['behavior' => $behavior]);
     /** @var \Magento\Framework\Filesystem $filesystem */
     $filesystem = $this->_objectManager->create('Magento\\Framework\\Filesystem');
     $rootDirectory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
     $this->_entityAdapter->getErrorAggregator()->initValidationStrategy(ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_STOP_ON_ERROR, 10);
     // set fixture CSV file
     $result = $this->_entityAdapter->setSource(\Magento\ImportExport\Model\Import\Adapter::findAdapterFor($sourceFile, $rootDirectory))->validateData()->hasToBeTerminated();
     if ($errors) {
         $this->assertTrue($result);
     } else {
         $this->assertFalse($result);
     }
     // assert validation errors
     // can't use error codes because entity adapter gathers only error messages from aggregated adapters
     $actualErrors = array_values($this->_entityAdapter->getErrorAggregator()->getRowsGroupedByErrorCode());
     $this->assertEquals($errors, $actualErrors);
     // assert data before import
     $this->_assertCustomerData($dataBefore);
     // import data
     $this->_entityAdapter->importData();
     // assert data after import
     $this->_assertCustomerData($dataAfter);
 }
示例#8
0
 /**
  * Test import data method with delete behaviour
  *
  * @magentoDataFixture Magento/Customer/_files/import_export/customers_for_address_import.php
  */
 public function testImportDataDelete()
 {
     // set behaviour
     $this->_entityAdapter->setParameters(array('behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE));
     // set fixture CSV file
     $sourceFile = __DIR__ . '/_files/address_import_delete.csv';
     /** @var $objectManager \Magento\TestFramework\ObjectManager */
     $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
     $filesystem = $objectManager->create('Magento\\Framework\\App\\Filesystem');
     $directoryWrite = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::ROOT_DIR);
     $result = $this->_entityAdapter->setSource(\Magento\ImportExport\Model\Import\Adapter::findAdapterFor($sourceFile, $directoryWrite))->isDataValid();
     $this->assertTrue($result, 'Validation result must be true.');
     // import data
     $this->_entityAdapter->importData();
     // key attribute
     $keyAttribute = 'postcode';
     // get addresses
     /** @var $addressCollection \Magento\Customer\Model\Resource\Address\Collection */
     $addressCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Customer\\Model\\Resource\\Address\\Collection');
     $addressCollection->addAttributeToSelect($keyAttribute);
     $addresses = array();
     /** @var $address \Magento\Customer\Model\Address */
     foreach ($addressCollection as $address) {
         $addresses[$address->getData($keyAttribute)] = $address;
     }
     // is addresses exists
     $this->assertArrayNotHasKey($this->_deleteData['delete'], $addresses, 'Address must not exist.');
     $this->assertArrayHasKey($this->_deleteData['not_delete'], $addresses, 'Address must exist.');
 }
示例#9
0
文件: Import.php 项目: aiesh/magento2
 /**
  * Returns source adapter object.
  *
  * @param string $sourceFile Full path to source file
  * @return \Magento\ImportExport\Model\Import\AbstractSource
  */
 protected function _getSourceAdapter($sourceFile)
 {
     return \Magento\ImportExport\Model\Import\Adapter::findAdapterFor($sourceFile, $this->_filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::ROOT_DIR));
 }