/features/step_definitions/custom_field_steps.rb

https://gitlab.com/MichelZuniga/openproject · Ruby · 117 lines · 69 code · 20 blank · 28 comment · 15 complexity · d0cef29b5da81b927921d8f40dd0efff MD5 · raw file

  1. #-- encoding: UTF-8
  2. #-- copyright
  3. # OpenProject is a project management system.
  4. # Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License version 3.
  8. #
  9. # OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
  10. # Copyright (C) 2006-2013 Jean-Philippe Lang
  11. # Copyright (C) 2010-2013 the ChiliProject Team
  12. #
  13. # This program is free software; you can redistribute it and/or
  14. # modify it under the terms of the GNU General Public License
  15. # as published by the Free Software Foundation; either version 2
  16. # of the License, or (at your option) any later version.
  17. #
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with this program; if not, write to the Free Software
  25. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  26. #
  27. # See doc/COPYRIGHT.rdoc for more details.
  28. #++
  29. [CustomField, WorkPackageCustomField].each do |const|
  30. InstanceFinder.register(const, Proc.new { |name| const.find_by(name: name) })
  31. RouteMap.register(const, '/custom_fields')
  32. end
  33. Given /^the following (user|issue|work package) custom fields are defined:$/ do |type, table|
  34. type = (type.gsub(' ', '_') + '_custom_field').to_sym
  35. as_admin do
  36. table.hashes.each_with_index do |r, _i|
  37. attr_hash = { name: r['name'],
  38. field_format: r['type'] }
  39. attr_hash[:possible_values] = r['possible_values'].split(',').map(&:strip) if r['possible_values']
  40. attr_hash[:is_required] = (r[:required] == 'true') if r[:required]
  41. attr_hash[:editable] = (r[:editable] == 'true') if r[:editable]
  42. attr_hash[:visible] = (r[:visible] == 'true') if r[:visible]
  43. attr_hash[:is_filter] = (r[:is_filter] == 'true') if r[:is_filter]
  44. attr_hash[:default_value] = r[:default_value] ? r[:default_value] : nil
  45. attr_hash[:is_for_all] = r[:is_for_all] || true
  46. FactoryGirl.create type, attr_hash
  47. end
  48. end
  49. end
  50. Given /^the user "(.+?)" has the user custom field "(.+?)" set to "(.+?)"$/ do |login, field_name, value|
  51. user = User.find_by_login(login)
  52. custom_field = UserCustomField.find_by(name: field_name)
  53. user.custom_values.build(custom_field: custom_field, value: value)
  54. user.save!
  55. end
  56. Given /^the work package "(.+?)" has the custom field "(.+?)" set to "(.+?)"$/ do |wp_name, field_name, value|
  57. wp = InstanceFinder.find(WorkPackage, wp_name)
  58. custom_field = InstanceFinder.find(WorkPackageCustomField, field_name)
  59. custom_value = wp.custom_values.detect { |cv| cv.custom_field_id == custom_field.id }
  60. if custom_value
  61. custom_value.value = value
  62. else
  63. wp.custom_values.build(custom_field: custom_field, value: value)
  64. end
  65. wp.save!
  66. end
  67. Given /^the work package "(.+?)" has the custom user field "(.+?)" set to "(.+?)"$/ do |wp_name, field_name, username|
  68. user = User.find_by_login(username)
  69. steps %{
  70. Given the work package "#{wp_name}" has the custom field "#{field_name}" set to "#{user.id}"
  71. }
  72. end
  73. Given(/^the custom field "(.*?)" is enabled for the project "(.*?)"$/) do |field_name, project_name|
  74. custom_field = WorkPackageCustomField.find_by(name: field_name)
  75. project = Project.find_by(name: project_name)
  76. project.work_package_custom_fields << custom_field
  77. project.save!
  78. end
  79. Given(/^the custom field "(.*?)" is disabled for the project "(.*?)"$/) do |field_name, project_name|
  80. custom_field = WorkPackageCustomField.find_by(name: field_name)
  81. project = Project.find_by(name: project_name)
  82. project.work_package_custom_fields.delete custom_field
  83. end
  84. Given /^the custom field "(.+)" is( not)? summable$/ do |field_name, negative|
  85. custom_field = WorkPackageCustomField.find_by(name: field_name)
  86. Setting.work_package_list_summable_columns = negative ?
  87. Setting.work_package_list_summable_columns - ["cf_#{custom_field.id}"] :
  88. Setting.work_package_list_summable_columns << "cf_#{custom_field.id}"
  89. end
  90. Given /^the custom field "(.*?)" is activated for type "(.*?)"$/ do |field_name, type_name|
  91. custom_field = WorkPackageCustomField.find_by(name: field_name)
  92. type = ::Type.find_by(name: type_name)
  93. custom_field.types << type
  94. end
  95. Given /^there are no custom fields$/ do
  96. CustomField.destroy_all
  97. end