@@ -1,121 +1,128 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 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 QueriesController < ApplicationController |
|
19 | 19 | menu_item :issues |
|
20 | 20 | before_filter :find_query, :except => [:new, :create, :index] |
|
21 | 21 | before_filter :find_optional_project, :only => [:new, :create] |
|
22 | 22 | |
|
23 | 23 | accept_api_auth :index |
|
24 | 24 | |
|
25 | 25 | include QueriesHelper |
|
26 | 26 | |
|
27 | 27 | def index |
|
28 | 28 | case params[:format] |
|
29 | 29 | when 'xml', 'json' |
|
30 | 30 | @offset, @limit = api_offset_and_limit |
|
31 | 31 | else |
|
32 | 32 | @limit = per_page_option |
|
33 | 33 | end |
|
34 | 34 | @query_count = IssueQuery.visible.count |
|
35 | 35 | @query_pages = Paginator.new @query_count, @limit, params['page'] |
|
36 | 36 | @queries = IssueQuery.visible. |
|
37 | 37 | order("#{Query.table_name}.name"). |
|
38 | 38 | limit(@limit). |
|
39 | 39 | offset(@offset). |
|
40 | 40 | to_a |
|
41 | 41 | respond_to do |format| |
|
42 | 42 | format.html {render_error :status => 406} |
|
43 | 43 | format.api |
|
44 | 44 | end |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | def new |
|
48 | 48 | @query = IssueQuery.new |
|
49 | 49 | @query.user = User.current |
|
50 | 50 | @query.project = @project |
|
51 | @query.visibility = IssueQuery::VISIBILITY_PRIVATE unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin? | |
|
52 | 51 | @query.build_from_params(params) |
|
53 | 52 | end |
|
54 | 53 | |
|
55 | 54 | def create |
|
56 |
@query = IssueQuery.new |
|
|
55 | @query = IssueQuery.new | |
|
57 | 56 | @query.user = User.current |
|
58 |
@query.project = |
|
|
59 | @query.visibility = IssueQuery::VISIBILITY_PRIVATE unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin? | |
|
60 | @query.build_from_params(params) | |
|
61 | @query.column_names = nil if params[:default_columns] | |
|
57 | @query.project = @project | |
|
58 | update_query_from_params | |
|
62 | 59 | |
|
63 | 60 | if @query.save |
|
64 | 61 | flash[:notice] = l(:notice_successful_create) |
|
65 | 62 | redirect_to_issues(:query_id => @query) |
|
66 | 63 | else |
|
67 | 64 | render :action => 'new', :layout => !request.xhr? |
|
68 | 65 | end |
|
69 | 66 | end |
|
70 | 67 | |
|
71 | 68 | def edit |
|
72 | 69 | end |
|
73 | 70 | |
|
74 | 71 | def update |
|
75 | @query.attributes = params[:query] | |
|
76 | @query.project = nil if params[:query_is_for_all] | |
|
77 | @query.visibility = IssueQuery::VISIBILITY_PRIVATE unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin? | |
|
78 | @query.build_from_params(params) | |
|
79 | @query.column_names = nil if params[:default_columns] | |
|
72 | update_query_from_params | |
|
80 | 73 | |
|
81 | 74 | if @query.save |
|
82 | 75 | flash[:notice] = l(:notice_successful_update) |
|
83 | 76 | redirect_to_issues(:query_id => @query) |
|
84 | 77 | else |
|
85 | 78 | render :action => 'edit' |
|
86 | 79 | end |
|
87 | 80 | end |
|
88 | 81 | |
|
89 | 82 | def destroy |
|
90 | 83 | @query.destroy |
|
91 | 84 | redirect_to_issues(:set_filter => 1) |
|
92 | 85 | end |
|
93 | 86 | |
|
94 | 87 | private |
|
95 | 88 | def find_query |
|
96 | 89 | @query = IssueQuery.find(params[:id]) |
|
97 | 90 | @project = @query.project |
|
98 | 91 | render_403 unless @query.editable_by?(User.current) |
|
99 | 92 | rescue ActiveRecord::RecordNotFound |
|
100 | 93 | render_404 |
|
101 | 94 | end |
|
102 | 95 | |
|
103 | 96 | def find_optional_project |
|
104 | 97 | @project = Project.find(params[:project_id]) if params[:project_id] |
|
105 | 98 | render_403 unless User.current.allowed_to?(:save_queries, @project, :global => true) |
|
106 | 99 | rescue ActiveRecord::RecordNotFound |
|
107 | 100 | render_404 |
|
108 | 101 | end |
|
109 | 102 | |
|
103 | def update_query_from_params | |
|
104 | @query.project = params[:query_is_for_all] ? nil : @project | |
|
105 | @query.build_from_params(params) | |
|
106 | @query.column_names = nil if params[:default_columns] | |
|
107 | @query.sort_criteria = params[:query] && params[:query][:sort_criteria] | |
|
108 | @query.name = params[:query] && params[:query][:name] | |
|
109 | if User.current.allowed_to?(:manage_public_queries, @query.project) || User.current.admin? | |
|
110 | @query.visibility = (params[:query] && params[:query][:visibility]) || IssueQuery::VISIBILITY_PRIVATE | |
|
111 | else | |
|
112 | @query.visibility = IssueQuery::VISIBILITY_PRIVATE | |
|
113 | end | |
|
114 | @query | |
|
115 | end | |
|
116 | ||
|
110 | 117 | def redirect_to_issues(options) |
|
111 | 118 | if params[:gantt] |
|
112 | 119 | if @project |
|
113 | 120 | redirect_to project_gantt_path(@project, options) |
|
114 | 121 | else |
|
115 | 122 | redirect_to issues_gantt_path(options) |
|
116 | 123 | end |
|
117 | 124 | else |
|
118 | 125 | redirect_to _project_issues_path(@project, options) |
|
119 | 126 | end |
|
120 | 127 | end |
|
121 | 128 | end |
@@ -1,902 +1,904 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 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 QueryColumn |
|
19 | 19 | attr_accessor :name, :sortable, :groupable, :default_order |
|
20 | 20 | include Redmine::I18n |
|
21 | 21 | |
|
22 | 22 | def initialize(name, options={}) |
|
23 | 23 | self.name = name |
|
24 | 24 | self.sortable = options[:sortable] |
|
25 | 25 | self.groupable = options[:groupable] || false |
|
26 | 26 | if groupable == true |
|
27 | 27 | self.groupable = name.to_s |
|
28 | 28 | end |
|
29 | 29 | self.default_order = options[:default_order] |
|
30 | 30 | @inline = options.key?(:inline) ? options[:inline] : true |
|
31 | 31 | @caption_key = options[:caption] || "field_#{name}".to_sym |
|
32 | 32 | @frozen = options[:frozen] |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | def caption |
|
36 | 36 | case @caption_key |
|
37 | 37 | when Symbol |
|
38 | 38 | l(@caption_key) |
|
39 | 39 | when Proc |
|
40 | 40 | @caption_key.call |
|
41 | 41 | else |
|
42 | 42 | @caption_key |
|
43 | 43 | end |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | # Returns true if the column is sortable, otherwise false |
|
47 | 47 | def sortable? |
|
48 | 48 | !@sortable.nil? |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | 51 | def sortable |
|
52 | 52 | @sortable.is_a?(Proc) ? @sortable.call : @sortable |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def inline? |
|
56 | 56 | @inline |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | def frozen? |
|
60 | 60 | @frozen |
|
61 | 61 | end |
|
62 | 62 | |
|
63 | 63 | def value(object) |
|
64 | 64 | object.send name |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def value_object(object) |
|
68 | 68 | object.send name |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def css_classes |
|
72 | 72 | name |
|
73 | 73 | end |
|
74 | 74 | end |
|
75 | 75 | |
|
76 | 76 | class QueryCustomFieldColumn < QueryColumn |
|
77 | 77 | |
|
78 | 78 | def initialize(custom_field) |
|
79 | 79 | self.name = "cf_#{custom_field.id}".to_sym |
|
80 | 80 | self.sortable = custom_field.order_statement || false |
|
81 | 81 | self.groupable = custom_field.group_statement || false |
|
82 | 82 | @inline = true |
|
83 | 83 | @cf = custom_field |
|
84 | 84 | end |
|
85 | 85 | |
|
86 | 86 | def caption |
|
87 | 87 | @cf.name |
|
88 | 88 | end |
|
89 | 89 | |
|
90 | 90 | def custom_field |
|
91 | 91 | @cf |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def value_object(object) |
|
95 | 95 | if custom_field.visible_by?(object.project, User.current) |
|
96 | 96 | cv = object.custom_values.select {|v| v.custom_field_id == @cf.id} |
|
97 | 97 | cv.size > 1 ? cv.sort {|a,b| a.value.to_s <=> b.value.to_s} : cv.first |
|
98 | 98 | else |
|
99 | 99 | nil |
|
100 | 100 | end |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def value(object) |
|
104 | 104 | raw = value_object(object) |
|
105 | 105 | if raw.is_a?(Array) |
|
106 | 106 | raw.map {|r| @cf.cast_value(r.value)} |
|
107 | 107 | elsif raw |
|
108 | 108 | @cf.cast_value(raw.value) |
|
109 | 109 | else |
|
110 | 110 | nil |
|
111 | 111 | end |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | def css_classes |
|
115 | 115 | @css_classes ||= "#{name} #{@cf.field_format}" |
|
116 | 116 | end |
|
117 | 117 | end |
|
118 | 118 | |
|
119 | 119 | class QueryAssociationCustomFieldColumn < QueryCustomFieldColumn |
|
120 | 120 | |
|
121 | 121 | def initialize(association, custom_field) |
|
122 | 122 | super(custom_field) |
|
123 | 123 | self.name = "#{association}.cf_#{custom_field.id}".to_sym |
|
124 | 124 | # TODO: support sorting/grouping by association custom field |
|
125 | 125 | self.sortable = false |
|
126 | 126 | self.groupable = false |
|
127 | 127 | @association = association |
|
128 | 128 | end |
|
129 | 129 | |
|
130 | 130 | def value_object(object) |
|
131 | 131 | if assoc = object.send(@association) |
|
132 | 132 | super(assoc) |
|
133 | 133 | end |
|
134 | 134 | end |
|
135 | 135 | |
|
136 | 136 | def css_classes |
|
137 | 137 | @css_classes ||= "#{@association}_cf_#{@cf.id} #{@cf.field_format}" |
|
138 | 138 | end |
|
139 | 139 | end |
|
140 | 140 | |
|
141 | 141 | class Query < ActiveRecord::Base |
|
142 | 142 | class StatementInvalid < ::ActiveRecord::StatementInvalid |
|
143 | 143 | end |
|
144 | 144 | |
|
145 | 145 | VISIBILITY_PRIVATE = 0 |
|
146 | 146 | VISIBILITY_ROLES = 1 |
|
147 | 147 | VISIBILITY_PUBLIC = 2 |
|
148 | 148 | |
|
149 | 149 | belongs_to :project |
|
150 | 150 | belongs_to :user |
|
151 | 151 | has_and_belongs_to_many :roles, :join_table => "#{table_name_prefix}queries_roles#{table_name_suffix}", :foreign_key => "query_id" |
|
152 | 152 | serialize :filters |
|
153 | 153 | serialize :column_names |
|
154 | 154 | serialize :sort_criteria, Array |
|
155 | 155 | serialize :options, Hash |
|
156 | 156 | |
|
157 | 157 | attr_protected :project_id, :user_id |
|
158 | 158 | |
|
159 | 159 | validates_presence_of :name |
|
160 | 160 | validates_length_of :name, :maximum => 255 |
|
161 | 161 | validates :visibility, :inclusion => { :in => [VISIBILITY_PUBLIC, VISIBILITY_ROLES, VISIBILITY_PRIVATE] } |
|
162 | 162 | validate :validate_query_filters |
|
163 | 163 | validate do |query| |
|
164 | 164 | errors.add(:base, l(:label_role_plural) + ' ' + l('activerecord.errors.messages.blank')) if query.visibility == VISIBILITY_ROLES && roles.blank? |
|
165 | 165 | end |
|
166 | 166 | |
|
167 | 167 | after_save do |query| |
|
168 | 168 | if query.visibility_changed? && query.visibility != VISIBILITY_ROLES |
|
169 | 169 | query.roles.clear |
|
170 | 170 | end |
|
171 | 171 | end |
|
172 | 172 | |
|
173 | 173 | class_attribute :operators |
|
174 | 174 | self.operators = { |
|
175 | 175 | "=" => :label_equals, |
|
176 | 176 | "!" => :label_not_equals, |
|
177 | 177 | "o" => :label_open_issues, |
|
178 | 178 | "c" => :label_closed_issues, |
|
179 | 179 | "!*" => :label_none, |
|
180 | 180 | "*" => :label_any, |
|
181 | 181 | ">=" => :label_greater_or_equal, |
|
182 | 182 | "<=" => :label_less_or_equal, |
|
183 | 183 | "><" => :label_between, |
|
184 | 184 | "<t+" => :label_in_less_than, |
|
185 | 185 | ">t+" => :label_in_more_than, |
|
186 | 186 | "><t+"=> :label_in_the_next_days, |
|
187 | 187 | "t+" => :label_in, |
|
188 | 188 | "t" => :label_today, |
|
189 | 189 | "ld" => :label_yesterday, |
|
190 | 190 | "w" => :label_this_week, |
|
191 | 191 | "lw" => :label_last_week, |
|
192 | 192 | "l2w" => [:label_last_n_weeks, {:count => 2}], |
|
193 | 193 | "m" => :label_this_month, |
|
194 | 194 | "lm" => :label_last_month, |
|
195 | 195 | "y" => :label_this_year, |
|
196 | 196 | ">t-" => :label_less_than_ago, |
|
197 | 197 | "<t-" => :label_more_than_ago, |
|
198 | 198 | "><t-"=> :label_in_the_past_days, |
|
199 | 199 | "t-" => :label_ago, |
|
200 | 200 | "~" => :label_contains, |
|
201 | 201 | "!~" => :label_not_contains, |
|
202 | 202 | "=p" => :label_any_issues_in_project, |
|
203 | 203 | "=!p" => :label_any_issues_not_in_project, |
|
204 | 204 | "!p" => :label_no_issues_in_project |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | class_attribute :operators_by_filter_type |
|
208 | 208 | self.operators_by_filter_type = { |
|
209 | 209 | :list => [ "=", "!" ], |
|
210 | 210 | :list_status => [ "o", "=", "!", "c", "*" ], |
|
211 | 211 | :list_optional => [ "=", "!", "!*", "*" ], |
|
212 | 212 | :list_subprojects => [ "*", "!*", "=" ], |
|
213 | 213 | :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "><t+", "t+", "t", "ld", "w", "lw", "l2w", "m", "lm", "y", ">t-", "<t-", "><t-", "t-", "!*", "*" ], |
|
214 | 214 | :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "><t-", "t-", "t", "ld", "w", "lw", "l2w", "m", "lm", "y", "!*", "*" ], |
|
215 | 215 | :string => [ "=", "~", "!", "!~", "!*", "*" ], |
|
216 | 216 | :text => [ "~", "!~", "!*", "*" ], |
|
217 | 217 | :integer => [ "=", ">=", "<=", "><", "!*", "*" ], |
|
218 | 218 | :float => [ "=", ">=", "<=", "><", "!*", "*" ], |
|
219 | 219 | :relation => ["=", "=p", "=!p", "!p", "!*", "*"], |
|
220 | 220 | :tree => ["=", "~", "!*", "*"] |
|
221 | 221 | } |
|
222 | 222 | |
|
223 | 223 | class_attribute :available_columns |
|
224 | 224 | self.available_columns = [] |
|
225 | 225 | |
|
226 | 226 | class_attribute :queried_class |
|
227 | 227 | |
|
228 | 228 | def queried_table_name |
|
229 | 229 | @queried_table_name ||= self.class.queried_class.table_name |
|
230 | 230 | end |
|
231 | 231 | |
|
232 | 232 | def initialize(attributes=nil, *args) |
|
233 | 233 | super attributes |
|
234 | 234 | @is_for_all = project.nil? |
|
235 | 235 | end |
|
236 | 236 | |
|
237 | 237 | # Builds the query from the given params |
|
238 | 238 | def build_from_params(params) |
|
239 | 239 | if params[:fields] || params[:f] |
|
240 | 240 | self.filters = {} |
|
241 | 241 | add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v]) |
|
242 | 242 | else |
|
243 | 243 | available_filters.keys.each do |field| |
|
244 | 244 | add_short_filter(field, params[field]) if params[field] |
|
245 | 245 | end |
|
246 | 246 | end |
|
247 | 247 | self.group_by = params[:group_by] || (params[:query] && params[:query][:group_by]) |
|
248 | 248 | self.column_names = params[:c] || (params[:query] && params[:query][:column_names]) |
|
249 | 249 | self |
|
250 | 250 | end |
|
251 | 251 | |
|
252 | 252 | # Builds a new query from the given params and attributes |
|
253 | 253 | def self.build_from_params(params, attributes={}) |
|
254 | 254 | new(attributes).build_from_params(params) |
|
255 | 255 | end |
|
256 | 256 | |
|
257 | 257 | def validate_query_filters |
|
258 | 258 | filters.each_key do |field| |
|
259 | 259 | if values_for(field) |
|
260 | 260 | case type_for(field) |
|
261 | 261 | when :integer |
|
262 | 262 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+$/) } |
|
263 | 263 | when :float |
|
264 | 264 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+(\.\d*)?$/) } |
|
265 | 265 | when :date, :date_past |
|
266 | 266 | case operator_for(field) |
|
267 | 267 | when "=", ">=", "<=", "><" |
|
268 | 268 | add_filter_error(field, :invalid) if values_for(field).detect {|v| |
|
269 | 269 | v.present? && (!v.match(/\A\d{4}-\d{2}-\d{2}(T\d{2}((:)?\d{2}){0,2}(Z|\d{2}:?\d{2})?)?\z/) || parse_date(v).nil?) |
|
270 | 270 | } |
|
271 | 271 | when ">t-", "<t-", "t-", ">t+", "<t+", "t+", "><t+", "><t-" |
|
272 | 272 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } |
|
273 | 273 | end |
|
274 | 274 | end |
|
275 | 275 | end |
|
276 | 276 | |
|
277 | 277 | add_filter_error(field, :blank) unless |
|
278 | 278 | # filter requires one or more values |
|
279 | 279 | (values_for(field) and !values_for(field).first.blank?) or |
|
280 | 280 | # filter doesn't require any value |
|
281 | 281 | ["o", "c", "!*", "*", "t", "ld", "w", "lw", "l2w", "m", "lm", "y"].include? operator_for(field) |
|
282 | 282 | end if filters |
|
283 | 283 | end |
|
284 | 284 | |
|
285 | 285 | def add_filter_error(field, message) |
|
286 | 286 | m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages') |
|
287 | 287 | errors.add(:base, m) |
|
288 | 288 | end |
|
289 | 289 | |
|
290 | 290 | def editable_by?(user) |
|
291 | 291 | return false unless user |
|
292 | 292 | # Admin can edit them all and regular users can edit their private queries |
|
293 | 293 | return true if user.admin? || (is_private? && self.user_id == user.id) |
|
294 | 294 | # Members can not edit public queries that are for all project (only admin is allowed to) |
|
295 | 295 | is_public? && !@is_for_all && user.allowed_to?(:manage_public_queries, project) |
|
296 | 296 | end |
|
297 | 297 | |
|
298 | 298 | def trackers |
|
299 | 299 | @trackers ||= project.nil? ? Tracker.sorted.to_a : project.rolled_up_trackers |
|
300 | 300 | end |
|
301 | 301 | |
|
302 | 302 | # Returns a hash of localized labels for all filter operators |
|
303 | 303 | def self.operators_labels |
|
304 | 304 | operators.inject({}) {|h, operator| h[operator.first] = l(*operator.last); h} |
|
305 | 305 | end |
|
306 | 306 | |
|
307 | 307 | # Returns a representation of the available filters for JSON serialization |
|
308 | 308 | def available_filters_as_json |
|
309 | 309 | json = {} |
|
310 | 310 | available_filters.each do |field, options| |
|
311 | 311 | json[field] = options.slice(:type, :name, :values).stringify_keys |
|
312 | 312 | end |
|
313 | 313 | json |
|
314 | 314 | end |
|
315 | 315 | |
|
316 | 316 | def all_projects |
|
317 | 317 | @all_projects ||= Project.visible.to_a |
|
318 | 318 | end |
|
319 | 319 | |
|
320 | 320 | def all_projects_values |
|
321 | 321 | return @all_projects_values if @all_projects_values |
|
322 | 322 | |
|
323 | 323 | values = [] |
|
324 | 324 | Project.project_tree(all_projects) do |p, level| |
|
325 | 325 | prefix = (level > 0 ? ('--' * level + ' ') : '') |
|
326 | 326 | values << ["#{prefix}#{p.name}", p.id.to_s] |
|
327 | 327 | end |
|
328 | 328 | @all_projects_values = values |
|
329 | 329 | end |
|
330 | 330 | |
|
331 | 331 | # Adds available filters |
|
332 | 332 | def initialize_available_filters |
|
333 | 333 | # implemented by sub-classes |
|
334 | 334 | end |
|
335 | 335 | protected :initialize_available_filters |
|
336 | 336 | |
|
337 | 337 | # Adds an available filter |
|
338 | 338 | def add_available_filter(field, options) |
|
339 | 339 | @available_filters ||= ActiveSupport::OrderedHash.new |
|
340 | 340 | @available_filters[field] = options |
|
341 | 341 | @available_filters |
|
342 | 342 | end |
|
343 | 343 | |
|
344 | 344 | # Removes an available filter |
|
345 | 345 | def delete_available_filter(field) |
|
346 | 346 | if @available_filters |
|
347 | 347 | @available_filters.delete(field) |
|
348 | 348 | end |
|
349 | 349 | end |
|
350 | 350 | |
|
351 | 351 | # Return a hash of available filters |
|
352 | 352 | def available_filters |
|
353 | 353 | unless @available_filters |
|
354 | 354 | initialize_available_filters |
|
355 | 355 | @available_filters.each do |field, options| |
|
356 | 356 | options[:name] ||= l(options[:label] || "field_#{field}".gsub(/_id$/, '')) |
|
357 | 357 | end |
|
358 | 358 | end |
|
359 | 359 | @available_filters |
|
360 | 360 | end |
|
361 | 361 | |
|
362 | 362 | def add_filter(field, operator, values=nil) |
|
363 | 363 | # values must be an array |
|
364 | 364 | return unless values.nil? || values.is_a?(Array) |
|
365 | 365 | # check if field is defined as an available filter |
|
366 | 366 | if available_filters.has_key? field |
|
367 | 367 | filter_options = available_filters[field] |
|
368 | 368 | filters[field] = {:operator => operator, :values => (values || [''])} |
|
369 | 369 | end |
|
370 | 370 | end |
|
371 | 371 | |
|
372 | 372 | def add_short_filter(field, expression) |
|
373 | 373 | return unless expression && available_filters.has_key?(field) |
|
374 | 374 | field_type = available_filters[field][:type] |
|
375 | 375 | operators_by_filter_type[field_type].sort.reverse.detect do |operator| |
|
376 | 376 | next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/ |
|
377 | 377 | values = $1 |
|
378 | 378 | add_filter field, operator, values.present? ? values.split('|') : [''] |
|
379 | 379 | end || add_filter(field, '=', expression.split('|')) |
|
380 | 380 | end |
|
381 | 381 | |
|
382 | 382 | # Add multiple filters using +add_filter+ |
|
383 | 383 | def add_filters(fields, operators, values) |
|
384 | 384 | if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash)) |
|
385 | 385 | fields.each do |field| |
|
386 | 386 | add_filter(field, operators[field], values && values[field]) |
|
387 | 387 | end |
|
388 | 388 | end |
|
389 | 389 | end |
|
390 | 390 | |
|
391 | 391 | def has_filter?(field) |
|
392 | 392 | filters and filters[field] |
|
393 | 393 | end |
|
394 | 394 | |
|
395 | 395 | def type_for(field) |
|
396 | 396 | available_filters[field][:type] if available_filters.has_key?(field) |
|
397 | 397 | end |
|
398 | 398 | |
|
399 | 399 | def operator_for(field) |
|
400 | 400 | has_filter?(field) ? filters[field][:operator] : nil |
|
401 | 401 | end |
|
402 | 402 | |
|
403 | 403 | def values_for(field) |
|
404 | 404 | has_filter?(field) ? filters[field][:values] : nil |
|
405 | 405 | end |
|
406 | 406 | |
|
407 | 407 | def value_for(field, index=0) |
|
408 | 408 | (values_for(field) || [])[index] |
|
409 | 409 | end |
|
410 | 410 | |
|
411 | 411 | def label_for(field) |
|
412 | 412 | label = available_filters[field][:name] if available_filters.has_key?(field) |
|
413 | 413 | label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field) |
|
414 | 414 | end |
|
415 | 415 | |
|
416 | 416 | def self.add_available_column(column) |
|
417 | 417 | self.available_columns << (column) if column.is_a?(QueryColumn) |
|
418 | 418 | end |
|
419 | 419 | |
|
420 | 420 | # Returns an array of columns that can be used to group the results |
|
421 | 421 | def groupable_columns |
|
422 | 422 | available_columns.select {|c| c.groupable} |
|
423 | 423 | end |
|
424 | 424 | |
|
425 | 425 | # Returns a Hash of columns and the key for sorting |
|
426 | 426 | def sortable_columns |
|
427 | 427 | available_columns.inject({}) {|h, column| |
|
428 | 428 | h[column.name.to_s] = column.sortable |
|
429 | 429 | h |
|
430 | 430 | } |
|
431 | 431 | end |
|
432 | 432 | |
|
433 | 433 | def columns |
|
434 | 434 | # preserve the column_names order |
|
435 | 435 | cols = (has_default_columns? ? default_columns_names : column_names).collect do |name| |
|
436 | 436 | available_columns.find { |col| col.name == name } |
|
437 | 437 | end.compact |
|
438 | 438 | available_columns.select(&:frozen?) | cols |
|
439 | 439 | end |
|
440 | 440 | |
|
441 | 441 | def inline_columns |
|
442 | 442 | columns.select(&:inline?) |
|
443 | 443 | end |
|
444 | 444 | |
|
445 | 445 | def block_columns |
|
446 | 446 | columns.reject(&:inline?) |
|
447 | 447 | end |
|
448 | 448 | |
|
449 | 449 | def available_inline_columns |
|
450 | 450 | available_columns.select(&:inline?) |
|
451 | 451 | end |
|
452 | 452 | |
|
453 | 453 | def available_block_columns |
|
454 | 454 | available_columns.reject(&:inline?) |
|
455 | 455 | end |
|
456 | 456 | |
|
457 | 457 | def default_columns_names |
|
458 | 458 | [] |
|
459 | 459 | end |
|
460 | 460 | |
|
461 | 461 | def column_names=(names) |
|
462 | 462 | if names |
|
463 | 463 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } |
|
464 | 464 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } |
|
465 | 465 | # Set column_names to nil if default columns |
|
466 | 466 | if names == default_columns_names |
|
467 | 467 | names = nil |
|
468 | 468 | end |
|
469 | 469 | end |
|
470 | 470 | write_attribute(:column_names, names) |
|
471 | 471 | end |
|
472 | 472 | |
|
473 | 473 | def has_column?(column) |
|
474 | 474 | column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column) |
|
475 | 475 | end |
|
476 | 476 | |
|
477 | 477 | def has_custom_field_column? |
|
478 | 478 | columns.any? {|column| column.is_a? QueryCustomFieldColumn} |
|
479 | 479 | end |
|
480 | 480 | |
|
481 | 481 | def has_default_columns? |
|
482 | 482 | column_names.nil? || column_names.empty? |
|
483 | 483 | end |
|
484 | 484 | |
|
485 | 485 | def sort_criteria=(arg) |
|
486 | 486 | c = [] |
|
487 | 487 | if arg.is_a?(Hash) |
|
488 | 488 | arg = arg.keys.sort.collect {|k| arg[k]} |
|
489 | 489 | end |
|
490 | if arg | |
|
490 | 491 | c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, (o == 'desc' || o == false) ? 'desc' : 'asc']} |
|
492 | end | |
|
491 | 493 | write_attribute(:sort_criteria, c) |
|
492 | 494 | end |
|
493 | 495 | |
|
494 | 496 | def sort_criteria |
|
495 | 497 | read_attribute(:sort_criteria) || [] |
|
496 | 498 | end |
|
497 | 499 | |
|
498 | 500 | def sort_criteria_key(arg) |
|
499 | 501 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].first |
|
500 | 502 | end |
|
501 | 503 | |
|
502 | 504 | def sort_criteria_order(arg) |
|
503 | 505 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].last |
|
504 | 506 | end |
|
505 | 507 | |
|
506 | 508 | def sort_criteria_order_for(key) |
|
507 | 509 | sort_criteria.detect {|k, order| key.to_s == k}.try(:last) |
|
508 | 510 | end |
|
509 | 511 | |
|
510 | 512 | # Returns the SQL sort order that should be prepended for grouping |
|
511 | 513 | def group_by_sort_order |
|
512 | 514 | if grouped? && (column = group_by_column) |
|
513 | 515 | order = (sort_criteria_order_for(column.name) || column.default_order).try(:upcase) |
|
514 | 516 | column.sortable.is_a?(Array) ? |
|
515 | 517 | column.sortable.collect {|s| "#{s} #{order}"}.join(',') : |
|
516 | 518 | "#{column.sortable} #{order}" |
|
517 | 519 | end |
|
518 | 520 | end |
|
519 | 521 | |
|
520 | 522 | # Returns true if the query is a grouped query |
|
521 | 523 | def grouped? |
|
522 | 524 | !group_by_column.nil? |
|
523 | 525 | end |
|
524 | 526 | |
|
525 | 527 | def group_by_column |
|
526 | 528 | groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} |
|
527 | 529 | end |
|
528 | 530 | |
|
529 | 531 | def group_by_statement |
|
530 | 532 | group_by_column.try(:groupable) |
|
531 | 533 | end |
|
532 | 534 | |
|
533 | 535 | def project_statement |
|
534 | 536 | project_clauses = [] |
|
535 | 537 | if project && !project.descendants.active.empty? |
|
536 | 538 | ids = [project.id] |
|
537 | 539 | if has_filter?("subproject_id") |
|
538 | 540 | case operator_for("subproject_id") |
|
539 | 541 | when '=' |
|
540 | 542 | # include the selected subprojects |
|
541 | 543 | ids += values_for("subproject_id").each(&:to_i) |
|
542 | 544 | when '!*' |
|
543 | 545 | # main project only |
|
544 | 546 | else |
|
545 | 547 | # all subprojects |
|
546 | 548 | ids += project.descendants.collect(&:id) |
|
547 | 549 | end |
|
548 | 550 | elsif Setting.display_subprojects_issues? |
|
549 | 551 | ids += project.descendants.collect(&:id) |
|
550 | 552 | end |
|
551 | 553 | project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',') |
|
552 | 554 | elsif project |
|
553 | 555 | project_clauses << "#{Project.table_name}.id = %d" % project.id |
|
554 | 556 | end |
|
555 | 557 | project_clauses.any? ? project_clauses.join(' AND ') : nil |
|
556 | 558 | end |
|
557 | 559 | |
|
558 | 560 | def statement |
|
559 | 561 | # filters clauses |
|
560 | 562 | filters_clauses = [] |
|
561 | 563 | filters.each_key do |field| |
|
562 | 564 | next if field == "subproject_id" |
|
563 | 565 | v = values_for(field).clone |
|
564 | 566 | next unless v and !v.empty? |
|
565 | 567 | operator = operator_for(field) |
|
566 | 568 | |
|
567 | 569 | # "me" value substitution |
|
568 | 570 | if %w(assigned_to_id author_id user_id watcher_id).include?(field) |
|
569 | 571 | if v.delete("me") |
|
570 | 572 | if User.current.logged? |
|
571 | 573 | v.push(User.current.id.to_s) |
|
572 | 574 | v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id' |
|
573 | 575 | else |
|
574 | 576 | v.push("0") |
|
575 | 577 | end |
|
576 | 578 | end |
|
577 | 579 | end |
|
578 | 580 | |
|
579 | 581 | if field == 'project_id' |
|
580 | 582 | if v.delete('mine') |
|
581 | 583 | v += User.current.memberships.map(&:project_id).map(&:to_s) |
|
582 | 584 | end |
|
583 | 585 | end |
|
584 | 586 | |
|
585 | 587 | if field =~ /cf_(\d+)$/ |
|
586 | 588 | # custom field |
|
587 | 589 | filters_clauses << sql_for_custom_field(field, operator, v, $1) |
|
588 | 590 | elsif respond_to?("sql_for_#{field}_field") |
|
589 | 591 | # specific statement |
|
590 | 592 | filters_clauses << send("sql_for_#{field}_field", field, operator, v) |
|
591 | 593 | else |
|
592 | 594 | # regular field |
|
593 | 595 | filters_clauses << '(' + sql_for_field(field, operator, v, queried_table_name, field) + ')' |
|
594 | 596 | end |
|
595 | 597 | end if filters and valid? |
|
596 | 598 | |
|
597 | 599 | if (c = group_by_column) && c.is_a?(QueryCustomFieldColumn) |
|
598 | 600 | # Excludes results for which the grouped custom field is not visible |
|
599 | 601 | filters_clauses << c.custom_field.visibility_by_project_condition |
|
600 | 602 | end |
|
601 | 603 | |
|
602 | 604 | filters_clauses << project_statement |
|
603 | 605 | filters_clauses.reject!(&:blank?) |
|
604 | 606 | |
|
605 | 607 | filters_clauses.any? ? filters_clauses.join(' AND ') : nil |
|
606 | 608 | end |
|
607 | 609 | |
|
608 | 610 | private |
|
609 | 611 | |
|
610 | 612 | def sql_for_custom_field(field, operator, value, custom_field_id) |
|
611 | 613 | db_table = CustomValue.table_name |
|
612 | 614 | db_field = 'value' |
|
613 | 615 | filter = @available_filters[field] |
|
614 | 616 | return nil unless filter |
|
615 | 617 | if filter[:field].format.target_class && filter[:field].format.target_class <= User |
|
616 | 618 | if value.delete('me') |
|
617 | 619 | value.push User.current.id.to_s |
|
618 | 620 | end |
|
619 | 621 | end |
|
620 | 622 | not_in = nil |
|
621 | 623 | if operator == '!' |
|
622 | 624 | # Makes ! operator work for custom fields with multiple values |
|
623 | 625 | operator = '=' |
|
624 | 626 | not_in = 'NOT' |
|
625 | 627 | end |
|
626 | 628 | customized_key = "id" |
|
627 | 629 | customized_class = queried_class |
|
628 | 630 | if field =~ /^(.+)\.cf_/ |
|
629 | 631 | assoc = $1 |
|
630 | 632 | customized_key = "#{assoc}_id" |
|
631 | 633 | customized_class = queried_class.reflect_on_association(assoc.to_sym).klass.base_class rescue nil |
|
632 | 634 | raise "Unknown #{queried_class.name} association #{assoc}" unless customized_class |
|
633 | 635 | end |
|
634 | 636 | where = sql_for_field(field, operator, value, db_table, db_field, true) |
|
635 | 637 | if operator =~ /[<>]/ |
|
636 | 638 | where = "(#{where}) AND #{db_table}.#{db_field} <> ''" |
|
637 | 639 | end |
|
638 | 640 | "#{queried_table_name}.#{customized_key} #{not_in} IN (" + |
|
639 | 641 | "SELECT #{customized_class.table_name}.id FROM #{customized_class.table_name}" + |
|
640 | 642 | " LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='#{customized_class}' AND #{db_table}.customized_id=#{customized_class.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id}" + |
|
641 | 643 | " WHERE (#{where}) AND (#{filter[:field].visibility_by_project_condition}))" |
|
642 | 644 | end |
|
643 | 645 | |
|
644 | 646 | # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ |
|
645 | 647 | def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) |
|
646 | 648 | sql = '' |
|
647 | 649 | case operator |
|
648 | 650 | when "=" |
|
649 | 651 | if value.any? |
|
650 | 652 | case type_for(field) |
|
651 | 653 | when :date, :date_past |
|
652 | 654 | sql = date_clause(db_table, db_field, parse_date(value.first), parse_date(value.first), is_custom_filter) |
|
653 | 655 | when :integer |
|
654 | 656 | if is_custom_filter |
|
655 | 657 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) = #{value.first.to_i})" |
|
656 | 658 | else |
|
657 | 659 | sql = "#{db_table}.#{db_field} = #{value.first.to_i}" |
|
658 | 660 | end |
|
659 | 661 | when :float |
|
660 | 662 | if is_custom_filter |
|
661 | 663 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5})" |
|
662 | 664 | else |
|
663 | 665 | sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}" |
|
664 | 666 | end |
|
665 | 667 | else |
|
666 | 668 | sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{self.class.connection.quote_string(val)}'"}.join(",") + ")" |
|
667 | 669 | end |
|
668 | 670 | else |
|
669 | 671 | # IN an empty set |
|
670 | 672 | sql = "1=0" |
|
671 | 673 | end |
|
672 | 674 | when "!" |
|
673 | 675 | if value.any? |
|
674 | 676 | sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{self.class.connection.quote_string(val)}'"}.join(",") + "))" |
|
675 | 677 | else |
|
676 | 678 | # NOT IN an empty set |
|
677 | 679 | sql = "1=1" |
|
678 | 680 | end |
|
679 | 681 | when "!*" |
|
680 | 682 | sql = "#{db_table}.#{db_field} IS NULL" |
|
681 | 683 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter |
|
682 | 684 | when "*" |
|
683 | 685 | sql = "#{db_table}.#{db_field} IS NOT NULL" |
|
684 | 686 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter |
|
685 | 687 | when ">=" |
|
686 | 688 | if [:date, :date_past].include?(type_for(field)) |
|
687 | 689 | sql = date_clause(db_table, db_field, parse_date(value.first), nil, is_custom_filter) |
|
688 | 690 | else |
|
689 | 691 | if is_custom_filter |
|
690 | 692 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) >= #{value.first.to_f})" |
|
691 | 693 | else |
|
692 | 694 | sql = "#{db_table}.#{db_field} >= #{value.first.to_f}" |
|
693 | 695 | end |
|
694 | 696 | end |
|
695 | 697 | when "<=" |
|
696 | 698 | if [:date, :date_past].include?(type_for(field)) |
|
697 | 699 | sql = date_clause(db_table, db_field, nil, parse_date(value.first), is_custom_filter) |
|
698 | 700 | else |
|
699 | 701 | if is_custom_filter |
|
700 | 702 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) <= #{value.first.to_f})" |
|
701 | 703 | else |
|
702 | 704 | sql = "#{db_table}.#{db_field} <= #{value.first.to_f}" |
|
703 | 705 | end |
|
704 | 706 | end |
|
705 | 707 | when "><" |
|
706 | 708 | if [:date, :date_past].include?(type_for(field)) |
|
707 | 709 | sql = date_clause(db_table, db_field, parse_date(value[0]), parse_date(value[1]), is_custom_filter) |
|
708 | 710 | else |
|
709 | 711 | if is_custom_filter |
|
710 | 712 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})" |
|
711 | 713 | else |
|
712 | 714 | sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}" |
|
713 | 715 | end |
|
714 | 716 | end |
|
715 | 717 | when "o" |
|
716 | 718 | sql = "#{queried_table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{self.class.connection.quoted_false})" if field == "status_id" |
|
717 | 719 | when "c" |
|
718 | 720 | sql = "#{queried_table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{self.class.connection.quoted_true})" if field == "status_id" |
|
719 | 721 | when "><t-" |
|
720 | 722 | # between today - n days and today |
|
721 | 723 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0, is_custom_filter) |
|
722 | 724 | when ">t-" |
|
723 | 725 | # >= today - n days |
|
724 | 726 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, nil, is_custom_filter) |
|
725 | 727 | when "<t-" |
|
726 | 728 | # <= today - n days |
|
727 | 729 | sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i, is_custom_filter) |
|
728 | 730 | when "t-" |
|
729 | 731 | # = n days in past |
|
730 | 732 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i, is_custom_filter) |
|
731 | 733 | when "><t+" |
|
732 | 734 | # between today and today + n days |
|
733 | 735 | sql = relative_date_clause(db_table, db_field, 0, value.first.to_i, is_custom_filter) |
|
734 | 736 | when ">t+" |
|
735 | 737 | # >= today + n days |
|
736 | 738 | sql = relative_date_clause(db_table, db_field, value.first.to_i, nil, is_custom_filter) |
|
737 | 739 | when "<t+" |
|
738 | 740 | # <= today + n days |
|
739 | 741 | sql = relative_date_clause(db_table, db_field, nil, value.first.to_i, is_custom_filter) |
|
740 | 742 | when "t+" |
|
741 | 743 | # = today + n days |
|
742 | 744 | sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i, is_custom_filter) |
|
743 | 745 | when "t" |
|
744 | 746 | # = today |
|
745 | 747 | sql = relative_date_clause(db_table, db_field, 0, 0, is_custom_filter) |
|
746 | 748 | when "ld" |
|
747 | 749 | # = yesterday |
|
748 | 750 | sql = relative_date_clause(db_table, db_field, -1, -1, is_custom_filter) |
|
749 | 751 | when "w" |
|
750 | 752 | # = this week |
|
751 | 753 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
752 | 754 | day_of_week = Date.today.cwday |
|
753 | 755 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
754 | 756 | sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6, is_custom_filter) |
|
755 | 757 | when "lw" |
|
756 | 758 | # = last week |
|
757 | 759 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
758 | 760 | day_of_week = Date.today.cwday |
|
759 | 761 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
760 | 762 | sql = relative_date_clause(db_table, db_field, - days_ago - 7, - days_ago - 1, is_custom_filter) |
|
761 | 763 | when "l2w" |
|
762 | 764 | # = last 2 weeks |
|
763 | 765 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
764 | 766 | day_of_week = Date.today.cwday |
|
765 | 767 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
766 | 768 | sql = relative_date_clause(db_table, db_field, - days_ago - 14, - days_ago - 1, is_custom_filter) |
|
767 | 769 | when "m" |
|
768 | 770 | # = this month |
|
769 | 771 | date = Date.today |
|
770 | 772 | sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month, is_custom_filter) |
|
771 | 773 | when "lm" |
|
772 | 774 | # = last month |
|
773 | 775 | date = Date.today.prev_month |
|
774 | 776 | sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month, is_custom_filter) |
|
775 | 777 | when "y" |
|
776 | 778 | # = this year |
|
777 | 779 | date = Date.today |
|
778 | 780 | sql = date_clause(db_table, db_field, date.beginning_of_year, date.end_of_year, is_custom_filter) |
|
779 | 781 | when "~" |
|
780 | 782 | sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{self.class.connection.quote_string(value.first.to_s.downcase)}%'" |
|
781 | 783 | when "!~" |
|
782 | 784 | sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{self.class.connection.quote_string(value.first.to_s.downcase)}%'" |
|
783 | 785 | else |
|
784 | 786 | raise "Unknown query operator #{operator}" |
|
785 | 787 | end |
|
786 | 788 | |
|
787 | 789 | return sql |
|
788 | 790 | end |
|
789 | 791 | |
|
790 | 792 | # Adds a filter for the given custom field |
|
791 | 793 | def add_custom_field_filter(field, assoc=nil) |
|
792 | 794 | options = field.format.query_filter_options(field, self) |
|
793 | 795 | if field.format.target_class && field.format.target_class <= User |
|
794 | 796 | if options[:values].is_a?(Array) && User.current.logged? |
|
795 | 797 | options[:values].unshift ["<< #{l(:label_me)} >>", "me"] |
|
796 | 798 | end |
|
797 | 799 | end |
|
798 | 800 | |
|
799 | 801 | filter_id = "cf_#{field.id}" |
|
800 | 802 | filter_name = field.name |
|
801 | 803 | if assoc.present? |
|
802 | 804 | filter_id = "#{assoc}.#{filter_id}" |
|
803 | 805 | filter_name = l("label_attribute_of_#{assoc}", :name => filter_name) |
|
804 | 806 | end |
|
805 | 807 | add_available_filter filter_id, options.merge({ |
|
806 | 808 | :name => filter_name, |
|
807 | 809 | :field => field |
|
808 | 810 | }) |
|
809 | 811 | end |
|
810 | 812 | |
|
811 | 813 | # Adds filters for the given custom fields scope |
|
812 | 814 | def add_custom_fields_filters(scope, assoc=nil) |
|
813 | 815 | scope.visible.where(:is_filter => true).sorted.each do |field| |
|
814 | 816 | add_custom_field_filter(field, assoc) |
|
815 | 817 | end |
|
816 | 818 | end |
|
817 | 819 | |
|
818 | 820 | # Adds filters for the given associations custom fields |
|
819 | 821 | def add_associations_custom_fields_filters(*associations) |
|
820 | 822 | fields_by_class = CustomField.visible.where(:is_filter => true).group_by(&:class) |
|
821 | 823 | associations.each do |assoc| |
|
822 | 824 | association_klass = queried_class.reflect_on_association(assoc).klass |
|
823 | 825 | fields_by_class.each do |field_class, fields| |
|
824 | 826 | if field_class.customized_class <= association_klass |
|
825 | 827 | fields.sort.each do |field| |
|
826 | 828 | add_custom_field_filter(field, assoc) |
|
827 | 829 | end |
|
828 | 830 | end |
|
829 | 831 | end |
|
830 | 832 | end |
|
831 | 833 | end |
|
832 | 834 | |
|
833 | 835 | def quoted_time(time, is_custom_filter) |
|
834 | 836 | if is_custom_filter |
|
835 | 837 | # Custom field values are stored as strings in the DB |
|
836 | 838 | # using this format that does not depend on DB date representation |
|
837 | 839 | time.strftime("%Y-%m-%d %H:%M:%S") |
|
838 | 840 | else |
|
839 | 841 | self.class.connection.quoted_date(time) |
|
840 | 842 | end |
|
841 | 843 | end |
|
842 | 844 | |
|
843 | 845 | # Returns a SQL clause for a date or datetime field. |
|
844 | 846 | def date_clause(table, field, from, to, is_custom_filter) |
|
845 | 847 | s = [] |
|
846 | 848 | if from |
|
847 | 849 | if from.is_a?(Date) |
|
848 | 850 | from = Time.local(from.year, from.month, from.day).yesterday.end_of_day |
|
849 | 851 | else |
|
850 | 852 | from = from - 1 # second |
|
851 | 853 | end |
|
852 | 854 | if self.class.default_timezone == :utc |
|
853 | 855 | from = from.utc |
|
854 | 856 | end |
|
855 | 857 | s << ("#{table}.#{field} > '%s'" % [quoted_time(from, is_custom_filter)]) |
|
856 | 858 | end |
|
857 | 859 | if to |
|
858 | 860 | if to.is_a?(Date) |
|
859 | 861 | to = Time.local(to.year, to.month, to.day).end_of_day |
|
860 | 862 | end |
|
861 | 863 | if self.class.default_timezone == :utc |
|
862 | 864 | to = to.utc |
|
863 | 865 | end |
|
864 | 866 | s << ("#{table}.#{field} <= '%s'" % [quoted_time(to, is_custom_filter)]) |
|
865 | 867 | end |
|
866 | 868 | s.join(' AND ') |
|
867 | 869 | end |
|
868 | 870 | |
|
869 | 871 | # Returns a SQL clause for a date or datetime field using relative dates. |
|
870 | 872 | def relative_date_clause(table, field, days_from, days_to, is_custom_filter) |
|
871 | 873 | date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil), is_custom_filter) |
|
872 | 874 | end |
|
873 | 875 | |
|
874 | 876 | # Returns a Date or Time from the given filter value |
|
875 | 877 | def parse_date(arg) |
|
876 | 878 | if arg.to_s =~ /\A\d{4}-\d{2}-\d{2}T/ |
|
877 | 879 | Time.parse(arg) rescue nil |
|
878 | 880 | else |
|
879 | 881 | Date.parse(arg) rescue nil |
|
880 | 882 | end |
|
881 | 883 | end |
|
882 | 884 | |
|
883 | 885 | # Additional joins required for the given sort options |
|
884 | 886 | def joins_for_order_statement(order_options) |
|
885 | 887 | joins = [] |
|
886 | 888 | |
|
887 | 889 | if order_options |
|
888 | 890 | if order_options.include?('authors') |
|
889 | 891 | joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{queried_table_name}.author_id" |
|
890 | 892 | end |
|
891 | 893 | order_options.scan(/cf_\d+/).uniq.each do |name| |
|
892 | 894 | column = available_columns.detect {|c| c.name.to_s == name} |
|
893 | 895 | join = column && column.custom_field.join_for_order_statement |
|
894 | 896 | if join |
|
895 | 897 | joins << join |
|
896 | 898 | end |
|
897 | 899 | end |
|
898 | 900 | end |
|
899 | 901 | |
|
900 | 902 | joins.any? ? joins.join(' ') : nil |
|
901 | 903 | end |
|
902 | 904 | end |
@@ -1,87 +1,89 | |||
|
1 | 1 | <%= error_messages_for 'query' %> |
|
2 | 2 | |
|
3 | 3 | <div class="box"> |
|
4 | 4 | <div class="tabular"> |
|
5 | 5 | <%= hidden_field_tag 'gantt', '1' if params[:gantt] %> |
|
6 | 6 | |
|
7 | 7 | <p><label for="query_name"><%=l(:field_name)%></label> |
|
8 | 8 | <%= text_field 'query', 'name', :size => 80 %></p> |
|
9 | 9 | |
|
10 | <% if User.current.admin? || User.current.allowed_to?(:manage_public_queries, @project) %> | |
|
10 | <% if User.current.admin? || User.current.allowed_to?(:manage_public_queries, @query.project) %> | |
|
11 | 11 | <p><label><%=l(:field_visible)%></label> |
|
12 | 12 | <label class="block"><%= radio_button 'query', 'visibility', Query::VISIBILITY_PRIVATE %> <%= l(:label_visibility_private) %></label> |
|
13 | <label class="block"><%= radio_button 'query', 'visibility', Query::VISIBILITY_PUBLIC %> <%= l(:label_visibility_public) %></label> | |
|
13 | 14 | <label class="block"><%= radio_button 'query', 'visibility', Query::VISIBILITY_ROLES %> <%= l(:label_visibility_roles) %>:</label> |
|
14 | 15 | <% Role.givable.sorted.each do |role| %> |
|
15 | 16 | <label class="block role-visibility"><%= check_box_tag 'query[role_ids][]', role.id, @query.roles.include?(role), :id => nil %> <%= role.name %></label> |
|
16 | 17 | <% end %> |
|
17 | <label class="block"><%= radio_button 'query', 'visibility', Query::VISIBILITY_PUBLIC %> <%= l(:label_visibility_public) %></label> | |
|
18 | 18 | <%= hidden_field_tag 'query[role_ids][]', '' %> |
|
19 | 19 | </p> |
|
20 | 20 | <% end %> |
|
21 | 21 | |
|
22 | 22 | <p><label for="query_is_for_all"><%=l(:field_is_for_all)%></label> |
|
23 | <%= check_box_tag 'query_is_for_all', 1, @query.project.nil?, | |
|
24 | :disabled => (!@query.new_record? && (@query.project.nil? || (@query.is_public? && !User.current.admin?))) %></p> | |
|
23 | <%= check_box_tag 'query_is_for_all', 1, @query.project.nil?, :class => (User.current.admin? ? '' : 'disable-unless-private') %></p> | |
|
25 | 24 | |
|
26 | 25 | <% unless params[:gantt] %> |
|
27 | 26 | <fieldset><legend><%= l(:label_options) %></legend> |
|
28 | 27 | <p><label for="query_default_columns"><%=l(:label_default_columns)%></label> |
|
29 | 28 | <%= check_box_tag 'default_columns', 1, @query.has_default_columns?, :id => 'query_default_columns', |
|
30 | 29 | :onclick => 'if (this.checked) {$("#columns").hide();} else {$("#columns").show();}' %></p> |
|
31 | 30 | |
|
32 | 31 | <p><label for="query_group_by"><%= l(:field_group_by) %></label> |
|
33 | 32 | <%= select 'query', 'group_by', @query.groupable_columns.collect {|c| [c.caption, c.name.to_s]}, :include_blank => true %></p> |
|
34 | 33 | |
|
35 | 34 | <p><label><%= l(:button_show) %></label> |
|
36 | 35 | <%= available_block_columns_tags(@query) %></p> |
|
37 | 36 | </fieldset> |
|
38 | 37 | <% else %> |
|
39 | 38 | <fieldset><legend><%= l(:label_options) %></legend> |
|
40 | 39 | <p><label><%= l(:button_show) %></label> |
|
41 | 40 | <label class="inline"><%= check_box_tag "query[draw_relations]", "1", @query.draw_relations %> <%= l(:label_related_issues) %></label> |
|
42 | 41 | <label class="inline"><%= check_box_tag "query[draw_progress_line]", "1", @query.draw_progress_line %> <%= l(:label_gantt_progress_line) %></label> |
|
43 | 42 | </p> |
|
44 | 43 | </fieldset> |
|
45 | 44 | <% end %> |
|
46 | 45 | </div> |
|
47 | 46 | |
|
48 | 47 | <fieldset id="filters"><legend><%= l(:label_filter_plural) %></legend> |
|
49 | 48 | <%= render :partial => 'queries/filters', :locals => {:query => query}%> |
|
50 | 49 | </fieldset> |
|
51 | 50 | |
|
52 | 51 | <% unless params[:gantt] %> |
|
53 | 52 | <fieldset><legend><%= l(:label_sort) %></legend> |
|
54 | 53 | <% 3.times do |i| %> |
|
55 | 54 | <%= i+1 %>: |
|
56 | 55 | <%= label_tag "query_sort_criteria_attribute_" + i.to_s, |
|
57 | 56 | l(:description_query_sort_criteria_attribute), :class => "hidden-for-sighted" %> |
|
58 | 57 | <%= select_tag("query[sort_criteria][#{i}][]", |
|
59 | 58 | options_for_select([[]] + query.available_columns.select(&:sortable?).collect {|column| [column.caption, column.name.to_s]}, @query.sort_criteria_key(i)), |
|
60 | 59 | :id => "query_sort_criteria_attribute_" + i.to_s)%> |
|
61 | 60 | <%= label_tag "query_sort_criteria_direction_" + i.to_s, |
|
62 | 61 | l(:description_query_sort_criteria_direction), :class => "hidden-for-sighted" %> |
|
63 | 62 | <%= select_tag("query[sort_criteria][#{i}][]", |
|
64 | 63 | options_for_select([[], [l(:label_ascending), 'asc'], [l(:label_descending), 'desc']], @query.sort_criteria_order(i)), |
|
65 | 64 | :id => "query_sort_criteria_direction_" + i.to_s) %> |
|
66 | 65 | <br /> |
|
67 | 66 | <% end %> |
|
68 | 67 | </fieldset> |
|
69 | 68 | <% end %> |
|
70 | 69 | |
|
71 | 70 | <% unless params[:gantt] %> |
|
72 | 71 | <%= content_tag 'fieldset', :id => 'columns', :style => (query.has_default_columns? ? 'display:none;' : nil) do %> |
|
73 | 72 | <legend><%= l(:field_column_names) %></legend> |
|
74 | 73 | <%= render_query_columns_selection(query) %> |
|
75 | 74 | <% end %> |
|
76 | 75 | <% end %> |
|
77 | 76 | |
|
78 | 77 | </div> |
|
79 | 78 | |
|
80 | 79 | <%= javascript_tag do %> |
|
81 | 80 | $(document).ready(function(){ |
|
82 | 81 | $("input[name='query[visibility]']").change(function(){ |
|
83 | var checked = $('#query_visibility_1').is(':checked'); | |
|
84 | $("input[name='query[role_ids][]'][type=checkbox]").attr('disabled', !checked); | |
|
82 | var roles_checked = $('#query_visibility_1').is(':checked'); | |
|
83 | var private_checked = $('#query_visibility_0').is(':checked'); | |
|
84 | $("input[name='query[role_ids][]'][type=checkbox]").attr('disabled', !roles_checked); | |
|
85 | if (!private_checked) $("input.disable-unless-private").attr('checked', false); | |
|
86 | $("input.disable-unless-private").attr('disabled', !private_checked); | |
|
85 | 87 | }).trigger('change'); |
|
86 | 88 | }); |
|
87 | 89 | <% end %> |
@@ -1,297 +1,358 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 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 QueriesControllerTest < ActionController::TestCase |
|
21 | 21 | fixtures :projects, :users, :members, :member_roles, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :queries, :enabled_modules |
|
22 | 22 | |
|
23 | 23 | def setup |
|
24 | 24 | User.current = nil |
|
25 | 25 | end |
|
26 | 26 | |
|
27 | 27 | def test_index |
|
28 | 28 | get :index |
|
29 | 29 | # HTML response not implemented |
|
30 | 30 | assert_response 406 |
|
31 | 31 | end |
|
32 | 32 | |
|
33 | 33 | def test_new_project_query |
|
34 | 34 | @request.session[:user_id] = 2 |
|
35 | 35 | get :new, :project_id => 1 |
|
36 | 36 | assert_response :success |
|
37 | 37 | assert_template 'new' |
|
38 | 38 | assert_select 'input[name=?][value="0"][checked=checked]', 'query[visibility]' |
|
39 | 39 | assert_select 'input[name=query_is_for_all][type=checkbox]:not([checked]):not([disabled])' |
|
40 | 40 | assert_select 'select[name=?]', 'c[]' do |
|
41 | 41 | assert_select 'option[value=tracker]' |
|
42 | 42 | assert_select 'option[value=subject]' |
|
43 | 43 | end |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | def test_new_global_query |
|
47 | 47 | @request.session[:user_id] = 2 |
|
48 | 48 | get :new |
|
49 | 49 | assert_response :success |
|
50 | 50 | assert_template 'new' |
|
51 | 51 | assert_select 'input[name=?]', 'query[visibility]', 0 |
|
52 | 52 | assert_select 'input[name=query_is_for_all][type=checkbox][checked]:not([disabled])' |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def test_new_on_invalid_project |
|
56 | 56 | @request.session[:user_id] = 2 |
|
57 | 57 | get :new, :project_id => 'invalid' |
|
58 | 58 | assert_response 404 |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | def test_create_project_public_query |
|
62 | 62 | @request.session[:user_id] = 2 |
|
63 | 63 | post :create, |
|
64 | 64 | :project_id => 'ecookbook', |
|
65 | 65 | :default_columns => '1', |
|
66 | 66 | :f => ["status_id", "assigned_to_id"], |
|
67 | 67 | :op => {"assigned_to_id" => "=", "status_id" => "o"}, |
|
68 | 68 | :v => { "assigned_to_id" => ["1"], "status_id" => ["1"]}, |
|
69 | 69 | :query => {"name" => "test_new_project_public_query", "visibility" => "2"} |
|
70 | 70 | |
|
71 | 71 | q = Query.find_by_name('test_new_project_public_query') |
|
72 | 72 | assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => q |
|
73 | 73 | assert q.is_public? |
|
74 | 74 | assert q.has_default_columns? |
|
75 | 75 | assert q.valid? |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | def test_create_project_private_query |
|
79 | 79 | @request.session[:user_id] = 3 |
|
80 | 80 | post :create, |
|
81 | 81 | :project_id => 'ecookbook', |
|
82 | 82 | :default_columns => '1', |
|
83 | 83 | :fields => ["status_id", "assigned_to_id"], |
|
84 | 84 | :operators => {"assigned_to_id" => "=", "status_id" => "o"}, |
|
85 | 85 | :values => { "assigned_to_id" => ["1"], "status_id" => ["1"]}, |
|
86 |
:query => {"name" => "test_new_project_private_query", "visibility" => " |
|
|
86 | :query => {"name" => "test_new_project_private_query", "visibility" => "0"} | |
|
87 | 87 | |
|
88 | 88 | q = Query.find_by_name('test_new_project_private_query') |
|
89 | 89 | assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => q |
|
90 | 90 | assert !q.is_public? |
|
91 | 91 | assert q.has_default_columns? |
|
92 | 92 | assert q.valid? |
|
93 | 93 | end |
|
94 | 94 | |
|
95 | 95 | def test_create_global_private_query_with_custom_columns |
|
96 | 96 | @request.session[:user_id] = 3 |
|
97 | 97 | post :create, |
|
98 | 98 | :fields => ["status_id", "assigned_to_id"], |
|
99 | 99 | :operators => {"assigned_to_id" => "=", "status_id" => "o"}, |
|
100 | 100 | :values => { "assigned_to_id" => ["me"], "status_id" => ["1"]}, |
|
101 |
:query => {"name" => "test_new_global_private_query", "visibility" => " |
|
|
101 | :query => {"name" => "test_new_global_private_query", "visibility" => "0"}, | |
|
102 | 102 | :c => ["", "tracker", "subject", "priority", "category"] |
|
103 | 103 | |
|
104 | 104 | q = Query.find_by_name('test_new_global_private_query') |
|
105 | 105 | assert_redirected_to :controller => 'issues', :action => 'index', :project_id => nil, :query_id => q |
|
106 | 106 | assert !q.is_public? |
|
107 | 107 | assert !q.has_default_columns? |
|
108 | 108 | assert_equal [:id, :tracker, :subject, :priority, :category], q.columns.collect {|c| c.name} |
|
109 | 109 | assert q.valid? |
|
110 | 110 | end |
|
111 | 111 | |
|
112 | 112 | def test_create_global_query_with_custom_filters |
|
113 | 113 | @request.session[:user_id] = 3 |
|
114 | 114 | post :create, |
|
115 | 115 | :fields => ["assigned_to_id"], |
|
116 | 116 | :operators => {"assigned_to_id" => "="}, |
|
117 | 117 | :values => { "assigned_to_id" => ["me"]}, |
|
118 | 118 | :query => {"name" => "test_new_global_query"} |
|
119 | 119 | |
|
120 | 120 | q = Query.find_by_name('test_new_global_query') |
|
121 | 121 | assert_redirected_to :controller => 'issues', :action => 'index', :project_id => nil, :query_id => q |
|
122 | assert !q.is_public? | |
|
122 | 123 | assert !q.has_filter?(:status_id) |
|
123 | 124 | assert_equal ['assigned_to_id'], q.filters.keys |
|
124 | 125 | assert q.valid? |
|
125 | 126 | end |
|
126 | 127 | |
|
127 | 128 | def test_create_with_sort |
|
128 | 129 | @request.session[:user_id] = 1 |
|
129 | 130 | post :create, |
|
130 | 131 | :default_columns => '1', |
|
131 | 132 | :operators => {"status_id" => "o"}, |
|
132 | 133 | :values => {"status_id" => ["1"]}, |
|
133 | 134 | :query => {:name => "test_new_with_sort", |
|
134 | 135 | :visibility => "2", |
|
135 | 136 | :sort_criteria => {"0" => ["due_date", "desc"], "1" => ["tracker", ""]}} |
|
136 | 137 | |
|
137 | 138 | query = Query.find_by_name("test_new_with_sort") |
|
138 | 139 | assert_not_nil query |
|
139 | 140 | assert_equal [['due_date', 'desc'], ['tracker', 'asc']], query.sort_criteria |
|
140 | 141 | end |
|
141 | 142 | |
|
142 | 143 | def test_create_with_failure |
|
143 | 144 | @request.session[:user_id] = 2 |
|
144 | 145 | assert_no_difference '::Query.count' do |
|
145 | 146 | post :create, :project_id => 'ecookbook', :query => {:name => ''} |
|
146 | 147 | end |
|
147 | 148 | assert_response :success |
|
148 | 149 | assert_template 'new' |
|
149 | 150 | assert_select 'input[name=?]', 'query[name]' |
|
150 | 151 | end |
|
151 | 152 | |
|
152 | 153 | def test_create_global_query_from_gantt |
|
153 | 154 | @request.session[:user_id] = 1 |
|
154 | 155 | assert_difference 'IssueQuery.count' do |
|
155 | 156 | post :create, |
|
156 | 157 | :gantt => 1, |
|
157 | 158 | :operators => {"status_id" => "o"}, |
|
158 | 159 | :values => {"status_id" => ["1"]}, |
|
159 | 160 | :query => {:name => "test_create_from_gantt", |
|
160 | 161 | :draw_relations => '1', |
|
161 | 162 | :draw_progress_line => '1'} |
|
162 | 163 | assert_response 302 |
|
163 | 164 | end |
|
164 | 165 | query = IssueQuery.order('id DESC').first |
|
165 | 166 | assert_redirected_to "/issues/gantt?query_id=#{query.id}" |
|
166 | 167 | assert_equal true, query.draw_relations |
|
167 | 168 | assert_equal true, query.draw_progress_line |
|
168 | 169 | end |
|
169 | 170 | |
|
170 | 171 | def test_create_project_query_from_gantt |
|
171 | 172 | @request.session[:user_id] = 1 |
|
172 | 173 | assert_difference 'IssueQuery.count' do |
|
173 | 174 | post :create, |
|
174 | 175 | :project_id => 'ecookbook', |
|
175 | 176 | :gantt => 1, |
|
176 | 177 | :operators => {"status_id" => "o"}, |
|
177 | 178 | :values => {"status_id" => ["1"]}, |
|
178 | 179 | :query => {:name => "test_create_from_gantt", |
|
179 | 180 | :draw_relations => '0', |
|
180 | 181 | :draw_progress_line => '0'} |
|
181 | 182 | assert_response 302 |
|
182 | 183 | end |
|
183 | 184 | query = IssueQuery.order('id DESC').first |
|
184 | 185 | assert_redirected_to "/projects/ecookbook/issues/gantt?query_id=#{query.id}" |
|
185 | 186 | assert_equal false, query.draw_relations |
|
186 | 187 | assert_equal false, query.draw_progress_line |
|
187 | 188 | end |
|
188 | 189 | |
|
190 | def test_create_project_public_query_should_force_private_without_manage_public_queries_permission | |
|
191 | @request.session[:user_id] = 3 | |
|
192 | query = new_record(Query) do | |
|
193 | post :create, | |
|
194 | :project_id => 'ecookbook', | |
|
195 | :query => {"name" => "name", "visibility" => "2"} | |
|
196 | assert_response 302 | |
|
197 | end | |
|
198 | assert_not_nil query.project | |
|
199 | assert_equal Query::VISIBILITY_PRIVATE, query.visibility | |
|
200 | end | |
|
201 | ||
|
202 | def test_create_global_public_query_should_force_private_without_manage_public_queries_permission | |
|
203 | @request.session[:user_id] = 3 | |
|
204 | query = new_record(Query) do | |
|
205 | post :create, | |
|
206 | :project_id => 'ecookbook', :query_is_for_all => '1', | |
|
207 | :query => {"name" => "name", "visibility" => "2"} | |
|
208 | assert_response 302 | |
|
209 | end | |
|
210 | assert_nil query.project | |
|
211 | assert_equal Query::VISIBILITY_PRIVATE, query.visibility | |
|
212 | end | |
|
213 | ||
|
214 | def test_create_project_public_query_with_manage_public_queries_permission | |
|
215 | @request.session[:user_id] = 2 | |
|
216 | query = new_record(Query) do | |
|
217 | post :create, | |
|
218 | :project_id => 'ecookbook', | |
|
219 | :query => {"name" => "name", "visibility" => "2"} | |
|
220 | assert_response 302 | |
|
221 | end | |
|
222 | assert_not_nil query.project | |
|
223 | assert_equal Query::VISIBILITY_PUBLIC, query.visibility | |
|
224 | end | |
|
225 | ||
|
226 | def test_create_global_public_query_should_force_private_with_manage_public_queries_permission | |
|
227 | @request.session[:user_id] = 2 | |
|
228 | query = new_record(Query) do | |
|
229 | post :create, | |
|
230 | :project_id => 'ecookbook', :query_is_for_all => '1', | |
|
231 | :query => {"name" => "name", "visibility" => "2"} | |
|
232 | assert_response 302 | |
|
233 | end | |
|
234 | assert_nil query.project | |
|
235 | assert_equal Query::VISIBILITY_PRIVATE, query.visibility | |
|
236 | end | |
|
237 | ||
|
238 | def test_create_global_public_query_by_admin | |
|
239 | @request.session[:user_id] = 1 | |
|
240 | query = new_record(Query) do | |
|
241 | post :create, | |
|
242 | :project_id => 'ecookbook', :query_is_for_all => '1', | |
|
243 | :query => {"name" => "name", "visibility" => "2"} | |
|
244 | assert_response 302 | |
|
245 | end | |
|
246 | assert_nil query.project | |
|
247 | assert_equal Query::VISIBILITY_PUBLIC, query.visibility | |
|
248 | end | |
|
249 | ||
|
189 | 250 | def test_edit_global_public_query |
|
190 | 251 | @request.session[:user_id] = 1 |
|
191 | 252 | get :edit, :id => 4 |
|
192 | 253 | assert_response :success |
|
193 | 254 | assert_template 'edit' |
|
194 | 255 | assert_select 'input[name=?][value="2"][checked=checked]', 'query[visibility]' |
|
195 |
assert_select 'input[name=query_is_for_all][type=checkbox][checked=checked] |
|
|
256 | assert_select 'input[name=query_is_for_all][type=checkbox][checked=checked]' | |
|
196 | 257 | end |
|
197 | 258 | |
|
198 | 259 | def test_edit_global_private_query |
|
199 | 260 | @request.session[:user_id] = 3 |
|
200 | 261 | get :edit, :id => 3 |
|
201 | 262 | assert_response :success |
|
202 | 263 | assert_template 'edit' |
|
203 | 264 | assert_select 'input[name=?]', 'query[visibility]', 0 |
|
204 |
assert_select 'input[name=query_is_for_all][type=checkbox][checked=checked] |
|
|
265 | assert_select 'input[name=query_is_for_all][type=checkbox][checked=checked]' | |
|
205 | 266 | end |
|
206 | 267 | |
|
207 | 268 | def test_edit_project_private_query |
|
208 | 269 | @request.session[:user_id] = 3 |
|
209 | 270 | get :edit, :id => 2 |
|
210 | 271 | assert_response :success |
|
211 | 272 | assert_template 'edit' |
|
212 | 273 | assert_select 'input[name=?]', 'query[visibility]', 0 |
|
213 |
assert_select 'input[name=query_is_for_all][type=checkbox]:not([checked]) |
|
|
274 | assert_select 'input[name=query_is_for_all][type=checkbox]:not([checked])' | |
|
214 | 275 | end |
|
215 | 276 | |
|
216 | 277 | def test_edit_project_public_query |
|
217 | 278 | @request.session[:user_id] = 2 |
|
218 | 279 | get :edit, :id => 1 |
|
219 | 280 | assert_response :success |
|
220 | 281 | assert_template 'edit' |
|
221 | 282 | assert_select 'input[name=?][value="2"][checked=checked]', 'query[visibility]' |
|
222 |
assert_select 'input[name=query_is_for_all][type=checkbox] |
|
|
283 | assert_select 'input[name=query_is_for_all][type=checkbox]:not([checked])' | |
|
223 | 284 | end |
|
224 | 285 | |
|
225 | 286 | def test_edit_sort_criteria |
|
226 | 287 | @request.session[:user_id] = 1 |
|
227 | 288 | get :edit, :id => 5 |
|
228 | 289 | assert_response :success |
|
229 | 290 | assert_template 'edit' |
|
230 | 291 | assert_select 'select[name=?]', 'query[sort_criteria][0][]' do |
|
231 | 292 | assert_select 'option[value=priority][selected=selected]' |
|
232 | 293 | assert_select 'option[value=desc][selected=selected]' |
|
233 | 294 | end |
|
234 | 295 | end |
|
235 | 296 | |
|
236 | 297 | def test_edit_invalid_query |
|
237 | 298 | @request.session[:user_id] = 2 |
|
238 | 299 | get :edit, :id => 99 |
|
239 | 300 | assert_response 404 |
|
240 | 301 | end |
|
241 | 302 | |
|
242 | 303 | def test_udpate_global_private_query |
|
243 | 304 | @request.session[:user_id] = 3 |
|
244 | 305 | put :update, |
|
245 | 306 | :id => 3, |
|
246 | 307 | :default_columns => '1', |
|
247 | 308 | :fields => ["status_id", "assigned_to_id"], |
|
248 | 309 | :operators => {"assigned_to_id" => "=", "status_id" => "o"}, |
|
249 | 310 | :values => { "assigned_to_id" => ["me"], "status_id" => ["1"]}, |
|
250 | 311 | :query => {"name" => "test_edit_global_private_query", "visibility" => "2"} |
|
251 | 312 | |
|
252 | 313 | assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 3 |
|
253 | 314 | q = Query.find_by_name('test_edit_global_private_query') |
|
254 | 315 | assert !q.is_public? |
|
255 | 316 | assert q.has_default_columns? |
|
256 | 317 | assert q.valid? |
|
257 | 318 | end |
|
258 | 319 | |
|
259 | 320 | def test_update_global_public_query |
|
260 | 321 | @request.session[:user_id] = 1 |
|
261 | 322 | put :update, |
|
262 | 323 | :id => 4, |
|
263 | 324 | :default_columns => '1', |
|
264 | 325 | :fields => ["status_id", "assigned_to_id"], |
|
265 | 326 | :operators => {"assigned_to_id" => "=", "status_id" => "o"}, |
|
266 | 327 | :values => { "assigned_to_id" => ["1"], "status_id" => ["1"]}, |
|
267 | 328 | :query => {"name" => "test_edit_global_public_query", "visibility" => "2"} |
|
268 | 329 | |
|
269 | 330 | assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 4 |
|
270 | 331 | q = Query.find_by_name('test_edit_global_public_query') |
|
271 | 332 | assert q.is_public? |
|
272 | 333 | assert q.has_default_columns? |
|
273 | 334 | assert q.valid? |
|
274 | 335 | end |
|
275 | 336 | |
|
276 | 337 | def test_update_with_failure |
|
277 | 338 | @request.session[:user_id] = 1 |
|
278 | 339 | put :update, :id => 4, :query => {:name => ''} |
|
279 | 340 | assert_response :success |
|
280 | 341 | assert_template 'edit' |
|
281 | 342 | end |
|
282 | 343 | |
|
283 | 344 | def test_destroy |
|
284 | 345 | @request.session[:user_id] = 2 |
|
285 | 346 | delete :destroy, :id => 1 |
|
286 | 347 | assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :set_filter => 1, :query_id => nil |
|
287 | 348 | assert_nil Query.find_by_id(1) |
|
288 | 349 | end |
|
289 | 350 | |
|
290 | 351 | def test_backslash_should_be_escaped_in_filters |
|
291 | 352 | @request.session[:user_id] = 2 |
|
292 | 353 | get :new, :subject => 'foo/bar' |
|
293 | 354 | assert_response :success |
|
294 | 355 | assert_template 'new' |
|
295 | 356 | assert_include 'addFilter("subject", "=", ["foo\/bar"]);', response.body |
|
296 | 357 | end |
|
297 | 358 | end |
@@ -1,346 +1,355 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 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 | if ENV["COVERAGE"] |
|
19 | 19 | require 'simplecov' |
|
20 | 20 | require File.expand_path(File.dirname(__FILE__) + "/coverage/html_formatter") |
|
21 | 21 | SimpleCov.formatter = Redmine::Coverage::HtmlFormatter |
|
22 | 22 | SimpleCov.start 'rails' |
|
23 | 23 | end |
|
24 | 24 | |
|
25 | 25 | ENV["RAILS_ENV"] = "test" |
|
26 | 26 | require File.expand_path(File.dirname(__FILE__) + "/../config/environment") |
|
27 | 27 | require 'rails/test_help' |
|
28 | 28 | require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s |
|
29 | 29 | |
|
30 | 30 | require File.expand_path(File.dirname(__FILE__) + '/object_helpers') |
|
31 | 31 | include ObjectHelpers |
|
32 | 32 | |
|
33 | 33 | require 'net/ldap' |
|
34 | 34 | require 'mocha/setup' |
|
35 | 35 | |
|
36 | 36 | Redmine::SudoMode.disable! |
|
37 | 37 | |
|
38 | 38 | class ActionView::TestCase |
|
39 | 39 | helper :application |
|
40 | 40 | include ApplicationHelper |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | class ActiveSupport::TestCase |
|
44 | 44 | include ActionDispatch::TestProcess |
|
45 | 45 | |
|
46 | 46 | self.use_transactional_fixtures = true |
|
47 | 47 | self.use_instantiated_fixtures = false |
|
48 | 48 | |
|
49 | 49 | def uploaded_test_file(name, mime) |
|
50 | 50 | fixture_file_upload("files/#{name}", mime, true) |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | # Mock out a file |
|
54 | 54 | def self.mock_file |
|
55 | 55 | file = 'a_file.png' |
|
56 | 56 | file.stubs(:size).returns(32) |
|
57 | 57 | file.stubs(:original_filename).returns('a_file.png') |
|
58 | 58 | file.stubs(:content_type).returns('image/png') |
|
59 | 59 | file.stubs(:read).returns(false) |
|
60 | 60 | file |
|
61 | 61 | end |
|
62 | 62 | |
|
63 | 63 | def mock_file |
|
64 | 64 | self.class.mock_file |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def mock_file_with_options(options={}) |
|
68 | 68 | file = '' |
|
69 | 69 | file.stubs(:size).returns(32) |
|
70 | 70 | original_filename = options[:original_filename] || nil |
|
71 | 71 | file.stubs(:original_filename).returns(original_filename) |
|
72 | 72 | content_type = options[:content_type] || nil |
|
73 | 73 | file.stubs(:content_type).returns(content_type) |
|
74 | 74 | file.stubs(:read).returns(false) |
|
75 | 75 | file |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | # Use a temporary directory for attachment related tests |
|
79 | 79 | def set_tmp_attachments_directory |
|
80 | 80 | Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test") |
|
81 | 81 | unless File.directory?("#{Rails.root}/tmp/test/attachments") |
|
82 | 82 | Dir.mkdir "#{Rails.root}/tmp/test/attachments" |
|
83 | 83 | end |
|
84 | 84 | Attachment.storage_path = "#{Rails.root}/tmp/test/attachments" |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | def set_fixtures_attachments_directory |
|
88 | 88 | Attachment.storage_path = "#{Rails.root}/test/fixtures/files" |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | def with_settings(options, &block) |
|
92 | 92 | saved_settings = options.keys.inject({}) do |h, k| |
|
93 | 93 | h[k] = case Setting[k] |
|
94 | 94 | when Symbol, false, true, nil |
|
95 | 95 | Setting[k] |
|
96 | 96 | else |
|
97 | 97 | Setting[k].dup |
|
98 | 98 | end |
|
99 | 99 | h |
|
100 | 100 | end |
|
101 | 101 | options.each {|k, v| Setting[k] = v} |
|
102 | 102 | yield |
|
103 | 103 | ensure |
|
104 | 104 | saved_settings.each {|k, v| Setting[k] = v} if saved_settings |
|
105 | 105 | end |
|
106 | 106 | |
|
107 | 107 | # Yields the block with user as the current user |
|
108 | 108 | def with_current_user(user, &block) |
|
109 | 109 | saved_user = User.current |
|
110 | 110 | User.current = user |
|
111 | 111 | yield |
|
112 | 112 | ensure |
|
113 | 113 | User.current = saved_user |
|
114 | 114 | end |
|
115 | 115 | |
|
116 | 116 | def with_locale(locale, &block) |
|
117 | 117 | saved_localed = ::I18n.locale |
|
118 | 118 | ::I18n.locale = locale |
|
119 | 119 | yield |
|
120 | 120 | ensure |
|
121 | 121 | ::I18n.locale = saved_localed |
|
122 | 122 | end |
|
123 | 123 | |
|
124 | 124 | def self.ldap_configured? |
|
125 | 125 | @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389) |
|
126 | 126 | return @test_ldap.bind |
|
127 | 127 | rescue Exception => e |
|
128 | 128 | # LDAP is not listening |
|
129 | 129 | return nil |
|
130 | 130 | end |
|
131 | 131 | |
|
132 | 132 | def self.convert_installed? |
|
133 | 133 | Redmine::Thumbnail.convert_available? |
|
134 | 134 | end |
|
135 | 135 | |
|
136 | 136 | def convert_installed? |
|
137 | 137 | self.class.convert_installed? |
|
138 | 138 | end |
|
139 | 139 | |
|
140 | 140 | # Returns the path to the test +vendor+ repository |
|
141 | 141 | def self.repository_path(vendor) |
|
142 | 142 | path = Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s |
|
143 | 143 | # Unlike ruby, JRuby returns Rails.root with backslashes under Windows |
|
144 | 144 | path.tr("\\", "/") |
|
145 | 145 | end |
|
146 | 146 | |
|
147 | 147 | # Returns the url of the subversion test repository |
|
148 | 148 | def self.subversion_repository_url |
|
149 | 149 | path = repository_path('subversion') |
|
150 | 150 | path = '/' + path unless path.starts_with?('/') |
|
151 | 151 | "file://#{path}" |
|
152 | 152 | end |
|
153 | 153 | |
|
154 | 154 | # Returns true if the +vendor+ test repository is configured |
|
155 | 155 | def self.repository_configured?(vendor) |
|
156 | 156 | File.directory?(repository_path(vendor)) |
|
157 | 157 | end |
|
158 | 158 | |
|
159 | 159 | def repository_path_hash(arr) |
|
160 | 160 | hs = {} |
|
161 | 161 | hs[:path] = arr.join("/") |
|
162 | 162 | hs[:param] = arr.join("/") |
|
163 | 163 | hs |
|
164 | 164 | end |
|
165 | 165 | |
|
166 | 166 | def sqlite? |
|
167 | 167 | ActiveRecord::Base.connection.adapter_name =~ /sqlite/i |
|
168 | 168 | end |
|
169 | 169 | |
|
170 | 170 | def mysql? |
|
171 | 171 | ActiveRecord::Base.connection.adapter_name =~ /mysql/i |
|
172 | 172 | end |
|
173 | 173 | |
|
174 | 174 | def postgresql? |
|
175 | 175 | ActiveRecord::Base.connection.adapter_name =~ /postgresql/i |
|
176 | 176 | end |
|
177 | 177 | |
|
178 | 178 | def quoted_date(date) |
|
179 | 179 | date = Date.parse(date) if date.is_a?(String) |
|
180 | 180 | ActiveRecord::Base.connection.quoted_date(date) |
|
181 | 181 | end |
|
182 | 182 | |
|
183 | # Asserts that a new record for the given class is created | |
|
184 | # and returns it | |
|
185 | def new_record(klass, &block) | |
|
186 | assert_difference "#{klass}.count" do | |
|
187 | yield | |
|
188 | end | |
|
189 | klass.order(:id => :desc).first | |
|
190 | end | |
|
191 | ||
|
183 | 192 | def assert_save(object) |
|
184 | 193 | saved = object.save |
|
185 | 194 | message = "#{object.class} could not be saved" |
|
186 | 195 | errors = object.errors.full_messages.map {|m| "- #{m}"} |
|
187 | 196 | message << ":\n#{errors.join("\n")}" if errors.any? |
|
188 | 197 | assert_equal true, saved, message |
|
189 | 198 | end |
|
190 | 199 | |
|
191 | 200 | def assert_select_error(arg) |
|
192 | 201 | assert_select '#errorExplanation', :text => arg |
|
193 | 202 | end |
|
194 | 203 | |
|
195 | 204 | def assert_include(expected, s, message=nil) |
|
196 | 205 | assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"") |
|
197 | 206 | end |
|
198 | 207 | |
|
199 | 208 | def assert_not_include(expected, s, message=nil) |
|
200 | 209 | assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"") |
|
201 | 210 | end |
|
202 | 211 | |
|
203 | 212 | def assert_select_in(text, *args, &block) |
|
204 | 213 | d = Nokogiri::HTML(CGI::unescapeHTML(String.new(text))).root |
|
205 | 214 | assert_select(d, *args, &block) |
|
206 | 215 | end |
|
207 | 216 | |
|
208 | 217 | def assert_select_email(*args, &block) |
|
209 | 218 | email = ActionMailer::Base.deliveries.last |
|
210 | 219 | assert_not_nil email |
|
211 | 220 | html_body = email.parts.detect {|part| part.content_type.include?('text/html')}.try(&:body) |
|
212 | 221 | assert_not_nil html_body |
|
213 | 222 | assert_select_in html_body.encoded, *args, &block |
|
214 | 223 | end |
|
215 | 224 | |
|
216 | 225 | def assert_mail_body_match(expected, mail, message=nil) |
|
217 | 226 | if expected.is_a?(String) |
|
218 | 227 | assert_include expected, mail_body(mail), message |
|
219 | 228 | else |
|
220 | 229 | assert_match expected, mail_body(mail), message |
|
221 | 230 | end |
|
222 | 231 | end |
|
223 | 232 | |
|
224 | 233 | def assert_mail_body_no_match(expected, mail, message=nil) |
|
225 | 234 | if expected.is_a?(String) |
|
226 | 235 | assert_not_include expected, mail_body(mail), message |
|
227 | 236 | else |
|
228 | 237 | assert_no_match expected, mail_body(mail), message |
|
229 | 238 | end |
|
230 | 239 | end |
|
231 | 240 | |
|
232 | 241 | def mail_body(mail) |
|
233 | 242 | mail.parts.first.body.encoded |
|
234 | 243 | end |
|
235 | 244 | |
|
236 | 245 | # Returns the lft value for a new root issue |
|
237 | 246 | def new_issue_lft |
|
238 | 247 | 1 |
|
239 | 248 | end |
|
240 | 249 | end |
|
241 | 250 | |
|
242 | 251 | module Redmine |
|
243 | 252 | class RoutingTest < ActionDispatch::IntegrationTest |
|
244 | 253 | def should_route(arg) |
|
245 | 254 | arg = arg.dup |
|
246 | 255 | request = arg.keys.detect {|key| key.is_a?(String)} |
|
247 | 256 | raise ArgumentError unless request |
|
248 | 257 | options = arg.slice!(request) |
|
249 | 258 | |
|
250 | 259 | raise ArgumentError unless request =~ /\A(GET|POST|PUT|PATCH|DELETE)\s+(.+)\z/ |
|
251 | 260 | method, path = $1.downcase.to_sym, $2 |
|
252 | 261 | |
|
253 | 262 | raise ArgumentError unless arg.values.first =~ /\A(.+)#(.+)\z/ |
|
254 | 263 | controller, action = $1, $2 |
|
255 | 264 | |
|
256 | 265 | assert_routing( |
|
257 | 266 | {:method => method, :path => path}, |
|
258 | 267 | options.merge(:controller => controller, :action => action) |
|
259 | 268 | ) |
|
260 | 269 | end |
|
261 | 270 | end |
|
262 | 271 | |
|
263 | 272 | class IntegrationTest < ActionDispatch::IntegrationTest |
|
264 | 273 | def log_user(login, password) |
|
265 | 274 | User.anonymous |
|
266 | 275 | get "/login" |
|
267 | 276 | assert_equal nil, session[:user_id] |
|
268 | 277 | assert_response :success |
|
269 | 278 | assert_template "account/login" |
|
270 | 279 | post "/login", :username => login, :password => password |
|
271 | 280 | assert_equal login, User.find(session[:user_id]).login |
|
272 | 281 | end |
|
273 | 282 | |
|
274 | 283 | def credentials(user, password=nil) |
|
275 | 284 | {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)} |
|
276 | 285 | end |
|
277 | 286 | end |
|
278 | 287 | |
|
279 | 288 | module ApiTest |
|
280 | 289 | API_FORMATS = %w(json xml).freeze |
|
281 | 290 | |
|
282 | 291 | # Base class for API tests |
|
283 | 292 | class Base < Redmine::IntegrationTest |
|
284 | 293 | def setup |
|
285 | 294 | Setting.rest_api_enabled = '1' |
|
286 | 295 | end |
|
287 | 296 | |
|
288 | 297 | def teardown |
|
289 | 298 | Setting.rest_api_enabled = '0' |
|
290 | 299 | end |
|
291 | 300 | |
|
292 | 301 | # Uploads content using the XML API and returns the attachment token |
|
293 | 302 | def xml_upload(content, credentials) |
|
294 | 303 | upload('xml', content, credentials) |
|
295 | 304 | end |
|
296 | 305 | |
|
297 | 306 | # Uploads content using the JSON API and returns the attachment token |
|
298 | 307 | def json_upload(content, credentials) |
|
299 | 308 | upload('json', content, credentials) |
|
300 | 309 | end |
|
301 | 310 | |
|
302 | 311 | def upload(format, content, credentials) |
|
303 | 312 | set_tmp_attachments_directory |
|
304 | 313 | assert_difference 'Attachment.count' do |
|
305 | 314 | post "/uploads.#{format}", content, {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials) |
|
306 | 315 | assert_response :created |
|
307 | 316 | end |
|
308 | 317 | data = response_data |
|
309 | 318 | assert_kind_of Hash, data['upload'] |
|
310 | 319 | token = data['upload']['token'] |
|
311 | 320 | assert_not_nil token |
|
312 | 321 | token |
|
313 | 322 | end |
|
314 | 323 | |
|
315 | 324 | # Parses the response body based on its content type |
|
316 | 325 | def response_data |
|
317 | 326 | unless response.content_type.to_s =~ /^application\/(.+)/ |
|
318 | 327 | raise "Unexpected response type: #{response.content_type}" |
|
319 | 328 | end |
|
320 | 329 | format = $1 |
|
321 | 330 | case format |
|
322 | 331 | when 'xml' |
|
323 | 332 | Hash.from_xml(response.body) |
|
324 | 333 | when 'json' |
|
325 | 334 | ActiveSupport::JSON.decode(response.body) |
|
326 | 335 | else |
|
327 | 336 | raise "Unknown response format: #{format}" |
|
328 | 337 | end |
|
329 | 338 | end |
|
330 | 339 | end |
|
331 | 340 | |
|
332 | 341 | class Routing < Redmine::RoutingTest |
|
333 | 342 | def should_route(arg) |
|
334 | 343 | arg = arg.dup |
|
335 | 344 | request = arg.keys.detect {|key| key.is_a?(String)} |
|
336 | 345 | raise ArgumentError unless request |
|
337 | 346 | options = arg.slice!(request) |
|
338 | 347 | |
|
339 | 348 | API_FORMATS.each do |format| |
|
340 | 349 | format_request = request.sub /$/, ".#{format}" |
|
341 | 350 | super options.merge(format_request => arg[request], :format => format) |
|
342 | 351 | end |
|
343 | 352 | end |
|
344 | 353 | end |
|
345 | 354 | end |
|
346 | 355 | end |
General Comments 0
You need to be logged in to leave comments.
Login now