notification_factory_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe NotificationFactory do
  4. # WARNING: This spec relies on the presence of
  5. # *actual* view templates in the app/ directory.
  6. # Deleting them from the repo will break the tests!
  7. describe '::template_read' do
  8. let(:rendered_locale) { 'en' }
  9. let(:parsed_template) { { subject: template_lines.first, body: template_lines.drop(1).join } }
  10. let(:template_lines) { File.readlines(template_path) }
  11. let(:template_path) { Rails.root.join("app/views/mailer/signup/#{rendered_locale}.html.erb") }
  12. let(:read_params) do
  13. { type: 'mailer', template: 'signup', locale: 'en', format: 'html' }
  14. end
  15. it 'returns template file content as { subject: <first line>, body: <rest of file> }' do
  16. expect(described_class.template_read(read_params))
  17. .to eq(parsed_template)
  18. end
  19. context 'when selecting a template file to render' do
  20. # see https://github.com/zammad/zammad/issues/845#issuecomment-395084348
  21. context 'and file with ‘.custom’ suffix is available' do
  22. let(:template_path) { Rails.root.to_s + "/app/views/mailer/signup/#{rendered_locale}.html.erb.custom" }
  23. it 'uses that file' do
  24. File.write(template_path, "Subject\nBody\nbody\n")
  25. expect(described_class.template_read(read_params))
  26. .to eq({ subject: "Subject\n", body: "Body\nbody\n" })
  27. ensure
  28. File.delete(template_path)
  29. end
  30. end
  31. context 'if no locale given in arguments, and no default locale is set' do
  32. before { Setting.set('locale_default', nil) }
  33. it 'renders en-us template' do
  34. expect(described_class.template_read(read_params.except(:locale)))
  35. .to eq(parsed_template)
  36. end
  37. end
  38. context 'if no locale given in arguments, but default locale is set' do
  39. before { Setting.set('locale_default', 'de-de') }
  40. let(:rendered_locale) { 'de' }
  41. it 'tries template for default locale' do
  42. expect(described_class.template_read(read_params.except(:locale)))
  43. .to eq(parsed_template)
  44. end
  45. context 'and no such template exists' do
  46. before { Setting.set('locale_default', 'xx') }
  47. let(:rendered_locale) { 'en' }
  48. it 'falls back to en template' do
  49. expect(described_class.template_read(read_params.except(:locale)))
  50. .to eq(parsed_template)
  51. end
  52. end
  53. end
  54. context 'if locale given in arguments' do
  55. let(:rendered_locale) { 'de' }
  56. it 'tries template for given locale' do
  57. expect(described_class.template_read(read_params.merge(locale: 'de-de')))
  58. .to eq(parsed_template)
  59. end
  60. context 'and no such template exists' do
  61. let(:rendered_locale) { 'en' }
  62. it 'falls back to en template' do
  63. expect(described_class.template_read(read_params.merge(locale: 'xx')))
  64. .to eq(parsed_template)
  65. end
  66. end
  67. end
  68. end
  69. end
  70. describe '::application_template_read' do
  71. let(:read_params) { { type: 'mailer', format: 'html' } }
  72. let(:template_path) { Rails.root.join('app/views/mailer/application.html.erb') }
  73. it 'returns template file content as string' do
  74. expect(described_class.application_template_read(read_params))
  75. .to eq(File.read(template_path))
  76. end
  77. end
  78. end