backend_spec.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Validations::ObjectManager::AttributeValidator::Backend, :aggregate_failures do
  4. describe 'backend interface' do
  5. subject(:backend) do
  6. described_class.new(
  7. record: record,
  8. attribute: attribute
  9. )
  10. end
  11. let(:record) { build(:user) }
  12. let(:attribute) { ObjectManager::Attribute.find_by(name: 'firstname') }
  13. it 'has attr_accessor for record' do
  14. expect(backend.record).to eq(record)
  15. end
  16. it 'has attr_accessor for attribute' do
  17. expect(backend.attribute).to eq(attribute)
  18. end
  19. it 'has attr_accessor for value' do
  20. expect(backend.value).to eq(record[attribute.name])
  21. end
  22. it 'has attr_accessor for previous_value' do
  23. record.save!
  24. previous_value = record[attribute.name]
  25. record[attribute.name] = 'changed'
  26. expect(backend.previous_value).to eq(previous_value)
  27. end
  28. describe '.invalid_because_attribute' do
  29. before do
  30. backend.invalid_because_attribute(message, **options)
  31. end
  32. shared_examples 'basic error handling' do
  33. it 'adds Rails validation error' do
  34. expect(record.errors.count).to be(1)
  35. expect(record.errors.to_hash).to have_key(attribute.name.to_sym)
  36. end
  37. end
  38. context 'with plain message without parameter interpolation' do
  39. let(:message) { 'has value that is ...' }
  40. let(:options) { {} }
  41. include_examples 'basic error handling'
  42. context 'when translating the error message' do
  43. let(:custom_translations) { { 'has value that is ...' => 'hat einen Wert von ...', 'This field %s' => 'Dieses Feld %{message}' } }
  44. it 'produces a translated error message' do
  45. allow(Translation).to receive(:translate) { |_locale, string| custom_translations[string] || string }
  46. expect(record.errors.first.message).to eq('has value that is ...')
  47. expect(record.errors.first.localized_full_message(no_field_name: true, locale: 'de-de')).to eq('Dieses Feld hat einen Wert von ...')
  48. end
  49. end
  50. end
  51. context 'with message including parameter interpolation' do
  52. let(:message) { 'has value that is other than %{expected}' }
  53. let(:options) { { expected: 'my_value' } }
  54. include_examples 'basic error handling'
  55. context 'when translating the error message' do
  56. let(:custom_translations) { { 'has value that is other than %{expected}' => 'hat einen Wert abweichend von %{expected}', 'This field %s' => 'Dieses Feld %{message}' } }
  57. it 'produces a translated error message' do
  58. allow(Translation).to receive(:translate) { |_locale, string| custom_translations[string] || string }
  59. expect(record.errors.first.message).to eq('has value that is other than my_value')
  60. expect(record.errors.first.localized_full_message(no_field_name: true, locale: 'de-de')).to eq('Dieses Feld hat einen Wert abweichend von my_value')
  61. end
  62. end
  63. end
  64. end
  65. end
  66. end