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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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_tests = 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, :params => {
  38. :project_id => 1,
  39. :enumerations => {
  40. "9"=> {"parent_id"=>"9", "custom_field_values"=> {"7" => "1"}, "active"=>"0"}, # Design, De-activate
  41. "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"}, # Development, Change custom value
  42. "14"=>{"parent_id"=>"14", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"}, # Inactive Activity, Activate with custom value
  43. "11"=>{"parent_id"=>"11", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"} # QA, no changes
  44. }
  45. }
  46. assert_response :redirect
  47. assert_redirected_to '/projects/ecookbook/settings/activities'
  48. # Created project specific activities...
  49. project = Project.find('ecookbook')
  50. # ... Design
  51. design = project.time_entry_activities.find_by_name("Design")
  52. assert design, "Project activity not found"
  53. assert_equal 9, design.parent_id # Relate to the system activity
  54. assert_not_equal design.parent.id, design.id # Different records
  55. assert_equal design.parent.name, design.name # Same name
  56. assert !design.active?
  57. # ... Development
  58. development = project.time_entry_activities.find_by_name("Development")
  59. assert development, "Project activity not found"
  60. assert_equal 10, development.parent_id # Relate to the system activity
  61. assert_not_equal development.parent.id, development.id # Different records
  62. assert_equal development.parent.name, development.name # Same name
  63. assert development.active?
  64. assert_equal "0", development.custom_value_for(billable_field).value
  65. # ... Inactive Activity
  66. previously_inactive = project.time_entry_activities.find_by_name("Inactive Activity")
  67. assert previously_inactive, "Project activity not found"
  68. assert_equal 14, previously_inactive.parent_id # Relate to the system activity
  69. assert_not_equal previously_inactive.parent.id, previously_inactive.id # Different records
  70. assert_equal previously_inactive.parent.name, previously_inactive.name # Same name
  71. assert previously_inactive.active?
  72. assert_equal "1", previously_inactive.custom_value_for(billable_field).value
  73. # ... QA
  74. assert_nil project.time_entry_activities.find_by_name("QA"), "Custom QA activity created when it wasn't modified"
  75. end
  76. def test_update_will_update_project_specific_activities
  77. @request.session[:user_id] = 2 # manager
  78. project_activity = TimeEntryActivity.new({
  79. :name => 'Project Specific',
  80. :parent => TimeEntryActivity.first,
  81. :project => Project.find(1),
  82. :active => true
  83. })
  84. assert project_activity.save
  85. project_activity_two = TimeEntryActivity.new({
  86. :name => 'Project Specific Two',
  87. :parent => TimeEntryActivity.last,
  88. :project => Project.find(1),
  89. :active => true
  90. })
  91. assert project_activity_two.save
  92. put :update, :params => {
  93. :project_id => 1,
  94. :enumerations => {
  95. project_activity.id => {"custom_field_values"=> {"7" => "1"}, "active"=>"0"}, # De-activate
  96. project_activity_two.id => {"custom_field_values"=>{"7" => "1"}, "active"=>"0"} # De-activate
  97. }
  98. }
  99. assert_response :redirect
  100. assert_redirected_to '/projects/ecookbook/settings/activities'
  101. # Created project specific activities...
  102. project = Project.find('ecookbook')
  103. assert_equal 2, project.time_entry_activities.count
  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. activity_two = project.time_entry_activities.find_by_name(project_activity_two.name)
  109. assert activity_two, "Project activity not found"
  110. assert_equal project_activity_two.id, activity_two.id
  111. assert !activity_two.active?
  112. end
  113. def test_update_when_creating_new_activities_will_convert_existing_data
  114. assert_equal 3, TimeEntry.where(:activity_id => 9, :project_id => 1).count
  115. @request.session[:user_id] = 2 # manager
  116. put :update, :params => {
  117. :project_id => 1,
  118. :enumerations => {
  119. "9"=> {
  120. "parent_id"=>"9", "custom_field_values"=> {
  121. "7" => "1"}, "active"=>"0"} # Design, De-activate
  122. }
  123. }
  124. assert_response :redirect
  125. # No more TimeEntries using the system activity
  126. assert_equal 0, TimeEntry.where(:activity_id => 9, :project_id => 1).count,
  127. "Time Entries still assigned to system activities"
  128. # All TimeEntries using project activity
  129. project_specific_activity = TimeEntryActivity.find_by_parent_id_and_project_id(9, 1)
  130. assert_equal 3, TimeEntry.where(:activity_id => project_specific_activity.id,
  131. :project_id => 1).count
  132. "No Time Entries assigned to the project activity"
  133. end
  134. def test_update_when_creating_new_activities_will_not_convert_existing_data_if_an_exception_is_raised
  135. # TODO: Need to cause an exception on create but these tests
  136. # aren't setup for mocking. Just create a record now so the
  137. # second one is a dupicate
  138. user = User.find(1)
  139. parent = TimeEntryActivity.find(9)
  140. TimeEntryActivity.create!({:name => parent.name, :project_id => 1,
  141. :position => parent.position, :active => true, :parent_id => 9})
  142. TimeEntry.create!({:project_id => 1, :hours => 1.0, :user => user, :author => user,
  143. :issue_id => 3, :activity_id => 10, :spent_on => '2009-01-01'})
  144. assert_equal 3, TimeEntry.where(:activity_id => 9, :project_id => 1).count
  145. assert_equal 1, TimeEntry.where(:activity_id => 10, :project_id => 1).count
  146. @request.session[:user_id] = 2 # manager
  147. put :update, :params => {
  148. :project_id => 1,
  149. :enumerations => {
  150. # Design
  151. "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"},
  152. # Development, Change custom value
  153. "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"}
  154. }
  155. }
  156. assert_response :redirect
  157. # TimeEntries shouldn't have been reassigned on the failed record
  158. assert_equal 3, TimeEntry.where(:activity_id => 9,
  159. :project_id => 1).count
  160. "Time Entries are not assigned to system activities"
  161. # TimeEntries shouldn't have been reassigned on the saved record either
  162. assert_equal 1, TimeEntry.where(:activity_id => 10,
  163. :project_id => 1).count
  164. "Time Entries are not assigned to system activities"
  165. end
  166. def test_destroy
  167. @request.session[:user_id] = 2 # manager
  168. project_activity = TimeEntryActivity.new({
  169. :name => 'Project Specific',
  170. :parent => TimeEntryActivity.first,
  171. :project => Project.find(1),
  172. :active => true
  173. })
  174. assert project_activity.save
  175. project_activity_two = TimeEntryActivity.new({
  176. :name => 'Project Specific Two',
  177. :parent => TimeEntryActivity.last,
  178. :project => Project.find(1),
  179. :active => true
  180. })
  181. assert project_activity_two.save
  182. delete :destroy, :params => {
  183. :project_id => 1
  184. }
  185. assert_response :redirect
  186. assert_redirected_to '/projects/ecookbook/settings/activities'
  187. assert_nil TimeEntryActivity.find_by_id(project_activity.id)
  188. assert_nil TimeEntryActivity.find_by_id(project_activity_two.id)
  189. end
  190. def test_destroy_should_reassign_time_entries_back_to_the_system_activity
  191. @request.session[:user_id] = 2 # manager
  192. project_activity = TimeEntryActivity.new({
  193. :name => 'Project Specific Design',
  194. :parent => TimeEntryActivity.find(9),
  195. :project => Project.find(1),
  196. :active => true
  197. })
  198. assert project_activity.save
  199. assert TimeEntry.where(["project_id = ? AND activity_id = ?", 1, 9]).
  200. update_all("activity_id = '#{project_activity.id}'")
  201. assert_equal 3, TimeEntry.where(:activity_id => project_activity.id,
  202. :project_id => 1).count
  203. delete :destroy, :params => {
  204. :project_id => 1
  205. }
  206. assert_response :redirect
  207. assert_redirected_to '/projects/ecookbook/settings/activities'
  208. assert_nil TimeEntryActivity.find_by_id(project_activity.id)
  209. assert_equal 0, TimeEntry.where(
  210. :activity_id => project_activity.id,
  211. :project_id => 1
  212. ).count,
  213. "TimeEntries still assigned to project specific activity"
  214. assert_equal 3, TimeEntry.where(
  215. :activity_id => 9,
  216. :project_id => 1
  217. ).count,
  218. "TimeEntries still assigned to project specific activity"
  219. end
  220. end