;]
[askyou.git] / test / unit / changeset_test.rb
blob8010383feab5c037c95e9517fa439f597ba1719f
1 # encoding: utf-8
3 # Redmine - project management software
4 # Copyright (C) 2006-2010  Jean-Philippe Lang
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 require File.dirname(__FILE__) + '/../test_helper'
22 class ChangesetTest < ActiveSupport::TestCase
23   fixtures :projects, :repositories, :issues, :issue_statuses, :changesets, :changes, :issue_categories, :enumerations, :custom_fields, :custom_values, :users, :members, :member_roles, :trackers
25   def setup
26   end
27   
28   def test_ref_keywords_any
29     ActionMailer::Base.deliveries.clear
30     Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
31     Setting.commit_fix_done_ratio = '90'
32     Setting.commit_ref_keywords = '*'
33     Setting.commit_fix_keywords = 'fixes , closes'
34     
35     c = Changeset.new(:repository => Project.find(1).repository,
36                       :committed_on => Time.now,
37                       :comments => 'New commit (#2). Fixes #1')
38     c.scan_comment_for_issue_ids
39     
40     assert_equal [1, 2], c.issue_ids.sort
41     fixed = Issue.find(1)
42     assert fixed.closed?
43     assert_equal 90, fixed.done_ratio
44     assert_equal 1, ActionMailer::Base.deliveries.size
45   end
46   
47   def test_ref_keywords_any_line_start
48     Setting.commit_ref_keywords = '*'
50     c = Changeset.new(:repository => Project.find(1).repository,
51                       :committed_on => Time.now,
52                       :comments => '#1 is the reason of this commit')
53     c.scan_comment_for_issue_ids
55     assert_equal [1], c.issue_ids.sort
56   end
58   def test_ref_keywords_allow_brackets_around_a_issue_number
59     Setting.commit_ref_keywords = '*'
61     c = Changeset.new(:repository => Project.find(1).repository,
62                       :committed_on => Time.now,
63                       :comments => '[#1] Worked on this issue')
64     c.scan_comment_for_issue_ids
66     assert_equal [1], c.issue_ids.sort
67   end
69   def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
70     Setting.commit_ref_keywords = '*'
72     c = Changeset.new(:repository => Project.find(1).repository,
73                       :committed_on => Time.now,
74                       :comments => '[#1 #2, #3] Worked on these')
75     c.scan_comment_for_issue_ids
77     assert_equal [1,2,3], c.issue_ids.sort
78   end
79   
80   def test_commit_referencing_a_subproject_issue
81     c = Changeset.new(:repository => Project.find(1).repository,
82                       :committed_on => Time.now,
83                       :comments => 'refs #5, a subproject issue')
84     c.scan_comment_for_issue_ids
85     
86     assert_equal [5], c.issue_ids.sort
87     assert c.issues.first.project != c.project
88   end
90   def test_commit_referencing_a_parent_project_issue
91     # repository of child project
92     r = Repository::Subversion.create!(:project => Project.find(3), :url => 'svn://localhost/test')
93       
94     c = Changeset.new(:repository => r,
95                       :committed_on => Time.now,
96                       :comments => 'refs #2, an issue of a parent project')
97     c.scan_comment_for_issue_ids
98     
99     assert_equal [2], c.issue_ids.sort
100     assert c.issues.first.project != c.project
101   end
103   def test_previous
104     changeset = Changeset.find_by_revision('3')
105     assert_equal Changeset.find_by_revision('2'), changeset.previous
106   end
108   def test_previous_nil
109     changeset = Changeset.find_by_revision('1')
110     assert_nil changeset.previous
111   end
113   def test_next
114     changeset = Changeset.find_by_revision('2')
115     assert_equal Changeset.find_by_revision('3'), changeset.next
116   end
118   def test_next_nil
119     changeset = Changeset.find_by_revision('10')
120     assert_nil changeset.next
121   end
122   
123   def test_comments_should_be_converted_to_utf8
124     with_settings :commit_logs_encoding => 'ISO-8859-1' do
125       c = Changeset.new
126       c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
127       assert_equal "Texte encodé en ISO-8859-1.", c.comments
128     end
129   end
130   
131   def test_invalid_utf8_sequences_in_comments_should_be_stripped
132     c = Changeset.new
133     c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
134     assert_equal "Texte encod en ISO-8859-1.", c.comments
135   end