comparison test/unit/changeset_test.rb @ 1526:404aa68d4227

Merge from live branch
author Chris Cannam
date Thu, 11 Sep 2014 12:46:20 +0100
parents dffacf8a6908
children
comparison
equal deleted inserted replaced
1493:a5f2bdf3b486 1526:404aa68d4227
1 # encoding: utf-8 1 # encoding: utf-8
2 # 2 #
3 # Redmine - project management software 3 # Redmine - project management software
4 # Copyright (C) 2006-2012 Jean-Philippe Lang 4 # Copyright (C) 2006-2014 Jean-Philippe Lang
5 # 5 #
6 # This program is free software; you can redistribute it and/or 6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License 7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2 8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version. 9 # of the License, or (at your option) any later version.
28 :users, :members, :member_roles, :trackers, 28 :users, :members, :member_roles, :trackers,
29 :enabled_modules, :roles 29 :enabled_modules, :roles
30 30
31 def test_ref_keywords_any 31 def test_ref_keywords_any
32 ActionMailer::Base.deliveries.clear 32 ActionMailer::Base.deliveries.clear
33 Setting.commit_fix_status_id = IssueStatus.find( 33 Setting.commit_ref_keywords = '*'
34 :first, :conditions => ["is_closed = ?", true]).id 34 Setting.commit_update_keywords = [{'keywords' => 'fixes , closes', 'status_id' => '5', 'done_ratio' => '90'}]
35 Setting.commit_fix_done_ratio = '90'
36 Setting.commit_ref_keywords = '*'
37 Setting.commit_fix_keywords = 'fixes , closes'
38 35
39 c = Changeset.new(:repository => Project.find(1).repository, 36 c = Changeset.new(:repository => Project.find(1).repository,
40 :committed_on => Time.now, 37 :committed_on => Time.now,
41 :comments => 'New commit (#2). Fixes #1', 38 :comments => 'New commit (#2). Fixes #1',
42 :revision => '12345') 39 :revision => '12345')
48 assert_equal 1, ActionMailer::Base.deliveries.size 45 assert_equal 1, ActionMailer::Base.deliveries.size
49 end 46 end
50 47
51 def test_ref_keywords 48 def test_ref_keywords
52 Setting.commit_ref_keywords = 'refs' 49 Setting.commit_ref_keywords = 'refs'
53 Setting.commit_fix_keywords = '' 50 Setting.commit_update_keywords = ''
54 c = Changeset.new(:repository => Project.find(1).repository, 51 c = Changeset.new(:repository => Project.find(1).repository,
55 :committed_on => Time.now, 52 :committed_on => Time.now,
56 :comments => 'Ignores #2. Refs #1', 53 :comments => 'Ignores #2. Refs #1',
57 :revision => '12345') 54 :revision => '12345')
58 assert c.save 55 assert c.save
59 assert_equal [1], c.issue_ids.sort 56 assert_equal [1], c.issue_ids.sort
60 end 57 end
61 58
62 def test_ref_keywords_any_only 59 def test_ref_keywords_any_only
63 Setting.commit_ref_keywords = '*' 60 Setting.commit_ref_keywords = '*'
64 Setting.commit_fix_keywords = '' 61 Setting.commit_update_keywords = ''
65 c = Changeset.new(:repository => Project.find(1).repository, 62 c = Changeset.new(:repository => Project.find(1).repository,
66 :committed_on => Time.now, 63 :committed_on => Time.now,
67 :comments => 'Ignores #2. Refs #1', 64 :comments => 'Ignores #2. Refs #1',
68 :revision => '12345') 65 :revision => '12345')
69 assert c.save 66 assert c.save
97 assert_difference 'TimeEntry.count' do 94 assert_difference 'TimeEntry.count' do
98 c.scan_comment_for_issue_ids 95 c.scan_comment_for_issue_ids
99 end 96 end
100 assert_equal [1], c.issue_ids.sort 97 assert_equal [1], c.issue_ids.sort
101 98
102 time = TimeEntry.first(:order => 'id desc') 99 time = TimeEntry.order('id desc').first
103 assert_equal 1, time.issue_id 100 assert_equal 1, time.issue_id
104 assert_equal 1, time.project_id 101 assert_equal 1, time.project_id
105 assert_equal 2, time.user_id 102 assert_equal 2, time.user_id
106 assert_equal expected_hours, time.hours, 103 assert_equal expected_hours, time.hours,
107 "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}" 104 "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}"
111 "r520 was expected in time_entry comments: #{time.comments}" 108 "r520 was expected in time_entry comments: #{time.comments}"
112 end 109 end
113 end 110 end
114 111
115 def test_ref_keywords_closing_with_timelog 112 def test_ref_keywords_closing_with_timelog
116 Setting.commit_fix_status_id = IssueStatus.find( 113 Setting.commit_ref_keywords = '*'
117 :first, :conditions => ["is_closed = ?", true]).id 114 Setting.commit_update_keywords = [{'keywords' => 'fixes , closes',
118 Setting.commit_ref_keywords = '*' 115 'status_id' => IssueStatus.where(:is_closed => true).first.id.to_s}]
119 Setting.commit_fix_keywords = 'fixes , closes'
120 Setting.commit_logtime_enabled = '1' 116 Setting.commit_logtime_enabled = '1'
121 117
122 c = Changeset.new(:repository => Project.find(1).repository, 118 c = Changeset.new(:repository => Project.find(1).repository,
123 :committed_on => Time.now, 119 :committed_on => Time.now,
124 :comments => 'This is a comment. Fixes #1 @4.5, #2 @1', 120 :comments => 'This is a comment. Fixes #1 @4.5, #2 @1',
129 125
130 assert_equal [1, 2], c.issue_ids.sort 126 assert_equal [1, 2], c.issue_ids.sort
131 assert Issue.find(1).closed? 127 assert Issue.find(1).closed?
132 assert Issue.find(2).closed? 128 assert Issue.find(2).closed?
133 129
134 times = TimeEntry.all(:order => 'id desc', :limit => 2) 130 times = TimeEntry.order('id desc').limit(2)
135 assert_equal [1, 2], times.collect(&:issue_id).sort 131 assert_equal [1, 2], times.collect(&:issue_id).sort
136 end 132 end
137 133
138 def test_ref_keywords_any_line_start 134 def test_ref_keywords_any_line_start
139 Setting.commit_ref_keywords = '*' 135 Setting.commit_ref_keywords = '*'
161 :committed_on => Time.now, 157 :committed_on => Time.now,
162 :comments => '[#1 #2, #3] Worked on these', 158 :comments => '[#1 #2, #3] Worked on these',
163 :revision => '12345') 159 :revision => '12345')
164 assert c.save 160 assert c.save
165 assert_equal [1,2,3], c.issue_ids.sort 161 assert_equal [1,2,3], c.issue_ids.sort
162 end
163
164 def test_update_keywords_with_multiple_rules
165 with_settings :commit_update_keywords => [
166 {'keywords' => 'fixes, closes', 'status_id' => '5'},
167 {'keywords' => 'resolves', 'status_id' => '3'}
168 ] do
169
170 issue1 = Issue.generate!
171 issue2 = Issue.generate!
172 Changeset.generate!(:comments => "Closes ##{issue1.id}\nResolves ##{issue2.id}")
173 assert_equal 5, issue1.reload.status_id
174 assert_equal 3, issue2.reload.status_id
175 end
176 end
177
178 def test_update_keywords_with_multiple_rules_should_match_tracker
179 with_settings :commit_update_keywords => [
180 {'keywords' => 'fixes', 'status_id' => '5', 'if_tracker_id' => '2'},
181 {'keywords' => 'fixes', 'status_id' => '3', 'if_tracker_id' => ''}
182 ] do
183
184 issue1 = Issue.generate!(:tracker_id => 2)
185 issue2 = Issue.generate!
186 Changeset.generate!(:comments => "Fixes ##{issue1.id}, ##{issue2.id}")
187 assert_equal 5, issue1.reload.status_id
188 assert_equal 3, issue2.reload.status_id
189 end
190 end
191
192 def test_update_keywords_with_multiple_rules_and_no_match
193 with_settings :commit_update_keywords => [
194 {'keywords' => 'fixes', 'status_id' => '5', 'if_tracker_id' => '2'},
195 {'keywords' => 'fixes', 'status_id' => '3', 'if_tracker_id' => '3'}
196 ] do
197
198 issue1 = Issue.generate!(:tracker_id => 2)
199 issue2 = Issue.generate!
200 Changeset.generate!(:comments => "Fixes ##{issue1.id}, ##{issue2.id}")
201 assert_equal 5, issue1.reload.status_id
202 assert_equal 1, issue2.reload.status_id # no updates
203 end
166 end 204 end
167 205
168 def test_commit_referencing_a_subproject_issue 206 def test_commit_referencing_a_subproject_issue
169 c = Changeset.new(:repository => Project.find(1).repository, 207 c = Changeset.new(:repository => Project.find(1).repository,
170 :committed_on => Time.now, 208 :committed_on => Time.now,
174 assert_equal [5], c.issue_ids.sort 212 assert_equal [5], c.issue_ids.sort
175 assert c.issues.first.project != c.project 213 assert c.issues.first.project != c.project
176 end 214 end
177 215
178 def test_commit_closing_a_subproject_issue 216 def test_commit_closing_a_subproject_issue
179 with_settings :commit_fix_status_id => 5, :commit_fix_keywords => 'closes', 217 with_settings :commit_update_keywords => [{'keywords' => 'closes', 'status_id' => '5'}],
180 :default_language => 'en' do 218 :default_language => 'en' do
181 issue = Issue.find(5) 219 issue = Issue.find(5)
182 assert !issue.closed? 220 assert !issue.closed?
183 assert_difference 'Journal.count' do 221 assert_difference 'Journal.count' do
184 c = Changeset.new(:repository => Project.find(1).repository, 222 c = Changeset.new(:repository => Project.find(1).repository,
186 :comments => 'closes #5, a subproject issue', 224 :comments => 'closes #5, a subproject issue',
187 :revision => '12345') 225 :revision => '12345')
188 assert c.save 226 assert c.save
189 end 227 end
190 assert issue.reload.closed? 228 assert issue.reload.closed?
191 journal = Journal.first(:order => 'id DESC') 229 journal = Journal.order('id DESC').first
192 assert_equal issue, journal.issue 230 assert_equal issue, journal.issue
193 assert_include "Applied in changeset ecookbook:r12345.", journal.notes 231 assert_include "Applied in changeset ecookbook:r12345.", journal.notes
194 end 232 end
195 end 233 end
196 234
210 248
211 def test_commit_referencing_a_project_with_commit_cross_project_ref_disabled 249 def test_commit_referencing_a_project_with_commit_cross_project_ref_disabled
212 r = Repository::Subversion.create!( 250 r = Repository::Subversion.create!(
213 :project => Project.find(3), 251 :project => Project.find(3),
214 :url => 'svn://localhost/test') 252 :url => 'svn://localhost/test')
215
216 with_settings :commit_cross_project_ref => '0' do 253 with_settings :commit_cross_project_ref => '0' do
217 c = Changeset.new(:repository => r, 254 c = Changeset.new(:repository => r,
218 :committed_on => Time.now, 255 :committed_on => Time.now,
219 :comments => 'refs #4, an issue of a different project', 256 :comments => 'refs #4, an issue of a different project',
220 :revision => '12345') 257 :revision => '12345')
225 262
226 def test_commit_referencing_a_project_with_commit_cross_project_ref_enabled 263 def test_commit_referencing_a_project_with_commit_cross_project_ref_enabled
227 r = Repository::Subversion.create!( 264 r = Repository::Subversion.create!(
228 :project => Project.find(3), 265 :project => Project.find(3),
229 :url => 'svn://localhost/test') 266 :url => 'svn://localhost/test')
230
231 with_settings :commit_cross_project_ref => '1' do 267 with_settings :commit_cross_project_ref => '1' do
232 c = Changeset.new(:repository => r, 268 c = Changeset.new(:repository => r,
233 :committed_on => Time.now, 269 :committed_on => Time.now,
234 :comments => 'refs #4, an issue of a different project', 270 :comments => 'refs #4, an issue of a different project',
235 :revision => '12345') 271 :revision => '12345')
236 assert c.save 272 assert c.save
237 assert_equal [4], c.issue_ids 273 assert_equal [4], c.issue_ids
238 end 274 end
239 end 275 end
240 276
277 def test_old_commits_should_not_update_issues_nor_log_time
278 Setting.commit_ref_keywords = '*'
279 Setting.commit_update_keywords = {'fixes , closes' => {'status_id' => '5', 'done_ratio' => '90'}}
280 Setting.commit_logtime_enabled = '1'
281
282 repository = Project.find(1).repository
283 repository.created_on = Time.now
284 repository.save!
285
286 c = Changeset.new(:repository => repository,
287 :committed_on => 1.month.ago,
288 :comments => 'New commit (#2). Fixes #1 @1h',
289 :revision => '12345')
290 assert_no_difference 'TimeEntry.count' do
291 assert c.save
292 end
293 assert_equal [1, 2], c.issue_ids.sort
294 issue = Issue.find(1)
295 assert_equal 1, issue.status_id
296 assert_equal 0, issue.done_ratio
297 end
298
241 def test_text_tag_revision 299 def test_text_tag_revision
242 c = Changeset.new(:revision => '520') 300 c = Changeset.new(:revision => '520')
243 assert_equal 'r520', c.text_tag 301 assert_equal 'r520', c.text_tag
244 end 302 end
245 303
256 def test_text_tag_revision_with_repository_identifier 314 def test_text_tag_revision_with_repository_identifier
257 r = Repository::Subversion.create!( 315 r = Repository::Subversion.create!(
258 :project_id => 1, 316 :project_id => 1,
259 :url => 'svn://localhost/test', 317 :url => 'svn://localhost/test',
260 :identifier => 'documents') 318 :identifier => 'documents')
261
262 c = Changeset.new(:revision => '520', :repository => r) 319 c = Changeset.new(:revision => '520', :repository => r)
263 assert_equal 'documents|r520', c.text_tag 320 assert_equal 'documents|r520', c.text_tag
264 assert_equal 'ecookbook:documents|r520', c.text_tag(Project.find(2)) 321 assert_equal 'ecookbook:documents|r520', c.text_tag(Project.find(2))
265 end 322 end
266 323
282 end 339 end
283 340
284 def test_text_tag_hash_all_number 341 def test_text_tag_hash_all_number
285 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789') 342 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
286 assert_equal 'commit:0123456789', c.text_tag 343 assert_equal 'commit:0123456789', c.text_tag
344 end
345
346 def test_text_tag_hash_with_repository_identifier
347 r = Repository::Subversion.new(
348 :project_id => 1,
349 :url => 'svn://localhost/test',
350 :identifier => 'documents')
351 c = Changeset.new(:revision => '7234cb27', :scmid => '7234cb27', :repository => r)
352 assert_equal 'commit:documents|7234cb27', c.text_tag
353 assert_equal 'ecookbook:commit:documents|7234cb27', c.text_tag(Project.find(2))
287 end 354 end
288 355
289 def test_previous 356 def test_previous
290 changeset = Changeset.find_by_revision('3') 357 changeset = Changeset.find_by_revision('3')
291 assert_equal Changeset.find_by_revision('2'), changeset.previous 358 assert_equal Changeset.find_by_revision('2'), changeset.previous
467 assert_equal "UTF-8", c.comments.encoding.to_s 534 assert_equal "UTF-8", c.comments.encoding.to_s
468 assert_equal "UTF-8", c.committer.encoding.to_s 535 assert_equal "UTF-8", c.committer.encoding.to_s
469 end 536 end
470 end 537 end
471 538
539 def test_comments_should_accept_more_than_64k
540 c = Changeset.new(:repository => Repository.first,
541 :committed_on => Time.now,
542 :revision => '123',
543 :scmid => '12345',
544 :comments => "a" * 500.kilobyte)
545 assert c.save
546 c.reload
547 assert_equal 500.kilobyte, c.comments.size
548 end
549
472 def test_identifier 550 def test_identifier
473 c = Changeset.find_by_revision('1') 551 c = Changeset.find_by_revision('1')
474 assert_equal c.revision, c.identifier 552 assert_equal c.revision, c.identifier
475 end 553 end
476 end 554 end