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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 TimeEntryTest < ActiveSupport::TestCase
  19. include Redmine::I18n
  20. fixtures :issues, :projects, :users, :time_entries,
  21. :members, :roles, :member_roles,
  22. :trackers, :issue_statuses,
  23. :projects_trackers,
  24. :journals, :journal_details,
  25. :issue_categories, :enumerations,
  26. :groups_users,
  27. :enabled_modules
  28. def test_visibility_with_permission_to_view_all_time_entries
  29. user = User.generate!
  30. role = Role.generate!(:permissions => [:view_time_entries], :time_entries_visibility => 'all')
  31. Role.non_member.remove_permission! :view_time_entries
  32. project = Project.find(1)
  33. User.add_to_project user, project, role
  34. own = TimeEntry.generate! :user => user, :project => project
  35. other = TimeEntry.generate! :user => User.find(2), :project => project
  36. assert TimeEntry.visible(user).find_by_id(own.id)
  37. assert TimeEntry.visible(user).find_by_id(other.id)
  38. assert own.visible?(user)
  39. assert other.visible?(user)
  40. end
  41. def test_visibility_with_permission_to_view_own_time_entries
  42. user = User.generate!
  43. role = Role.generate!(:permissions => [:view_time_entries], :time_entries_visibility => 'own')
  44. Role.non_member.remove_permission! :view_time_entries
  45. project = Project.find(1)
  46. User.add_to_project user, project, role
  47. own = TimeEntry.generate! :user => user, :project => project
  48. other = TimeEntry.generate! :user => User.find(2), :project => project
  49. assert TimeEntry.visible(user).find_by_id(own.id)
  50. assert_nil TimeEntry.visible(user).find_by_id(other.id)
  51. assert own.visible?(user)
  52. assert_equal false, other.visible?(user)
  53. end
  54. def test_hours_format
  55. assertions = { "2" => 2.0,
  56. "21.1" => 21.1,
  57. "2,1" => 2.1,
  58. "1,5h" => 1.5,
  59. "7:12" => 7.2,
  60. "10h" => 10.0,
  61. "10 h" => 10.0,
  62. "45m" => 0.75,
  63. "45 m" => 0.75,
  64. "3h15" => 3.25,
  65. "3h 15" => 3.25,
  66. "3 h 15" => 3.25,
  67. "3 h 15m" => 3.25,
  68. "3 h 15 m" => 3.25,
  69. "3 hours" => 3.0,
  70. "12min" => 0.2,
  71. "12 Min" => 0.2,
  72. }
  73. assertions.each do |k, v|
  74. t = TimeEntry.new(:hours => k)
  75. assert_equal v, t.hours, "Converting #{k} failed:"
  76. end
  77. end
  78. def test_hours_should_default_to_nil
  79. assert_nil TimeEntry.new.hours
  80. end
  81. def test_should_accept_0_hours
  82. entry = TimeEntry.generate
  83. entry.hours = 0
  84. assert entry.save
  85. end
  86. def test_should_not_accept_0_hours_if_disabled
  87. with_settings :timelog_accept_0_hours => '0' do
  88. entry = TimeEntry.generate
  89. entry.hours = 0
  90. assert !entry.save
  91. assert entry.errors[:hours].present?
  92. end
  93. end
  94. def test_should_not_accept_more_than_maximum_hours_per_day_and_user
  95. with_settings :timelog_max_hours_per_day => '8' do
  96. entry = TimeEntry.generate(:spent_on => '2017-07-16', :hours => 6.0, :user_id => 2)
  97. assert entry.save
  98. entry = TimeEntry.generate(:spent_on => '2017-07-16', :hours => 1.5, :user_id => 2)
  99. assert entry.save
  100. entry = TimeEntry.generate(:spent_on => '2017-07-16', :hours => 3.0, :user_id => 2)
  101. assert !entry.save
  102. end
  103. end
  104. def test_spent_on_with_blank
  105. c = TimeEntry.new
  106. c.spent_on = ''
  107. assert_nil c.spent_on
  108. end
  109. def test_spent_on_with_nil
  110. c = TimeEntry.new
  111. c.spent_on = nil
  112. assert_nil c.spent_on
  113. end
  114. def test_spent_on_with_string
  115. c = TimeEntry.new
  116. c.spent_on = "2011-01-14"
  117. assert_equal Date.parse("2011-01-14"), c.spent_on
  118. end
  119. def test_spent_on_with_invalid_string
  120. c = TimeEntry.new
  121. c.spent_on = "foo"
  122. assert_nil c.spent_on
  123. end
  124. def test_spent_on_with_date
  125. c = TimeEntry.new
  126. c.spent_on = Date.today
  127. assert_equal Date.today, c.spent_on
  128. end
  129. def test_spent_on_with_time
  130. c = TimeEntry.new
  131. c.spent_on = Time.now
  132. assert_kind_of Date, c.spent_on
  133. end
  134. def test_validate_time_entry
  135. anon = User.anonymous
  136. project = Project.find(1)
  137. issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
  138. :priority => IssuePriority.all.first, :subject => 'test_create',
  139. :description => 'IssueTest#test_create', :estimated_hours => '1:30')
  140. assert issue.save
  141. activity = TimeEntryActivity.find_by_name('Design')
  142. te = TimeEntry.create(:spent_on => '2010-01-01',
  143. :hours => 100000,
  144. :issue => issue,
  145. :project => project,
  146. :user => anon,
  147. :activity => activity)
  148. assert_equal 1, te.errors.count
  149. end
  150. def test_acitivity_should_belong_to_project_activities
  151. activity = TimeEntryActivity.create!(:name => 'Other project activity', :project_id => 2, :active => true)
  152. entry = TimeEntry.new(:spent_on => Date.today, :hours => 1.0, :user => User.find(1), :project_id => 1, :activity => activity)
  153. assert !entry.valid?
  154. assert_include I18n.translate('activerecord.errors.messages.inclusion'), entry.errors[:activity_id]
  155. end
  156. def test_spent_on_with_2_digits_year_should_not_be_valid
  157. entry = TimeEntry.new(:project => Project.find(1), :user => User.find(1), :activity => TimeEntryActivity.first, :hours => 1)
  158. entry.spent_on = "09-02-04"
  159. assert !entry.valid?
  160. assert_include I18n.translate('activerecord.errors.messages.not_a_date'), entry.errors[:spent_on]
  161. end
  162. def test_set_project_if_nil
  163. anon = User.anonymous
  164. project = Project.find(1)
  165. issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
  166. :priority => IssuePriority.all.first, :subject => 'test_create',
  167. :description => 'IssueTest#test_create', :estimated_hours => '1:30')
  168. assert issue.save
  169. activity = TimeEntryActivity.find_by_name('Design')
  170. te = TimeEntry.create(:spent_on => '2010-01-01',
  171. :hours => 10,
  172. :issue => issue,
  173. :user => anon,
  174. :activity => activity)
  175. assert_equal project.id, te.project.id
  176. end
  177. def test_create_with_required_issue_id_and_comment_should_be_validated
  178. set_language_if_valid 'en'
  179. with_settings :timelog_required_fields => ['issue_id' , 'comments'] do
  180. entry = TimeEntry.new(:project => Project.find(1), :spent_on => Date.today, :user => User.find(1), :activity => TimeEntryActivity.first, :hours => 1)
  181. assert !entry.save
  182. assert_equal ["Comment cannot be blank", "Issue cannot be blank"], entry.errors.full_messages.sort
  183. end
  184. end
  185. end