summaryrefslogtreecommitdiffstats
path: root/test/unit/workflow_transition_test.rb
blob: 345ac83397ca7635ea3e16861c53b8d71ec2298d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require_relative '../test_helper'

class WorkflowTransitionTest < ActiveSupport::TestCase
  fixtures :roles, :trackers, :issue_statuses

  def setup
    User.current = nil
    WorkflowTransition.delete_all
  end

  def test_replace_transitions_should_create_enabled_transitions
    w = WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
                                   :old_status_id => 1, :new_status_id => 2)

    transitions = {'1' => {
      '2' => {'always' => '1'},
      '3' => {'always' => '1'}
    }}
    assert_difference 'WorkflowTransition.count' do
      WorkflowTransition.replace_transitions(Tracker.find(1), Role.find(1), transitions)
    end
    assert WorkflowTransition.where(:role_id => 1, :tracker_id => 1,
                                    :old_status_id => 1,
                                    :new_status_id => 3).exists?
  end

  def test_replace_transitions_should_delete_disabled_transitions
    w1 = WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
                                    :old_status_id => 1, :new_status_id => 2)
    w2 = WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
                                    :old_status_id => 1, :new_status_id => 3)

    transitions = {'1' => {
      '2' => {'always' => '0'},
      '3' => {'always' => '1'}
    }}
    assert_difference 'WorkflowTransition.count', -1 do
      WorkflowTransition.replace_transitions(Tracker.find(1), Role.find(1), transitions)
    end
    assert !WorkflowTransition.exists?(w1.id)
  end

  def test_replace_transitions_should_create_enabled_additional_transitions
    transitions = {'1' => {
      '2' => {'always' => '0', 'assignee' => '0', 'author' => '1'}
    }}
    assert_difference 'WorkflowTransition.count' do
      WorkflowTransition.replace_transitions(Tracker.find(1), Role.find(1), transitions)
    end
    w = WorkflowTransition.where(:role_id => 1, :tracker_id => 1,
                                 :old_status_id => 1, :new_status_id => 2).first
    assert w
    assert_equal false, w.assignee
    assert_equal true, w.author
  end

  def test_replace_transitions_should_delete_disabled_additional_transitions
    w = WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
                                   :old_status_id => 1, :new_status_id => 2,
                                   :assignee => true)

    transitions = {'1' => {
      '2' => {'always' => '0', 'assignee' => '0', 'author' => '0'}
    }}
    assert_difference 'WorkflowTransition.count', -1 do
      WorkflowTransition.replace_transitions(Tracker.find(1), Role.find(1), transitions)
    end
    assert !WorkflowTransition.exists?(w.id)
  end

  def test_replace_transitions_should_update_additional_transitions
    WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
                               :old_status_id => 1, :new_status_id => 2,
                               :assignee => true)

    transitions = {'1' => {
      '2' => {'always' => '0', 'assignee' => '0', 'author' => '1'}
    }}
    assert_no_difference 'WorkflowTransition.count' do
      WorkflowTransition.replace_transitions(Tracker.find(1), Role.find(1), transitions)
    end
    w = WorkflowTransition.where(:role_id => 1, :tracker_id => 1,
                                 :old_status_id => 1, :new_status_id => 2).first
    assert w
    assert_equal false, w.assignee
    assert_equal true, w.author
  end
end