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.

time_entry_test.rb 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 TimeEntryTest < ActiveSupport::TestCase
  20. include Redmine::I18n
  21. fixtures :issues, :projects, :users, :time_entries,
  22. :members, :roles, :member_roles,
  23. :trackers, :issue_statuses,
  24. :projects_trackers,
  25. :journals, :journal_details,
  26. :issue_categories, :enumerations,
  27. :groups_users,
  28. :enabled_modules,
  29. :custom_fields, :custom_fields_projects, :custom_values
  30. def setup
  31. User.current = nil
  32. end
  33. def test_visibility_with_permission_to_view_all_time_entries
  34. user = User.generate!
  35. role = Role.generate!(:permissions => [:view_time_entries], :time_entries_visibility => 'all')
  36. Role.non_member.remove_permission! :view_time_entries
  37. project = Project.find(1)
  38. User.add_to_project user, project, role
  39. own = TimeEntry.generate! :user => user, :project => project
  40. other = TimeEntry.generate! :user => User.find(2), :project => project
  41. assert TimeEntry.visible(user).find_by_id(own.id)
  42. assert TimeEntry.visible(user).find_by_id(other.id)
  43. assert own.visible?(user)
  44. assert other.visible?(user)
  45. end
  46. def test_visibility_with_permission_to_view_own_time_entries
  47. user = User.generate!
  48. role = Role.generate!(:permissions => [:view_time_entries], :time_entries_visibility => 'own')
  49. Role.non_member.remove_permission! :view_time_entries
  50. project = Project.find(1)
  51. User.add_to_project user, project, role
  52. own = TimeEntry.generate! :user => user, :project => project
  53. other = TimeEntry.generate! :user => User.find(2), :project => project
  54. assert TimeEntry.visible(user).find_by_id(own.id)
  55. assert_nil TimeEntry.visible(user).find_by_id(other.id)
  56. assert own.visible?(user)
  57. assert_equal false, other.visible?(user)
  58. end
  59. def test_hours_format
  60. assertions = {
  61. "2" => 2.0,
  62. "21.1" => 21.1,
  63. "2,1" => 2.1,
  64. "1,5h" => 1.5,
  65. "7:12" => 7.2,
  66. "10h" => 10.0,
  67. "10 h" => 10.0,
  68. "45m" => 0.75,
  69. "45 m" => 0.75,
  70. "3h15" => 3.25,
  71. "3h 15" => 3.25,
  72. "3 h 15" => 3.25,
  73. "3 h 15m" => 3.25,
  74. "3 h 15 m" => 3.25,
  75. "3 hours" => 3.0,
  76. "12min" => 0.2,
  77. "12 Min" => 0.2,
  78. }
  79. assertions.each do |k, v|
  80. t = TimeEntry.new(:hours => k)
  81. assert_equal v, t.hours, "Converting #{k} failed:"
  82. end
  83. end
  84. def test_hours_should_default_to_nil
  85. assert_nil TimeEntry.new.hours
  86. end
  87. def test_should_accept_0_hours
  88. entry = TimeEntry.generate
  89. entry.hours = 0
  90. assert entry.save
  91. end
  92. def test_should_not_accept_0_hours_if_disabled
  93. with_settings :timelog_accept_0_hours => '0' do
  94. entry = TimeEntry.generate
  95. entry.hours = 0
  96. assert !entry.save
  97. assert entry.errors[:hours].present?
  98. end
  99. end
  100. def test_should_not_accept_more_than_maximum_hours_per_day_and_user
  101. with_settings :timelog_max_hours_per_day => '8' do
  102. entry = TimeEntry.generate(:spent_on => '2017-07-16', :hours => 6.0, :user_id => 2)
  103. assert entry.save
  104. entry = TimeEntry.generate(:spent_on => '2017-07-16', :hours => 1.5, :user_id => 2)
  105. assert entry.save
  106. entry = TimeEntry.generate(:spent_on => '2017-07-16', :hours => 3.0, :user_id => 2)
  107. assert !entry.save
  108. end
  109. end
  110. def test_activity_id_should_default_activity_id
  111. project = Project.find(1)
  112. default_activity = TimeEntryActivity.find(10)
  113. entry = TimeEntry.new(project: project)
  114. assert_equal entry.activity_id, default_activity.id
  115. # If there are project specific activities
  116. project_specific_default_activity = TimeEntryActivity.create!(name: 'Development', parent_id: 10, project_id: project.id, is_default: false)
  117. entry = TimeEntry.new(project: project)
  118. assert_not_equal entry.activity_id, default_activity.id
  119. assert_equal entry.activity_id, project_specific_default_activity.id
  120. end
  121. def test_activity_id_should_be_set_automatically_if_there_is_only_one_activity_available
  122. project = Project.find(1)
  123. TimeEntry.destroy_all
  124. TimeEntryActivity.destroy_all
  125. only_one_activity = TimeEntryActivity.create!(
  126. name: 'Development',
  127. parent_id: nil,
  128. project_id: nil,
  129. is_default: false
  130. )
  131. entry = TimeEntry.new(project: project)
  132. assert_equal entry.activity_id, only_one_activity.id
  133. end
  134. def test_should_accept_future_dates
  135. entry = TimeEntry.generate
  136. entry.spent_on = User.current.today + 1
  137. assert entry.save
  138. end
  139. def test_should_not_accept_future_dates_if_disabled
  140. with_settings :timelog_accept_future_dates => '0' do
  141. entry = TimeEntry.generate
  142. entry.spent_on = User.current.today + 1
  143. assert !entry.save
  144. assert entry.errors[:base].present?
  145. end
  146. end
  147. def test_should_require_spent_on
  148. with_settings :timelog_accept_future_dates => '0' do
  149. entry = TimeEntry.find(1)
  150. entry.spent_on = ''
  151. assert !entry.save
  152. assert entry.errors[:spent_on].present?
  153. end
  154. end
  155. def test_spent_on_with_blank
  156. c = TimeEntry.new
  157. c.spent_on = ''
  158. assert_nil c.spent_on
  159. end
  160. def test_spent_on_with_nil
  161. c = TimeEntry.new
  162. c.spent_on = nil
  163. assert_nil c.spent_on
  164. end
  165. def test_spent_on_with_string
  166. c = TimeEntry.new
  167. c.spent_on = "2011-01-14"
  168. assert_equal Date.parse("2011-01-14"), c.spent_on
  169. end
  170. def test_spent_on_with_invalid_string
  171. c = TimeEntry.new
  172. c.spent_on = "foo"
  173. assert_nil c.spent_on
  174. end
  175. def test_spent_on_with_date
  176. c = TimeEntry.new
  177. c.spent_on = Date.today
  178. assert_equal Date.today, c.spent_on
  179. end
  180. def test_spent_on_with_time
  181. c = TimeEntry.new
  182. c.spent_on = Time.now
  183. assert_kind_of Date, c.spent_on
  184. end
  185. def test_validate_time_entry
  186. anon = User.anonymous
  187. project = Project.find(1)
  188. issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
  189. :priority => IssuePriority.first, :subject => 'test_create',
  190. :description => 'IssueTest#test_create', :estimated_hours => '1:30')
  191. assert issue.save
  192. activity = TimeEntryActivity.find_by_name('Design')
  193. te = TimeEntry.create(:spent_on => '2010-01-01',
  194. :hours => 100000,
  195. :issue => issue,
  196. :project => project,
  197. :user => anon,
  198. :author => anon,
  199. :activity => activity)
  200. assert_equal 1, te.errors.count
  201. end
  202. def test_acitivity_should_belong_to_project_activities
  203. activity = TimeEntryActivity.create!(:name => 'Other project activity', :project_id => 2, :active => true)
  204. entry = TimeEntry.new(:spent_on => Date.today, :hours => 1.0, :user => User.find(1), :project_id => 1, :activity => activity)
  205. assert entry.invalid?
  206. assert_include I18n.translate('activerecord.errors.messages.inclusion'), entry.errors[:activity_id]
  207. end
  208. def test_spent_on_with_2_digits_year_should_not_be_valid
  209. entry = TimeEntry.new(:project => Project.find(1), :user => User.find(1), :activity => TimeEntryActivity.first, :hours => 1)
  210. entry.spent_on = "09-02-04"
  211. assert entry.invalid?
  212. assert_include I18n.translate('activerecord.errors.messages.not_a_date'), entry.errors[:spent_on]
  213. end
  214. def test_set_project_if_nil
  215. anon = User.anonymous
  216. project = Project.find(1)
  217. issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
  218. :priority => IssuePriority.first, :subject => 'test_create',
  219. :description => 'IssueTest#test_create', :estimated_hours => '1:30')
  220. assert issue.save
  221. activity = TimeEntryActivity.find_by_name('Design')
  222. te = TimeEntry.create(:spent_on => '2010-01-01',
  223. :hours => 10,
  224. :issue => issue,
  225. :user => anon,
  226. :activity => activity)
  227. assert_equal project.id, te.project.id
  228. end
  229. def test_create_with_required_issue_id_and_comment_should_be_validated
  230. set_language_if_valid 'en'
  231. with_settings :timelog_required_fields => ['issue_id', 'comments'] do
  232. entry = TimeEntry.new(:project => Project.find(1),
  233. :spent_on => Date.today,
  234. :author => User.find(1),
  235. :user => User.find(1),
  236. :activity => TimeEntryActivity.first,
  237. :hours => 1)
  238. assert !entry.save
  239. assert_equal ["Comment cannot be blank", "Issue cannot be blank"], entry.errors.full_messages.sort
  240. end
  241. end
  242. def test_create_should_validate_user_id
  243. set_language_if_valid 'en'
  244. entry = TimeEntry.new(:spent_on => '2010-01-01',
  245. :hours => 10,
  246. :project_id => 1,
  247. :user_id => 4)
  248. assert !entry.save
  249. assert_equal ["User is invalid"], entry.errors.full_messages.sort
  250. end
  251. def test_assignable_users_should_include_active_project_members_with_log_time_permission
  252. Role.find(2).remove_permission! :log_time
  253. time_entry = TimeEntry.find(1)
  254. assert_equal [2], time_entry.assignable_users.map(&:id)
  255. end
  256. end