You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

project_enumerations_controller_test.rb 11KB

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