group_hierarchy_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe GroupHierarchy, db_strategy: :reset, type: :db_migration do
  4. describe '#change' do
  5. before do
  6. remove_foreign_key :groups, column: :parent_id
  7. remove_column :groups, :parent_id
  8. remove_column :groups, :name_last
  9. change_column :groups, :name, :string, limit: 160
  10. Group.reset_column_information
  11. migrate
  12. end
  13. it 'increases limit for name column' do
  14. expect(Group.column_for_attribute(:name).sql_type_metadata.limit).to eq((160 * 6) + (2 * 5))
  15. end
  16. it 'adds name_last column' do
  17. expect(Group.column_for_attribute(:name_last).sql_type_metadata.type).to eq(:string)
  18. end
  19. it 'adds parent_id column' do
  20. expect(Group.column_for_attribute(:parent_id).sql_type_metadata.type).to eq(:integer)
  21. end
  22. end
  23. describe '#migrate_group_name' do
  24. context 'when group name does not contain reserved characters' do
  25. let(:user) { create(:user) }
  26. let(:group) { create(:group, name: 'A', updated_by_id: user.id) }
  27. before do
  28. group
  29. described_class.new.migrate_group_name
  30. end
  31. it 'does not migrate name' do
  32. expect(group.reload).to have_attributes(name: 'A', name_last: 'A', updated_by_id: 1)
  33. end
  34. end
  35. context 'when group name contains reserved characters' do
  36. context 'without conflicting group names' do
  37. let(:group) { create(:group) }
  38. before do
  39. group.update_columns(name: 'A::B')
  40. described_class.new.migrate_group_name
  41. end
  42. it 'migrates name with an alternative delimiter' do
  43. expect(group.reload).to have_attributes(name: 'A-B', name_last: 'A-B', updated_by_id: 1)
  44. end
  45. end
  46. context 'with conflicting target group names' do
  47. let(:group1) { create(:group) }
  48. let(:group2) { create(:group, name: 'A-B-C') }
  49. let(:group3) { create(:group, name: 'A--B--C') }
  50. before do
  51. group1.update_columns(name: 'A::B::C') && group2 && group3
  52. described_class.new.migrate_group_name
  53. end
  54. it 'migrates name with an longer alternative delimiter' do
  55. expect(group1.reload).to have_attributes(name: 'A---B---C', name_last: 'A---B---C')
  56. end
  57. end
  58. end
  59. end
  60. end