annotate .svn/pristine/ab/ab99b73dd4f0c1391903ead6d881d8b5a0522aba.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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