@@ -1,120 +1,120 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | class TimeEntry < ActiveRecord::Base |
|
19 | 19 | include Redmine::SafeAttributes |
|
20 | 20 | # could have used polymorphic association |
|
21 | 21 | # project association here allows easy loading of time entries at project level with one database trip |
|
22 | 22 | belongs_to :project |
|
23 | 23 | belongs_to :issue |
|
24 | 24 | belongs_to :user |
|
25 | 25 | belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id' |
|
26 | 26 | |
|
27 | 27 | attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek |
|
28 | 28 | |
|
29 | 29 | acts_as_customizable |
|
30 | 30 | acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"}, |
|
31 | 31 | :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}}, |
|
32 | 32 | :author => :user, |
|
33 | 33 | :description => :comments |
|
34 | 34 | |
|
35 | 35 | acts_as_activity_provider :timestamp => "#{table_name}.created_on", |
|
36 | 36 | :author_key => :user_id, |
|
37 | 37 | :find_options => {:include => :project} |
|
38 | 38 | |
|
39 | 39 | validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on |
|
40 | 40 | validates_numericality_of :hours, :allow_nil => true, :message => :invalid |
|
41 | 41 | validates_length_of :comments, :maximum => 255, :allow_nil => true |
|
42 | 42 | before_validation :set_project_if_nil |
|
43 | 43 | validate :validate_time_entry |
|
44 | 44 | |
|
45 | 45 | named_scope :visible, lambda {|*args| { |
|
46 | 46 | :include => :project, |
|
47 | 47 | :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args) |
|
48 | 48 | }} |
|
49 | 49 | named_scope :on_issue, lambda {|issue| { |
|
50 | 50 | :include => :issue, |
|
51 | 51 | :conditions => "#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}" |
|
52 | 52 | }} |
|
53 | 53 | named_scope :on_project, lambda {|project, include_subprojects| { |
|
54 | 54 | :include => :project, |
|
55 | 55 | :conditions => project.project_condition(include_subprojects) |
|
56 | 56 | }} |
|
57 | 57 | named_scope :spent_between, lambda {|from, to| |
|
58 | 58 | if from && to |
|
59 | 59 | {:conditions => ["#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to]} |
|
60 | 60 | elsif from |
|
61 | 61 | {:conditions => ["#{TimeEntry.table_name}.spent_on >= ?", from]} |
|
62 | 62 | elsif to |
|
63 | 63 | {:conditions => ["#{TimeEntry.table_name}.spent_on <= ?", to]} |
|
64 | 64 | else |
|
65 | 65 | {} |
|
66 | 66 | end |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values' | |
|
69 | safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields' | |
|
70 | 70 | |
|
71 | 71 | def initialize(attributes=nil, *args) |
|
72 | 72 | super |
|
73 | 73 | if new_record? && self.activity.nil? |
|
74 | 74 | if default_activity = TimeEntryActivity.default |
|
75 | 75 | self.activity_id = default_activity.id |
|
76 | 76 | end |
|
77 | 77 | self.hours = nil if hours == 0 |
|
78 | 78 | end |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def set_project_if_nil |
|
82 | 82 | self.project = issue.project if issue && project.nil? |
|
83 | 83 | end |
|
84 | 84 | |
|
85 | 85 | def validate_time_entry |
|
86 | 86 | errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000) |
|
87 | 87 | errors.add :project_id, :invalid if project.nil? |
|
88 | 88 | errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | def hours=(h) |
|
92 | 92 | write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) |
|
93 | 93 | end |
|
94 | 94 | |
|
95 | 95 | def hours |
|
96 | 96 | h = read_attribute(:hours) |
|
97 | 97 | if h.is_a?(Float) |
|
98 | 98 | h.round(2) |
|
99 | 99 | else |
|
100 | 100 | h |
|
101 | 101 | end |
|
102 | 102 | end |
|
103 | 103 | |
|
104 | 104 | # tyear, tmonth, tweek assigned where setting spent_on attributes |
|
105 | 105 | # these attributes make time aggregations easier |
|
106 | 106 | def spent_on=(date) |
|
107 | 107 | super |
|
108 | 108 | if spent_on.is_a?(Time) |
|
109 | 109 | self.spent_on = spent_on.to_date |
|
110 | 110 | end |
|
111 | 111 | self.tyear = spent_on ? spent_on.year : nil |
|
112 | 112 | self.tmonth = spent_on ? spent_on.month : nil |
|
113 | 113 | self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil |
|
114 | 114 | end |
|
115 | 115 | |
|
116 | 116 | # Returns true if the time entry can be edited by usr, otherwise false |
|
117 | 117 | def editable_by?(usr) |
|
118 | 118 | (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) |
|
119 | 119 | end |
|
120 | 120 | end |
@@ -1,148 +1,163 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 ApiTest::TimeEntriesTest < ActionController::IntegrationTest |
|
21 | 21 | fixtures :projects, :trackers, :issue_statuses, :issues, |
|
22 | 22 | :enumerations, :users, :issue_categories, |
|
23 | 23 | :projects_trackers, |
|
24 | 24 | :roles, |
|
25 | 25 | :member_roles, |
|
26 | 26 | :members, |
|
27 | 27 | :enabled_modules, |
|
28 | 28 | :workflows, |
|
29 | 29 | :time_entries |
|
30 | 30 | |
|
31 | 31 | def setup |
|
32 | 32 | Setting.rest_api_enabled = '1' |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | context "GET /time_entries.xml" do |
|
36 | 36 | should "return time entries" do |
|
37 | 37 | get '/time_entries.xml', {}, credentials('jsmith') |
|
38 | 38 | assert_response :success |
|
39 | 39 | assert_equal 'application/xml', @response.content_type |
|
40 | 40 | assert_tag :tag => 'time_entries', |
|
41 | 41 | :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}} |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | context "with limit" do |
|
45 | 45 | should "return limited results" do |
|
46 | 46 | get '/time_entries.xml?limit=2', {}, credentials('jsmith') |
|
47 | 47 | assert_response :success |
|
48 | 48 | assert_equal 'application/xml', @response.content_type |
|
49 | 49 | assert_tag :tag => 'time_entries', |
|
50 | 50 | :children => {:count => 2} |
|
51 | 51 | end |
|
52 | 52 | end |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | context "GET /time_entries/2.xml" do |
|
56 | 56 | should "return requested time entry" do |
|
57 | 57 | get '/time_entries/2.xml', {}, credentials('jsmith') |
|
58 | 58 | assert_response :success |
|
59 | 59 | assert_equal 'application/xml', @response.content_type |
|
60 | 60 | assert_tag :tag => 'time_entry', |
|
61 | 61 | :child => {:tag => 'id', :content => '2'} |
|
62 | 62 | end |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | context "POST /time_entries.xml" do |
|
66 | 66 | context "with issue_id" do |
|
67 | 67 | should "return create time entry" do |
|
68 | 68 | assert_difference 'TimeEntry.count' do |
|
69 | 69 | post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith') |
|
70 | 70 | end |
|
71 | 71 | assert_response :created |
|
72 | 72 | assert_equal 'application/xml', @response.content_type |
|
73 | 73 | |
|
74 | 74 | entry = TimeEntry.first(:order => 'id DESC') |
|
75 | 75 | assert_equal 'jsmith', entry.user.login |
|
76 | 76 | assert_equal Issue.find(1), entry.issue |
|
77 | 77 | assert_equal Project.find(1), entry.project |
|
78 | 78 | assert_equal Date.parse('2010-12-02'), entry.spent_on |
|
79 | 79 | assert_equal 3.5, entry.hours |
|
80 | 80 | assert_equal TimeEntryActivity.find(11), entry.activity |
|
81 | 81 | end |
|
82 | ||
|
83 | should "accept custom fields" do | |
|
84 | field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string') | |
|
85 | ||
|
86 | assert_difference 'TimeEntry.count' do | |
|
87 | post '/time_entries.xml', {:time_entry => { | |
|
88 | :issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11', :custom_fields => [{:id => field.id.to_s, :value => 'accepted'}] | |
|
89 | }}, credentials('jsmith') | |
|
90 | end | |
|
91 | assert_response :created | |
|
92 | assert_equal 'application/xml', @response.content_type | |
|
93 | ||
|
94 | entry = TimeEntry.first(:order => 'id DESC') | |
|
95 | assert_equal 'accepted', entry.custom_field_value(field) | |
|
96 | end | |
|
82 | 97 | end |
|
83 | 98 | |
|
84 | 99 | context "with project_id" do |
|
85 | 100 | should "return create time entry" do |
|
86 | 101 | assert_difference 'TimeEntry.count' do |
|
87 | 102 | post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith') |
|
88 | 103 | end |
|
89 | 104 | assert_response :created |
|
90 | 105 | assert_equal 'application/xml', @response.content_type |
|
91 | 106 | |
|
92 | 107 | entry = TimeEntry.first(:order => 'id DESC') |
|
93 | 108 | assert_equal 'jsmith', entry.user.login |
|
94 | 109 | assert_nil entry.issue |
|
95 | 110 | assert_equal Project.find(1), entry.project |
|
96 | 111 | assert_equal Date.parse('2010-12-02'), entry.spent_on |
|
97 | 112 | assert_equal 3.5, entry.hours |
|
98 | 113 | assert_equal TimeEntryActivity.find(11), entry.activity |
|
99 | 114 | end |
|
100 | 115 | end |
|
101 | 116 | |
|
102 | 117 | context "with invalid parameters" do |
|
103 | 118 | should "return errors" do |
|
104 | 119 | assert_no_difference 'TimeEntry.count' do |
|
105 | 120 | post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, credentials('jsmith') |
|
106 | 121 | end |
|
107 | 122 | assert_response :unprocessable_entity |
|
108 | 123 | assert_equal 'application/xml', @response.content_type |
|
109 | 124 | |
|
110 | 125 | assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"} |
|
111 | 126 | end |
|
112 | 127 | end |
|
113 | 128 | end |
|
114 | 129 | |
|
115 | 130 | context "PUT /time_entries/2.xml" do |
|
116 | 131 | context "with valid parameters" do |
|
117 | 132 | should "update time entry" do |
|
118 | 133 | assert_no_difference 'TimeEntry.count' do |
|
119 | 134 | put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith') |
|
120 | 135 | end |
|
121 | 136 | assert_response :ok |
|
122 | 137 | assert_equal 'API Update', TimeEntry.find(2).comments |
|
123 | 138 | end |
|
124 | 139 | end |
|
125 | 140 | |
|
126 | 141 | context "with invalid parameters" do |
|
127 | 142 | should "return errors" do |
|
128 | 143 | assert_no_difference 'TimeEntry.count' do |
|
129 | 144 | put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith') |
|
130 | 145 | end |
|
131 | 146 | assert_response :unprocessable_entity |
|
132 | 147 | assert_equal 'application/xml', @response.content_type |
|
133 | 148 | |
|
134 | 149 | assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"} |
|
135 | 150 | end |
|
136 | 151 | end |
|
137 | 152 | end |
|
138 | 153 | |
|
139 | 154 | context "DELETE /time_entries/2.xml" do |
|
140 | 155 | should "destroy time entry" do |
|
141 | 156 | assert_difference 'TimeEntry.count', -1 do |
|
142 | 157 | delete '/time_entries/2.xml', {}, credentials('jsmith') |
|
143 | 158 | end |
|
144 | 159 | assert_response :ok |
|
145 | 160 | assert_nil TimeEntry.find_by_id(2) |
|
146 | 161 | end |
|
147 | 162 | end |
|
148 | 163 | end |
General Comments 0
You need to be logged in to leave comments.
Login now