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

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