public function testCreate() { // Login as root $model = new LoginForm('RegularLogin'); $model->username = "******"; $model->password = "******"; $this->assertTrue($model->login()); // Create temp file $tmpfile1 = tempnam(Yii::app()->getRuntimePath(), "test"); $tmpfile = $tmpfile1 . '.zip'; $f = fopen($tmpfile, 'w'); fwrite($f, 'MZP1234567'); fclose($f); // Create new model $model = new CrashReport(); $model->project_id = 1; $model->ignoreFileAttachmentErrors = true; // Ignore errors $model->fileAttachment = new CUploadedFile('test.zip', $tmpfile, 'application/octet-stream', 5, 0); $model->md5 = md5_file($tmpfile); $model->emailfrom = '*****@*****.**'; $model->crashguid = '88fbb0ca-899b-4921-87a5-8e0df65d14cf'; // Apply changes - should fail (guid already exists) $saved = $model->save(); $this->assertFalse($saved); $model->crashguid = '00fbb0ca-899b-4921-87a5-8e0df65d14cf'; // Apply changes $saved = $model->save(); $this->assertTrue($saved); // Remove temp file unlink($tmpfile); unlink($tmpfile1); }
/** * This action allows user to upload a crash report ZIP file using * an external uploader tool. * @throws Exception */ public function actionUploadExternal() { // Create new AR model $report = new CrashReport(); // Start db transaction $transaction = $report->dbConnection->beginTransaction(); // Fill model attributes try { if (isset($_POST['crashrptver'])) { $report->crashrptver = $_POST['crashrptver']; } if (isset($_POST['crashguid'])) { $report->crashguid = $_POST['crashguid']; } if (isset($_POST['appname'])) { $projectName = $_POST['appname']; $project = Project::model()->find('name=:name', array(':name' => $projectName)); if ($project === null) { throw new Exception('Such a project name not found'); } $report->project_id = $project->id; } // Get file attachment if (array_key_exists("crashrpt", $_FILES)) { $report->fileAttachment = new CUploadedFile($_FILES['crashrpt']['name'], $_FILES['crashrpt']['tmp_name'], $_FILES['crashrpt']['type'], $_FILES['crashrpt']['size'], $_FILES['crashrpt']['error']); } if (isset($_POST['appversion'])) { $report->appversion = $_POST['appversion']; } if (isset($_POST['md5'])) { $report->md5 = $_POST['md5']; } if (isset($_POST['emailfrom'])) { $report->emailfrom = $_POST['emailfrom']; } if (isset($_POST['description'])) { $report->description = $_POST['description']; } if (isset($_POST['exceptionmodule'])) { $report->exceptionmodule = $_POST['exceptionmodule']; } if (isset($_POST['exceptionmodulebase'])) { $report->exceptionmodulebase = $_POST['exceptionmodulebase']; } if (isset($_POST['exceptionaddress'])) { $report->exceptionaddress = $_POST['exceptionaddress']; } // This will create new record in the db table if ($report->save()) { // Commit db transaction $transaction->commit(); } } catch (Exception $e) { $transaction->rollBack(); $report->addError('crashrpt', 'Exception caught: ' . $e->getMessage()); } $this->renderPartial('_upload', array('model' => $report)); }
/** * Imports crash report files for certain project version. * @param string $dirName Directory name. * @param string $projectId Project ID. * @param string $projVer Project version. * @return integer count of crash report imported; or -1 on error. */ private function importCrashReports($dirName, $projectId, $projVerId) { //echo 'Importing crash report files from dir: '.$dirName.'\n'; // Get file list in the directory $fileList = scandir($dirName); if ($fileList == false) { Yii::log('Directory name is invalid: ' . $dirName, 'error'); return -1; // Error } // Walk through files foreach ($fileList as $index => $file) { // Get abs path $path = $dirName . '/' . $file; // Strip file parts $path_parts = pathinfo($path); if ($file != '.' && $file != '..' && is_file($path) && strtolower($path_parts['extension']) == 'zip') { //echo 'Importing crash report file: '.$path.'\n'; // Create new AR record $crashReport = new CrashReport(); $crashReport->project_id = $projectId; $crashReport->appversion = $projVerId; $crashReport->fileAttachment = new CUploadedFile($file, $path, 'application/zip', filesize($path), ''); // The following is to copy attachment file correctly. $crashReport->fileAttachmentIsUploaded = false; // Save changes to database if (!$crashReport->save()) { Yii::log('Could not import crash report file:' . $path, 'error'); $errors = $crashReport->getErrors(); //var_dump($errors); } else { // Increment counter $this->_importedCrashReportsCount++; } } } // Done return $this->_importedCrashReportsCount; }