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

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