Skip to content

Commit

Permalink
Add tests for text_message #17
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Jan 26, 2022
1 parent 369b63d commit 9bf6147
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
5 changes: 3 additions & 2 deletions lib/redmine_issue_assign_notice/issue_hook_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class IssueHookListener < Redmine::Hook::Listener

def initialize
@client = NoticeClient.new
@message_helper = MessageHelper.new
Rails.logger.debug "[RedmineIssueAssignNotice] IssueHookListener#initialize"
end

Expand Down Expand Up @@ -52,9 +53,9 @@ def notice(issue, old_assgined_to, new_assgined_to, note, author)
end

formatter = RedmineIssueAssignNotice::Formatter.create notice_url
textMessage = TextMessage.new(formatter)
message_creater = TextMessage.new(@message_helper, formatter)

message = textMessage.create(issue, old_assgined_to, new_assgined_to, note, author)
message = message_creater.create(issue, old_assgined_to, new_assgined_to, note, author)

Rails.logger.debug "[RedmineIssueAssignNotice] IssueHookListener#notice message:#{message}"

Expand Down
4 changes: 2 additions & 2 deletions lib/redmine_issue_assign_notice/message_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module RedmineIssueAssignNotice
class MessageHelper
def self.mention_target(assgined_to, author)
def mention_target(assgined_to, author)

if assgined_to.nil? ||
Setting.plugin_redmine_issue_assign_notice['mention_to_assignee'] != '1' ||
Expand All @@ -17,7 +17,7 @@ def self.mention_target(assgined_to, author)
noteice_field.value
end

def self.issue_url(issue)
def issue_url(issue)
"#{Setting.protocol}://#{Setting.host_name}/issues/#{issue.id}"
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/redmine_issue_assign_notice/text_message.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module RedmineIssueAssignNotice
class TextMessage
def initialize(formatter)
def initialize(helper, formatter)
@helper = helper
@formatter = formatter
end

def create(issue, old_assgined_to, new_assgined_to, note, author)

text = ""

mention_to = MessageHelper.mention_target(new_assgined_to, author)
mention_to = @helper.mention_target(new_assgined_to, author)
if mention_to.present?
text << @formatter.mention(mention_to)
text << " "
Expand All @@ -17,7 +18,7 @@ def create(issue, old_assgined_to, new_assgined_to, note, author)
text << "Assign changed from #{@formatter.user_name old_assgined_to} to #{@formatter.user_name new_assgined_to}"
text << @formatter.change_line
text << "[#{@formatter.escape issue.project}] "
text << @formatter.link("#{issue.tracker} ##{issue.id}", MessageHelper.issue_url(issue))
text << @formatter.link("#{issue.tracker} ##{issue.id}", @helper.issue_url(issue))
text << " #{@formatter.escape issue.subject} (#{@formatter.escape issue.status})"
text << @formatter.change_line
text << @formatter.trimming(note)
Expand Down
63 changes: 63 additions & 0 deletions test/unit/text_message_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require File.expand_path('../../test_helper', __FILE__)
require File.expand_path('../../../lib/redmine_issue_assign_notice/text_message', __FILE__)

class TextMesaageTest < ActiveSupport::TestCase
fixtures :users, :projects, :trackers, :issues, :issue_statuses

def test_create

# ARRANGE
message_helper = RedmineIssueAssignNotice::MessageHelper.new
formatter = RedmineIssueAssignNotice::Formatter.create ""
message_creater = RedmineIssueAssignNotice::TextMessage.new(message_helper, formatter)

issue = Issue.find(1)
old_assgined_to = User.find(1)
new_assgined_to = User.find(2)
note = "xxxxxxx"
author = User.find(1)

# ACTION
message = message_creater.create(issue, old_assgined_to, new_assgined_to, note, author)

# ASSERT
assert_equal(
{
:text =>
"Assign changed from _Redmine Admin_ to _John Smith_\n" +
"[eCookbook] <http://localhost:3000/issues/1|Bug #1> Cannot print recipes (New)\n" +
"xxxxxxx"
},
message)
end

def test_create_mention

# ARRANGE
message_helper = RedmineIssueAssignNotice::MessageHelper.new
formatter = RedmineIssueAssignNotice::Formatter.create ""
message_creater = RedmineIssueAssignNotice::TextMessage.new(message_helper, formatter)

issue = Issue.find(1)
old_assgined_to = nil
new_assgined_to = User.find(2)
note = "xxxxxxx"
author = User.find(1)

message_helper.stubs(:mention_target).returns("user")

# ACTION
message = message_creater.create(issue, old_assgined_to, new_assgined_to, note, author)

# ASSERT
assert_equal(
{
:text =>
"@user Assign changed from _[none]_ to _John Smith_\n" +
"[eCookbook] <http://localhost:3000/issues/1|Bug #1> Cannot print recipes (New)\n" +
"xxxxxxx"
},
message)
end

end

0 comments on commit 9bf6147

Please sign in to comment.