/** * @expectedException FailedFileUploadException */ public function testMakeFileViaUploadThatIsNotACsv() { Yii::app()->user->userModel = User::getByUsername('super'); $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files'); $contents = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt'); $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt'; self::resetAndPopulateFilesArrayByFilePathAndName('test', $filePath, 'testNote.txt'); $uploadedFile = ImportUploadedFileUtil::getByNameCatchErrorAndEnsureFileIsACSV('test'); }
/** * Ajax action called from user interface to upload an import file. If a file for this import model is * already uploaded, then this will overwrite it. * @param string $filesVariableName * @param string $id (should be integer, but php type casting doesn't work so well) */ public function actionUploadFile($filesVariableName, $id) { assert('is_string($filesVariableName)'); $import = Import::getById((int) $id); $importWizardForm = ImportWizardUtil::makeFormByImport($import); $importWizardForm->setAttributes($_POST['ImportWizardForm']); if (!$importWizardForm->validateRowColumnDelimeterIsNotEmpty()) { $fileUploadData = array('error' => Zurmo::t('ImportModule', 'Error: Invalid delimiter')); } elseif (!$importWizardForm->validateRowColumnEnclosureIsNotEmpty()) { $fileUploadData = array('error' => Zurmo::t('ImportModule', 'Error: Invalid qualifier')); } else { try { $uploadedFile = ImportUploadedFileUtil::getByNameCatchErrorAndEnsureFileIsACSV($filesVariableName); assert('$uploadedFile instanceof CUploadedFile'); ImportUploadedFileUtil::convertWindowsAndMacLineEndingsIntoUnixLineEndings($uploadedFile->getTempName()); $fileHandle = fopen($uploadedFile->getTempName(), 'r'); if ($fileHandle !== false) { $tempTableName = $import->getTempTableName(); try { $tableCreated = ImportDatabaseUtil::makeDatabaseTableByFileHandleAndTableName($fileHandle, $tempTableName, $importWizardForm->rowColumnDelimiter, $importWizardForm->rowColumnEnclosure, $importWizardForm->firstRowIsHeaderRow); if (!$tableCreated) { throw new FailedFileUploadException(Zurmo::t('ImportModule', 'Failed to create temporary database table from CSV.')); } } catch (BulkInsertFailedException $e) { throw new FailedFileUploadException($e->getMessage()); } catch (TooManyColumnsFailedException $e) { throw new FailedFileUploadException($e->getMessage()); } $fileUploadData = array('name' => $uploadedFile->getName(), 'type' => $uploadedFile->getType(), 'size' => $uploadedFile->getSize()); ImportWizardUtil::setFormByFileUploadDataAndTableName($importWizardForm, $fileUploadData, $tempTableName); ImportWizardUtil::setImportSerializedDataFromForm($importWizardForm, $import); if (!$import->save()) { throw new FailedFileUploadException(Zurmo::t('ImportModule', 'Import model failed to save.')); } } else { throw new FailedFileUploadException(Zurmo::t('ImportModule', 'Failed to open the uploaded file.')); } $fileUploadData['id'] = $import->id; } catch (FailedFileUploadException $e) { $fileUploadData = array('error' => Zurmo::t('Core', 'Error') . ' ' . $e->getMessage()); ImportWizardUtil::clearFileAndRelatedDataFromImport($import); } } echo CJSON::encode(array($fileUploadData)); Yii::app()->end(0, false); }