You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

custom_field_user_format_test.rb 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative '../test_helper'
  19. class CustomFieldUserFormatTest < ActiveSupport::TestCase
  20. fixtures :custom_fields, :projects, :members, :users, :member_roles, :trackers, :issues
  21. def setup
  22. User.current = nil
  23. @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user')
  24. end
  25. def test_possible_values_options_with_no_arguments
  26. assert_equal [], @field.possible_values_options
  27. assert_equal [], @field.possible_values_options(nil)
  28. end
  29. def test_possible_values_options_with_project_resource
  30. project = Project.find(1)
  31. possible_values_options = @field.possible_values_options(project.issues.first)
  32. assert possible_values_options.any?
  33. assert_equal project.users.sort.map {|u| [u.name, u.id.to_s]}, possible_values_options
  34. end
  35. def test_possible_values_options_with_array
  36. projects = Project.find([1, 2])
  37. possible_values_options = @field.possible_values_options(projects)
  38. assert possible_values_options.any?
  39. assert_equal (projects.first.users & projects.last.users).sort.map {|u| [u.name, u.id.to_s]}, possible_values_options
  40. end
  41. def test_possible_custom_value_options_should_not_include_locked_users
  42. custom_value = CustomValue.new(:customized => Issue.find(1), :custom_field => @field)
  43. assert_include '2', @field.possible_custom_value_options(custom_value).map(&:last)
  44. assert User.find(2).lock!
  45. assert_not_include '2', @field.possible_custom_value_options(custom_value).map(&:last)
  46. end
  47. def test_possible_custom_value_options_should_include_user_that_was_assigned_to_the_custom_value
  48. user = User.generate!
  49. custom_value = CustomValue.new(:customized => Issue.find(1), :custom_field => @field)
  50. assert_not_include user.id.to_s, @field.possible_custom_value_options(custom_value).map(&:last)
  51. custom_value.value = user.id
  52. custom_value.save!
  53. assert_include user.id.to_s, @field.possible_custom_value_options(custom_value).map(&:last)
  54. end
  55. def test_cast_blank_value
  56. assert_nil @field.cast_value(nil)
  57. assert_nil @field.cast_value("")
  58. end
  59. def test_cast_valid_value
  60. user = @field.cast_value("2")
  61. assert_kind_of User, user
  62. assert_equal User.find(2), user
  63. end
  64. def test_cast_invalid_value
  65. assert_nil @field.cast_value("187")
  66. end
  67. end