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