public function testAttributeLabels() { $model = new BugStatusChange(); $attrLabels = $model->attributeLabels(); $this->assertTrue($attrLabels['bug_id'] == 'Bug'); $this->assertTrue($attrLabels['merged_into'] == 'Merged Into'); }
/** * 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; }