20171023000001_fixed_store_upgrade_ror_45_spec.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. # Rails 5.0 has changed to only store and read ActiveSupport::HashWithIndifferentAccess from stores
  4. # we extended lib/core_ext/active_record/store/indifferent_coder.rb to read also ActionController::Parameters
  5. # and convert them to ActiveSupport::HashWithIndifferentAccess for migration in db/migrate/20171023000001_fixed_store_upgrade_ror_45.rb.
  6. RSpec.describe FixedStoreUpgradeRor45, type: :db_migration do
  7. subject(:taskbar) { Taskbar.last }
  8. context 'when DB contains `store`d attributes saved as unpermitted ActionController::Parameters' do
  9. before do
  10. ActiveRecord::Base.connection.execute(<<~SQL.tap { |sql| sql.delete!('`') if !mysql? }) # rubocop:disable Rails/SquishedSQLHeredocs
  11. INSERT INTO taskbars (`user_id`, `key`, `callback`, `state`, `params`, `prio`, `notify`, `active`, `preferences`, `last_contact`, `updated_at`, `created_at`)
  12. VALUES (#{user.id},
  13. 'Ticket-123',
  14. 'TicketZoom',
  15. '#{state.to_yaml}',
  16. '#{params.to_yaml}',
  17. 1,
  18. FALSE,
  19. TRUE,
  20. '#{preferences.to_yaml}',
  21. '#{last_contact}',
  22. '#{last_contact}',
  23. '#{last_contact}')
  24. SQL
  25. end
  26. let(:mysql?) { ActiveRecord::Base.connection_db_config.configuration_hash[:adapter] == 'mysql2' }
  27. let(:user) { User.last }
  28. let(:last_contact) { '2017-09-01 10:10:00' }
  29. let(:state) { ActionController::Parameters.new('ticket' => {}, 'article' => {}) }
  30. let(:params) { ActionController::Parameters.new('ticket_id' => 1234, 'shown' => true) }
  31. let(:preferences) do
  32. ActionController::Parameters.new(
  33. 'tasks' => [
  34. {
  35. 'id' => 99_282,
  36. 'user_id' => 85_370,
  37. 'last_contact' => 1.week.after(Time.zone.parse(last_contact)),
  38. 'changed' => true
  39. }
  40. ]
  41. )
  42. end
  43. it 'converts `store`d attributes to ActiveSupport::HashWithIndifferentAccess, preserving original values' do
  44. expect { migrate }
  45. .to change { taskbar.reload.read_attribute_before_type_cast(:state) }
  46. .and not_change { taskbar.reload.state }
  47. .and change { taskbar.reload.read_attribute_before_type_cast(:params) }
  48. .and not_change { taskbar.reload.params }
  49. .and change { taskbar.reload.read_attribute_before_type_cast(:preferences) }
  50. expect(taskbar.attributes.slice('params', 'preferences', 'state').values)
  51. .to all(be_a(ActiveSupport::HashWithIndifferentAccess))
  52. end
  53. end
  54. end