# frozen_string_literal: true # Redmine - project management software # Copyright (C) 2006- Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. require_relative '../../../../test_helper' require 'redmine/field_format' module Redmine::FieldFormat class ProgressbarFormatTest < ActionView::TestCase def setup @field = IssueCustomField.new(name: 'ProgressbarTest', field_format: 'progressbar') @format = Redmine::FieldFormat::ProgressbarFormat.instance end def test_validate_invalid_value cv = CustomValue.new(custom_field: @field, value: '120') assert_include ::I18n.t('activerecord.errors.messages.invalid'), @format.validate_custom_value(cv) end def test_validate_numericality cv = CustomValue.new(custom_field: @field, value: 'abc') assert_include ::I18n.t('activerecord.errors.messages.not_a_number'), @format.validate_custom_value(cv) end def test_cast_value_clamping assert_equal 0, @field.cast_value('-10') assert_equal 0, @field.cast_value('0') assert_equal 50, @field.cast_value('50') assert_equal 100, @field.cast_value('120') end def test_empty_value assert_nil @field.cast_value('') end def test_totalable_support assert_not @format.totalable_supported? end def test_validate_non_numeric_value_should_fail assert_include ::I18n.t('activerecord.errors.messages.not_a_number'), @format.validate_single_value(@field, "abc") end def test_validate_negative_value_should_fail assert_include ::I18n.t('activerecord.errors.messages.invalid'), @format.validate_single_value(@field, "-10") end def test_validate_value_above_100_should_fail assert_include ::I18n.t('activerecord.errors.messages.invalid'), @format.validate_single_value(@field, "150") end def test_validate_valid_value_should_pass assert_empty @format.validate_single_value(@field, "50") assert_empty @format.validate_single_value(@field, "0") assert_empty @format.validate_single_value(@field, "100") end def test_validate_blank_value_should_pass assert_empty @format.validate_single_value(@field, "") end def test_query_filter_options options = @format.query_filter_options(@field, nil) assert_equal :integer, options[:type] end def test_default_ratio_interval_should_be_default_issue_done_ratio_interval @field.save assert_equal 10, @field.ratio_interval end def test_ratio_interval @field.update(ratio_interval: 5) assert_equal 5, @field.ratio_interval end def test_edit_tag_possible_values_with_ratio_interval [5, 10].each do |ratio_interval| @field.update(ratio_interval: ratio_interval) value = CustomValue.new(custom_field: @field, value: '90') tag = @field.format.edit_tag(self, 'id', 'name', value) assert_select_in tag, 'select' do assert_select 'option', 100 / ratio_interval + 1 end end end def test_bulk_edit_tag_possible_values_with_ratio_interval [5, 10].each do |ratio_interval| @field.update(ratio_interval: ratio_interval) value = CustomValue.new(custom_field: @field, value: '90') objects = [Issue.new, Issue.new] tag = @field.format.bulk_edit_tag(self, 'id', 'name', @field, objects, value) assert_select_in tag, 'select' do |select| assert_select select.first, 'option', 100 / ratio_interval + 2 end end end def test_formatted_value_with_html_true expected = progress_bar(50) formatted = @format.formatted_value(self, @field, 50, Issue.new, true) assert_equal expected, formatted assert formatted.html_safe? end def test_formatted_value_with_html_false formatted = @format.formatted_value(self, @field, 50, Issue.new, false) assert_equal '50', formatted end end end ef='#n20'>20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package convert
import (
"context"
"strings"
issues_model "code.gitea.io/gitea/models/issues"
user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
)
// ToPullReview convert a review to api format
func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model.User) (*api.PullReview, error) {
if err := r.LoadAttributes(ctx); err != nil {
if !user_model.IsErrUserNotExist(err) {
return nil, err
}
r.Reviewer = user_model.NewGhostUser()
}
apiTeam, err := ToTeam(ctx, r.ReviewerTeam)
if err != nil {
return nil, err
}
result := &api.PullReview{
ID: r.ID,
Reviewer: ToUser(ctx, r.Reviewer, doer),
ReviewerTeam: apiTeam,
State: api.ReviewStateUnknown,
Body: r.Content,
CommitID: r.CommitID,
Stale: r.Stale,
Official: r.Official,
Dismissed: r.Dismissed,
CodeCommentsCount: r.GetCodeCommentsCount(ctx),
Submitted: r.CreatedUnix.AsTime(),
Updated: r.UpdatedUnix.AsTime(),
HTMLURL: r.HTMLURL(ctx),
HTMLPullURL: r.Issue.HTMLURL(),
}
switch r.Type {
case issues_model.ReviewTypeApprove:
result.State = api.ReviewStateApproved
case issues_model.ReviewTypeReject:
result.State = api.ReviewStateRequestChanges
case issues_model.ReviewTypeComment:
result.State = api.ReviewStateComment
case issues_model.ReviewTypePending:
result.State = api.ReviewStatePending
case issues_model.ReviewTypeRequest:
result.State = api.ReviewStateRequestReview
}
return result, nil
}
// ToPullReviewList convert a list of review to it's api format
func ToPullReviewList(ctx context.Context, rl []*issues_model.Review, doer *user_model.User) ([]*api.PullReview, error) {
result := make([]*api.PullReview, 0, len(rl))
for i := range rl {
// show pending reviews only for the user who created them
if rl[i].Type == issues_model.ReviewTypePending && !(doer.IsAdmin || doer.ID == rl[i].ReviewerID) {
continue
}
r, err := ToPullReview(ctx, rl[i], doer)
if err != nil {
return nil, err
}
result = append(result, r)
}
return result, nil
}
// ToPullReviewCommentList convert the CodeComments of an review to it's api format
func ToPullReviewCommentList(ctx context.Context, review *issues_model.Review, doer *user_model.User) ([]*api.PullReviewComment, error) {
if err := review.LoadAttributes(ctx); err != nil {
if !user_model.IsErrUserNotExist(err) {
return nil, err
}
review.Reviewer = user_model.NewGhostUser()
}
apiComments := make([]*api.PullReviewComment, 0, len(review.CodeComments))
for _, lines := range review.CodeComments {
for _, comments := range lines {
for _, comment := range comments {
apiComment := &api.PullReviewComment{
ID: comment.ID,
Body: comment.Content,
Poster: ToUser(ctx, comment.Poster, doer),
Resolver: ToUser(ctx, comment.ResolveDoer, doer),
ReviewID: review.ID,
Created: comment.CreatedUnix.AsTime(),
Updated: comment.UpdatedUnix.AsTime(),
Path: comment.TreePath,
CommitID: comment.CommitSHA,
OrigCommitID: comment.OldRef,
DiffHunk: patch2diff(comment.Patch),
HTMLURL: comment.HTMLURL(ctx),
HTMLPullURL: review.Issue.HTMLURL(),
}
if comment.Line < 0 {
apiComment.OldLineNum = comment.UnsignedLine()
} else {
apiComment.LineNum = comment.UnsignedLine()
}
apiComments = append(apiComments, apiComment)
}
}
}
return apiComments, nil
}
func patch2diff(patch string) string {
split := strings.Split(patch, "\n@@")
if len(split) == 2 {
return "@@" + split[1]
}
return ""
}