public function testDumpFileAttachmentContent() { // Find $model = BugAttachment::model()->findByPk(1); $this->assertTrue($model != null); // Create temp file for output $outFile = tempnam(Yii::app()->getRuntimePath(), "test"); $model->dumpFileAttachmentContent($outFile); // Ensure the file exists $this->assertTrue(file_exists($outFile)); // Ensure file size is not 0 $this->assertTrue(filesize($outFile) > 0); // Delete file @unlink($outFile); }
/** * Downloads a bug attachment file. * @param type $id * @throws CHttpException */ public function actionDownloadAttachment($id) { $bugAttachment = BugAttachment::model()->findByPk($id); if ($bugAttachment === null) { throw new CHttpException(403, 'Invalid request'); } // Get the bug this attachment belongs to $bug = $bugAttachment->bugChange->bug; // Check that user is authorized to access this model $this->checkAuthorization($bug); // Dump contents of the attachment $bugAttachment->dumpFileAttachmentContent(); }
/** * Returns count of bug attachment files in this project. * @param integer $totalFileSize On output, receives the total size in bytes of files. * @param integer $percentOfDiskQuota On output, receives percent of disk quota. * @param integer $appver Optional. If specified, count of files is calculated for the given version. * @return integer count of bug attachment files. */ public function getBugAttachmentCount(&$totalFileSize, &$percentOfDiskQuota, $appver = Project::PROJ_VER_ALL) { // Find all bug attachment files belonging to this project and having // $appver application version. $criteria = new CDbCriteria(); $criteria->compare('b.project_id', $this->id, false, 'AND'); if ($appver != Project::PROJ_VER_ALL) { $criteria->compare('b.appversion_id', $appver, false, 'AND'); } $criteria->join = 'LEFT JOIN {{bug_change}} bc ON t.bug_change_id=bc.id'; $criteria->join .= ' LEFT JOIN {{bug}} b ON bc.bug_id=b.id'; $bugAttachments = BugAttachment::model()->findAll($criteria); // Calculate count of bug attachments $count = count($bugAttachments); // Calculate total file size $totalFileSize = 0; foreach ($bugAttachments as $bugAttachment) { $totalFileSize += $bugAttachment->filesize; } // Calc percent of disk quota if ($this->bug_attachment_files_disc_quota <= 0) { $percentOfDiskQuota = -1; } else { $percentOfDiskQuota = 100 * $totalFileSize / ($this->bug_attachment_files_disc_quota * 1024 * 1024); } // Return file count return $count; }
/** * This method applies changes to model fields. Used when * changing/commenting bug. * @param array $attributes new (changed) model attributes. * @return boolean true on success. */ public function change($attributes) { // Apply 'change' scenario. $this->scenario = 'change'; // Remove "empty" attributes foreach ($attributes as $name => $val) { if (trim($val) == '') { unset($attributes[$name]); } } // Begin DB transaction $transaction = Yii::app()->db->beginTransaction(); try { $bugChanged = false; // Check if title has changed if (array_key_exists('summary', $attributes)) { if ($this->summary != $attributes['summary']) { $this->summary = $attributes['summary']; $bugChanged = true; } } // Check if crashgroups changed if (array_key_exists('crashgroups', $attributes)) { $my_crashgroups = MiscHelpers::splitNumberList($this->crashgroups); $their_crashgroups = MiscHelpers::splitNumberList($attributes['crashgroups']); $diff_crashgroups = array_merge(array_diff($my_crashgroups, $their_crashgroups), array_diff($their_crashgroups, $my_crashgroups)); if (count($diff_crashgroups) != 0) { $this->crashgroups = $attributes['crashgroups']; $bugChanged = true; } } // Check if crashreports changed if (array_key_exists('crashreports', $attributes)) { $my_crashreports = MiscHelpers::splitNumberList($this->crashreports); $their_crashreports = MiscHelpers::splitNumberList($attributes['crashreports']); $diff_crashreports = array_merge(array_diff($my_crashreports, $their_crashreports), array_diff($their_crashreports, $my_crashreports)); if (count($diff_crashreports) != 0) { $this->crashreports = $attributes['crashreports']; $bugChanged = true; } } // Create new BugChange model $bugChange = new BugChange(); $bugChange->bug_id = $this->id; // bug id $bugChange->user_id = Yii::app()->user->id; // logged-in user $bugChange->timestamp = time(); // current time $bugChange->flags = 0; // Check if comment presents if (array_key_exists('comment', $attributes)) { $this->comment = $attributes['comment']; // Create new comment record $bugComment = new BugComment(); $bugComment->text = $attributes['comment']; // Save comment record if (!$bugComment->save()) { $errors = $bugComment->getErrors(); foreach ($errors as $fieldName => $fieldErrors) { $this->addErrors('comment', $fieldErrors); } throw new Exception('Error saving comment.'); } // Set link between bug change record and comment record $bugChange->comment_id = $bugComment->id; } // Check file attachment $bugAttachment = null; if (isset($this->fileAttachment) && $this->fileAttachment instanceof CUploadedFile) { // Create attachment record $bugAttachment = new BugAttachment(); $bugAttachment->fileAttachment = $this->fileAttachment; } // Create new bug status change model $bugStatusChange = new BugStatusChange(); // Check if some of bug attribute values have changed $changed = false; foreach ($attributes as $name => $val) { // Check if attribute has changed if (array_key_exists($name, $bugStatusChange->attributes) && $this->{$name} != $val) { $changed = true; // Attribute has changed $bugStatusChange->{$name} = $val; $this->{$name} = $val; } } if ($changed) { if (!$bugStatusChange->save()) { $errors = $bugStatusChange->getErrors(); throw new Exception('Error saving bug status change.'); } $bugChange->status_change_id = $bugStatusChange->id; } // Check if at least something (status fields, attachment or comment) // have changed $someFieldsChanged = isset($bugChange->status_change_id) || $bugAttachment != null || isset($bugChange->comment_id); if (!$someFieldsChanged && !$bugChanged) { $this->addError('comment', 'At least one field should be changed.'); throw new Exception('Nothing changed.'); } // Save changes to bug record if (!isset($this->fileAttachment)) { $this->fileAttachment = ''; } if (!$this->save()) { throw new Exception('Error saving bug.'); } // Save bug change record if ($someFieldsChanged && !$bugChange->save()) { $errors = $bugChange->getErrors(); foreach ($errors as $fieldName => $fieldErrors) { foreach ($fieldErrors as $error) { Yii::log('Error saving bug: ' . $error, 'error'); } $this->addErrors('comment', $fieldErrors); } throw new Exception('Error saving bug change.'); } // Save file attachment if ($bugAttachment != null) { $bugAttachment->bug_change_id = $bugChange->id; if (!$bugAttachment->save()) { $errors = $bugAttachment->getErrors(); foreach ($errors as $fieldName => $fieldErrors) { $this->addErrors('fileAttachment', $fieldErrors); } throw new Exception('Error saving bug attachemnt.'); } } // Reset comment and file attachment $this->comment = ''; unset($this->fileAttachment); // Update crash reports and crash groups associations $this->updateAssociatedCrashReportsAndCrashGroups(); // Commit transaction $transaction->commit(); } catch (Exception $e) { // Roll back transaction $transaction->rollBack(); // Add a message to log. Yii::log('An exception caught: ' . $e->getMessage(), "error"); return false; } // Send notification email $this->sendBugChangeNotificationEmail($bugChange); // Success. return true; }