Chris@0: # encoding: utf-8 Chris@0: # Chris@0: # Redmine - project management software Chris@0: # Copyright (C) 2006-2010 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@117: require File.expand_path('../../test_helper', __FILE__) Chris@0: Chris@0: class ChangesetTest < ActiveSupport::TestCase Chris@0: fixtures :projects, :repositories, :issues, :issue_statuses, :changesets, :changes, :issue_categories, :enumerations, :custom_fields, :custom_values, :users, :members, :member_roles, :trackers Chris@0: Chris@0: def setup Chris@0: end Chris@0: Chris@0: def test_ref_keywords_any Chris@0: ActionMailer::Base.deliveries.clear Chris@0: Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id Chris@0: Setting.commit_fix_done_ratio = '90' Chris@0: Setting.commit_ref_keywords = '*' Chris@0: Setting.commit_fix_keywords = 'fixes , closes' Chris@0: Chris@0: c = Changeset.new(:repository => Project.find(1).repository, Chris@0: :committed_on => Time.now, Chris@0: :comments => 'New commit (#2). Fixes #1') Chris@0: c.scan_comment_for_issue_ids Chris@0: Chris@0: assert_equal [1, 2], c.issue_ids.sort Chris@0: fixed = Issue.find(1) Chris@0: assert fixed.closed? Chris@0: assert_equal 90, fixed.done_ratio Chris@0: assert_equal 1, ActionMailer::Base.deliveries.size Chris@0: end Chris@0: Chris@117: def test_ref_keywords Chris@117: Setting.commit_ref_keywords = 'refs' Chris@117: Setting.commit_fix_keywords = '' Chris@117: Chris@117: c = Changeset.new(:repository => Project.find(1).repository, Chris@117: :committed_on => Time.now, Chris@117: :comments => 'Ignores #2. Refs #1') Chris@117: c.scan_comment_for_issue_ids Chris@117: Chris@117: assert_equal [1], c.issue_ids.sort Chris@117: end Chris@117: Chris@117: def test_ref_keywords_any_only Chris@117: Setting.commit_ref_keywords = '*' Chris@117: Setting.commit_fix_keywords = '' Chris@117: Chris@117: c = Changeset.new(:repository => Project.find(1).repository, Chris@117: :committed_on => Time.now, Chris@117: :comments => 'Ignores #2. Refs #1') Chris@117: c.scan_comment_for_issue_ids Chris@117: Chris@117: assert_equal [1, 2], c.issue_ids.sort Chris@117: end Chris@117: Chris@117: def test_ref_keywords_any_with_timelog Chris@117: Setting.commit_ref_keywords = '*' Chris@117: Setting.commit_logtime_enabled = '1' Chris@117: Chris@117: c = Changeset.new(:repository => Project.find(1).repository, Chris@117: :committed_on => 24.hours.ago, Chris@117: :comments => 'Worked on this issue #1 @2h', Chris@117: :revision => '520', Chris@117: :user => User.find(2)) Chris@117: assert_difference 'TimeEntry.count' do Chris@117: c.scan_comment_for_issue_ids Chris@117: end Chris@117: assert_equal [1], c.issue_ids.sort Chris@117: Chris@117: time = TimeEntry.first(:order => 'id desc') Chris@117: assert_equal 1, time.issue_id Chris@117: assert_equal 1, time.project_id Chris@117: assert_equal 2, time.user_id Chris@117: assert_equal 2.0, time.hours Chris@117: assert_equal Date.yesterday, time.spent_on Chris@117: assert time.activity.is_default? Chris@117: assert time.comments.include?('r520'), "r520 was expected in time_entry comments: #{time.comments}" Chris@117: end Chris@117: Chris@117: def test_ref_keywords_closing_with_timelog Chris@117: Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id Chris@117: Setting.commit_ref_keywords = '*' Chris@117: Setting.commit_fix_keywords = 'fixes , closes' Chris@117: Setting.commit_logtime_enabled = '1' Chris@117: Chris@117: c = Changeset.new(:repository => Project.find(1).repository, Chris@117: :committed_on => Time.now, Chris@117: :comments => 'This is a comment. Fixes #1 @2.5, #2 @1', Chris@117: :user => User.find(2)) Chris@117: assert_difference 'TimeEntry.count', 2 do Chris@117: c.scan_comment_for_issue_ids Chris@117: end Chris@117: Chris@117: assert_equal [1, 2], c.issue_ids.sort Chris@117: assert Issue.find(1).closed? Chris@117: assert Issue.find(2).closed? Chris@117: Chris@117: times = TimeEntry.all(:order => 'id desc', :limit => 2) Chris@117: assert_equal [1, 2], times.collect(&:issue_id).sort Chris@117: end Chris@117: Chris@0: def test_ref_keywords_any_line_start Chris@0: Setting.commit_ref_keywords = '*' Chris@0: Chris@0: c = Changeset.new(:repository => Project.find(1).repository, Chris@0: :committed_on => Time.now, Chris@0: :comments => '#1 is the reason of this commit') Chris@0: c.scan_comment_for_issue_ids Chris@0: Chris@0: assert_equal [1], c.issue_ids.sort Chris@0: end Chris@0: Chris@0: def test_ref_keywords_allow_brackets_around_a_issue_number Chris@0: Setting.commit_ref_keywords = '*' Chris@0: Chris@0: c = Changeset.new(:repository => Project.find(1).repository, Chris@0: :committed_on => Time.now, Chris@0: :comments => '[#1] Worked on this issue') Chris@0: c.scan_comment_for_issue_ids Chris@0: Chris@0: assert_equal [1], c.issue_ids.sort Chris@0: end Chris@0: Chris@0: def test_ref_keywords_allow_brackets_around_multiple_issue_numbers Chris@0: Setting.commit_ref_keywords = '*' Chris@0: Chris@0: c = Changeset.new(:repository => Project.find(1).repository, Chris@0: :committed_on => Time.now, Chris@0: :comments => '[#1 #2, #3] Worked on these') Chris@0: c.scan_comment_for_issue_ids Chris@0: Chris@0: assert_equal [1,2,3], c.issue_ids.sort Chris@0: end Chris@0: Chris@0: def test_commit_referencing_a_subproject_issue Chris@0: c = Changeset.new(:repository => Project.find(1).repository, Chris@0: :committed_on => Time.now, Chris@0: :comments => 'refs #5, a subproject issue') Chris@0: c.scan_comment_for_issue_ids Chris@0: Chris@0: assert_equal [5], c.issue_ids.sort Chris@0: assert c.issues.first.project != c.project Chris@0: end Chris@0: Chris@0: def test_commit_referencing_a_parent_project_issue Chris@0: # repository of child project Chris@0: r = Repository::Subversion.create!(:project => Project.find(3), :url => 'svn://localhost/test') Chris@0: Chris@0: c = Changeset.new(:repository => r, Chris@0: :committed_on => Time.now, Chris@0: :comments => 'refs #2, an issue of a parent project') Chris@0: c.scan_comment_for_issue_ids Chris@0: Chris@0: assert_equal [2], c.issue_ids.sort Chris@0: assert c.issues.first.project != c.project Chris@0: end Chris@117: Chris@117: def test_text_tag_revision Chris@117: c = Changeset.new(:revision => '520') Chris@117: assert_equal 'r520', c.text_tag Chris@117: end Chris@117: Chris@117: def test_text_tag_hash Chris@117: c = Changeset.new(:scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518', :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518') Chris@117: assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag Chris@117: end Chris@117: Chris@117: def test_text_tag_hash_all_number Chris@117: c = Changeset.new(:scmid => '0123456789', :revision => '0123456789') Chris@117: assert_equal 'commit:0123456789', c.text_tag Chris@117: end Chris@0: Chris@0: def test_previous Chris@0: changeset = Changeset.find_by_revision('3') Chris@0: assert_equal Changeset.find_by_revision('2'), changeset.previous Chris@0: end Chris@0: Chris@0: def test_previous_nil Chris@0: changeset = Changeset.find_by_revision('1') Chris@0: assert_nil changeset.previous Chris@0: end Chris@0: Chris@0: def test_next Chris@0: changeset = Changeset.find_by_revision('2') Chris@0: assert_equal Changeset.find_by_revision('3'), changeset.next Chris@0: end Chris@0: Chris@0: def test_next_nil Chris@0: changeset = Changeset.find_by_revision('10') Chris@0: assert_nil changeset.next Chris@0: end Chris@0: Chris@0: def test_comments_should_be_converted_to_utf8 Chris@0: with_settings :commit_logs_encoding => 'ISO-8859-1' do Chris@0: c = Changeset.new Chris@0: c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt") Chris@0: assert_equal "Texte encodé en ISO-8859-1.", c.comments Chris@0: end Chris@0: end Chris@0: Chris@0: def test_invalid_utf8_sequences_in_comments_should_be_stripped Chris@0: c = Changeset.new Chris@0: c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt") Chris@0: assert_equal "Texte encod en ISO-8859-1.", c.comments Chris@0: end Chris@117: Chris@117: def test_identifier Chris@117: c = Changeset.find_by_revision('1') Chris@117: assert_equal c.revision, c.identifier Chris@117: end Chris@0: end