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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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_relative '../test_helper'
  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_should_not_create_project_specific_activities_when_setting_empty_value_in_custom_field_with_default_value_of_nil
  81. system_activity = TimeEntryActivity.find(9) # Design
  82. custom_field_value = system_activity.custom_field_values.detect{|cfv| cfv.custom_field.id == 7}
  83. assert_nil custom_field_value.value
  84. assert_no_difference 'TimeEntryActivity.count' do
  85. @request.session[:user_id] = 2 # manager
  86. put(
  87. :update,
  88. :params => {
  89. :project_id => 1,
  90. :enumerations => {
  91. "9" => {"parent_id" => "9", "custom_field_values" => {"7" => ""}, "active" => "1"}
  92. }
  93. }
  94. )
  95. assert_response :redirect
  96. end
  97. end
  98. def test_update_will_update_project_specific_activities
  99. @request.session[:user_id] = 2 # manager
  100. project_activity = TimeEntryActivity.new({
  101. :name => 'Project Specific',
  102. :parent => TimeEntryActivity.first,
  103. :project => Project.find(1),
  104. :active => true
  105. })
  106. assert project_activity.save
  107. project_activity_two = TimeEntryActivity.new({
  108. :name => 'Project Specific Two',
  109. :parent => TimeEntryActivity.last,
  110. :project => Project.find(1),
  111. :active => true
  112. })
  113. assert project_activity_two.save
  114. put(
  115. :update,
  116. :params => {
  117. :project_id => 1,
  118. :enumerations => {
  119. project_activity.id => {
  120. "custom_field_values"=> {"7" => "1"},
  121. "active"=>"0"
  122. }, # De-activate
  123. project_activity_two.id => {
  124. "custom_field_values"=>{"7" => "1"},
  125. "active"=>"0"
  126. } # De-activate
  127. }
  128. }
  129. )
  130. assert_response :redirect
  131. assert_redirected_to '/projects/ecookbook/settings/activities'
  132. # Created project specific activities...
  133. project = Project.find('ecookbook')
  134. assert_equal 2, project.time_entry_activities.count
  135. activity_one = project.time_entry_activities.find_by_name(project_activity.name)
  136. assert activity_one, "Project activity not found"
  137. assert_equal project_activity.id, activity_one.id
  138. assert !activity_one.active?
  139. activity_two = project.time_entry_activities.find_by_name(project_activity_two.name)
  140. assert activity_two, "Project activity not found"
  141. assert_equal project_activity_two.id, activity_two.id
  142. assert !activity_two.active?
  143. end
  144. def test_update_when_creating_new_activities_will_convert_existing_data
  145. assert_equal 3, TimeEntry.where(:activity_id => 9, :project_id => 1).count
  146. @request.session[:user_id] = 2 # manager
  147. put(
  148. :update,
  149. :params => {
  150. :project_id => 1,
  151. :enumerations => {
  152. "9"=> {
  153. "parent_id"=>"9",
  154. "custom_field_values"=> {
  155. "7" => "1"
  156. },
  157. "active"=>"0"
  158. } # Design, De-activate
  159. }
  160. }
  161. )
  162. assert_response :redirect
  163. # No more TimeEntries using the system activity
  164. assert_equal 0, TimeEntry.where(:activity_id => 9, :project_id => 1).count,
  165. "Time Entries still assigned to system activities"
  166. # All TimeEntries using project activity
  167. project_specific_activity = TimeEntryActivity.find_by_parent_id_and_project_id(9, 1)
  168. assert_equal 3, TimeEntry.where(:activity_id => project_specific_activity.id,
  169. :project_id => 1).count,
  170. "No Time Entries assigned to the project activity"
  171. end
  172. def test_update_when_creating_new_activities_will_not_convert_existing_data_if_an_exception_is_raised
  173. # TODO: Need to cause an exception on create but these tests
  174. # aren't setup for mocking. Just create a record now so the
  175. # second one is a dupicate
  176. user = User.find(1)
  177. parent = TimeEntryActivity.find(9)
  178. TimeEntryActivity.create!({:name => parent.name, :project_id => 1,
  179. :position => parent.position, :active => true, :parent_id => 9})
  180. TimeEntry.create!({:project_id => 1, :hours => 1.0, :user => user, :author => user,
  181. :issue_id => 3, :activity_id => 10, :spent_on => '2009-01-01'})
  182. assert_equal 3, TimeEntry.where(:activity_id => 9, :project_id => 1).count
  183. assert_equal 1, TimeEntry.where(:activity_id => 10, :project_id => 1).count
  184. @request.session[:user_id] = 2 # manager
  185. put(
  186. :update, :params => {
  187. :project_id => 1,
  188. :enumerations => {
  189. # Design
  190. "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"},
  191. # Development, Change custom value
  192. "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"}
  193. }
  194. }
  195. )
  196. assert_response :redirect
  197. # TimeEntries shouldn't have been reassigned on the failed record
  198. assert_equal 3, TimeEntry.where(:activity_id => 9,
  199. :project_id => 1).count,
  200. "Time Entries are not assigned to system activities"
  201. # TimeEntries shouldn't have been reassigned on the saved record either
  202. assert_equal 1, TimeEntry.where(:activity_id => 10,
  203. :project_id => 1).count,
  204. "Time Entries are not assigned to system activities"
  205. end
  206. def test_destroy
  207. @request.session[:user_id] = 2 # manager
  208. project_activity = TimeEntryActivity.new({
  209. :name => 'Project Specific',
  210. :parent => TimeEntryActivity.first,
  211. :project => Project.find(1),
  212. :active => true
  213. })
  214. assert project_activity.save
  215. project_activity_two = TimeEntryActivity.new({
  216. :name => 'Project Specific Two',
  217. :parent => TimeEntryActivity.last,
  218. :project => Project.find(1),
  219. :active => true
  220. })
  221. assert project_activity_two.save
  222. delete(:destroy, :params => {:project_id => 1})
  223. assert_response :redirect
  224. assert_redirected_to '/projects/ecookbook/settings/activities'
  225. assert_nil TimeEntryActivity.find_by_id(project_activity.id)
  226. assert_nil TimeEntryActivity.find_by_id(project_activity_two.id)
  227. end
  228. def test_destroy_should_reassign_time_entries_back_to_the_system_activity
  229. @request.session[:user_id] = 2 # manager
  230. project_activity = TimeEntryActivity.new({
  231. :name => 'Project Specific Design',
  232. :parent => TimeEntryActivity.find(9),
  233. :project => Project.find(1),
  234. :active => true
  235. })
  236. assert project_activity.save
  237. assert TimeEntry.where(["project_id = ? AND activity_id = ?", 1, 9]).
  238. update_all("activity_id = '#{project_activity.id}'")
  239. assert_equal 3, TimeEntry.where(:activity_id => project_activity.id,
  240. :project_id => 1).count
  241. delete(:destroy, :params => {:project_id => 1})
  242. assert_response :redirect
  243. assert_redirected_to '/projects/ecookbook/settings/activities'
  244. assert_nil TimeEntryActivity.find_by_id(project_activity.id)
  245. assert_equal(
  246. 0,
  247. TimeEntry.
  248. where(
  249. :activity_id => project_activity.id,
  250. :project_id => 1
  251. ).count,
  252. "TimeEntries still assigned to project specific activity"
  253. )
  254. assert_equal(
  255. 3,
  256. TimeEntry.
  257. where(
  258. :activity_id => 9,
  259. :project_id => 1
  260. ).count,
  261. "TimeEntries still assigned to project specific activity"
  262. )
  263. end
  264. end