To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 92 / 929002b516dc5d82c7d3b223bcd28e93bb353f29.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (9.71 KB)

1 1296:038ba2d95de8 Chris
require File.expand_path('../../test_helper', __FILE__)
2
3
class ProjectEnumerationsControllerTest < ActionController::TestCase
4
  fixtures :projects, :trackers, :issue_statuses, :issues,
5
           :enumerations, :users, :issue_categories,
6
           :projects_trackers,
7
           :roles,
8
           :member_roles,
9
           :members,
10
           :enabled_modules,
11
           :workflows,
12
           :custom_fields, :custom_fields_projects,
13
           :custom_fields_trackers, :custom_values,
14
           :time_entries
15
16
  self.use_transactional_fixtures = false
17
18
  def setup
19
    @request.session[:user_id] = nil
20
    Setting.default_language = 'en'
21
  end
22
23
  def test_update_to_override_system_activities
24
    @request.session[:user_id] = 2 # manager
25
    billable_field = TimeEntryActivityCustomField.find_by_name("Billable")
26
27
    put :update, :project_id => 1, :enumerations => {
28
      "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # Design, De-activate
29
      "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"}, # Development, Change custom value
30
      "14"=>{"parent_id"=>"14", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"}, # Inactive Activity, Activate with custom value
31
      "11"=>{"parent_id"=>"11", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"} # QA, no changes
32
    }
33
34
    assert_response :redirect
35
    assert_redirected_to '/projects/ecookbook/settings/activities'
36
37
    # Created project specific activities...
38
    project = Project.find('ecookbook')
39
40
    # ... Design
41
    design = project.time_entry_activities.find_by_name("Design")
42
    assert design, "Project activity not found"
43
44
    assert_equal 9, design.parent_id # Relate to the system activity
45
    assert_not_equal design.parent.id, design.id # Different records
46
    assert_equal design.parent.name, design.name # Same name
47
    assert !design.active?
48
49
    # ... Development
50
    development = project.time_entry_activities.find_by_name("Development")
51
    assert development, "Project activity not found"
52
53
    assert_equal 10, development.parent_id # Relate to the system activity
54
    assert_not_equal development.parent.id, development.id # Different records
55
    assert_equal development.parent.name, development.name # Same name
56
    assert development.active?
57
    assert_equal "0", development.custom_value_for(billable_field).value
58
59
    # ... Inactive Activity
60
    previously_inactive = project.time_entry_activities.find_by_name("Inactive Activity")
61
    assert previously_inactive, "Project activity not found"
62
63
    assert_equal 14, previously_inactive.parent_id # Relate to the system activity
64
    assert_not_equal previously_inactive.parent.id, previously_inactive.id # Different records
65
    assert_equal previously_inactive.parent.name, previously_inactive.name # Same name
66
    assert previously_inactive.active?
67
    assert_equal "1", previously_inactive.custom_value_for(billable_field).value
68
69
    # ... QA
70
    assert_equal nil, project.time_entry_activities.find_by_name("QA"), "Custom QA activity created when it wasn't modified"
71
  end
72
73
  def test_update_will_update_project_specific_activities
74
    @request.session[:user_id] = 2 # manager
75
76
    project_activity = TimeEntryActivity.new({
77
                                               :name => 'Project Specific',
78
                                               :parent => TimeEntryActivity.find(:first),
79
                                               :project => Project.find(1),
80
                                               :active => true
81
                                             })
82
    assert project_activity.save
83
    project_activity_two = TimeEntryActivity.new({
84
                                                   :name => 'Project Specific Two',
85
                                                   :parent => TimeEntryActivity.find(:last),
86
                                                   :project => Project.find(1),
87
                                                   :active => true
88
                                                 })
89
    assert project_activity_two.save
90
91
92
    put :update, :project_id => 1, :enumerations => {
93
      project_activity.id => {"custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # De-activate
94
      project_activity_two.id => {"custom_field_values"=>{"7" => "1"}, "active"=>"0"} # De-activate
95
    }
96
97
    assert_response :redirect
98
    assert_redirected_to '/projects/ecookbook/settings/activities'
99
100
    # Created project specific activities...
101
    project = Project.find('ecookbook')
102
    assert_equal 2, project.time_entry_activities.count
103
104
    activity_one = project.time_entry_activities.find_by_name(project_activity.name)
105
    assert activity_one, "Project activity not found"
106
    assert_equal project_activity.id, activity_one.id
107
    assert !activity_one.active?
108
109
    activity_two = project.time_entry_activities.find_by_name(project_activity_two.name)
110
    assert activity_two, "Project activity not found"
111
    assert_equal project_activity_two.id, activity_two.id
112
    assert !activity_two.active?
113
  end
114
115
  def test_update_when_creating_new_activities_will_convert_existing_data
116
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size
117
118
    @request.session[:user_id] = 2 # manager
119
    put :update, :project_id => 1, :enumerations => {
120
      "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"} # Design, De-activate
121
    }
122
    assert_response :redirect
123
124
    # No more TimeEntries using the system activity
125
    assert_equal 0, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "Time Entries still assigned to system activities"
126
    # All TimeEntries using project activity
127
    project_specific_activity = TimeEntryActivity.find_by_parent_id_and_project_id(9, 1)
128
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(project_specific_activity.id, 1).size, "No Time Entries assigned to the project activity"
129
  end
130
131
  def test_update_when_creating_new_activities_will_not_convert_existing_data_if_an_exception_is_raised
132
    # TODO: Need to cause an exception on create but these tests
133
    # aren't setup for mocking.  Just create a record now so the
134
    # second one is a dupicate
135
    parent = TimeEntryActivity.find(9)
136
    TimeEntryActivity.create!({:name => parent.name, :project_id => 1, :position => parent.position, :active => true})
137
    TimeEntry.create!({:project_id => 1, :hours => 1.0, :user => User.find(1), :issue_id => 3, :activity_id => 10, :spent_on => '2009-01-01'})
138
139
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size
140
    assert_equal 1, TimeEntry.find_all_by_activity_id_and_project_id(10, 1).size
141
142
    @request.session[:user_id] = 2 # manager
143
    put :update, :project_id => 1, :enumerations => {
144
      "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # Design
145
      "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"} # Development, Change custom value
146
    }
147
    assert_response :redirect
148
149
    # TimeEntries shouldn't have been reassigned on the failed record
150
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "Time Entries are not assigned to system activities"
151
    # TimeEntries shouldn't have been reassigned on the saved record either
152
    assert_equal 1, TimeEntry.find_all_by_activity_id_and_project_id(10, 1).size, "Time Entries are not assigned to system activities"
153
  end
154
155
  def test_destroy
156
    @request.session[:user_id] = 2 # manager
157
    project_activity = TimeEntryActivity.new({
158
                                               :name => 'Project Specific',
159
                                               :parent => TimeEntryActivity.find(:first),
160
                                               :project => Project.find(1),
161
                                               :active => true
162
                                             })
163
    assert project_activity.save
164
    project_activity_two = TimeEntryActivity.new({
165
                                                   :name => 'Project Specific Two',
166
                                                   :parent => TimeEntryActivity.find(:last),
167
                                                   :project => Project.find(1),
168
                                                   :active => true
169
                                                 })
170
    assert project_activity_two.save
171
172
    delete :destroy, :project_id => 1
173
    assert_response :redirect
174
    assert_redirected_to '/projects/ecookbook/settings/activities'
175
176
    assert_nil TimeEntryActivity.find_by_id(project_activity.id)
177
    assert_nil TimeEntryActivity.find_by_id(project_activity_two.id)
178
  end
179
180
  def test_destroy_should_reassign_time_entries_back_to_the_system_activity
181
    @request.session[:user_id] = 2 # manager
182
    project_activity = TimeEntryActivity.new({
183
                                               :name => 'Project Specific Design',
184
                                               :parent => TimeEntryActivity.find(9),
185
                                               :project => Project.find(1),
186
                                               :active => true
187
                                             })
188
    assert project_activity.save
189
    assert TimeEntry.update_all("activity_id = '#{project_activity.id}'", ["project_id = ? AND activity_id = ?", 1, 9])
190
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(project_activity.id, 1).size
191
192
    delete :destroy, :project_id => 1
193
    assert_response :redirect
194
    assert_redirected_to '/projects/ecookbook/settings/activities'
195
196
    assert_nil TimeEntryActivity.find_by_id(project_activity.id)
197
    assert_equal 0, TimeEntry.find_all_by_activity_id_and_project_id(project_activity.id, 1).size, "TimeEntries still assigned to project specific activity"
198
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "TimeEntries still assigned to project specific activity"
199
  end
200
201
end