overview_spec.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/application_model_examples'
  4. RSpec.describe Overview, type: :model do
  5. it_behaves_like 'ApplicationModel',
  6. can_assets: { associations: :users, selectors: :condition },
  7. can_create_update: { unique_name: false }
  8. it_behaves_like 'Association clears cache', association: :roles
  9. it_behaves_like 'Association clears cache', association: :users
  10. context 'link generation' do
  11. it 'generates from name' do
  12. overview = create(:overview, name: 'Not Shown Admin 2')
  13. expect(overview.link).to eq('not_shown_admin_2')
  14. end
  15. it 'ensures uniquenes' do
  16. overview1, overview2, overview3 = create_list(:overview, 3, name: 'Übersicht')
  17. expect(overview1.link).not_to eq(overview2.link)
  18. expect(overview1.link).not_to eq(overview3.link)
  19. expect(overview2.link).not_to eq(overview3.link)
  20. end
  21. context 'given link' do
  22. it 'keeps on create' do
  23. overview = create(:overview, name: 'Übersicht', link: 'my_overview')
  24. expect(overview.link).to eq('my_overview')
  25. end
  26. it 'keeps on update' do
  27. overview = create(:overview, name: 'Übersicht')
  28. overview.update!(link: 'my_overview_2')
  29. expect(overview.link).to eq('my_overview_2')
  30. end
  31. end
  32. context 'URL save' do
  33. it 'handles umlauts' do
  34. overview = create(:overview, name: 'Übersicht')
  35. expect(overview.link).to eq('ubersicht')
  36. end
  37. it 'handles spaces' do
  38. overview = create(:overview, name: " Meine Übersicht \n")
  39. expect(overview.link).to eq('meine_ubersicht')
  40. end
  41. it 'handles special chars' do
  42. overview = create(:overview, name: 'Д дФ ф')
  43. expect(overview.link).to match(%r{^[a-z0-9-]{36}$})
  44. end
  45. it 'removes special char fallback if possible' do
  46. overview = create(:overview, name: ' Д дФ ф abc ')
  47. expect(overview.link).to eq('abc')
  48. end
  49. end
  50. end
  51. describe '#rearrangement' do
  52. it 'rearranges prio of other overviews on prio change' do
  53. overview1 = create(:overview, prio: 1)
  54. overview2 = create(:overview, prio: 2)
  55. overview3 = create(:overview, prio: 3)
  56. overview2.update!(prio: 3)
  57. overviews = described_class.reorder(prio: :asc).pluck(:id)
  58. expect(overviews.first).to eq(overview1.id)
  59. expect(overviews.second).to eq(overview3.id)
  60. expect(overviews.third).to eq(overview2.id)
  61. end
  62. end
  63. describe '#fill_prio' do
  64. before do
  65. described_class.destroy_all
  66. end
  67. it 'fill an empty prio with the maximum prio plus one' do
  68. overview1 = create(:overview, prio: 1)
  69. overview2 = create(:overview, prio: 200)
  70. overview3 = create(:overview, prio: nil)
  71. overviews = described_class.reorder(prio: :asc).pluck(:id)
  72. expect(overviews).to eq [overview1.id, overview2.id, overview3.id]
  73. end
  74. it 'sets first Overview priority as 0' do
  75. overview = create(:overview, prio: nil)
  76. expect(overview.prio).to be 0
  77. end
  78. it 'sets new Overview priority as +1' do
  79. create(:overview, prio: 123)
  80. overview_next = create(:overview, prio: nil)
  81. expect(overview_next.prio).to be 124
  82. end
  83. end
  84. describe 'Make conditions AND/OR and thus enable several conditions of same type #2185' do
  85. it 'does create overviews with the expert structure' do
  86. expect { create(:overview, :condition_expert) }.not_to raise_error
  87. end
  88. end
  89. end