annotate test/unit/changeset_test.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents cbce1fd3b1b7
children 5f33065ddc4b
rev   line source
Chris@0 1 # encoding: utf-8
Chris@0 2 #
Chris@0 3 # Redmine - project management software
Chris@909 4 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@0 5 #
Chris@0 6 # This program is free software; you can redistribute it and/or
Chris@0 7 # modify it under the terms of the GNU General Public License
Chris@0 8 # as published by the Free Software Foundation; either version 2
Chris@0 9 # of the License, or (at your option) any later version.
Chris@909 10 #
Chris@0 11 # This program is distributed in the hope that it will be useful,
Chris@0 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 14 # GNU General Public License for more details.
Chris@909 15 #
Chris@0 16 # You should have received a copy of the GNU General Public License
Chris@0 17 # along with this program; if not, write to the Free Software
Chris@0 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 19
Chris@119 20 require File.expand_path('../../test_helper', __FILE__)
Chris@0 21
Chris@0 22 class ChangesetTest < ActiveSupport::TestCase
Chris@441 23 fixtures :projects, :repositories,
Chris@441 24 :issues, :issue_statuses, :issue_categories,
Chris@909 25 :changesets, :changes,
Chris@441 26 :enumerations,
Chris@441 27 :custom_fields, :custom_values,
Chris@441 28 :users, :members, :member_roles, :trackers,
Chris@441 29 :enabled_modules, :roles
Chris@0 30
Chris@0 31 def setup
Chris@0 32 end
Chris@441 33
Chris@0 34 def test_ref_keywords_any
Chris@0 35 ActionMailer::Base.deliveries.clear
Chris@441 36 Setting.commit_fix_status_id = IssueStatus.find(
Chris@441 37 :first, :conditions => ["is_closed = ?", true]).id
Chris@0 38 Setting.commit_fix_done_ratio = '90'
Chris@0 39 Setting.commit_ref_keywords = '*'
Chris@0 40 Setting.commit_fix_keywords = 'fixes , closes'
Chris@441 41
Chris@441 42 c = Changeset.new(:repository => Project.find(1).repository,
Chris@0 43 :committed_on => Time.now,
Chris@909 44 :comments => 'New commit (#2). Fixes #1',
Chris@909 45 :revision => '12345')
Chris@909 46 assert c.save
Chris@0 47 assert_equal [1, 2], c.issue_ids.sort
Chris@0 48 fixed = Issue.find(1)
Chris@0 49 assert fixed.closed?
Chris@0 50 assert_equal 90, fixed.done_ratio
Chris@0 51 assert_equal 1, ActionMailer::Base.deliveries.size
Chris@0 52 end
Chris@441 53
Chris@119 54 def test_ref_keywords
Chris@119 55 Setting.commit_ref_keywords = 'refs'
Chris@119 56 Setting.commit_fix_keywords = ''
Chris@441 57 c = Changeset.new(:repository => Project.find(1).repository,
Chris@119 58 :committed_on => Time.now,
Chris@909 59 :comments => 'Ignores #2. Refs #1',
Chris@909 60 :revision => '12345')
Chris@909 61 assert c.save
Chris@119 62 assert_equal [1], c.issue_ids.sort
Chris@119 63 end
Chris@441 64
Chris@119 65 def test_ref_keywords_any_only
Chris@119 66 Setting.commit_ref_keywords = '*'
Chris@119 67 Setting.commit_fix_keywords = ''
Chris@441 68 c = Changeset.new(:repository => Project.find(1).repository,
Chris@119 69 :committed_on => Time.now,
Chris@909 70 :comments => 'Ignores #2. Refs #1',
Chris@909 71 :revision => '12345')
Chris@909 72 assert c.save
Chris@119 73 assert_equal [1, 2], c.issue_ids.sort
Chris@119 74 end
Chris@441 75
Chris@119 76 def test_ref_keywords_any_with_timelog
Chris@119 77 Setting.commit_ref_keywords = '*'
Chris@119 78 Setting.commit_logtime_enabled = '1'
Chris@119 79
Chris@245 80 {
Chris@245 81 '2' => 2.0,
Chris@245 82 '2h' => 2.0,
Chris@245 83 '2hours' => 2.0,
Chris@245 84 '15m' => 0.25,
Chris@245 85 '15min' => 0.25,
Chris@245 86 '3h15' => 3.25,
Chris@245 87 '3h15m' => 3.25,
Chris@245 88 '3h15min' => 3.25,
Chris@245 89 '3:15' => 3.25,
Chris@245 90 '3.25' => 3.25,
Chris@245 91 '3.25h' => 3.25,
Chris@245 92 '3,25' => 3.25,
Chris@245 93 '3,25h' => 3.25,
Chris@245 94 }.each do |syntax, expected_hours|
Chris@441 95 c = Changeset.new(:repository => Project.find(1).repository,
Chris@245 96 :committed_on => 24.hours.ago,
Chris@441 97 :comments => "Worked on this issue #1 @#{syntax}",
Chris@441 98 :revision => '520',
Chris@441 99 :user => User.find(2))
Chris@245 100 assert_difference 'TimeEntry.count' do
Chris@245 101 c.scan_comment_for_issue_ids
Chris@245 102 end
Chris@245 103 assert_equal [1], c.issue_ids.sort
Chris@441 104
Chris@245 105 time = TimeEntry.first(:order => 'id desc')
Chris@245 106 assert_equal 1, time.issue_id
Chris@245 107 assert_equal 1, time.project_id
Chris@245 108 assert_equal 2, time.user_id
Chris@441 109 assert_equal expected_hours, time.hours,
Chris@441 110 "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}"
Chris@245 111 assert_equal Date.yesterday, time.spent_on
Chris@245 112 assert time.activity.is_default?
Chris@441 113 assert time.comments.include?('r520'),
Chris@441 114 "r520 was expected in time_entry comments: #{time.comments}"
Chris@119 115 end
Chris@119 116 end
Chris@441 117
Chris@119 118 def test_ref_keywords_closing_with_timelog
Chris@441 119 Setting.commit_fix_status_id = IssueStatus.find(
Chris@441 120 :first, :conditions => ["is_closed = ?", true]).id
Chris@119 121 Setting.commit_ref_keywords = '*'
Chris@119 122 Setting.commit_fix_keywords = 'fixes , closes'
Chris@119 123 Setting.commit_logtime_enabled = '1'
Chris@909 124
Chris@441 125 c = Changeset.new(:repository => Project.find(1).repository,
Chris@119 126 :committed_on => Time.now,
Chris@441 127 :comments => 'This is a comment. Fixes #1 @4.5, #2 @1',
Chris@441 128 :user => User.find(2))
Chris@119 129 assert_difference 'TimeEntry.count', 2 do
Chris@119 130 c.scan_comment_for_issue_ids
Chris@119 131 end
Chris@441 132
Chris@119 133 assert_equal [1, 2], c.issue_ids.sort
Chris@119 134 assert Issue.find(1).closed?
Chris@119 135 assert Issue.find(2).closed?
Chris@119 136
Chris@119 137 times = TimeEntry.all(:order => 'id desc', :limit => 2)
Chris@119 138 assert_equal [1, 2], times.collect(&:issue_id).sort
Chris@119 139 end
Chris@441 140
Chris@0 141 def test_ref_keywords_any_line_start
Chris@0 142 Setting.commit_ref_keywords = '*'
Chris@441 143 c = Changeset.new(:repository => Project.find(1).repository,
Chris@0 144 :committed_on => Time.now,
Chris@909 145 :comments => '#1 is the reason of this commit',
Chris@909 146 :revision => '12345')
Chris@909 147 assert c.save
Chris@0 148 assert_equal [1], c.issue_ids.sort
Chris@0 149 end
Chris@0 150
Chris@0 151 def test_ref_keywords_allow_brackets_around_a_issue_number
Chris@0 152 Setting.commit_ref_keywords = '*'
Chris@441 153 c = Changeset.new(:repository => Project.find(1).repository,
Chris@0 154 :committed_on => Time.now,
Chris@909 155 :comments => '[#1] Worked on this issue',
Chris@909 156 :revision => '12345')
Chris@909 157 assert c.save
Chris@0 158 assert_equal [1], c.issue_ids.sort
Chris@0 159 end
Chris@0 160
Chris@0 161 def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
Chris@0 162 Setting.commit_ref_keywords = '*'
Chris@441 163 c = Changeset.new(:repository => Project.find(1).repository,
Chris@0 164 :committed_on => Time.now,
Chris@909 165 :comments => '[#1 #2, #3] Worked on these',
Chris@909 166 :revision => '12345')
Chris@909 167 assert c.save
Chris@0 168 assert_equal [1,2,3], c.issue_ids.sort
Chris@0 169 end
Chris@441 170
Chris@0 171 def test_commit_referencing_a_subproject_issue
Chris@441 172 c = Changeset.new(:repository => Project.find(1).repository,
Chris@0 173 :committed_on => Time.now,
Chris@909 174 :comments => 'refs #5, a subproject issue',
Chris@909 175 :revision => '12345')
Chris@909 176 assert c.save
Chris@0 177 assert_equal [5], c.issue_ids.sort
Chris@0 178 assert c.issues.first.project != c.project
Chris@0 179 end
Chris@0 180
Chris@0 181 def test_commit_referencing_a_parent_project_issue
Chris@0 182 # repository of child project
Chris@441 183 r = Repository::Subversion.create!(
Chris@441 184 :project => Project.find(3),
Chris@441 185 :url => 'svn://localhost/test')
Chris@441 186 c = Changeset.new(:repository => r,
Chris@0 187 :committed_on => Time.now,
Chris@909 188 :comments => 'refs #2, an issue of a parent project',
Chris@909 189 :revision => '12345')
Chris@909 190 assert c.save
Chris@0 191 assert_equal [2], c.issue_ids.sort
Chris@0 192 assert c.issues.first.project != c.project
Chris@0 193 end
Chris@245 194
Chris@119 195 def test_text_tag_revision
Chris@119 196 c = Changeset.new(:revision => '520')
Chris@119 197 assert_equal 'r520', c.text_tag
Chris@119 198 end
Chris@245 199
Chris@119 200 def test_text_tag_hash
Chris@441 201 c = Changeset.new(
Chris@441 202 :scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518',
Chris@441 203 :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518')
Chris@119 204 assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag
Chris@119 205 end
Chris@119 206
Chris@119 207 def test_text_tag_hash_all_number
Chris@119 208 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
Chris@119 209 assert_equal 'commit:0123456789', c.text_tag
Chris@119 210 end
Chris@0 211
Chris@0 212 def test_previous
Chris@0 213 changeset = Changeset.find_by_revision('3')
Chris@0 214 assert_equal Changeset.find_by_revision('2'), changeset.previous
Chris@0 215 end
Chris@0 216
Chris@0 217 def test_previous_nil
Chris@0 218 changeset = Changeset.find_by_revision('1')
Chris@0 219 assert_nil changeset.previous
Chris@0 220 end
Chris@0 221
Chris@0 222 def test_next
Chris@0 223 changeset = Changeset.find_by_revision('2')
Chris@0 224 assert_equal Changeset.find_by_revision('3'), changeset.next
Chris@0 225 end
Chris@0 226
Chris@0 227 def test_next_nil
Chris@0 228 changeset = Changeset.find_by_revision('10')
Chris@0 229 assert_nil changeset.next
Chris@0 230 end
Chris@245 231
Chris@0 232 def test_comments_should_be_converted_to_utf8
Chris@441 233 proj = Project.find(3)
Chris@441 234 # str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
Chris@441 235 str = "Texte encod\xe9 en ISO-8859-1."
Chris@441 236 str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
Chris@441 237 r = Repository::Bazaar.create!(
Chris@441 238 :project => proj,
Chris@441 239 :url => '/tmp/test/bazaar',
Chris@245 240 :log_encoding => 'ISO-8859-1' )
Chris@441 241 assert r
Chris@441 242 c = Changeset.new(:repository => r,
Chris@441 243 :committed_on => Time.now,
Chris@441 244 :revision => '123',
Chris@441 245 :scmid => '12345',
Chris@441 246 :comments => str)
Chris@441 247 assert( c.save )
Chris@441 248 str_utf8 = "Texte encod\xc3\xa9 en ISO-8859-1."
Chris@441 249 str_utf8.force_encoding("UTF-8") if str_utf8.respond_to?(:force_encoding)
Chris@441 250 assert_equal str_utf8, c.comments
Chris@0 251 end
Chris@245 252
Chris@441 253 def test_invalid_utf8_sequences_in_comments_should_be_replaced_latin1
Chris@441 254 proj = Project.find(3)
Chris@441 255 # str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
Chris@441 256 str1 = "Texte encod\xe9 en ISO-8859-1."
Chris@441 257 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
Chris@441 258 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
Chris@441 259 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
Chris@441 260 r = Repository::Bazaar.create!(
Chris@441 261 :project => proj,
Chris@441 262 :url => '/tmp/test/bazaar',
Chris@245 263 :log_encoding => 'UTF-8' )
Chris@441 264 assert r
Chris@441 265 c = Changeset.new(:repository => r,
Chris@441 266 :committed_on => Time.now,
Chris@441 267 :revision => '123',
Chris@441 268 :scmid => '12345',
Chris@441 269 :comments => str1,
Chris@441 270 :committer => str2)
Chris@441 271 assert( c.save )
Chris@441 272 assert_equal "Texte encod? en ISO-8859-1.", c.comments
Chris@441 273 assert_equal "?a?b?c?d?e test", c.committer
Chris@441 274 end
Chris@441 275
Chris@441 276 def test_invalid_utf8_sequences_in_comments_should_be_replaced_ja_jis
Chris@441 277 proj = Project.find(3)
Chris@441 278 str = "test\xb5\xfetest\xb5\xfe"
Chris@441 279 if str.respond_to?(:force_encoding)
Chris@441 280 str.force_encoding('ASCII-8BIT')
Chris@441 281 end
Chris@441 282 r = Repository::Bazaar.create!(
Chris@441 283 :project => proj,
Chris@441 284 :url => '/tmp/test/bazaar',
Chris@441 285 :log_encoding => 'ISO-2022-JP' )
Chris@441 286 assert r
Chris@441 287 c = Changeset.new(:repository => r,
Chris@441 288 :committed_on => Time.now,
Chris@441 289 :revision => '123',
Chris@441 290 :scmid => '12345',
Chris@441 291 :comments => str)
Chris@441 292 assert( c.save )
Chris@441 293 assert_equal "test??test??", c.comments
Chris@245 294 end
Chris@245 295
Chris@245 296 def test_comments_should_be_converted_all_latin1_to_utf8
Chris@441 297 s1 = "\xC2\x80"
Chris@441 298 s2 = "\xc3\x82\xc2\x80"
Chris@441 299 s4 = s2.dup
Chris@441 300 if s1.respond_to?(:force_encoding)
Chris@441 301 s3 = s1.dup
Chris@441 302 s1.force_encoding('ASCII-8BIT')
Chris@441 303 s2.force_encoding('ASCII-8BIT')
Chris@441 304 s3.force_encoding('ISO-8859-1')
Chris@441 305 s4.force_encoding('UTF-8')
Chris@441 306 assert_equal s3.encode('UTF-8'), s4
Chris@441 307 end
Chris@441 308 proj = Project.find(3)
Chris@441 309 r = Repository::Bazaar.create!(
Chris@441 310 :project => proj,
Chris@441 311 :url => '/tmp/test/bazaar',
Chris@245 312 :log_encoding => 'ISO-8859-1' )
Chris@441 313 assert r
Chris@441 314 c = Changeset.new(:repository => r,
Chris@441 315 :committed_on => Time.now,
Chris@441 316 :revision => '123',
Chris@441 317 :scmid => '12345',
Chris@441 318 :comments => s1)
Chris@441 319 assert( c.save )
Chris@441 320 assert_equal s4, c.comments
Chris@441 321 end
Chris@441 322
Chris@441 323 def test_invalid_utf8_sequences_in_paths_should_be_replaced
Chris@441 324 proj = Project.find(3)
Chris@441 325 str1 = "Texte encod\xe9 en ISO-8859-1"
Chris@441 326 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
Chris@441 327 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
Chris@441 328 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
Chris@441 329 r = Repository::Bazaar.create!(
Chris@441 330 :project => proj,
Chris@441 331 :url => '/tmp/test/bazaar',
Chris@441 332 :log_encoding => 'UTF-8' )
Chris@441 333 assert r
Chris@441 334 cs = Changeset.new(
Chris@441 335 :repository => r,
Chris@441 336 :committed_on => Time.now,
Chris@441 337 :revision => '123',
Chris@441 338 :scmid => '12345',
Chris@441 339 :comments => "test")
Chris@441 340 assert(cs.save)
Chris@441 341 ch = Change.new(
Chris@441 342 :changeset => cs,
Chris@441 343 :action => "A",
Chris@441 344 :path => str1,
Chris@441 345 :from_path => str2,
Chris@441 346 :from_revision => "345")
Chris@441 347 assert(ch.save)
Chris@441 348 assert_equal "Texte encod? en ISO-8859-1", ch.path
Chris@441 349 assert_equal "?a?b?c?d?e test", ch.from_path
Chris@441 350 end
Chris@441 351
Chris@441 352 def test_comments_nil
Chris@441 353 proj = Project.find(3)
Chris@441 354 r = Repository::Bazaar.create!(
Chris@441 355 :project => proj,
Chris@441 356 :url => '/tmp/test/bazaar',
Chris@441 357 :log_encoding => 'ISO-8859-1' )
Chris@441 358 assert r
Chris@441 359 c = Changeset.new(:repository => r,
Chris@441 360 :committed_on => Time.now,
Chris@441 361 :revision => '123',
Chris@441 362 :scmid => '12345',
Chris@441 363 :comments => nil,
Chris@441 364 :committer => nil)
Chris@441 365 assert( c.save )
Chris@441 366 assert_equal "", c.comments
Chris@441 367 assert_equal nil, c.committer
Chris@441 368 if c.comments.respond_to?(:force_encoding)
Chris@441 369 assert_equal "UTF-8", c.comments.encoding.to_s
Chris@441 370 end
Chris@441 371 end
Chris@441 372
Chris@441 373 def test_comments_empty
Chris@441 374 proj = Project.find(3)
Chris@441 375 r = Repository::Bazaar.create!(
Chris@441 376 :project => proj,
Chris@441 377 :url => '/tmp/test/bazaar',
Chris@441 378 :log_encoding => 'ISO-8859-1' )
Chris@441 379 assert r
Chris@441 380 c = Changeset.new(:repository => r,
Chris@441 381 :committed_on => Time.now,
Chris@441 382 :revision => '123',
Chris@441 383 :scmid => '12345',
Chris@441 384 :comments => "",
Chris@441 385 :committer => "")
Chris@441 386 assert( c.save )
Chris@441 387 assert_equal "", c.comments
Chris@441 388 assert_equal "", c.committer
Chris@441 389 if c.comments.respond_to?(:force_encoding)
Chris@441 390 assert_equal "UTF-8", c.comments.encoding.to_s
Chris@441 391 assert_equal "UTF-8", c.committer.encoding.to_s
Chris@441 392 end
Chris@0 393 end
Chris@119 394
Chris@119 395 def test_identifier
Chris@119 396 c = Changeset.find_by_revision('1')
Chris@119 397 assert_equal c.revision, c.identifier
Chris@119 398 end
Chris@0 399 end