annotate test/unit/changeset_test.rb @ 1082:997f6d7738f7 bug_531

In repo controller entry action, show the page for the file even if it's binary (so user still has access to history etc links). This makes it possible to use the entry action as the default when a file is clicked on
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 22 Nov 2012 18:04:17 +0000
parents 5f33065ddc4b
children 433d4f72a19b
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@929 181 def test_commit_closing_a_subproject_issue
Chris@929 182 with_settings :commit_fix_status_id => 5, :commit_fix_keywords => 'closes' do
Chris@929 183 issue = Issue.find(5)
Chris@929 184 assert !issue.closed?
Chris@929 185 assert_difference 'Journal.count' do
Chris@929 186 c = Changeset.new(:repository => Project.find(1).repository,
Chris@929 187 :committed_on => Time.now,
Chris@929 188 :comments => 'closes #5, a subproject issue',
Chris@929 189 :revision => '12345')
Chris@929 190 assert c.save
Chris@929 191 end
Chris@929 192 assert issue.reload.closed?
Chris@929 193 journal = Journal.first(:order => 'id DESC')
Chris@929 194 assert_equal issue, journal.issue
Chris@929 195 assert_include "Applied in changeset ecookbook:r12345.", journal.notes
Chris@929 196 end
Chris@929 197 end
Chris@929 198
Chris@0 199 def test_commit_referencing_a_parent_project_issue
Chris@0 200 # repository of child project
Chris@441 201 r = Repository::Subversion.create!(
Chris@441 202 :project => Project.find(3),
Chris@441 203 :url => 'svn://localhost/test')
Chris@441 204 c = Changeset.new(:repository => r,
Chris@0 205 :committed_on => Time.now,
Chris@909 206 :comments => 'refs #2, an issue of a parent project',
Chris@909 207 :revision => '12345')
Chris@909 208 assert c.save
Chris@0 209 assert_equal [2], c.issue_ids.sort
Chris@0 210 assert c.issues.first.project != c.project
Chris@0 211 end
Chris@245 212
Chris@119 213 def test_text_tag_revision
Chris@119 214 c = Changeset.new(:revision => '520')
Chris@119 215 assert_equal 'r520', c.text_tag
Chris@119 216 end
Chris@245 217
Chris@929 218 def test_text_tag_revision_with_same_project
Chris@929 219 c = Changeset.new(:revision => '520', :repository => Project.find(1).repository)
Chris@929 220 assert_equal 'r520', c.text_tag(Project.find(1))
Chris@929 221 end
Chris@929 222
Chris@929 223 def test_text_tag_revision_with_different_project
Chris@929 224 c = Changeset.new(:revision => '520', :repository => Project.find(1).repository)
Chris@929 225 assert_equal 'ecookbook:r520', c.text_tag(Project.find(2))
Chris@929 226 end
Chris@929 227
Chris@119 228 def test_text_tag_hash
Chris@441 229 c = Changeset.new(
Chris@441 230 :scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518',
Chris@441 231 :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518')
Chris@119 232 assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag
Chris@119 233 end
Chris@119 234
Chris@929 235 def test_text_tag_hash_with_same_project
Chris@929 236 c = Changeset.new(:revision => '7234cb27', :scmid => '7234cb27', :repository => Project.find(1).repository)
Chris@929 237 assert_equal 'commit:7234cb27', c.text_tag(Project.find(1))
Chris@929 238 end
Chris@929 239
Chris@929 240 def test_text_tag_hash_with_different_project
Chris@929 241 c = Changeset.new(:revision => '7234cb27', :scmid => '7234cb27', :repository => Project.find(1).repository)
Chris@929 242 assert_equal 'ecookbook:commit:7234cb27', c.text_tag(Project.find(2))
Chris@929 243 end
Chris@929 244
Chris@119 245 def test_text_tag_hash_all_number
Chris@119 246 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
Chris@119 247 assert_equal 'commit:0123456789', c.text_tag
Chris@119 248 end
Chris@0 249
Chris@0 250 def test_previous
Chris@0 251 changeset = Changeset.find_by_revision('3')
Chris@0 252 assert_equal Changeset.find_by_revision('2'), changeset.previous
Chris@0 253 end
Chris@0 254
Chris@0 255 def test_previous_nil
Chris@0 256 changeset = Changeset.find_by_revision('1')
Chris@0 257 assert_nil changeset.previous
Chris@0 258 end
Chris@0 259
Chris@0 260 def test_next
Chris@0 261 changeset = Changeset.find_by_revision('2')
Chris@0 262 assert_equal Changeset.find_by_revision('3'), changeset.next
Chris@0 263 end
Chris@0 264
Chris@0 265 def test_next_nil
Chris@0 266 changeset = Changeset.find_by_revision('10')
Chris@0 267 assert_nil changeset.next
Chris@0 268 end
Chris@245 269
Chris@0 270 def test_comments_should_be_converted_to_utf8
Chris@441 271 proj = Project.find(3)
Chris@441 272 # str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
Chris@441 273 str = "Texte encod\xe9 en ISO-8859-1."
Chris@441 274 str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
Chris@441 275 r = Repository::Bazaar.create!(
Chris@441 276 :project => proj,
Chris@441 277 :url => '/tmp/test/bazaar',
Chris@245 278 :log_encoding => 'ISO-8859-1' )
Chris@441 279 assert r
Chris@441 280 c = Changeset.new(:repository => r,
Chris@441 281 :committed_on => Time.now,
Chris@441 282 :revision => '123',
Chris@441 283 :scmid => '12345',
Chris@441 284 :comments => str)
Chris@441 285 assert( c.save )
Chris@441 286 str_utf8 = "Texte encod\xc3\xa9 en ISO-8859-1."
Chris@441 287 str_utf8.force_encoding("UTF-8") if str_utf8.respond_to?(:force_encoding)
Chris@441 288 assert_equal str_utf8, c.comments
Chris@0 289 end
Chris@245 290
Chris@441 291 def test_invalid_utf8_sequences_in_comments_should_be_replaced_latin1
Chris@441 292 proj = Project.find(3)
Chris@441 293 # str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
Chris@441 294 str1 = "Texte encod\xe9 en ISO-8859-1."
Chris@441 295 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
Chris@441 296 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
Chris@441 297 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
Chris@441 298 r = Repository::Bazaar.create!(
Chris@441 299 :project => proj,
Chris@441 300 :url => '/tmp/test/bazaar',
Chris@245 301 :log_encoding => 'UTF-8' )
Chris@441 302 assert r
Chris@441 303 c = Changeset.new(:repository => r,
Chris@441 304 :committed_on => Time.now,
Chris@441 305 :revision => '123',
Chris@441 306 :scmid => '12345',
Chris@441 307 :comments => str1,
Chris@441 308 :committer => str2)
Chris@441 309 assert( c.save )
Chris@441 310 assert_equal "Texte encod? en ISO-8859-1.", c.comments
Chris@441 311 assert_equal "?a?b?c?d?e test", c.committer
Chris@441 312 end
Chris@441 313
Chris@441 314 def test_invalid_utf8_sequences_in_comments_should_be_replaced_ja_jis
Chris@441 315 proj = Project.find(3)
Chris@441 316 str = "test\xb5\xfetest\xb5\xfe"
Chris@441 317 if str.respond_to?(:force_encoding)
Chris@441 318 str.force_encoding('ASCII-8BIT')
Chris@441 319 end
Chris@441 320 r = Repository::Bazaar.create!(
Chris@441 321 :project => proj,
Chris@441 322 :url => '/tmp/test/bazaar',
Chris@441 323 :log_encoding => 'ISO-2022-JP' )
Chris@441 324 assert r
Chris@441 325 c = Changeset.new(:repository => r,
Chris@441 326 :committed_on => Time.now,
Chris@441 327 :revision => '123',
Chris@441 328 :scmid => '12345',
Chris@441 329 :comments => str)
Chris@441 330 assert( c.save )
Chris@441 331 assert_equal "test??test??", c.comments
Chris@245 332 end
Chris@245 333
Chris@245 334 def test_comments_should_be_converted_all_latin1_to_utf8
Chris@441 335 s1 = "\xC2\x80"
Chris@441 336 s2 = "\xc3\x82\xc2\x80"
Chris@441 337 s4 = s2.dup
Chris@441 338 if s1.respond_to?(:force_encoding)
Chris@441 339 s3 = s1.dup
Chris@441 340 s1.force_encoding('ASCII-8BIT')
Chris@441 341 s2.force_encoding('ASCII-8BIT')
Chris@441 342 s3.force_encoding('ISO-8859-1')
Chris@441 343 s4.force_encoding('UTF-8')
Chris@441 344 assert_equal s3.encode('UTF-8'), s4
Chris@441 345 end
Chris@441 346 proj = Project.find(3)
Chris@441 347 r = Repository::Bazaar.create!(
Chris@441 348 :project => proj,
Chris@441 349 :url => '/tmp/test/bazaar',
Chris@245 350 :log_encoding => 'ISO-8859-1' )
Chris@441 351 assert r
Chris@441 352 c = Changeset.new(:repository => r,
Chris@441 353 :committed_on => Time.now,
Chris@441 354 :revision => '123',
Chris@441 355 :scmid => '12345',
Chris@441 356 :comments => s1)
Chris@441 357 assert( c.save )
Chris@441 358 assert_equal s4, c.comments
Chris@441 359 end
Chris@441 360
Chris@441 361 def test_invalid_utf8_sequences_in_paths_should_be_replaced
Chris@441 362 proj = Project.find(3)
Chris@441 363 str1 = "Texte encod\xe9 en ISO-8859-1"
Chris@441 364 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
Chris@441 365 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
Chris@441 366 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
Chris@441 367 r = Repository::Bazaar.create!(
Chris@441 368 :project => proj,
Chris@441 369 :url => '/tmp/test/bazaar',
Chris@441 370 :log_encoding => 'UTF-8' )
Chris@441 371 assert r
Chris@441 372 cs = Changeset.new(
Chris@441 373 :repository => r,
Chris@441 374 :committed_on => Time.now,
Chris@441 375 :revision => '123',
Chris@441 376 :scmid => '12345',
Chris@441 377 :comments => "test")
Chris@441 378 assert(cs.save)
Chris@441 379 ch = Change.new(
Chris@441 380 :changeset => cs,
Chris@441 381 :action => "A",
Chris@441 382 :path => str1,
Chris@441 383 :from_path => str2,
Chris@441 384 :from_revision => "345")
Chris@441 385 assert(ch.save)
Chris@441 386 assert_equal "Texte encod? en ISO-8859-1", ch.path
Chris@441 387 assert_equal "?a?b?c?d?e test", ch.from_path
Chris@441 388 end
Chris@441 389
Chris@441 390 def test_comments_nil
Chris@441 391 proj = Project.find(3)
Chris@441 392 r = Repository::Bazaar.create!(
Chris@441 393 :project => proj,
Chris@441 394 :url => '/tmp/test/bazaar',
Chris@441 395 :log_encoding => 'ISO-8859-1' )
Chris@441 396 assert r
Chris@441 397 c = Changeset.new(:repository => r,
Chris@441 398 :committed_on => Time.now,
Chris@441 399 :revision => '123',
Chris@441 400 :scmid => '12345',
Chris@441 401 :comments => nil,
Chris@441 402 :committer => nil)
Chris@441 403 assert( c.save )
Chris@441 404 assert_equal "", c.comments
Chris@441 405 assert_equal nil, c.committer
Chris@441 406 if c.comments.respond_to?(:force_encoding)
Chris@441 407 assert_equal "UTF-8", c.comments.encoding.to_s
Chris@441 408 end
Chris@441 409 end
Chris@441 410
Chris@441 411 def test_comments_empty
Chris@441 412 proj = Project.find(3)
Chris@441 413 r = Repository::Bazaar.create!(
Chris@441 414 :project => proj,
Chris@441 415 :url => '/tmp/test/bazaar',
Chris@441 416 :log_encoding => 'ISO-8859-1' )
Chris@441 417 assert r
Chris@441 418 c = Changeset.new(:repository => r,
Chris@441 419 :committed_on => Time.now,
Chris@441 420 :revision => '123',
Chris@441 421 :scmid => '12345',
Chris@441 422 :comments => "",
Chris@441 423 :committer => "")
Chris@441 424 assert( c.save )
Chris@441 425 assert_equal "", c.comments
Chris@441 426 assert_equal "", c.committer
Chris@441 427 if c.comments.respond_to?(:force_encoding)
Chris@441 428 assert_equal "UTF-8", c.comments.encoding.to_s
Chris@441 429 assert_equal "UTF-8", c.committer.encoding.to_s
Chris@441 430 end
Chris@0 431 end
Chris@119 432
Chris@119 433 def test_identifier
Chris@119 434 c = Changeset.find_by_revision('1')
Chris@119 435 assert_equal c.revision, c.identifier
Chris@119 436 end
Chris@0 437 end