check_for_object_attributes_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe CheckForObjectAttributes, type: :db_migration do
  4. it 'performs no action for new systems', system_init_done: false do
  5. migrate do |instance|
  6. expect(instance).not_to receive(:attributes)
  7. end
  8. end
  9. context 'with a valid #data_option hash' do
  10. it 'does not change converted text attribute' do
  11. attribute = create(:object_manager_attribute_text)
  12. expect { migrate }
  13. .not_to change { attribute.reload.data_option }
  14. end
  15. it 'does not change select attribute' do
  16. attribute = create(:object_manager_attribute_select)
  17. expect { migrate }
  18. .not_to change { attribute.reload.data_option }
  19. end
  20. it 'does not change tree_select attribute' do
  21. attribute = create(:object_manager_attribute_tree_select)
  22. expect { migrate }
  23. .not_to change { attribute.reload.data_option }
  24. end
  25. it 'does not change multiselect attribute' do
  26. attribute = create(:object_manager_attribute_multiselect)
  27. expect { migrate }
  28. .not_to change { attribute.reload.data_option }
  29. end
  30. end
  31. context 'for #data_option key:' do
  32. context ':options' do
  33. it 'converts String to Hash' do
  34. wrong = {
  35. default: '',
  36. options: '',
  37. relation: '',
  38. type: 'text',
  39. maxlength: 255,
  40. null: true
  41. }
  42. attribute = create(:object_manager_attribute_text, data_option: wrong)
  43. migrate
  44. attribute.reload
  45. expect(attribute[:data_option][:options]).to be_a(Hash)
  46. expect(attribute[:data_option][:options]).to be_blank
  47. end
  48. end
  49. context ':relation' do
  50. it 'ensures an empty String' do
  51. wrong = {
  52. default: '',
  53. options: {},
  54. type: 'text',
  55. maxlength: 255,
  56. null: true
  57. }
  58. attribute = create(:object_manager_attribute_text, data_option: wrong)
  59. migrate
  60. attribute.reload
  61. expect(attribute[:data_option][:relation]).to be_a(String)
  62. end
  63. it 'converts Hash to String' do
  64. wrong = {
  65. default: '',
  66. options: {},
  67. relation: {},
  68. type: 'text',
  69. maxlength: 255,
  70. null: true
  71. }
  72. attribute = create(:object_manager_attribute_text, data_option: wrong)
  73. migrate
  74. attribute.reload
  75. expect(attribute[:data_option][:relation]).to be_a(String)
  76. expect(attribute[:data_option][:relation]).to be_blank
  77. end
  78. end
  79. # see https://github.com/zammad/zammad/issues/2159
  80. context ':null' do
  81. it 'does not fail on missing values' do
  82. wrong = {
  83. default: '',
  84. options: '', # <- this is not the attribute under test,
  85. relation: '', # but it must be invalid
  86. type: 'text', # to trigger a #save in the migration.
  87. maxlength: 255,
  88. }
  89. create(:object_manager_attribute_text)
  90. .update_columns(data_option: wrong)
  91. expect { migrate }.not_to raise_error
  92. end
  93. end
  94. end
  95. # regression test for issue #2318 - Upgrade to Zammad 2.7 was not possible (migration 20180220171219 CheckForObjectAttributes failed)
  96. context 'for interger attributes' do
  97. it 'missing :min and :max' do
  98. attribute = create(:object_manager_attribute_integer)
  99. attribute.update_columns(data_option: {})
  100. expect { migrate }.not_to raise_error
  101. attribute.reload
  102. expect(attribute[:data_option][:min]).to be_a(Integer)
  103. expect(attribute[:data_option][:max]).to be_a(Integer)
  104. end
  105. end
  106. end