@@ -1,1425 +1,1448 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 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 QueryTest < ActiveSupport::TestCase |
|
21 | 21 | include Redmine::I18n |
|
22 | 22 | |
|
23 | 23 | fixtures :projects, :enabled_modules, :users, :members, |
|
24 | 24 | :member_roles, :roles, :trackers, :issue_statuses, |
|
25 | 25 | :issue_categories, :enumerations, :issues, |
|
26 | 26 | :watchers, :custom_fields, :custom_values, :versions, |
|
27 | 27 | :queries, |
|
28 | 28 | :projects_trackers, |
|
29 | 29 | :custom_fields_trackers, |
|
30 | 30 | :workflows |
|
31 | 31 | |
|
32 | 32 | def test_query_with_roles_visibility_should_validate_roles |
|
33 | 33 | set_language_if_valid 'en' |
|
34 | 34 | query = IssueQuery.new(:name => 'Query', :visibility => IssueQuery::VISIBILITY_ROLES) |
|
35 | 35 | assert !query.save |
|
36 | 36 | assert_include "Roles can't be blank", query.errors.full_messages |
|
37 | 37 | query.role_ids = [1, 2] |
|
38 | 38 | assert query.save |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | def test_changing_roles_visibility_should_clear_roles |
|
42 | 42 | query = IssueQuery.create!(:name => 'Query', :visibility => IssueQuery::VISIBILITY_ROLES, :role_ids => [1, 2]) |
|
43 | 43 | assert_equal 2, query.roles.count |
|
44 | 44 | |
|
45 | 45 | query.visibility = IssueQuery::VISIBILITY_PUBLIC |
|
46 | 46 | query.save! |
|
47 | 47 | assert_equal 0, query.roles.count |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def test_available_filters_should_be_ordered |
|
51 | 51 | set_language_if_valid 'en' |
|
52 | 52 | query = IssueQuery.new |
|
53 | 53 | assert_equal 0, query.available_filters.keys.index('status_id') |
|
54 | 54 | expected_order = [ |
|
55 | 55 | "Status", |
|
56 | 56 | "Project", |
|
57 | 57 | "Tracker", |
|
58 | 58 | "Priority" |
|
59 | 59 | ] |
|
60 | 60 | assert_equal expected_order, |
|
61 | 61 | (query.available_filters.values.map{|v| v[:name]} & expected_order) |
|
62 | 62 | end |
|
63 | 63 | |
|
64 | 64 | def test_available_filters_with_custom_fields_should_be_ordered |
|
65 | 65 | set_language_if_valid 'en' |
|
66 | 66 | UserCustomField.create!( |
|
67 | 67 | :name => 'order test', :field_format => 'string', |
|
68 | 68 | :is_for_all => true, :is_filter => true |
|
69 | 69 | ) |
|
70 | 70 | query = IssueQuery.new |
|
71 | 71 | expected_order = [ |
|
72 | 72 | "Searchable field", |
|
73 | 73 | "Database", |
|
74 | 74 | "Project's Development status", |
|
75 | 75 | "Author's order test", |
|
76 | 76 | "Assignee's order test" |
|
77 | 77 | ] |
|
78 | 78 | assert_equal expected_order, |
|
79 | 79 | (query.available_filters.values.map{|v| v[:name]} & expected_order) |
|
80 | 80 | end |
|
81 | 81 | |
|
82 | 82 | def test_custom_fields_for_all_projects_should_be_available_in_global_queries |
|
83 | 83 | query = IssueQuery.new(:project => nil, :name => '_') |
|
84 | 84 | assert query.available_filters.has_key?('cf_1') |
|
85 | 85 | assert !query.available_filters.has_key?('cf_3') |
|
86 | 86 | end |
|
87 | 87 | |
|
88 | 88 | def test_system_shared_versions_should_be_available_in_global_queries |
|
89 | 89 | Version.find(2).update_attribute :sharing, 'system' |
|
90 | 90 | query = IssueQuery.new(:project => nil, :name => '_') |
|
91 | 91 | assert query.available_filters.has_key?('fixed_version_id') |
|
92 | 92 | assert query.available_filters['fixed_version_id'][:values].detect {|v| v.last == '2'} |
|
93 | 93 | end |
|
94 | 94 | |
|
95 | 95 | def test_project_filter_in_global_queries |
|
96 | 96 | query = IssueQuery.new(:project => nil, :name => '_') |
|
97 | 97 | project_filter = query.available_filters["project_id"] |
|
98 | 98 | assert_not_nil project_filter |
|
99 | 99 | project_ids = project_filter[:values].map{|p| p[1]} |
|
100 | 100 | assert project_ids.include?("1") #public project |
|
101 | 101 | assert !project_ids.include?("2") #private project user cannot see |
|
102 | 102 | end |
|
103 | 103 | |
|
104 | def test_available_filters_should_not_include_fields_disabled_on_all_trackers | |
|
105 | Tracker.all.each do |tracker| | |
|
106 | tracker.core_fields = Tracker::CORE_FIELDS - ['start_date'] | |
|
107 | tracker.save! | |
|
108 | end | |
|
109 | ||
|
110 | query = IssueQuery.new(:name => '_') | |
|
111 | assert_include 'due_date', query.available_filters | |
|
112 | assert_not_include 'start_date', query.available_filters | |
|
113 | end | |
|
114 | ||
|
104 | 115 | def find_issues_with_query(query) |
|
105 | 116 | Issue.joins(:status, :tracker, :project, :priority).where( |
|
106 | 117 | query.statement |
|
107 | 118 | ).to_a |
|
108 | 119 | end |
|
109 | 120 | |
|
110 | 121 | def assert_find_issues_with_query_is_successful(query) |
|
111 | 122 | assert_nothing_raised do |
|
112 | 123 | find_issues_with_query(query) |
|
113 | 124 | end |
|
114 | 125 | end |
|
115 | 126 | |
|
116 | 127 | def assert_query_statement_includes(query, condition) |
|
117 | 128 | assert_include condition, query.statement |
|
118 | 129 | end |
|
119 | 130 | |
|
120 | 131 | def assert_query_result(expected, query) |
|
121 | 132 | assert_nothing_raised do |
|
122 | 133 | assert_equal expected.map(&:id).sort, query.issues.map(&:id).sort |
|
123 | 134 | assert_equal expected.size, query.issue_count |
|
124 | 135 | end |
|
125 | 136 | end |
|
126 | 137 | |
|
127 | 138 | def test_query_should_allow_shared_versions_for_a_project_query |
|
128 | 139 | subproject_version = Version.find(4) |
|
129 | 140 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
130 | 141 | query.add_filter('fixed_version_id', '=', [subproject_version.id.to_s]) |
|
131 | 142 | |
|
132 | 143 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IN ('4')") |
|
133 | 144 | end |
|
134 | 145 | |
|
135 | 146 | def test_query_with_multiple_custom_fields |
|
136 | 147 | query = IssueQuery.find(1) |
|
137 | 148 | assert query.valid? |
|
138 | 149 | assert query.statement.include?("#{CustomValue.table_name}.value IN ('MySQL')") |
|
139 | 150 | issues = find_issues_with_query(query) |
|
140 | 151 | assert_equal 1, issues.length |
|
141 | 152 | assert_equal Issue.find(3), issues.first |
|
142 | 153 | end |
|
143 | 154 | |
|
144 | 155 | def test_operator_none |
|
145 | 156 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
146 | 157 | query.add_filter('fixed_version_id', '!*', ['']) |
|
147 | 158 | query.add_filter('cf_1', '!*', ['']) |
|
148 | 159 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL") |
|
149 | 160 | assert query.statement.include?("#{CustomValue.table_name}.value IS NULL OR #{CustomValue.table_name}.value = ''") |
|
150 | 161 | find_issues_with_query(query) |
|
151 | 162 | end |
|
152 | 163 | |
|
153 | 164 | def test_operator_none_for_integer |
|
154 | 165 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
155 | 166 | query.add_filter('estimated_hours', '!*', ['']) |
|
156 | 167 | issues = find_issues_with_query(query) |
|
157 | 168 | assert !issues.empty? |
|
158 | 169 | assert issues.all? {|i| !i.estimated_hours} |
|
159 | 170 | end |
|
160 | 171 | |
|
161 | 172 | def test_operator_none_for_date |
|
162 | 173 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
163 | 174 | query.add_filter('start_date', '!*', ['']) |
|
164 | 175 | issues = find_issues_with_query(query) |
|
165 | 176 | assert !issues.empty? |
|
166 | 177 | assert issues.all? {|i| i.start_date.nil?} |
|
167 | 178 | end |
|
168 | 179 | |
|
169 | 180 | def test_operator_none_for_string_custom_field |
|
170 | 181 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
171 | 182 | query.add_filter('cf_2', '!*', ['']) |
|
172 | 183 | assert query.has_filter?('cf_2') |
|
173 | 184 | issues = find_issues_with_query(query) |
|
174 | 185 | assert !issues.empty? |
|
175 | 186 | assert issues.all? {|i| i.custom_field_value(2).blank?} |
|
176 | 187 | end |
|
177 | 188 | |
|
178 | 189 | def test_operator_all |
|
179 | 190 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
180 | 191 | query.add_filter('fixed_version_id', '*', ['']) |
|
181 | 192 | query.add_filter('cf_1', '*', ['']) |
|
182 | 193 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL") |
|
183 | 194 | assert query.statement.include?("#{CustomValue.table_name}.value IS NOT NULL AND #{CustomValue.table_name}.value <> ''") |
|
184 | 195 | find_issues_with_query(query) |
|
185 | 196 | end |
|
186 | 197 | |
|
187 | 198 | def test_operator_all_for_date |
|
188 | 199 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
189 | 200 | query.add_filter('start_date', '*', ['']) |
|
190 | 201 | issues = find_issues_with_query(query) |
|
191 | 202 | assert !issues.empty? |
|
192 | 203 | assert issues.all? {|i| i.start_date.present?} |
|
193 | 204 | end |
|
194 | 205 | |
|
195 | 206 | def test_operator_all_for_string_custom_field |
|
196 | 207 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
197 | 208 | query.add_filter('cf_2', '*', ['']) |
|
198 | 209 | assert query.has_filter?('cf_2') |
|
199 | 210 | issues = find_issues_with_query(query) |
|
200 | 211 | assert !issues.empty? |
|
201 | 212 | assert issues.all? {|i| i.custom_field_value(2).present?} |
|
202 | 213 | end |
|
203 | 214 | |
|
204 | 215 | def test_numeric_filter_should_not_accept_non_numeric_values |
|
205 | 216 | query = IssueQuery.new(:name => '_') |
|
206 | 217 | query.add_filter('estimated_hours', '=', ['a']) |
|
207 | 218 | |
|
208 | 219 | assert query.has_filter?('estimated_hours') |
|
209 | 220 | assert !query.valid? |
|
210 | 221 | end |
|
211 | 222 | |
|
212 | 223 | def test_operator_is_on_float |
|
213 | 224 | Issue.where(:id => 2).update_all("estimated_hours = 171.2") |
|
214 | 225 | query = IssueQuery.new(:name => '_') |
|
215 | 226 | query.add_filter('estimated_hours', '=', ['171.20']) |
|
216 | 227 | issues = find_issues_with_query(query) |
|
217 | 228 | assert_equal 1, issues.size |
|
218 | 229 | assert_equal 2, issues.first.id |
|
219 | 230 | end |
|
220 | 231 | |
|
221 | 232 | def test_operator_is_on_integer_custom_field |
|
222 | 233 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true, :trackers => Tracker.all) |
|
223 | 234 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7') |
|
224 | 235 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12') |
|
225 | 236 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
226 | 237 | |
|
227 | 238 | query = IssueQuery.new(:name => '_') |
|
228 | 239 | query.add_filter("cf_#{f.id}", '=', ['12']) |
|
229 | 240 | issues = find_issues_with_query(query) |
|
230 | 241 | assert_equal 1, issues.size |
|
231 | 242 | assert_equal 2, issues.first.id |
|
232 | 243 | end |
|
233 | 244 | |
|
234 | 245 | def test_operator_is_on_integer_custom_field_should_accept_negative_value |
|
235 | 246 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true, :trackers => Tracker.all) |
|
236 | 247 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7') |
|
237 | 248 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12') |
|
238 | 249 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
239 | 250 | |
|
240 | 251 | query = IssueQuery.new(:name => '_') |
|
241 | 252 | query.add_filter("cf_#{f.id}", '=', ['-12']) |
|
242 | 253 | assert query.valid? |
|
243 | 254 | issues = find_issues_with_query(query) |
|
244 | 255 | assert_equal 1, issues.size |
|
245 | 256 | assert_equal 2, issues.first.id |
|
246 | 257 | end |
|
247 | 258 | |
|
248 | 259 | def test_operator_is_on_float_custom_field |
|
249 | 260 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true, :trackers => Tracker.all) |
|
250 | 261 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3') |
|
251 | 262 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12.7') |
|
252 | 263 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
253 | 264 | |
|
254 | 265 | query = IssueQuery.new(:name => '_') |
|
255 | 266 | query.add_filter("cf_#{f.id}", '=', ['12.7']) |
|
256 | 267 | issues = find_issues_with_query(query) |
|
257 | 268 | assert_equal 1, issues.size |
|
258 | 269 | assert_equal 2, issues.first.id |
|
259 | 270 | end |
|
260 | 271 | |
|
261 | 272 | def test_operator_is_on_float_custom_field_should_accept_negative_value |
|
262 | 273 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true, :trackers => Tracker.all) |
|
263 | 274 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3') |
|
264 | 275 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12.7') |
|
265 | 276 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
266 | 277 | |
|
267 | 278 | query = IssueQuery.new(:name => '_') |
|
268 | 279 | query.add_filter("cf_#{f.id}", '=', ['-12.7']) |
|
269 | 280 | assert query.valid? |
|
270 | 281 | issues = find_issues_with_query(query) |
|
271 | 282 | assert_equal 1, issues.size |
|
272 | 283 | assert_equal 2, issues.first.id |
|
273 | 284 | end |
|
274 | 285 | |
|
275 | 286 | def test_operator_is_on_multi_list_custom_field |
|
276 | 287 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true, |
|
277 | 288 | :possible_values => ['value1', 'value2', 'value3'], :multiple => true, :trackers => Tracker.all) |
|
278 | 289 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1') |
|
279 | 290 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2') |
|
280 | 291 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1') |
|
281 | 292 | |
|
282 | 293 | query = IssueQuery.new(:name => '_') |
|
283 | 294 | query.add_filter("cf_#{f.id}", '=', ['value1']) |
|
284 | 295 | issues = find_issues_with_query(query) |
|
285 | 296 | assert_equal [1, 3], issues.map(&:id).sort |
|
286 | 297 | |
|
287 | 298 | query = IssueQuery.new(:name => '_') |
|
288 | 299 | query.add_filter("cf_#{f.id}", '=', ['value2']) |
|
289 | 300 | issues = find_issues_with_query(query) |
|
290 | 301 | assert_equal [1], issues.map(&:id).sort |
|
291 | 302 | end |
|
292 | 303 | |
|
293 | 304 | def test_operator_is_not_on_multi_list_custom_field |
|
294 | 305 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true, |
|
295 | 306 | :possible_values => ['value1', 'value2', 'value3'], :multiple => true, :trackers => Tracker.all) |
|
296 | 307 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1') |
|
297 | 308 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2') |
|
298 | 309 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1') |
|
299 | 310 | |
|
300 | 311 | query = IssueQuery.new(:name => '_') |
|
301 | 312 | query.add_filter("cf_#{f.id}", '!', ['value1']) |
|
302 | 313 | issues = find_issues_with_query(query) |
|
303 | 314 | assert !issues.map(&:id).include?(1) |
|
304 | 315 | assert !issues.map(&:id).include?(3) |
|
305 | 316 | |
|
306 | 317 | query = IssueQuery.new(:name => '_') |
|
307 | 318 | query.add_filter("cf_#{f.id}", '!', ['value2']) |
|
308 | 319 | issues = find_issues_with_query(query) |
|
309 | 320 | assert !issues.map(&:id).include?(1) |
|
310 | 321 | assert issues.map(&:id).include?(3) |
|
311 | 322 | end |
|
312 | 323 | |
|
313 | 324 | def test_operator_is_on_is_private_field |
|
314 | 325 | # is_private filter only available for those who can set issues private |
|
315 | 326 | User.current = User.find(2) |
|
316 | 327 | |
|
317 | 328 | query = IssueQuery.new(:name => '_') |
|
318 | 329 | assert query.available_filters.key?('is_private') |
|
319 | 330 | |
|
320 | 331 | query.add_filter("is_private", '=', ['1']) |
|
321 | 332 | issues = find_issues_with_query(query) |
|
322 | 333 | assert issues.any? |
|
323 | 334 | assert_nil issues.detect {|issue| !issue.is_private?} |
|
324 | 335 | ensure |
|
325 | 336 | User.current = nil |
|
326 | 337 | end |
|
327 | 338 | |
|
328 | 339 | def test_operator_is_not_on_is_private_field |
|
329 | 340 | # is_private filter only available for those who can set issues private |
|
330 | 341 | User.current = User.find(2) |
|
331 | 342 | |
|
332 | 343 | query = IssueQuery.new(:name => '_') |
|
333 | 344 | assert query.available_filters.key?('is_private') |
|
334 | 345 | |
|
335 | 346 | query.add_filter("is_private", '!', ['1']) |
|
336 | 347 | issues = find_issues_with_query(query) |
|
337 | 348 | assert issues.any? |
|
338 | 349 | assert_nil issues.detect {|issue| issue.is_private?} |
|
339 | 350 | ensure |
|
340 | 351 | User.current = nil |
|
341 | 352 | end |
|
342 | 353 | |
|
343 | 354 | def test_operator_greater_than |
|
344 | 355 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
345 | 356 | query.add_filter('done_ratio', '>=', ['40']) |
|
346 | 357 | assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40.0") |
|
347 | 358 | find_issues_with_query(query) |
|
348 | 359 | end |
|
349 | 360 | |
|
350 | 361 | def test_operator_greater_than_a_float |
|
351 | 362 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
352 | 363 | query.add_filter('estimated_hours', '>=', ['40.5']) |
|
353 | 364 | assert query.statement.include?("#{Issue.table_name}.estimated_hours >= 40.5") |
|
354 | 365 | find_issues_with_query(query) |
|
355 | 366 | end |
|
356 | 367 | |
|
357 | 368 | def test_operator_greater_than_on_int_custom_field |
|
358 | 369 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true, :trackers => Tracker.all) |
|
359 | 370 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7') |
|
360 | 371 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12') |
|
361 | 372 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
362 | 373 | |
|
363 | 374 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
364 | 375 | query.add_filter("cf_#{f.id}", '>=', ['8']) |
|
365 | 376 | issues = find_issues_with_query(query) |
|
366 | 377 | assert_equal 1, issues.size |
|
367 | 378 | assert_equal 2, issues.first.id |
|
368 | 379 | end |
|
369 | 380 | |
|
370 | 381 | def test_operator_lesser_than |
|
371 | 382 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
372 | 383 | query.add_filter('done_ratio', '<=', ['30']) |
|
373 | 384 | assert query.statement.include?("#{Issue.table_name}.done_ratio <= 30.0") |
|
374 | 385 | find_issues_with_query(query) |
|
375 | 386 | end |
|
376 | 387 | |
|
377 | 388 | def test_operator_lesser_than_on_custom_field |
|
378 | 389 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
379 | 390 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
380 | 391 | query.add_filter("cf_#{f.id}", '<=', ['30']) |
|
381 | 392 | assert_match /CAST.+ <= 30\.0/, query.statement |
|
382 | 393 | find_issues_with_query(query) |
|
383 | 394 | end |
|
384 | 395 | |
|
385 | 396 | def test_operator_lesser_than_on_date_custom_field |
|
386 | 397 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'date', :is_filter => true, :is_for_all => true, :trackers => Tracker.all) |
|
387 | 398 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '2013-04-11') |
|
388 | 399 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '2013-05-14') |
|
389 | 400 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
390 | 401 | |
|
391 | 402 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
392 | 403 | query.add_filter("cf_#{f.id}", '<=', ['2013-05-01']) |
|
393 | 404 | issue_ids = find_issues_with_query(query).map(&:id) |
|
394 | 405 | assert_include 1, issue_ids |
|
395 | 406 | assert_not_include 2, issue_ids |
|
396 | 407 | assert_not_include 3, issue_ids |
|
397 | 408 | end |
|
398 | 409 | |
|
399 | 410 | def test_operator_between |
|
400 | 411 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
401 | 412 | query.add_filter('done_ratio', '><', ['30', '40']) |
|
402 | 413 | assert_include "#{Issue.table_name}.done_ratio BETWEEN 30.0 AND 40.0", query.statement |
|
403 | 414 | find_issues_with_query(query) |
|
404 | 415 | end |
|
405 | 416 | |
|
406 | 417 | def test_operator_between_on_custom_field |
|
407 | 418 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
408 | 419 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
409 | 420 | query.add_filter("cf_#{f.id}", '><', ['30', '40']) |
|
410 | 421 | assert_match /CAST.+ BETWEEN 30.0 AND 40.0/, query.statement |
|
411 | 422 | find_issues_with_query(query) |
|
412 | 423 | end |
|
413 | 424 | |
|
414 | 425 | def test_date_filter_should_not_accept_non_date_values |
|
415 | 426 | query = IssueQuery.new(:name => '_') |
|
416 | 427 | query.add_filter('created_on', '=', ['a']) |
|
417 | 428 | |
|
418 | 429 | assert query.has_filter?('created_on') |
|
419 | 430 | assert !query.valid? |
|
420 | 431 | end |
|
421 | 432 | |
|
422 | 433 | def test_date_filter_should_not_accept_invalid_date_values |
|
423 | 434 | query = IssueQuery.new(:name => '_') |
|
424 | 435 | query.add_filter('created_on', '=', ['2011-01-34']) |
|
425 | 436 | |
|
426 | 437 | assert query.has_filter?('created_on') |
|
427 | 438 | assert !query.valid? |
|
428 | 439 | end |
|
429 | 440 | |
|
430 | 441 | def test_relative_date_filter_should_not_accept_non_integer_values |
|
431 | 442 | query = IssueQuery.new(:name => '_') |
|
432 | 443 | query.add_filter('created_on', '>t-', ['a']) |
|
433 | 444 | |
|
434 | 445 | assert query.has_filter?('created_on') |
|
435 | 446 | assert !query.valid? |
|
436 | 447 | end |
|
437 | 448 | |
|
438 | 449 | def test_operator_date_equals |
|
439 | 450 | query = IssueQuery.new(:name => '_') |
|
440 | 451 | query.add_filter('due_date', '=', ['2011-07-10']) |
|
441 | 452 | assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
442 | 453 | find_issues_with_query(query) |
|
443 | 454 | end |
|
444 | 455 | |
|
445 | 456 | def test_operator_date_lesser_than |
|
446 | 457 | query = IssueQuery.new(:name => '_') |
|
447 | 458 | query.add_filter('due_date', '<=', ['2011-07-10']) |
|
448 | 459 | assert_match /issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
449 | 460 | find_issues_with_query(query) |
|
450 | 461 | end |
|
451 | 462 | |
|
452 | 463 | def test_operator_date_lesser_than_with_timestamp |
|
453 | 464 | query = IssueQuery.new(:name => '_') |
|
454 | 465 | query.add_filter('updated_on', '<=', ['2011-07-10T19:13:52']) |
|
455 | 466 | assert_match /issues\.updated_on <= '2011-07-10 19:13:52/, query.statement |
|
456 | 467 | find_issues_with_query(query) |
|
457 | 468 | end |
|
458 | 469 | |
|
459 | 470 | def test_operator_date_greater_than |
|
460 | 471 | query = IssueQuery.new(:name => '_') |
|
461 | 472 | query.add_filter('due_date', '>=', ['2011-07-10']) |
|
462 | 473 | assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?'/, query.statement |
|
463 | 474 | find_issues_with_query(query) |
|
464 | 475 | end |
|
465 | 476 | |
|
466 | 477 | def test_operator_date_greater_than_with_timestamp |
|
467 | 478 | query = IssueQuery.new(:name => '_') |
|
468 | 479 | query.add_filter('updated_on', '>=', ['2011-07-10T19:13:52']) |
|
469 | 480 | assert_match /issues\.updated_on > '2011-07-10 19:13:51(\.0+)?'/, query.statement |
|
470 | 481 | find_issues_with_query(query) |
|
471 | 482 | end |
|
472 | 483 | |
|
473 | 484 | def test_operator_date_between |
|
474 | 485 | query = IssueQuery.new(:name => '_') |
|
475 | 486 | query.add_filter('due_date', '><', ['2011-06-23', '2011-07-10']) |
|
476 | 487 | assert_match /issues\.due_date > '2011-06-22 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?'/, query.statement |
|
477 | 488 | find_issues_with_query(query) |
|
478 | 489 | end |
|
479 | 490 | |
|
480 | 491 | def test_operator_in_more_than |
|
481 | 492 | Issue.find(7).update_attribute(:due_date, (Date.today + 15)) |
|
482 | 493 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
483 | 494 | query.add_filter('due_date', '>t+', ['15']) |
|
484 | 495 | issues = find_issues_with_query(query) |
|
485 | 496 | assert !issues.empty? |
|
486 | 497 | issues.each {|issue| assert(issue.due_date >= (Date.today + 15))} |
|
487 | 498 | end |
|
488 | 499 | |
|
489 | 500 | def test_operator_in_less_than |
|
490 | 501 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
491 | 502 | query.add_filter('due_date', '<t+', ['15']) |
|
492 | 503 | issues = find_issues_with_query(query) |
|
493 | 504 | assert !issues.empty? |
|
494 | 505 | issues.each {|issue| assert(issue.due_date <= (Date.today + 15))} |
|
495 | 506 | end |
|
496 | 507 | |
|
497 | 508 | def test_operator_in_the_next_days |
|
498 | 509 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
499 | 510 | query.add_filter('due_date', '><t+', ['15']) |
|
500 | 511 | issues = find_issues_with_query(query) |
|
501 | 512 | assert !issues.empty? |
|
502 | 513 | issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))} |
|
503 | 514 | end |
|
504 | 515 | |
|
505 | 516 | def test_operator_less_than_ago |
|
506 | 517 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
507 | 518 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
508 | 519 | query.add_filter('due_date', '>t-', ['3']) |
|
509 | 520 | issues = find_issues_with_query(query) |
|
510 | 521 | assert !issues.empty? |
|
511 | 522 | issues.each {|issue| assert(issue.due_date >= (Date.today - 3))} |
|
512 | 523 | end |
|
513 | 524 | |
|
514 | 525 | def test_operator_in_the_past_days |
|
515 | 526 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
516 | 527 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
517 | 528 | query.add_filter('due_date', '><t-', ['3']) |
|
518 | 529 | issues = find_issues_with_query(query) |
|
519 | 530 | assert !issues.empty? |
|
520 | 531 | issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)} |
|
521 | 532 | end |
|
522 | 533 | |
|
523 | 534 | def test_operator_more_than_ago |
|
524 | 535 | Issue.find(7).update_attribute(:due_date, (Date.today - 10)) |
|
525 | 536 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
526 | 537 | query.add_filter('due_date', '<t-', ['10']) |
|
527 | 538 | assert query.statement.include?("#{Issue.table_name}.due_date <=") |
|
528 | 539 | issues = find_issues_with_query(query) |
|
529 | 540 | assert !issues.empty? |
|
530 | 541 | issues.each {|issue| assert(issue.due_date <= (Date.today - 10))} |
|
531 | 542 | end |
|
532 | 543 | |
|
533 | 544 | def test_operator_in |
|
534 | 545 | Issue.find(7).update_attribute(:due_date, (Date.today + 2)) |
|
535 | 546 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
536 | 547 | query.add_filter('due_date', 't+', ['2']) |
|
537 | 548 | issues = find_issues_with_query(query) |
|
538 | 549 | assert !issues.empty? |
|
539 | 550 | issues.each {|issue| assert_equal((Date.today + 2), issue.due_date)} |
|
540 | 551 | end |
|
541 | 552 | |
|
542 | 553 | def test_operator_ago |
|
543 | 554 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
544 | 555 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
545 | 556 | query.add_filter('due_date', 't-', ['3']) |
|
546 | 557 | issues = find_issues_with_query(query) |
|
547 | 558 | assert !issues.empty? |
|
548 | 559 | issues.each {|issue| assert_equal((Date.today - 3), issue.due_date)} |
|
549 | 560 | end |
|
550 | 561 | |
|
551 | 562 | def test_operator_today |
|
552 | 563 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
553 | 564 | query.add_filter('due_date', 't', ['']) |
|
554 | 565 | issues = find_issues_with_query(query) |
|
555 | 566 | assert !issues.empty? |
|
556 | 567 | issues.each {|issue| assert_equal Date.today, issue.due_date} |
|
557 | 568 | end |
|
558 | 569 | |
|
559 |
def test_operator_ |
|
|
560 | query = IssueQuery.new(:project => Project.find(1), :name => '_') | |
|
561 | query.add_filter('due_date', 'w', ['']) | |
|
562 | find_issues_with_query(query) | |
|
570 | def test_operator_date_periods | |
|
571 | %w(t ld w lw l2w m lm y).each do |operator| | |
|
572 | query = IssueQuery.new(:name => '_') | |
|
573 | query.add_filter('due_date', operator, ['']) | |
|
574 | assert query.valid? | |
|
575 | assert query.issues | |
|
576 | end | |
|
563 | 577 | end |
|
564 | 578 | |
|
565 |
def test_operator_ |
|
|
566 | query = IssueQuery.new(:project => Project.find(1), :name => '_') | |
|
567 | query.add_filter('created_on', 'w', ['']) | |
|
568 | find_issues_with_query(query) | |
|
579 | def test_operator_datetime_periods | |
|
580 | %w(t ld w lw l2w m lm y).each do |operator| | |
|
581 | query = IssueQuery.new(:name => '_') | |
|
582 | query.add_filter('created_on', operator, ['']) | |
|
583 | assert query.valid? | |
|
584 | assert query.issues | |
|
585 | end | |
|
569 | 586 | end |
|
570 | 587 | |
|
571 | 588 | def test_operator_contains |
|
572 | 589 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
573 | 590 | query.add_filter('subject', '~', ['uNable']) |
|
574 | 591 | assert query.statement.include?("LOWER(#{Issue.table_name}.subject) LIKE '%unable%'") |
|
575 | 592 | result = find_issues_with_query(query) |
|
576 | 593 | assert result.empty? |
|
577 | 594 | result.each {|issue| assert issue.subject.downcase.include?('unable') } |
|
578 | 595 | end |
|
579 | 596 | |
|
580 | 597 | def test_range_for_this_week_with_week_starting_on_monday |
|
581 | 598 | I18n.locale = :fr |
|
582 | 599 | assert_equal '1', I18n.t(:general_first_day_of_week) |
|
583 | 600 | |
|
584 | 601 | Date.stubs(:today).returns(Date.parse('2011-04-29')) |
|
585 | 602 | |
|
586 | 603 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
587 | 604 | query.add_filter('due_date', 'w', ['']) |
|
588 | 605 | assert query.statement.match(/issues\.due_date > '2011-04-24 23:59:59(\.9+)?' AND issues\.due_date <= '2011-05-01 23:59:59(\.9+)?/), "range not found in #{query.statement}" |
|
589 | 606 | I18n.locale = :en |
|
590 | 607 | end |
|
591 | 608 | |
|
592 | 609 | def test_range_for_this_week_with_week_starting_on_sunday |
|
593 | 610 | I18n.locale = :en |
|
594 | 611 | assert_equal '7', I18n.t(:general_first_day_of_week) |
|
595 | 612 | |
|
596 | 613 | Date.stubs(:today).returns(Date.parse('2011-04-29')) |
|
597 | 614 | |
|
598 | 615 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
599 | 616 | query.add_filter('due_date', 'w', ['']) |
|
600 | 617 | assert query.statement.match(/issues\.due_date > '2011-04-23 23:59:59(\.9+)?' AND issues\.due_date <= '2011-04-30 23:59:59(\.9+)?/), "range not found in #{query.statement}" |
|
601 | 618 | end |
|
602 | 619 | |
|
603 | 620 | def test_operator_does_not_contains |
|
604 | 621 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
605 | 622 | query.add_filter('subject', '!~', ['uNable']) |
|
606 | 623 | assert query.statement.include?("LOWER(#{Issue.table_name}.subject) NOT LIKE '%unable%'") |
|
607 | 624 | find_issues_with_query(query) |
|
608 | 625 | end |
|
609 | 626 | |
|
610 | 627 | def test_filter_assigned_to_me |
|
611 | 628 | user = User.find(2) |
|
612 | 629 | group = Group.find(10) |
|
613 | 630 | User.current = user |
|
614 | 631 | i1 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => user) |
|
615 | 632 | i2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => group) |
|
616 | 633 | i3 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => Group.find(11)) |
|
617 | 634 | group.users << user |
|
618 | 635 | |
|
619 | 636 | query = IssueQuery.new(:name => '_', :filters => { 'assigned_to_id' => {:operator => '=', :values => ['me']}}) |
|
620 | 637 | result = query.issues |
|
621 | 638 | assert_equal Issue.visible.where(:assigned_to_id => ([2] + user.reload.group_ids)).sort_by(&:id), result.sort_by(&:id) |
|
622 | 639 | |
|
623 | 640 | assert result.include?(i1) |
|
624 | 641 | assert result.include?(i2) |
|
625 | 642 | assert !result.include?(i3) |
|
626 | 643 | end |
|
627 | 644 | |
|
628 | 645 | def test_user_custom_field_filtered_on_me |
|
629 | 646 | User.current = User.find(2) |
|
630 | 647 | cf = IssueCustomField.create!(:field_format => 'user', :is_for_all => true, :is_filter => true, :name => 'User custom field', :tracker_ids => [1]) |
|
631 | 648 | issue1 = Issue.create!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '2'}, :subject => 'Test', :author_id => 1) |
|
632 | 649 | issue2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '3'}) |
|
633 | 650 | |
|
634 | 651 | query = IssueQuery.new(:name => '_', :project => Project.find(1)) |
|
635 | 652 | filter = query.available_filters["cf_#{cf.id}"] |
|
636 | 653 | assert_not_nil filter |
|
637 | 654 | assert_include 'me', filter[:values].map{|v| v[1]} |
|
638 | 655 | |
|
639 | 656 | query.filters = { "cf_#{cf.id}" => {:operator => '=', :values => ['me']}} |
|
640 | 657 | result = query.issues |
|
641 | 658 | assert_equal 1, result.size |
|
642 | 659 | assert_equal issue1, result.first |
|
643 | 660 | end |
|
644 | 661 | |
|
662 | def test_filter_on_me_by_anonymous_user | |
|
663 | User.current = nil | |
|
664 | query = IssueQuery.new(:name => '_', :filters => { 'assigned_to_id' => {:operator => '=', :values => ['me']}}) | |
|
665 | assert_equal [], query.issues | |
|
666 | end | |
|
667 | ||
|
645 | 668 | def test_filter_my_projects |
|
646 | 669 | User.current = User.find(2) |
|
647 | 670 | query = IssueQuery.new(:name => '_') |
|
648 | 671 | filter = query.available_filters['project_id'] |
|
649 | 672 | assert_not_nil filter |
|
650 | 673 | assert_include 'mine', filter[:values].map{|v| v[1]} |
|
651 | 674 | |
|
652 | 675 | query.filters = { 'project_id' => {:operator => '=', :values => ['mine']}} |
|
653 | 676 | result = query.issues |
|
654 | 677 | assert_nil result.detect {|issue| !User.current.member_of?(issue.project)} |
|
655 | 678 | end |
|
656 | 679 | |
|
657 | 680 | def test_filter_watched_issues |
|
658 | 681 | User.current = User.find(1) |
|
659 | 682 | query = IssueQuery.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}}) |
|
660 | 683 | result = find_issues_with_query(query) |
|
661 | 684 | assert_not_nil result |
|
662 | 685 | assert !result.empty? |
|
663 | 686 | assert_equal Issue.visible.watched_by(User.current).sort_by(&:id), result.sort_by(&:id) |
|
664 | 687 | User.current = nil |
|
665 | 688 | end |
|
666 | 689 | |
|
667 | 690 | def test_filter_unwatched_issues |
|
668 | 691 | User.current = User.find(1) |
|
669 | 692 | query = IssueQuery.new(:name => '_', :filters => { 'watcher_id' => {:operator => '!', :values => ['me']}}) |
|
670 | 693 | result = find_issues_with_query(query) |
|
671 | 694 | assert_not_nil result |
|
672 | 695 | assert !result.empty? |
|
673 | 696 | assert_equal((Issue.visible - Issue.watched_by(User.current)).sort_by(&:id).size, result.sort_by(&:id).size) |
|
674 | 697 | User.current = nil |
|
675 | 698 | end |
|
676 | 699 | |
|
677 | 700 | def test_filter_on_custom_field_should_ignore_projects_with_field_disabled |
|
678 | 701 | field = IssueCustomField.generate!(:trackers => Tracker.all, :project_ids => [1, 3, 4], :is_filter => true) |
|
679 | 702 | Issue.generate!(:project_id => 3, :tracker_id => 2, :custom_field_values => {field.id.to_s => 'Foo'}) |
|
680 | 703 | Issue.generate!(:project_id => 4, :tracker_id => 2, :custom_field_values => {field.id.to_s => 'Foo'}) |
|
681 | 704 | |
|
682 | 705 | query = IssueQuery.new(:name => '_', :project => Project.find(1)) |
|
683 | 706 | query.filters = {"cf_#{field.id}" => {:operator => '=', :values => ['Foo']}} |
|
684 | 707 | assert_equal 2, find_issues_with_query(query).size |
|
685 | 708 | |
|
686 | 709 | field.project_ids = [1, 3] # Disable the field for project 4 |
|
687 | 710 | field.save! |
|
688 | 711 | assert_equal 1, find_issues_with_query(query).size |
|
689 | 712 | end |
|
690 | 713 | |
|
691 | 714 | def test_filter_on_custom_field_should_ignore_trackers_with_field_disabled |
|
692 | 715 | field = IssueCustomField.generate!(:tracker_ids => [1, 2], :is_for_all => true, :is_filter => true) |
|
693 | 716 | Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => 'Foo'}) |
|
694 | 717 | Issue.generate!(:project_id => 1, :tracker_id => 2, :custom_field_values => {field.id.to_s => 'Foo'}) |
|
695 | 718 | |
|
696 | 719 | query = IssueQuery.new(:name => '_', :project => Project.find(1)) |
|
697 | 720 | query.filters = {"cf_#{field.id}" => {:operator => '=', :values => ['Foo']}} |
|
698 | 721 | assert_equal 2, find_issues_with_query(query).size |
|
699 | 722 | |
|
700 | 723 | field.tracker_ids = [1] # Disable the field for tracker 2 |
|
701 | 724 | field.save! |
|
702 | 725 | assert_equal 1, find_issues_with_query(query).size |
|
703 | 726 | end |
|
704 | 727 | |
|
705 | 728 | def test_filter_on_project_custom_field |
|
706 | 729 | field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string') |
|
707 | 730 | CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo') |
|
708 | 731 | CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo') |
|
709 | 732 | |
|
710 | 733 | query = IssueQuery.new(:name => '_') |
|
711 | 734 | filter_name = "project.cf_#{field.id}" |
|
712 | 735 | assert_include filter_name, query.available_filters.keys |
|
713 | 736 | query.filters = {filter_name => {:operator => '=', :values => ['Foo']}} |
|
714 | 737 | assert_equal [3, 5], find_issues_with_query(query).map(&:project_id).uniq.sort |
|
715 | 738 | end |
|
716 | 739 | |
|
717 | 740 | def test_filter_on_author_custom_field |
|
718 | 741 | field = UserCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string') |
|
719 | 742 | CustomValue.create!(:custom_field => field, :customized => User.find(3), :value => 'Foo') |
|
720 | 743 | |
|
721 | 744 | query = IssueQuery.new(:name => '_') |
|
722 | 745 | filter_name = "author.cf_#{field.id}" |
|
723 | 746 | assert_include filter_name, query.available_filters.keys |
|
724 | 747 | query.filters = {filter_name => {:operator => '=', :values => ['Foo']}} |
|
725 | 748 | assert_equal [3], find_issues_with_query(query).map(&:author_id).uniq.sort |
|
726 | 749 | end |
|
727 | 750 | |
|
728 | 751 | def test_filter_on_assigned_to_custom_field |
|
729 | 752 | field = UserCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string') |
|
730 | 753 | CustomValue.create!(:custom_field => field, :customized => User.find(3), :value => 'Foo') |
|
731 | 754 | |
|
732 | 755 | query = IssueQuery.new(:name => '_') |
|
733 | 756 | filter_name = "assigned_to.cf_#{field.id}" |
|
734 | 757 | assert_include filter_name, query.available_filters.keys |
|
735 | 758 | query.filters = {filter_name => {:operator => '=', :values => ['Foo']}} |
|
736 | 759 | assert_equal [3], find_issues_with_query(query).map(&:assigned_to_id).uniq.sort |
|
737 | 760 | end |
|
738 | 761 | |
|
739 | 762 | def test_filter_on_fixed_version_custom_field |
|
740 | 763 | field = VersionCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string') |
|
741 | 764 | CustomValue.create!(:custom_field => field, :customized => Version.find(2), :value => 'Foo') |
|
742 | 765 | |
|
743 | 766 | query = IssueQuery.new(:name => '_') |
|
744 | 767 | filter_name = "fixed_version.cf_#{field.id}" |
|
745 | 768 | assert_include filter_name, query.available_filters.keys |
|
746 | 769 | query.filters = {filter_name => {:operator => '=', :values => ['Foo']}} |
|
747 | 770 | assert_equal [2], find_issues_with_query(query).map(&:fixed_version_id).uniq.sort |
|
748 | 771 | end |
|
749 | 772 | |
|
750 | 773 | def test_filter_on_relations_with_a_specific_issue |
|
751 | 774 | IssueRelation.delete_all |
|
752 | 775 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2)) |
|
753 | 776 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1)) |
|
754 | 777 | |
|
755 | 778 | query = IssueQuery.new(:name => '_') |
|
756 | 779 | query.filters = {"relates" => {:operator => '=', :values => ['1']}} |
|
757 | 780 | assert_equal [2, 3], find_issues_with_query(query).map(&:id).sort |
|
758 | 781 | |
|
759 | 782 | query = IssueQuery.new(:name => '_') |
|
760 | 783 | query.filters = {"relates" => {:operator => '=', :values => ['2']}} |
|
761 | 784 | assert_equal [1], find_issues_with_query(query).map(&:id).sort |
|
762 | 785 | end |
|
763 | 786 | |
|
764 | 787 | def test_filter_on_relations_with_any_issues_in_a_project |
|
765 | 788 | IssueRelation.delete_all |
|
766 | 789 | with_settings :cross_project_issue_relations => '1' do |
|
767 | 790 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first) |
|
768 | 791 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(2).issues.first) |
|
769 | 792 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(3).issues.first) |
|
770 | 793 | end |
|
771 | 794 | |
|
772 | 795 | query = IssueQuery.new(:name => '_') |
|
773 | 796 | query.filters = {"relates" => {:operator => '=p', :values => ['2']}} |
|
774 | 797 | assert_equal [1, 2], find_issues_with_query(query).map(&:id).sort |
|
775 | 798 | |
|
776 | 799 | query = IssueQuery.new(:name => '_') |
|
777 | 800 | query.filters = {"relates" => {:operator => '=p', :values => ['3']}} |
|
778 | 801 | assert_equal [1], find_issues_with_query(query).map(&:id).sort |
|
779 | 802 | |
|
780 | 803 | query = IssueQuery.new(:name => '_') |
|
781 | 804 | query.filters = {"relates" => {:operator => '=p', :values => ['4']}} |
|
782 | 805 | assert_equal [], find_issues_with_query(query).map(&:id).sort |
|
783 | 806 | end |
|
784 | 807 | |
|
785 | 808 | def test_filter_on_relations_with_any_issues_not_in_a_project |
|
786 | 809 | IssueRelation.delete_all |
|
787 | 810 | with_settings :cross_project_issue_relations => '1' do |
|
788 | 811 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first) |
|
789 | 812 | #IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(1).issues.first) |
|
790 | 813 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(3).issues.first) |
|
791 | 814 | end |
|
792 | 815 | |
|
793 | 816 | query = IssueQuery.new(:name => '_') |
|
794 | 817 | query.filters = {"relates" => {:operator => '=!p', :values => ['1']}} |
|
795 | 818 | assert_equal [1], find_issues_with_query(query).map(&:id).sort |
|
796 | 819 | end |
|
797 | 820 | |
|
798 | 821 | def test_filter_on_relations_with_no_issues_in_a_project |
|
799 | 822 | IssueRelation.delete_all |
|
800 | 823 | with_settings :cross_project_issue_relations => '1' do |
|
801 | 824 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first) |
|
802 | 825 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(3).issues.first) |
|
803 | 826 | IssueRelation.create!(:relation_type => "relates", :issue_to => Project.find(2).issues.first, :issue_from => Issue.find(3)) |
|
804 | 827 | end |
|
805 | 828 | |
|
806 | 829 | query = IssueQuery.new(:name => '_') |
|
807 | 830 | query.filters = {"relates" => {:operator => '!p', :values => ['2']}} |
|
808 | 831 | ids = find_issues_with_query(query).map(&:id).sort |
|
809 | 832 | assert_include 2, ids |
|
810 | 833 | assert_not_include 1, ids |
|
811 | 834 | assert_not_include 3, ids |
|
812 | 835 | end |
|
813 | 836 | |
|
814 | 837 | def test_filter_on_relations_with_no_issues |
|
815 | 838 | IssueRelation.delete_all |
|
816 | 839 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2)) |
|
817 | 840 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1)) |
|
818 | 841 | |
|
819 | 842 | query = IssueQuery.new(:name => '_') |
|
820 | 843 | query.filters = {"relates" => {:operator => '!*', :values => ['']}} |
|
821 | 844 | ids = find_issues_with_query(query).map(&:id) |
|
822 | 845 | assert_equal [], ids & [1, 2, 3] |
|
823 | 846 | assert_include 4, ids |
|
824 | 847 | end |
|
825 | 848 | |
|
826 | 849 | def test_filter_on_relations_with_any_issues |
|
827 | 850 | IssueRelation.delete_all |
|
828 | 851 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2)) |
|
829 | 852 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1)) |
|
830 | 853 | |
|
831 | 854 | query = IssueQuery.new(:name => '_') |
|
832 | 855 | query.filters = {"relates" => {:operator => '*', :values => ['']}} |
|
833 | 856 | assert_equal [1, 2, 3], find_issues_with_query(query).map(&:id).sort |
|
834 | 857 | end |
|
835 | 858 | |
|
836 | 859 | def test_filter_on_relations_should_not_ignore_other_filter |
|
837 | 860 | issue = Issue.generate! |
|
838 | 861 | issue1 = Issue.generate!(:status_id => 1) |
|
839 | 862 | issue2 = Issue.generate!(:status_id => 2) |
|
840 | 863 | IssueRelation.create!(:relation_type => "relates", :issue_from => issue, :issue_to => issue1) |
|
841 | 864 | IssueRelation.create!(:relation_type => "relates", :issue_from => issue, :issue_to => issue2) |
|
842 | 865 | |
|
843 | 866 | query = IssueQuery.new(:name => '_') |
|
844 | 867 | query.filters = { |
|
845 | 868 | "status_id" => {:operator => '=', :values => ['1']}, |
|
846 | 869 | "relates" => {:operator => '=', :values => [issue.id.to_s]} |
|
847 | 870 | } |
|
848 | 871 | assert_equal [issue1], find_issues_with_query(query) |
|
849 | 872 | end |
|
850 | 873 | |
|
851 | 874 | def test_statement_should_be_nil_with_no_filters |
|
852 | 875 | q = IssueQuery.new(:name => '_') |
|
853 | 876 | q.filters = {} |
|
854 | 877 | |
|
855 | 878 | assert q.valid? |
|
856 | 879 | assert_nil q.statement |
|
857 | 880 | end |
|
858 | 881 | |
|
859 | 882 | def test_default_columns |
|
860 | 883 | q = IssueQuery.new |
|
861 | 884 | assert q.columns.any? |
|
862 | 885 | assert q.inline_columns.any? |
|
863 | 886 | assert q.block_columns.empty? |
|
864 | 887 | end |
|
865 | 888 | |
|
866 | 889 | def test_set_column_names |
|
867 | 890 | q = IssueQuery.new |
|
868 | 891 | q.column_names = ['tracker', :subject, '', 'unknonw_column'] |
|
869 | 892 | assert_equal [:id, :tracker, :subject], q.columns.collect {|c| c.name} |
|
870 | 893 | end |
|
871 | 894 | |
|
872 | 895 | def test_has_column_should_accept_a_column_name |
|
873 | 896 | q = IssueQuery.new |
|
874 | 897 | q.column_names = ['tracker', :subject] |
|
875 | 898 | assert q.has_column?(:tracker) |
|
876 | 899 | assert !q.has_column?(:category) |
|
877 | 900 | end |
|
878 | 901 | |
|
879 | 902 | def test_has_column_should_accept_a_column |
|
880 | 903 | q = IssueQuery.new |
|
881 | 904 | q.column_names = ['tracker', :subject] |
|
882 | 905 | |
|
883 | 906 | tracker_column = q.available_columns.detect {|c| c.name==:tracker} |
|
884 | 907 | assert_kind_of QueryColumn, tracker_column |
|
885 | 908 | category_column = q.available_columns.detect {|c| c.name==:category} |
|
886 | 909 | assert_kind_of QueryColumn, category_column |
|
887 | 910 | |
|
888 | 911 | assert q.has_column?(tracker_column) |
|
889 | 912 | assert !q.has_column?(category_column) |
|
890 | 913 | end |
|
891 | 914 | |
|
892 | 915 | def test_inline_and_block_columns |
|
893 | 916 | q = IssueQuery.new |
|
894 | 917 | q.column_names = ['subject', 'description', 'tracker'] |
|
895 | 918 | |
|
896 | 919 | assert_equal [:id, :subject, :tracker], q.inline_columns.map(&:name) |
|
897 | 920 | assert_equal [:description], q.block_columns.map(&:name) |
|
898 | 921 | end |
|
899 | 922 | |
|
900 | 923 | def test_custom_field_columns_should_be_inline |
|
901 | 924 | q = IssueQuery.new |
|
902 | 925 | columns = q.available_columns.select {|column| column.is_a? QueryCustomFieldColumn} |
|
903 | 926 | assert columns.any? |
|
904 | 927 | assert_nil columns.detect {|column| !column.inline?} |
|
905 | 928 | end |
|
906 | 929 | |
|
907 | 930 | def test_query_should_preload_spent_hours |
|
908 | 931 | q = IssueQuery.new(:name => '_', :column_names => [:subject, :spent_hours]) |
|
909 | 932 | assert q.has_column?(:spent_hours) |
|
910 | 933 | issues = q.issues |
|
911 | 934 | assert_not_nil issues.first.instance_variable_get("@spent_hours") |
|
912 | 935 | end |
|
913 | 936 | |
|
914 | 937 | def test_groupable_columns_should_include_custom_fields |
|
915 | 938 | q = IssueQuery.new |
|
916 | 939 | column = q.groupable_columns.detect {|c| c.name == :cf_1} |
|
917 | 940 | assert_not_nil column |
|
918 | 941 | assert_kind_of QueryCustomFieldColumn, column |
|
919 | 942 | end |
|
920 | 943 | |
|
921 | 944 | def test_groupable_columns_should_not_include_multi_custom_fields |
|
922 | 945 | field = CustomField.find(1) |
|
923 | 946 | field.update_attribute :multiple, true |
|
924 | 947 | |
|
925 | 948 | q = IssueQuery.new |
|
926 | 949 | column = q.groupable_columns.detect {|c| c.name == :cf_1} |
|
927 | 950 | assert_nil column |
|
928 | 951 | end |
|
929 | 952 | |
|
930 | 953 | def test_groupable_columns_should_include_user_custom_fields |
|
931 | 954 | cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1], :field_format => 'user') |
|
932 | 955 | |
|
933 | 956 | q = IssueQuery.new |
|
934 | 957 | assert q.groupable_columns.detect {|c| c.name == "cf_#{cf.id}".to_sym} |
|
935 | 958 | end |
|
936 | 959 | |
|
937 | 960 | def test_groupable_columns_should_include_version_custom_fields |
|
938 | 961 | cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1], :field_format => 'version') |
|
939 | 962 | |
|
940 | 963 | q = IssueQuery.new |
|
941 | 964 | assert q.groupable_columns.detect {|c| c.name == "cf_#{cf.id}".to_sym} |
|
942 | 965 | end |
|
943 | 966 | |
|
944 | 967 | def test_grouped_with_valid_column |
|
945 | 968 | q = IssueQuery.new(:group_by => 'status') |
|
946 | 969 | assert q.grouped? |
|
947 | 970 | assert_not_nil q.group_by_column |
|
948 | 971 | assert_equal :status, q.group_by_column.name |
|
949 | 972 | assert_not_nil q.group_by_statement |
|
950 | 973 | assert_equal 'status', q.group_by_statement |
|
951 | 974 | end |
|
952 | 975 | |
|
953 | 976 | def test_grouped_with_invalid_column |
|
954 | 977 | q = IssueQuery.new(:group_by => 'foo') |
|
955 | 978 | assert !q.grouped? |
|
956 | 979 | assert_nil q.group_by_column |
|
957 | 980 | assert_nil q.group_by_statement |
|
958 | 981 | end |
|
959 | 982 | |
|
960 | 983 | def test_sortable_columns_should_sort_assignees_according_to_user_format_setting |
|
961 | 984 | with_settings :user_format => 'lastname_coma_firstname' do |
|
962 | 985 | q = IssueQuery.new |
|
963 | 986 | assert q.sortable_columns.has_key?('assigned_to') |
|
964 | 987 | assert_equal %w(users.lastname users.firstname users.id), q.sortable_columns['assigned_to'] |
|
965 | 988 | end |
|
966 | 989 | end |
|
967 | 990 | |
|
968 | 991 | def test_sortable_columns_should_sort_authors_according_to_user_format_setting |
|
969 | 992 | with_settings :user_format => 'lastname_coma_firstname' do |
|
970 | 993 | q = IssueQuery.new |
|
971 | 994 | assert q.sortable_columns.has_key?('author') |
|
972 | 995 | assert_equal %w(authors.lastname authors.firstname authors.id), q.sortable_columns['author'] |
|
973 | 996 | end |
|
974 | 997 | end |
|
975 | 998 | |
|
976 | 999 | def test_sortable_columns_should_include_custom_field |
|
977 | 1000 | q = IssueQuery.new |
|
978 | 1001 | assert q.sortable_columns['cf_1'] |
|
979 | 1002 | end |
|
980 | 1003 | |
|
981 | 1004 | def test_sortable_columns_should_not_include_multi_custom_field |
|
982 | 1005 | field = CustomField.find(1) |
|
983 | 1006 | field.update_attribute :multiple, true |
|
984 | 1007 | |
|
985 | 1008 | q = IssueQuery.new |
|
986 | 1009 | assert !q.sortable_columns['cf_1'] |
|
987 | 1010 | end |
|
988 | 1011 | |
|
989 | 1012 | def test_default_sort |
|
990 | 1013 | q = IssueQuery.new |
|
991 | 1014 | assert_equal [], q.sort_criteria |
|
992 | 1015 | end |
|
993 | 1016 | |
|
994 | 1017 | def test_set_sort_criteria_with_hash |
|
995 | 1018 | q = IssueQuery.new |
|
996 | 1019 | q.sort_criteria = {'0' => ['priority', 'desc'], '2' => ['tracker']} |
|
997 | 1020 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
998 | 1021 | end |
|
999 | 1022 | |
|
1000 | 1023 | def test_set_sort_criteria_with_array |
|
1001 | 1024 | q = IssueQuery.new |
|
1002 | 1025 | q.sort_criteria = [['priority', 'desc'], 'tracker'] |
|
1003 | 1026 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
1004 | 1027 | end |
|
1005 | 1028 | |
|
1006 | 1029 | def test_create_query_with_sort |
|
1007 | 1030 | q = IssueQuery.new(:name => 'Sorted') |
|
1008 | 1031 | q.sort_criteria = [['priority', 'desc'], 'tracker'] |
|
1009 | 1032 | assert q.save |
|
1010 | 1033 | q.reload |
|
1011 | 1034 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
1012 | 1035 | end |
|
1013 | 1036 | |
|
1014 | 1037 | def test_sort_by_string_custom_field_asc |
|
1015 | 1038 | q = IssueQuery.new |
|
1016 | 1039 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } |
|
1017 | 1040 | assert c |
|
1018 | 1041 | assert c.sortable |
|
1019 | 1042 | issues = q.issues(:order => "#{c.sortable} ASC") |
|
1020 | 1043 | values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} |
|
1021 | 1044 | assert !values.empty? |
|
1022 | 1045 | assert_equal values.sort, values |
|
1023 | 1046 | end |
|
1024 | 1047 | |
|
1025 | 1048 | def test_sort_by_string_custom_field_desc |
|
1026 | 1049 | q = IssueQuery.new |
|
1027 | 1050 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } |
|
1028 | 1051 | assert c |
|
1029 | 1052 | assert c.sortable |
|
1030 | 1053 | issues = q.issues(:order => "#{c.sortable} DESC") |
|
1031 | 1054 | values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} |
|
1032 | 1055 | assert !values.empty? |
|
1033 | 1056 | assert_equal values.sort.reverse, values |
|
1034 | 1057 | end |
|
1035 | 1058 | |
|
1036 | 1059 | def test_sort_by_float_custom_field_asc |
|
1037 | 1060 | q = IssueQuery.new |
|
1038 | 1061 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' } |
|
1039 | 1062 | assert c |
|
1040 | 1063 | assert c.sortable |
|
1041 | 1064 | issues = q.issues(:order => "#{c.sortable} ASC") |
|
1042 | 1065 | values = issues.collect {|i| begin; Kernel.Float(i.custom_value_for(c.custom_field).to_s); rescue; nil; end}.compact |
|
1043 | 1066 | assert !values.empty? |
|
1044 | 1067 | assert_equal values.sort, values |
|
1045 | 1068 | end |
|
1046 | 1069 | |
|
1047 | 1070 | def test_invalid_query_should_raise_query_statement_invalid_error |
|
1048 | 1071 | q = IssueQuery.new |
|
1049 | 1072 | assert_raise Query::StatementInvalid do |
|
1050 | 1073 | q.issues(:conditions => "foo = 1") |
|
1051 | 1074 | end |
|
1052 | 1075 | end |
|
1053 | 1076 | |
|
1054 | 1077 | def test_issue_count |
|
1055 | 1078 | q = IssueQuery.new(:name => '_') |
|
1056 | 1079 | issue_count = q.issue_count |
|
1057 | 1080 | assert_equal q.issues.size, issue_count |
|
1058 | 1081 | end |
|
1059 | 1082 | |
|
1060 | 1083 | def test_issue_count_with_archived_issues |
|
1061 | 1084 | p = Project.generate! do |project| |
|
1062 | 1085 | project.status = Project::STATUS_ARCHIVED |
|
1063 | 1086 | end |
|
1064 | 1087 | i = Issue.generate!( :project => p, :tracker => p.trackers.first ) |
|
1065 | 1088 | assert !i.visible? |
|
1066 | 1089 | |
|
1067 | 1090 | test_issue_count |
|
1068 | 1091 | end |
|
1069 | 1092 | |
|
1070 | 1093 | def test_issue_count_by_association_group |
|
1071 | 1094 | q = IssueQuery.new(:name => '_', :group_by => 'assigned_to') |
|
1072 | 1095 | count_by_group = q.issue_count_by_group |
|
1073 | 1096 | assert_kind_of Hash, count_by_group |
|
1074 | 1097 | assert_equal %w(NilClass User), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
1075 | 1098 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
1076 | 1099 | assert count_by_group.has_key?(User.find(3)) |
|
1077 | 1100 | end |
|
1078 | 1101 | |
|
1079 | 1102 | def test_issue_count_by_list_custom_field_group |
|
1080 | 1103 | q = IssueQuery.new(:name => '_', :group_by => 'cf_1') |
|
1081 | 1104 | count_by_group = q.issue_count_by_group |
|
1082 | 1105 | assert_kind_of Hash, count_by_group |
|
1083 | 1106 | assert_equal %w(NilClass String), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
1084 | 1107 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
1085 | 1108 | assert count_by_group.has_key?('MySQL') |
|
1086 | 1109 | end |
|
1087 | 1110 | |
|
1088 | 1111 | def test_issue_count_by_date_custom_field_group |
|
1089 | 1112 | q = IssueQuery.new(:name => '_', :group_by => 'cf_8') |
|
1090 | 1113 | count_by_group = q.issue_count_by_group |
|
1091 | 1114 | assert_kind_of Hash, count_by_group |
|
1092 | 1115 | assert_equal %w(Date NilClass), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
1093 | 1116 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
1094 | 1117 | end |
|
1095 | 1118 | |
|
1096 | 1119 | def test_issue_count_with_nil_group_only |
|
1097 | 1120 | Issue.update_all("assigned_to_id = NULL") |
|
1098 | 1121 | |
|
1099 | 1122 | q = IssueQuery.new(:name => '_', :group_by => 'assigned_to') |
|
1100 | 1123 | count_by_group = q.issue_count_by_group |
|
1101 | 1124 | assert_kind_of Hash, count_by_group |
|
1102 | 1125 | assert_equal 1, count_by_group.keys.size |
|
1103 | 1126 | assert_nil count_by_group.keys.first |
|
1104 | 1127 | end |
|
1105 | 1128 | |
|
1106 | 1129 | def test_issue_ids |
|
1107 | 1130 | q = IssueQuery.new(:name => '_') |
|
1108 | 1131 | order = "issues.subject, issues.id" |
|
1109 | 1132 | issues = q.issues(:order => order) |
|
1110 | 1133 | assert_equal issues.map(&:id), q.issue_ids(:order => order) |
|
1111 | 1134 | end |
|
1112 | 1135 | |
|
1113 | 1136 | def test_label_for |
|
1114 | 1137 | set_language_if_valid 'en' |
|
1115 | 1138 | q = IssueQuery.new |
|
1116 | 1139 | assert_equal 'Assignee', q.label_for('assigned_to_id') |
|
1117 | 1140 | end |
|
1118 | 1141 | |
|
1119 | 1142 | def test_label_for_fr |
|
1120 | 1143 | set_language_if_valid 'fr' |
|
1121 | 1144 | q = IssueQuery.new |
|
1122 | 1145 | assert_equal "Assign\xc3\xa9 \xc3\xa0".force_encoding('UTF-8'), q.label_for('assigned_to_id') |
|
1123 | 1146 | end |
|
1124 | 1147 | |
|
1125 | 1148 | def test_editable_by |
|
1126 | 1149 | admin = User.find(1) |
|
1127 | 1150 | manager = User.find(2) |
|
1128 | 1151 | developer = User.find(3) |
|
1129 | 1152 | |
|
1130 | 1153 | # Public query on project 1 |
|
1131 | 1154 | q = IssueQuery.find(1) |
|
1132 | 1155 | assert q.editable_by?(admin) |
|
1133 | 1156 | assert q.editable_by?(manager) |
|
1134 | 1157 | assert !q.editable_by?(developer) |
|
1135 | 1158 | |
|
1136 | 1159 | # Private query on project 1 |
|
1137 | 1160 | q = IssueQuery.find(2) |
|
1138 | 1161 | assert q.editable_by?(admin) |
|
1139 | 1162 | assert !q.editable_by?(manager) |
|
1140 | 1163 | assert q.editable_by?(developer) |
|
1141 | 1164 | |
|
1142 | 1165 | # Private query for all projects |
|
1143 | 1166 | q = IssueQuery.find(3) |
|
1144 | 1167 | assert q.editable_by?(admin) |
|
1145 | 1168 | assert !q.editable_by?(manager) |
|
1146 | 1169 | assert q.editable_by?(developer) |
|
1147 | 1170 | |
|
1148 | 1171 | # Public query for all projects |
|
1149 | 1172 | q = IssueQuery.find(4) |
|
1150 | 1173 | assert q.editable_by?(admin) |
|
1151 | 1174 | assert !q.editable_by?(manager) |
|
1152 | 1175 | assert !q.editable_by?(developer) |
|
1153 | 1176 | end |
|
1154 | 1177 | |
|
1155 | 1178 | def test_visible_scope |
|
1156 | 1179 | query_ids = IssueQuery.visible(User.anonymous).map(&:id) |
|
1157 | 1180 | |
|
1158 | 1181 | assert query_ids.include?(1), 'public query on public project was not visible' |
|
1159 | 1182 | assert query_ids.include?(4), 'public query for all projects was not visible' |
|
1160 | 1183 | assert !query_ids.include?(2), 'private query on public project was visible' |
|
1161 | 1184 | assert !query_ids.include?(3), 'private query for all projects was visible' |
|
1162 | 1185 | assert !query_ids.include?(7), 'public query on private project was visible' |
|
1163 | 1186 | end |
|
1164 | 1187 | |
|
1165 | 1188 | def test_query_with_public_visibility_should_be_visible_to_anyone |
|
1166 | 1189 | q = IssueQuery.create!(:name => 'Query', :visibility => IssueQuery::VISIBILITY_PUBLIC) |
|
1167 | 1190 | |
|
1168 | 1191 | assert q.visible?(User.anonymous) |
|
1169 | 1192 | assert IssueQuery.visible(User.anonymous).find_by_id(q.id) |
|
1170 | 1193 | |
|
1171 | 1194 | assert q.visible?(User.find(7)) |
|
1172 | 1195 | assert IssueQuery.visible(User.find(7)).find_by_id(q.id) |
|
1173 | 1196 | |
|
1174 | 1197 | assert q.visible?(User.find(2)) |
|
1175 | 1198 | assert IssueQuery.visible(User.find(2)).find_by_id(q.id) |
|
1176 | 1199 | |
|
1177 | 1200 | assert q.visible?(User.find(1)) |
|
1178 | 1201 | assert IssueQuery.visible(User.find(1)).find_by_id(q.id) |
|
1179 | 1202 | end |
|
1180 | 1203 | |
|
1181 | 1204 | def test_query_with_roles_visibility_should_be_visible_to_user_with_role |
|
1182 | 1205 | q = IssueQuery.create!(:name => 'Query', :visibility => IssueQuery::VISIBILITY_ROLES, :role_ids => [1,2]) |
|
1183 | 1206 | |
|
1184 | 1207 | assert !q.visible?(User.anonymous) |
|
1185 | 1208 | assert_nil IssueQuery.visible(User.anonymous).find_by_id(q.id) |
|
1186 | 1209 | |
|
1187 | 1210 | assert !q.visible?(User.find(7)) |
|
1188 | 1211 | assert_nil IssueQuery.visible(User.find(7)).find_by_id(q.id) |
|
1189 | 1212 | |
|
1190 | 1213 | assert q.visible?(User.find(2)) |
|
1191 | 1214 | assert IssueQuery.visible(User.find(2)).find_by_id(q.id) |
|
1192 | 1215 | |
|
1193 | 1216 | assert q.visible?(User.find(1)) |
|
1194 | 1217 | assert IssueQuery.visible(User.find(1)).find_by_id(q.id) |
|
1195 | 1218 | end |
|
1196 | 1219 | |
|
1197 | 1220 | def test_query_with_private_visibility_should_be_visible_to_owner |
|
1198 | 1221 | q = IssueQuery.create!(:name => 'Query', :visibility => IssueQuery::VISIBILITY_PRIVATE, :user => User.find(7)) |
|
1199 | 1222 | |
|
1200 | 1223 | assert !q.visible?(User.anonymous) |
|
1201 | 1224 | assert_nil IssueQuery.visible(User.anonymous).find_by_id(q.id) |
|
1202 | 1225 | |
|
1203 | 1226 | assert q.visible?(User.find(7)) |
|
1204 | 1227 | assert IssueQuery.visible(User.find(7)).find_by_id(q.id) |
|
1205 | 1228 | |
|
1206 | 1229 | assert !q.visible?(User.find(2)) |
|
1207 | 1230 | assert_nil IssueQuery.visible(User.find(2)).find_by_id(q.id) |
|
1208 | 1231 | |
|
1209 | 1232 | assert q.visible?(User.find(1)) |
|
1210 | 1233 | assert_nil IssueQuery.visible(User.find(1)).find_by_id(q.id) |
|
1211 | 1234 | end |
|
1212 | 1235 | |
|
1213 | 1236 | test "#available_filters should include users of visible projects in cross-project view" do |
|
1214 | 1237 | users = IssueQuery.new.available_filters["assigned_to_id"] |
|
1215 | 1238 | assert_not_nil users |
|
1216 | 1239 | assert users[:values].map{|u|u[1]}.include?("3") |
|
1217 | 1240 | end |
|
1218 | 1241 | |
|
1219 | 1242 | test "#available_filters should include users of subprojects" do |
|
1220 | 1243 | user1 = User.generate! |
|
1221 | 1244 | user2 = User.generate! |
|
1222 | 1245 | project = Project.find(1) |
|
1223 | 1246 | Member.create!(:principal => user1, :project => project.children.visible.first, :role_ids => [1]) |
|
1224 | 1247 | |
|
1225 | 1248 | users = IssueQuery.new(:project => project).available_filters["assigned_to_id"] |
|
1226 | 1249 | assert_not_nil users |
|
1227 | 1250 | assert users[:values].map{|u|u[1]}.include?(user1.id.to_s) |
|
1228 | 1251 | assert !users[:values].map{|u|u[1]}.include?(user2.id.to_s) |
|
1229 | 1252 | end |
|
1230 | 1253 | |
|
1231 | 1254 | test "#available_filters should include visible projects in cross-project view" do |
|
1232 | 1255 | projects = IssueQuery.new.available_filters["project_id"] |
|
1233 | 1256 | assert_not_nil projects |
|
1234 | 1257 | assert projects[:values].map{|u|u[1]}.include?("1") |
|
1235 | 1258 | end |
|
1236 | 1259 | |
|
1237 | 1260 | test "#available_filters should include 'member_of_group' filter" do |
|
1238 | 1261 | query = IssueQuery.new |
|
1239 | 1262 | assert query.available_filters.keys.include?("member_of_group") |
|
1240 | 1263 | assert_equal :list_optional, query.available_filters["member_of_group"][:type] |
|
1241 | 1264 | assert query.available_filters["member_of_group"][:values].present? |
|
1242 | 1265 | assert_equal Group.givable.sort.map {|g| [g.name, g.id.to_s]}, |
|
1243 | 1266 | query.available_filters["member_of_group"][:values].sort |
|
1244 | 1267 | end |
|
1245 | 1268 | |
|
1246 | 1269 | test "#available_filters should include 'assigned_to_role' filter" do |
|
1247 | 1270 | query = IssueQuery.new |
|
1248 | 1271 | assert query.available_filters.keys.include?("assigned_to_role") |
|
1249 | 1272 | assert_equal :list_optional, query.available_filters["assigned_to_role"][:type] |
|
1250 | 1273 | |
|
1251 | 1274 | assert query.available_filters["assigned_to_role"][:values].include?(['Manager','1']) |
|
1252 | 1275 | assert query.available_filters["assigned_to_role"][:values].include?(['Developer','2']) |
|
1253 | 1276 | assert query.available_filters["assigned_to_role"][:values].include?(['Reporter','3']) |
|
1254 | 1277 | |
|
1255 | 1278 | assert ! query.available_filters["assigned_to_role"][:values].include?(['Non member','4']) |
|
1256 | 1279 | assert ! query.available_filters["assigned_to_role"][:values].include?(['Anonymous','5']) |
|
1257 | 1280 | end |
|
1258 | 1281 | |
|
1259 | 1282 | def test_available_filters_should_include_custom_field_according_to_user_visibility |
|
1260 | 1283 | visible_field = IssueCustomField.generate!(:is_for_all => true, :is_filter => true, :visible => true) |
|
1261 | 1284 | hidden_field = IssueCustomField.generate!(:is_for_all => true, :is_filter => true, :visible => false, :role_ids => [1]) |
|
1262 | 1285 | |
|
1263 | 1286 | with_current_user User.find(3) do |
|
1264 | 1287 | query = IssueQuery.new |
|
1265 | 1288 | assert_include "cf_#{visible_field.id}", query.available_filters.keys |
|
1266 | 1289 | assert_not_include "cf_#{hidden_field.id}", query.available_filters.keys |
|
1267 | 1290 | end |
|
1268 | 1291 | end |
|
1269 | 1292 | |
|
1270 | 1293 | def test_available_columns_should_include_custom_field_according_to_user_visibility |
|
1271 | 1294 | visible_field = IssueCustomField.generate!(:is_for_all => true, :is_filter => true, :visible => true) |
|
1272 | 1295 | hidden_field = IssueCustomField.generate!(:is_for_all => true, :is_filter => true, :visible => false, :role_ids => [1]) |
|
1273 | 1296 | |
|
1274 | 1297 | with_current_user User.find(3) do |
|
1275 | 1298 | query = IssueQuery.new |
|
1276 | 1299 | assert_include :"cf_#{visible_field.id}", query.available_columns.map(&:name) |
|
1277 | 1300 | assert_not_include :"cf_#{hidden_field.id}", query.available_columns.map(&:name) |
|
1278 | 1301 | end |
|
1279 | 1302 | end |
|
1280 | 1303 | |
|
1281 | 1304 | def setup_member_of_group |
|
1282 | 1305 | Group.destroy_all # No fixtures |
|
1283 | 1306 | @user_in_group = User.generate! |
|
1284 | 1307 | @second_user_in_group = User.generate! |
|
1285 | 1308 | @user_in_group2 = User.generate! |
|
1286 | 1309 | @user_not_in_group = User.generate! |
|
1287 | 1310 | |
|
1288 | 1311 | @group = Group.generate!.reload |
|
1289 | 1312 | @group.users << @user_in_group |
|
1290 | 1313 | @group.users << @second_user_in_group |
|
1291 | 1314 | |
|
1292 | 1315 | @group2 = Group.generate!.reload |
|
1293 | 1316 | @group2.users << @user_in_group2 |
|
1294 | 1317 | |
|
1295 | 1318 | @query = IssueQuery.new(:name => '_') |
|
1296 | 1319 | end |
|
1297 | 1320 | |
|
1298 | 1321 | test "member_of_group filter should search assigned to for users in the group" do |
|
1299 | 1322 | setup_member_of_group |
|
1300 | 1323 | @query.add_filter('member_of_group', '=', [@group.id.to_s]) |
|
1301 | 1324 | |
|
1302 | 1325 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@group.id}')" |
|
1303 | 1326 | assert_find_issues_with_query_is_successful @query |
|
1304 | 1327 | end |
|
1305 | 1328 | |
|
1306 | 1329 | test "member_of_group filter should search not assigned to any group member (none)" do |
|
1307 | 1330 | setup_member_of_group |
|
1308 | 1331 | @query.add_filter('member_of_group', '!*', ['']) |
|
1309 | 1332 | |
|
1310 | 1333 | # Users not in a group |
|
1311 | 1334 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IS NULL OR #{Issue.table_name}.assigned_to_id NOT IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}','#{@group.id}','#{@group2.id}')" |
|
1312 | 1335 | assert_find_issues_with_query_is_successful @query |
|
1313 | 1336 | end |
|
1314 | 1337 | |
|
1315 | 1338 | test "member_of_group filter should search assigned to any group member (all)" do |
|
1316 | 1339 | setup_member_of_group |
|
1317 | 1340 | @query.add_filter('member_of_group', '*', ['']) |
|
1318 | 1341 | |
|
1319 | 1342 | # Only users in a group |
|
1320 | 1343 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}','#{@group.id}','#{@group2.id}')" |
|
1321 | 1344 | assert_find_issues_with_query_is_successful @query |
|
1322 | 1345 | end |
|
1323 | 1346 | |
|
1324 | 1347 | test "member_of_group filter should return an empty set with = empty group" do |
|
1325 | 1348 | setup_member_of_group |
|
1326 | 1349 | @empty_group = Group.generate! |
|
1327 | 1350 | @query.add_filter('member_of_group', '=', [@empty_group.id.to_s]) |
|
1328 | 1351 | |
|
1329 | 1352 | assert_equal [], find_issues_with_query(@query) |
|
1330 | 1353 | end |
|
1331 | 1354 | |
|
1332 | 1355 | test "member_of_group filter should return issues with ! empty group" do |
|
1333 | 1356 | setup_member_of_group |
|
1334 | 1357 | @empty_group = Group.generate! |
|
1335 | 1358 | @query.add_filter('member_of_group', '!', [@empty_group.id.to_s]) |
|
1336 | 1359 | |
|
1337 | 1360 | assert_find_issues_with_query_is_successful @query |
|
1338 | 1361 | end |
|
1339 | 1362 | |
|
1340 | 1363 | def setup_assigned_to_role |
|
1341 | 1364 | @manager_role = Role.find_by_name('Manager') |
|
1342 | 1365 | @developer_role = Role.find_by_name('Developer') |
|
1343 | 1366 | |
|
1344 | 1367 | @project = Project.generate! |
|
1345 | 1368 | @manager = User.generate! |
|
1346 | 1369 | @developer = User.generate! |
|
1347 | 1370 | @boss = User.generate! |
|
1348 | 1371 | @guest = User.generate! |
|
1349 | 1372 | User.add_to_project(@manager, @project, @manager_role) |
|
1350 | 1373 | User.add_to_project(@developer, @project, @developer_role) |
|
1351 | 1374 | User.add_to_project(@boss, @project, [@manager_role, @developer_role]) |
|
1352 | 1375 | |
|
1353 | 1376 | @issue1 = Issue.generate!(:project => @project, :assigned_to_id => @manager.id) |
|
1354 | 1377 | @issue2 = Issue.generate!(:project => @project, :assigned_to_id => @developer.id) |
|
1355 | 1378 | @issue3 = Issue.generate!(:project => @project, :assigned_to_id => @boss.id) |
|
1356 | 1379 | @issue4 = Issue.generate!(:project => @project, :assigned_to_id => @guest.id) |
|
1357 | 1380 | @issue5 = Issue.generate!(:project => @project) |
|
1358 | 1381 | |
|
1359 | 1382 | @query = IssueQuery.new(:name => '_', :project => @project) |
|
1360 | 1383 | end |
|
1361 | 1384 | |
|
1362 | 1385 | test "assigned_to_role filter should search assigned to for users with the Role" do |
|
1363 | 1386 | setup_assigned_to_role |
|
1364 | 1387 | @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s]) |
|
1365 | 1388 | |
|
1366 | 1389 | assert_query_result [@issue1, @issue3], @query |
|
1367 | 1390 | end |
|
1368 | 1391 | |
|
1369 | 1392 | test "assigned_to_role filter should search assigned to for users with the Role on the issue project" do |
|
1370 | 1393 | setup_assigned_to_role |
|
1371 | 1394 | other_project = Project.generate! |
|
1372 | 1395 | User.add_to_project(@developer, other_project, @manager_role) |
|
1373 | 1396 | @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s]) |
|
1374 | 1397 | |
|
1375 | 1398 | assert_query_result [@issue1, @issue3], @query |
|
1376 | 1399 | end |
|
1377 | 1400 | |
|
1378 | 1401 | test "assigned_to_role filter should return an empty set with empty role" do |
|
1379 | 1402 | setup_assigned_to_role |
|
1380 | 1403 | @empty_role = Role.generate! |
|
1381 | 1404 | @query.add_filter('assigned_to_role', '=', [@empty_role.id.to_s]) |
|
1382 | 1405 | |
|
1383 | 1406 | assert_query_result [], @query |
|
1384 | 1407 | end |
|
1385 | 1408 | |
|
1386 | 1409 | test "assigned_to_role filter should search assigned to for users without the Role" do |
|
1387 | 1410 | setup_assigned_to_role |
|
1388 | 1411 | @query.add_filter('assigned_to_role', '!', [@manager_role.id.to_s]) |
|
1389 | 1412 | |
|
1390 | 1413 | assert_query_result [@issue2, @issue4, @issue5], @query |
|
1391 | 1414 | end |
|
1392 | 1415 | |
|
1393 | 1416 | test "assigned_to_role filter should search assigned to for users not assigned to any Role (none)" do |
|
1394 | 1417 | setup_assigned_to_role |
|
1395 | 1418 | @query.add_filter('assigned_to_role', '!*', ['']) |
|
1396 | 1419 | |
|
1397 | 1420 | assert_query_result [@issue4, @issue5], @query |
|
1398 | 1421 | end |
|
1399 | 1422 | |
|
1400 | 1423 | test "assigned_to_role filter should search assigned to for users assigned to any Role (all)" do |
|
1401 | 1424 | setup_assigned_to_role |
|
1402 | 1425 | @query.add_filter('assigned_to_role', '*', ['']) |
|
1403 | 1426 | |
|
1404 | 1427 | assert_query_result [@issue1, @issue2, @issue3], @query |
|
1405 | 1428 | end |
|
1406 | 1429 | |
|
1407 | 1430 | test "assigned_to_role filter should return issues with ! empty role" do |
|
1408 | 1431 | setup_assigned_to_role |
|
1409 | 1432 | @empty_role = Role.generate! |
|
1410 | 1433 | @query.add_filter('assigned_to_role', '!', [@empty_role.id.to_s]) |
|
1411 | 1434 | |
|
1412 | 1435 | assert_query_result [@issue1, @issue2, @issue3, @issue4, @issue5], @query |
|
1413 | 1436 | end |
|
1414 | 1437 | |
|
1415 | 1438 | def test_query_column_should_accept_a_symbol_as_caption |
|
1416 | 1439 | set_language_if_valid 'en' |
|
1417 | 1440 | c = QueryColumn.new('foo', :caption => :general_text_Yes) |
|
1418 | 1441 | assert_equal 'Yes', c.caption |
|
1419 | 1442 | end |
|
1420 | 1443 | |
|
1421 | 1444 | def test_query_column_should_accept_a_proc_as_caption |
|
1422 | 1445 | c = QueryColumn.new('foo', :caption => lambda {'Foo'}) |
|
1423 | 1446 | assert_equal 'Foo', c.caption |
|
1424 | 1447 | end |
|
1425 | 1448 | end |
@@ -1,462 +1,468 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 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 RepositoryTest < ActiveSupport::TestCase |
|
21 | 21 | fixtures :projects, |
|
22 | 22 | :trackers, |
|
23 | 23 | :projects_trackers, |
|
24 | 24 | :enabled_modules, |
|
25 | 25 | :repositories, |
|
26 | 26 | :issues, |
|
27 | 27 | :issue_statuses, |
|
28 | 28 | :issue_categories, |
|
29 | 29 | :changesets, |
|
30 | 30 | :changes, |
|
31 | 31 | :users, |
|
32 | 32 | :members, |
|
33 | 33 | :member_roles, |
|
34 | 34 | :roles, |
|
35 | 35 | :enumerations |
|
36 | 36 | |
|
37 | 37 | include Redmine::I18n |
|
38 | 38 | |
|
39 | 39 | def setup |
|
40 | 40 | @repository = Project.find(1).repository |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | def test_blank_log_encoding_error_message |
|
44 | 44 | set_language_if_valid 'en' |
|
45 | 45 | repo = Repository::Bazaar.new( |
|
46 | 46 | :project => Project.find(3), |
|
47 | 47 | :url => "/test", |
|
48 | 48 | :log_encoding => '' |
|
49 | 49 | ) |
|
50 | 50 | assert !repo.save |
|
51 | 51 | assert_include "Commit messages encoding can't be blank", |
|
52 | 52 | repo.errors.full_messages |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def test_blank_log_encoding_error_message_fr |
|
56 | 56 | set_language_if_valid 'fr' |
|
57 | 57 | str = "Encodage des messages de commit doit \xc3\xaatre renseign\xc3\xa9(e)".force_encoding('UTF-8') |
|
58 | 58 | repo = Repository::Bazaar.new( |
|
59 | 59 | :project => Project.find(3), |
|
60 | 60 | :url => "/test" |
|
61 | 61 | ) |
|
62 | 62 | assert !repo.save |
|
63 | 63 | assert_include str, repo.errors.full_messages |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def test_create |
|
67 | 67 | repository = Repository::Subversion.new(:project => Project.find(3)) |
|
68 | 68 | assert !repository.save |
|
69 | 69 | |
|
70 | 70 | repository.url = "svn://localhost" |
|
71 | 71 | assert repository.save |
|
72 | 72 | repository.reload |
|
73 | 73 | |
|
74 | 74 | project = Project.find(3) |
|
75 | 75 | assert_equal repository, project.repository |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | def test_first_repository_should_be_set_as_default |
|
79 | 79 | repository1 = Repository::Subversion.new( |
|
80 | 80 | :project => Project.find(3), |
|
81 | 81 | :identifier => 'svn1', |
|
82 | 82 | :url => 'file:///svn1' |
|
83 | 83 | ) |
|
84 | 84 | assert repository1.save |
|
85 | 85 | assert repository1.is_default? |
|
86 | 86 | |
|
87 | 87 | repository2 = Repository::Subversion.new( |
|
88 | 88 | :project => Project.find(3), |
|
89 | 89 | :identifier => 'svn2', |
|
90 | 90 | :url => 'file:///svn2' |
|
91 | 91 | ) |
|
92 | 92 | assert repository2.save |
|
93 | 93 | assert !repository2.is_default? |
|
94 | 94 | |
|
95 | 95 | assert_equal repository1, Project.find(3).repository |
|
96 | 96 | assert_equal [repository1, repository2], Project.find(3).repositories.sort |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | def test_default_repository_should_be_one |
|
100 | 100 | assert_equal 0, Project.find(3).repositories.count |
|
101 | 101 | repository1 = Repository::Subversion.new( |
|
102 | 102 | :project => Project.find(3), |
|
103 | 103 | :identifier => 'svn1', |
|
104 | 104 | :url => 'file:///svn1' |
|
105 | 105 | ) |
|
106 | 106 | assert repository1.save |
|
107 | 107 | assert repository1.is_default? |
|
108 | 108 | |
|
109 | 109 | repository2 = Repository::Subversion.new( |
|
110 | 110 | :project => Project.find(3), |
|
111 | 111 | :identifier => 'svn2', |
|
112 | 112 | :url => 'file:///svn2', |
|
113 | 113 | :is_default => true |
|
114 | 114 | ) |
|
115 | 115 | assert repository2.save |
|
116 | 116 | assert repository2.is_default? |
|
117 | 117 | repository1.reload |
|
118 | 118 | assert !repository1.is_default? |
|
119 | 119 | |
|
120 | 120 | assert_equal repository2, Project.find(3).repository |
|
121 | 121 | assert_equal [repository2, repository1], Project.find(3).repositories.sort |
|
122 | 122 | end |
|
123 | 123 | |
|
124 | 124 | def test_identifier_should_accept_letters_digits_dashes_and_underscores |
|
125 | 125 | r = Repository::Subversion.new( |
|
126 | 126 | :project_id => 3, |
|
127 | 127 | :identifier => 'svn-123_45', |
|
128 | 128 | :url => 'file:///svn' |
|
129 | 129 | ) |
|
130 | 130 | assert r.save |
|
131 | 131 | end |
|
132 | 132 | |
|
133 | 133 | def test_identifier_should_not_be_frozen_for_a_new_repository |
|
134 | 134 | assert_equal false, Repository.new.identifier_frozen? |
|
135 | 135 | end |
|
136 | 136 | |
|
137 | 137 | def test_identifier_should_not_be_frozen_for_a_saved_repository_with_blank_identifier |
|
138 | 138 | Repository.where(:id => 10).update_all(["identifier = ''"]) |
|
139 | 139 | assert_equal false, Repository.find(10).identifier_frozen? |
|
140 | 140 | end |
|
141 | 141 | |
|
142 | 142 | def test_identifier_should_be_frozen_for_a_saved_repository_with_valid_identifier |
|
143 | 143 | Repository.where(:id => 10).update_all(["identifier = 'abc123'"]) |
|
144 | 144 | assert_equal true, Repository.find(10).identifier_frozen? |
|
145 | 145 | end |
|
146 | 146 | |
|
147 | 147 | def test_identifier_should_not_accept_change_if_frozen |
|
148 | 148 | r = Repository.new(:identifier => 'foo') |
|
149 | 149 | r.stubs(:identifier_frozen?).returns(true) |
|
150 | 150 | |
|
151 | 151 | r.identifier = 'bar' |
|
152 | 152 | assert_equal 'foo', r.identifier |
|
153 | 153 | end |
|
154 | 154 | |
|
155 | 155 | def test_identifier_should_accept_change_if_not_frozen |
|
156 | 156 | r = Repository.new(:identifier => 'foo') |
|
157 | 157 | r.stubs(:identifier_frozen?).returns(false) |
|
158 | 158 | |
|
159 | 159 | r.identifier = 'bar' |
|
160 | 160 | assert_equal 'bar', r.identifier |
|
161 | 161 | end |
|
162 | 162 | |
|
163 | 163 | def test_destroy |
|
164 | 164 | repository = Repository.find(10) |
|
165 | 165 | changesets = repository.changesets.count |
|
166 | 166 | changes = repository.filechanges.count |
|
167 | 167 | |
|
168 | 168 | assert_difference 'Changeset.count', -changesets do |
|
169 | 169 | assert_difference 'Change.count', -changes do |
|
170 | 170 | Repository.find(10).destroy |
|
171 | 171 | end |
|
172 | 172 | end |
|
173 | 173 | end |
|
174 | 174 | |
|
175 | 175 | def test_destroy_should_delete_parents_associations |
|
176 | 176 | changeset = Changeset.find(102) |
|
177 | 177 | changeset.parents = Changeset.where(:id => [100, 101]).to_a |
|
178 | 178 | assert_difference 'Changeset.connection.select_all("select * from changeset_parents").count', -2 do |
|
179 | 179 | Repository.find(10).destroy |
|
180 | 180 | end |
|
181 | 181 | end |
|
182 | 182 | |
|
183 | 183 | def test_destroy_should_delete_issues_associations |
|
184 | 184 | changeset = Changeset.find(102) |
|
185 | 185 | changeset.issues = Issue.where(:id => [1, 2]).to_a |
|
186 | 186 | assert_difference 'Changeset.connection.select_all("select * from changesets_issues").count', -2 do |
|
187 | 187 | Repository.find(10).destroy |
|
188 | 188 | end |
|
189 | 189 | end |
|
190 | 190 | |
|
191 | 191 | def test_should_not_create_with_disabled_scm |
|
192 | 192 | # disable Subversion |
|
193 | 193 | with_settings :enabled_scm => ['Darcs', 'Git'] do |
|
194 | 194 | repository = Repository::Subversion.new( |
|
195 | 195 | :project => Project.find(3), :url => "svn://localhost") |
|
196 | 196 | assert !repository.save |
|
197 | 197 | assert_include I18n.translate('activerecord.errors.messages.invalid'), |
|
198 | 198 | repository.errors[:type] |
|
199 | 199 | end |
|
200 | 200 | end |
|
201 | 201 | |
|
202 | 202 | def test_scan_changesets_for_issue_ids |
|
203 | 203 | Setting.default_language = 'en' |
|
204 | 204 | Setting.commit_ref_keywords = 'refs , references, IssueID' |
|
205 | 205 | Setting.commit_update_keywords = [ |
|
206 | 206 | {'keywords' => 'fixes , closes', |
|
207 | 207 | 'status_id' => IssueStatus.where(:is_closed => true).first.id, |
|
208 | 208 | 'done_ratio' => '90'} |
|
209 | 209 | ] |
|
210 | 210 | Setting.default_language = 'en' |
|
211 | 211 | ActionMailer::Base.deliveries.clear |
|
212 | 212 | |
|
213 | 213 | # make sure issue 1 is not already closed |
|
214 | 214 | fixed_issue = Issue.find(1) |
|
215 | 215 | assert !fixed_issue.closed? |
|
216 | 216 | old_status = fixed_issue.status |
|
217 | 217 | |
|
218 | 218 | with_settings :notified_events => %w(issue_added issue_updated) do |
|
219 | 219 | Repository.scan_changesets_for_issue_ids |
|
220 | 220 | end |
|
221 | 221 | assert_equal [101, 102], Issue.find(3).changeset_ids |
|
222 | 222 | |
|
223 | 223 | # fixed issues |
|
224 | 224 | fixed_issue.reload |
|
225 | 225 | assert fixed_issue.closed? |
|
226 | 226 | assert_equal 90, fixed_issue.done_ratio |
|
227 | 227 | assert_equal [101], fixed_issue.changeset_ids |
|
228 | 228 | |
|
229 | 229 | # issue change |
|
230 | 230 | journal = fixed_issue.journals.reorder('created_on desc').first |
|
231 | 231 | assert_equal User.find_by_login('dlopper'), journal.user |
|
232 | 232 | assert_equal 'Applied in changeset r2.', journal.notes |
|
233 | 233 | |
|
234 | 234 | # 2 email notifications |
|
235 | 235 | assert_equal 2, ActionMailer::Base.deliveries.size |
|
236 | 236 | mail = ActionMailer::Base.deliveries.first |
|
237 | 237 | assert_not_nil mail |
|
238 | 238 | assert mail.subject.starts_with?( |
|
239 | 239 | "[#{fixed_issue.project.name} - #{fixed_issue.tracker.name} ##{fixed_issue.id}]") |
|
240 | 240 | assert_mail_body_match( |
|
241 | 241 | "Status changed from #{old_status} to #{fixed_issue.status}", mail) |
|
242 | 242 | |
|
243 | 243 | # ignoring commits referencing an issue of another project |
|
244 | 244 | assert_equal [], Issue.find(4).changesets |
|
245 | 245 | end |
|
246 | 246 | |
|
247 | 247 | def test_for_changeset_comments_strip |
|
248 | 248 | repository = Repository::Mercurial.create( |
|
249 | 249 | :project => Project.find( 4 ), |
|
250 | 250 | :url => '/foo/bar/baz' ) |
|
251 | 251 | comment = <<-COMMENT |
|
252 | 252 | This is a loooooooooooooooooooooooooooong comment |
|
253 | 253 | |
|
254 | 254 | |
|
255 | 255 | COMMENT |
|
256 | 256 | changeset = Changeset.new( |
|
257 | 257 | :comments => comment, :commit_date => Time.now, |
|
258 | 258 | :revision => 0, :scmid => 'f39b7922fb3c', |
|
259 | 259 | :committer => 'foo <foo@example.com>', |
|
260 | 260 | :committed_on => Time.now, :repository => repository ) |
|
261 | 261 | assert( changeset.save ) |
|
262 | 262 | assert_not_equal( comment, changeset.comments ) |
|
263 | 263 | assert_equal( 'This is a loooooooooooooooooooooooooooong comment', |
|
264 | 264 | changeset.comments ) |
|
265 | 265 | end |
|
266 | 266 | |
|
267 | 267 | def test_for_urls_strip_cvs |
|
268 | 268 | repository = Repository::Cvs.create( |
|
269 | 269 | :project => Project.find(4), |
|
270 | 270 | :url => ' :pserver:login:password@host:/path/to/the/repository', |
|
271 | 271 | :root_url => 'foo ', |
|
272 | 272 | :log_encoding => 'UTF-8') |
|
273 | 273 | assert repository.save |
|
274 | 274 | repository.reload |
|
275 | 275 | assert_equal ':pserver:login:password@host:/path/to/the/repository', |
|
276 | 276 | repository.url |
|
277 | 277 | assert_equal 'foo', repository.root_url |
|
278 | 278 | end |
|
279 | 279 | |
|
280 | 280 | def test_for_urls_strip_subversion |
|
281 | 281 | repository = Repository::Subversion.create( |
|
282 | 282 | :project => Project.find(4), |
|
283 | 283 | :url => ' file:///dummy ') |
|
284 | 284 | assert repository.save |
|
285 | 285 | repository.reload |
|
286 | 286 | assert_equal 'file:///dummy', repository.url |
|
287 | 287 | end |
|
288 | 288 | |
|
289 | 289 | def test_for_urls_strip_git |
|
290 | 290 | repository = Repository::Git.create( |
|
291 | 291 | :project => Project.find(4), |
|
292 | 292 | :url => ' c:\dummy ') |
|
293 | 293 | assert repository.save |
|
294 | 294 | repository.reload |
|
295 | 295 | assert_equal 'c:\dummy', repository.url |
|
296 | 296 | end |
|
297 | 297 | |
|
298 | 298 | def test_manual_user_mapping |
|
299 | 299 | assert_no_difference "Changeset.where('user_id <> 2').count" do |
|
300 | 300 | c = Changeset.create!( |
|
301 | 301 | :repository => @repository, |
|
302 | 302 | :committer => 'foo', |
|
303 | 303 | :committed_on => Time.now, |
|
304 | 304 | :revision => 100, |
|
305 | 305 | :comments => 'Committed by foo.' |
|
306 | 306 | ) |
|
307 | 307 | assert_nil c.user |
|
308 | 308 | @repository.committer_ids = {'foo' => '2'} |
|
309 | 309 | assert_equal User.find(2), c.reload.user |
|
310 | 310 | # committer is now mapped |
|
311 | 311 | c = Changeset.create!( |
|
312 | 312 | :repository => @repository, |
|
313 | 313 | :committer => 'foo', |
|
314 | 314 | :committed_on => Time.now, |
|
315 | 315 | :revision => 101, |
|
316 | 316 | :comments => 'Another commit by foo.' |
|
317 | 317 | ) |
|
318 | 318 | assert_equal User.find(2), c.user |
|
319 | 319 | end |
|
320 | 320 | end |
|
321 | 321 | |
|
322 | 322 | def test_auto_user_mapping_by_username |
|
323 | 323 | c = Changeset.create!( |
|
324 | 324 | :repository => @repository, |
|
325 | 325 | :committer => 'jsmith', |
|
326 | 326 | :committed_on => Time.now, |
|
327 | 327 | :revision => 100, |
|
328 | 328 | :comments => 'Committed by john.' |
|
329 | 329 | ) |
|
330 | 330 | assert_equal User.find(2), c.user |
|
331 | 331 | end |
|
332 | 332 | |
|
333 | 333 | def test_auto_user_mapping_by_email |
|
334 | 334 | c = Changeset.create!( |
|
335 | 335 | :repository => @repository, |
|
336 | 336 | :committer => 'john <jsmith@somenet.foo>', |
|
337 | 337 | :committed_on => Time.now, |
|
338 | 338 | :revision => 100, |
|
339 | 339 | :comments => 'Committed by john.' |
|
340 | 340 | ) |
|
341 | 341 | assert_equal User.find(2), c.user |
|
342 | 342 | end |
|
343 | 343 | |
|
344 | 344 | def test_filesystem_avaialbe |
|
345 | 345 | klass = Repository::Filesystem |
|
346 | 346 | assert klass.scm_adapter_class |
|
347 | 347 | assert_equal true, klass.scm_available |
|
348 | 348 | end |
|
349 | 349 | |
|
350 | 350 | def test_extra_info_should_not_return_non_hash_value |
|
351 | 351 | repo = Repository.new |
|
352 | 352 | repo.extra_info = "foo" |
|
353 | 353 | assert_nil repo.extra_info |
|
354 | 354 | end |
|
355 | 355 | |
|
356 | 356 | def test_merge_extra_info |
|
357 | 357 | repo = Repository::Subversion.new(:project => Project.find(3)) |
|
358 | 358 | assert !repo.save |
|
359 | 359 | repo.url = "svn://localhost" |
|
360 | 360 | assert repo.save |
|
361 | 361 | repo.reload |
|
362 | 362 | project = Project.find(3) |
|
363 | 363 | assert_equal repo, project.repository |
|
364 | 364 | assert_nil repo.extra_info |
|
365 | 365 | h1 = {"test_1" => {"test_11" => "test_value_11"}} |
|
366 | 366 | repo.merge_extra_info(h1) |
|
367 | 367 | assert_equal h1, repo.extra_info |
|
368 | 368 | h2 = {"test_2" => { |
|
369 | 369 | "test_21" => "test_value_21", |
|
370 | 370 | "test_22" => "test_value_22", |
|
371 | 371 | }} |
|
372 | 372 | repo.merge_extra_info(h2) |
|
373 | 373 | assert_equal (h = {"test_11" => "test_value_11"}), |
|
374 | 374 | repo.extra_info["test_1"] |
|
375 | 375 | assert_equal "test_value_21", |
|
376 | 376 | repo.extra_info["test_2"]["test_21"] |
|
377 | 377 | h3 = {"test_2" => { |
|
378 | 378 | "test_23" => "test_value_23", |
|
379 | 379 | "test_24" => "test_value_24", |
|
380 | 380 | }} |
|
381 | 381 | repo.merge_extra_info(h3) |
|
382 | 382 | assert_equal (h = {"test_11" => "test_value_11"}), |
|
383 | 383 | repo.extra_info["test_1"] |
|
384 | 384 | assert_nil repo.extra_info["test_2"]["test_21"] |
|
385 | 385 | assert_equal "test_value_23", |
|
386 | 386 | repo.extra_info["test_2"]["test_23"] |
|
387 | 387 | end |
|
388 | 388 | |
|
389 | 389 | def test_sort_should_not_raise_an_error_with_nil_identifiers |
|
390 | 390 | r1 = Repository.new |
|
391 | 391 | r2 = Repository.new |
|
392 | 392 | |
|
393 | 393 | assert_nothing_raised do |
|
394 | 394 | [r1, r2].sort |
|
395 | 395 | end |
|
396 | 396 | end |
|
397 | 397 | |
|
398 | 398 | def test_stats_by_author_reflect_changesets_and_changes |
|
399 | 399 | repository = Repository.find(10) |
|
400 | 400 | |
|
401 | 401 | expected = {"Dave Lopper"=>{:commits_count=>10, :changes_count=>3}} |
|
402 | 402 | assert_equal expected, repository.stats_by_author |
|
403 | 403 | |
|
404 | 404 | set = Changeset.create!( |
|
405 | 405 | :repository => repository, |
|
406 | 406 | :committer => 'dlopper', |
|
407 | 407 | :committed_on => Time.now, |
|
408 | 408 | :revision => 101, |
|
409 | 409 | :comments => 'Another commit by foo.' |
|
410 | 410 | ) |
|
411 | 411 | Change.create!(:changeset => set, :action => 'A', :path => '/path/to/file1') |
|
412 | 412 | Change.create!(:changeset => set, :action => 'A', :path => '/path/to/file2') |
|
413 | 413 | expected = {"Dave Lopper"=>{:commits_count=>11, :changes_count=>5}} |
|
414 | 414 | assert_equal expected, repository.stats_by_author |
|
415 | 415 | end |
|
416 | 416 | |
|
417 | 417 | def test_stats_by_author_honnor_committers |
|
418 | 418 | # in fact it is really tested above, but let's have a dedicated test |
|
419 | 419 | # to ensure things are dynamically linked to Users |
|
420 | 420 | User.find_by_login("dlopper").update_attribute(:firstname, "Dave's") |
|
421 | 421 | repository = Repository.find(10) |
|
422 | 422 | expected = {"Dave's Lopper"=>{:commits_count=>10, :changes_count=>3}} |
|
423 | 423 | assert_equal expected, repository.stats_by_author |
|
424 | 424 | end |
|
425 | 425 | |
|
426 | 426 | def test_stats_by_author_doesnt_drop_unmapped_users |
|
427 | 427 | repository = Repository.find(10) |
|
428 | 428 | Changeset.create!( |
|
429 | 429 | :repository => repository, |
|
430 | 430 | :committer => 'unnamed <foo@bar.net>', |
|
431 | 431 | :committed_on => Time.now, |
|
432 | 432 | :revision => 101, |
|
433 | 433 | :comments => 'Another commit by foo.' |
|
434 | 434 | ) |
|
435 | 435 | |
|
436 | 436 | assert repository.stats_by_author.has_key?("unnamed <foo@bar.net>") |
|
437 | 437 | end |
|
438 | 438 | |
|
439 | 439 | def test_stats_by_author_merge_correctly |
|
440 | 440 | # as we honnor users->committer map and it's not injective, |
|
441 | 441 | # we must be sure merges happen correctly and stats are not |
|
442 | 442 | # wiped out when two source counts map to the same user. |
|
443 | 443 | # |
|
444 | 444 | # Here we have Changeset's with committer="dlopper" and others |
|
445 | 445 | # with committer="dlopper <dlopper@somefoo.net>" |
|
446 | 446 | repository = Repository.find(10) |
|
447 | 447 | |
|
448 | 448 | expected = {"Dave Lopper"=>{:commits_count=>10, :changes_count=>3}} |
|
449 | 449 | assert_equal expected, repository.stats_by_author |
|
450 | 450 | |
|
451 | 451 | set = Changeset.create!( |
|
452 | 452 | :repository => repository, |
|
453 | 453 | :committer => 'dlopper <dlopper@somefoo.net>', |
|
454 | 454 | :committed_on => Time.now, |
|
455 | 455 | :revision => 101, |
|
456 | 456 | :comments => 'Another commit by foo.' |
|
457 | 457 | ) |
|
458 | 458 | |
|
459 | 459 | expected = {"Dave Lopper"=>{:commits_count=>11, :changes_count=>3}} |
|
460 | 460 | assert_equal expected, repository.stats_by_author |
|
461 | 461 | end |
|
462 | ||
|
463 | def test_fetch_changesets | |
|
464 | # 2 repositories in fixtures | |
|
465 | Repository::Subversion.any_instance.expects(:fetch_changesets).twice.returns(true) | |
|
466 | Repository.fetch_changesets | |
|
467 | end | |
|
462 | 468 | end |
General Comments 0
You need to be logged in to leave comments.
Login now