@@ -1,184 +1,187 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class TimeEntryTest < ActiveSupport::TestCase |
|
21 | include Redmine::I18n | |
|
22 | ||
|
21 | 23 | fixtures :issues, :projects, :users, :time_entries, |
|
22 | 24 | :members, :roles, :member_roles, |
|
23 | 25 | :trackers, :issue_statuses, |
|
24 | 26 | :projects_trackers, |
|
25 | 27 | :journals, :journal_details, |
|
26 | 28 | :issue_categories, :enumerations, |
|
27 | 29 | :groups_users, |
|
28 | 30 | :enabled_modules |
|
29 | 31 | |
|
30 | 32 | def test_visibility_with_permission_to_view_all_time_entries |
|
31 | 33 | user = User.generate! |
|
32 | 34 | role = Role.generate!(:permissions => [:view_time_entries], :time_entries_visibility => 'all') |
|
33 | 35 | Role.non_member.remove_permission! :view_time_entries |
|
34 | 36 | project = Project.find(1) |
|
35 | 37 | User.add_to_project user, project, role |
|
36 | 38 | own = TimeEntry.generate! :user => user, :project => project |
|
37 | 39 | other = TimeEntry.generate! :user => User.find(2), :project => project |
|
38 | 40 | |
|
39 | 41 | assert TimeEntry.visible(user).find_by_id(own.id) |
|
40 | 42 | assert TimeEntry.visible(user).find_by_id(other.id) |
|
41 | 43 | |
|
42 | 44 | assert own.visible?(user) |
|
43 | 45 | assert other.visible?(user) |
|
44 | 46 | end |
|
45 | 47 | |
|
46 | 48 | def test_visibility_with_permission_to_view_own_time_entries |
|
47 | 49 | user = User.generate! |
|
48 | 50 | role = Role.generate!(:permissions => [:view_time_entries], :time_entries_visibility => 'own') |
|
49 | 51 | Role.non_member.remove_permission! :view_time_entries |
|
50 | 52 | project = Project.find(1) |
|
51 | 53 | User.add_to_project user, project, role |
|
52 | 54 | own = TimeEntry.generate! :user => user, :project => project |
|
53 | 55 | other = TimeEntry.generate! :user => User.find(2), :project => project |
|
54 | 56 | |
|
55 | 57 | assert TimeEntry.visible(user).find_by_id(own.id) |
|
56 | 58 | assert_nil TimeEntry.visible(user).find_by_id(other.id) |
|
57 | 59 | |
|
58 | 60 | assert own.visible?(user) |
|
59 | 61 | assert_equal false, other.visible?(user) |
|
60 | 62 | end |
|
61 | 63 | |
|
62 | 64 | def test_hours_format |
|
63 | 65 | assertions = { "2" => 2.0, |
|
64 | 66 | "21.1" => 21.1, |
|
65 | 67 | "2,1" => 2.1, |
|
66 | 68 | "1,5h" => 1.5, |
|
67 | 69 | "7:12" => 7.2, |
|
68 | 70 | "10h" => 10.0, |
|
69 | 71 | "10 h" => 10.0, |
|
70 | 72 | "45m" => 0.75, |
|
71 | 73 | "45 m" => 0.75, |
|
72 | 74 | "3h15" => 3.25, |
|
73 | 75 | "3h 15" => 3.25, |
|
74 | 76 | "3 h 15" => 3.25, |
|
75 | 77 | "3 h 15m" => 3.25, |
|
76 | 78 | "3 h 15 m" => 3.25, |
|
77 | 79 | "3 hours" => 3.0, |
|
78 | 80 | "12min" => 0.2, |
|
79 | 81 | "12 Min" => 0.2, |
|
80 | 82 | } |
|
81 | 83 | |
|
82 | 84 | assertions.each do |k, v| |
|
83 | 85 | t = TimeEntry.new(:hours => k) |
|
84 | 86 | assert_equal v, t.hours, "Converting #{k} failed:" |
|
85 | 87 | end |
|
86 | 88 | end |
|
87 | 89 | |
|
88 | 90 | def test_hours_should_default_to_nil |
|
89 | 91 | assert_nil TimeEntry.new.hours |
|
90 | 92 | end |
|
91 | 93 | |
|
92 | 94 | def test_spent_on_with_blank |
|
93 | 95 | c = TimeEntry.new |
|
94 | 96 | c.spent_on = '' |
|
95 | 97 | assert_nil c.spent_on |
|
96 | 98 | end |
|
97 | 99 | |
|
98 | 100 | def test_spent_on_with_nil |
|
99 | 101 | c = TimeEntry.new |
|
100 | 102 | c.spent_on = nil |
|
101 | 103 | assert_nil c.spent_on |
|
102 | 104 | end |
|
103 | 105 | |
|
104 | 106 | def test_spent_on_with_string |
|
105 | 107 | c = TimeEntry.new |
|
106 | 108 | c.spent_on = "2011-01-14" |
|
107 | 109 | assert_equal Date.parse("2011-01-14"), c.spent_on |
|
108 | 110 | end |
|
109 | 111 | |
|
110 | 112 | def test_spent_on_with_invalid_string |
|
111 | 113 | c = TimeEntry.new |
|
112 | 114 | c.spent_on = "foo" |
|
113 | 115 | assert_nil c.spent_on |
|
114 | 116 | end |
|
115 | 117 | |
|
116 | 118 | def test_spent_on_with_date |
|
117 | 119 | c = TimeEntry.new |
|
118 | 120 | c.spent_on = Date.today |
|
119 | 121 | assert_equal Date.today, c.spent_on |
|
120 | 122 | end |
|
121 | 123 | |
|
122 | 124 | def test_spent_on_with_time |
|
123 | 125 | c = TimeEntry.new |
|
124 | 126 | c.spent_on = Time.now |
|
125 | 127 | assert_kind_of Date, c.spent_on |
|
126 | 128 | end |
|
127 | 129 | |
|
128 | 130 | def test_validate_time_entry |
|
129 | 131 | anon = User.anonymous |
|
130 | 132 | project = Project.find(1) |
|
131 | 133 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1, |
|
132 | 134 | :priority => IssuePriority.all.first, :subject => 'test_create', |
|
133 | 135 | :description => 'IssueTest#test_create', :estimated_hours => '1:30') |
|
134 | 136 | assert issue.save |
|
135 | 137 | activity = TimeEntryActivity.find_by_name('Design') |
|
136 | 138 | te = TimeEntry.create(:spent_on => '2010-01-01', |
|
137 | 139 | :hours => 100000, |
|
138 | 140 | :issue => issue, |
|
139 | 141 | :project => project, |
|
140 | 142 | :user => anon, |
|
141 | 143 | :activity => activity) |
|
142 | 144 | assert_equal 1, te.errors.count |
|
143 | 145 | end |
|
144 | 146 | |
|
145 | 147 | def test_acitivity_should_belong_to_project_activities |
|
146 | 148 | activity = TimeEntryActivity.create!(:name => 'Other project activity', :project_id => 2, :active => true) |
|
147 | 149 | |
|
148 | 150 | entry = TimeEntry.new(:spent_on => Date.today, :hours => 1.0, :user => User.find(1), :project_id => 1, :activity => activity) |
|
149 | 151 | assert !entry.valid? |
|
150 | 152 | assert_include I18n.translate('activerecord.errors.messages.inclusion'), entry.errors[:activity_id] |
|
151 | 153 | end |
|
152 | 154 | |
|
153 | 155 | def test_spent_on_with_2_digits_year_should_not_be_valid |
|
154 | 156 | entry = TimeEntry.new(:project => Project.find(1), :user => User.find(1), :activity => TimeEntryActivity.first, :hours => 1) |
|
155 | 157 | entry.spent_on = "09-02-04" |
|
156 | 158 | assert !entry.valid? |
|
157 | 159 | assert_include I18n.translate('activerecord.errors.messages.not_a_date'), entry.errors[:spent_on] |
|
158 | 160 | end |
|
159 | 161 | |
|
160 | 162 | def test_set_project_if_nil |
|
161 | 163 | anon = User.anonymous |
|
162 | 164 | project = Project.find(1) |
|
163 | 165 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1, |
|
164 | 166 | :priority => IssuePriority.all.first, :subject => 'test_create', |
|
165 | 167 | :description => 'IssueTest#test_create', :estimated_hours => '1:30') |
|
166 | 168 | assert issue.save |
|
167 | 169 | activity = TimeEntryActivity.find_by_name('Design') |
|
168 | 170 | te = TimeEntry.create(:spent_on => '2010-01-01', |
|
169 | 171 | :hours => 10, |
|
170 | 172 | :issue => issue, |
|
171 | 173 | :user => anon, |
|
172 | 174 | :activity => activity) |
|
173 | 175 | assert_equal project.id, te.project.id |
|
174 | 176 | end |
|
175 | 177 | |
|
176 | 178 | def test_create_with_required_issue_id_and_comment_should_be_validated |
|
179 | set_language_if_valid 'en' | |
|
177 | 180 | with_settings :timelog_required_fields => ['issue_id' , 'comments'] do |
|
178 | 181 | entry = TimeEntry.new(:project => Project.find(1), :spent_on => Date.today, :user => User.find(1), :activity => TimeEntryActivity.first, :hours => 1) |
|
179 | 182 | |
|
180 | 183 | assert !entry.save |
|
181 | 184 | assert_equal ["Comment cannot be blank", "Issue cannot be blank"], entry.errors.full_messages.sort |
|
182 | 185 | end |
|
183 | 186 | end |
|
184 | 187 | end |
General Comments 0
You need to be logged in to leave comments.
Login now