notification_factory_mailer_template_test.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class NotificationFactoryMailerTemplateTest < ActiveSupport::TestCase
  4. test 'notifications template' do
  5. Translation.sync_locale_from_po('de-de')
  6. groups = Group.where(name: 'Users')
  7. roles = Role.where(name: 'Agent')
  8. agent1 = User.create!(
  9. login: 'notification-template-agent1@example.com',
  10. firstname: 'Notification<b>xxx</b>',
  11. lastname: 'Agent1<b>yyy</b>',
  12. email: 'notification-template-agent1@example.com',
  13. password: 'agentpw',
  14. active: true,
  15. roles: roles,
  16. groups: groups,
  17. preferences: {
  18. locale: 'de-de',
  19. },
  20. updated_by_id: 1,
  21. created_by_id: 1,
  22. )
  23. agent_current_user = User.create!(
  24. login: 'notification-template-current_user@example.com',
  25. firstname: 'Notification Current',
  26. lastname: 'User',
  27. email: 'notification-template-current_user@example.com',
  28. password: 'agentpw',
  29. active: true,
  30. roles: roles,
  31. groups: groups,
  32. preferences: {
  33. locale: 'de-de',
  34. },
  35. updated_by_id: 1,
  36. created_by_id: 1,
  37. )
  38. result = NotificationFactory::Mailer.template(
  39. template: 'password_reset',
  40. locale: 'de-de',
  41. objects: {
  42. user: agent1,
  43. },
  44. )
  45. assert_match('Zurücksetzen Ihres', result[:subject])
  46. assert_match('wir haben eine Anfrage zum Zurücksetzen', result[:body])
  47. assert_match('Ihr', result[:body])
  48. assert_match('Ihr', result[:body])
  49. assert_match('Notification&lt;b&gt;xxx&lt;/b&gt;', result[:body])
  50. assert_no_match('Your', result[:body])
  51. result = NotificationFactory::Mailer.template(
  52. template: 'password_reset',
  53. locale: 'de',
  54. objects: {
  55. user: agent1,
  56. },
  57. )
  58. assert_match('Zurücksetzen Ihres', result[:subject])
  59. assert_match('wir haben eine Anfrage zum Zurücksetzen', result[:body])
  60. assert_match('Ihr', result[:body])
  61. assert_match('Notification&lt;b&gt;xxx&lt;/b&gt;', result[:body])
  62. assert_no_match('Your', result[:body])
  63. result = NotificationFactory::Mailer.template(
  64. template: 'password_reset',
  65. locale: 'xx-us',
  66. objects: {
  67. user: agent1,
  68. },
  69. )
  70. assert_match('Reset your', result[:subject])
  71. assert_match('We received a request to reset the password', result[:body])
  72. assert_match('Your', result[:body])
  73. assert_match('Notification&lt;b&gt;xxx&lt;/b&gt;', result[:body])
  74. assert_no_match('Ihr', result[:body])
  75. ticket = Ticket.create(
  76. group_id: Group.lookup(name: 'Users').id,
  77. customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
  78. owner_id: User.lookup(login: '-').id,
  79. title: 'Welcome to Zammad!',
  80. state_id: Ticket::State.lookup(name: 'new').id,
  81. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  82. updated_by_id: 1,
  83. created_by_id: 1,
  84. )
  85. article = Ticket::Article.create(
  86. ticket_id: ticket.id,
  87. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  88. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  89. from: 'Zammad Feedback <feedback@zammad.org>',
  90. content_type: 'text/plain',
  91. body: 'Welcome!
  92. <b>test123</b>',
  93. internal: false,
  94. updated_by_id: 1,
  95. created_by_id: 1,
  96. )
  97. changes = {}
  98. result = NotificationFactory::Mailer.template(
  99. template: 'ticket_create',
  100. locale: 'xx-us',
  101. objects: {
  102. ticket: ticket,
  103. article: article,
  104. recipient: agent1,
  105. current_user: agent_current_user,
  106. changes: changes,
  107. },
  108. )
  109. assert_match('New ticket', result[:subject])
  110. assert_match('Notification&lt;b&gt;xxx&lt;/b&gt;', result[:body])
  111. assert_match('has been created by', result[:body])
  112. assert_match('&lt;b&gt;test123&lt;/b&gt;', result[:body])
  113. assert_match('Manage your notification settings', result[:body])
  114. assert_no_match('Dein', result[:body])
  115. assert_no_match('longname', result[:body])
  116. assert_match('Current User', result[:body])
  117. result = NotificationFactory::Mailer.template(
  118. template: 'ticket_create',
  119. locale: 'de-de',
  120. objects: {
  121. ticket: ticket,
  122. article: article,
  123. recipient: agent1,
  124. current_user: agent_current_user,
  125. changes: changes,
  126. },
  127. )
  128. assert_match('Neues Ticket', result[:subject])
  129. assert_match('Notification&lt;b&gt;xxx&lt;/b&gt;', result[:body])
  130. assert_match('ein neues Ticket', result[:body])
  131. assert_match('&lt;b&gt;test123&lt;/b&gt;', result[:body])
  132. assert_match(Translation.translate('de-de', 'Manage your notification settings'), result[:body])
  133. assert_no_match('Your', result[:body])
  134. assert_no_match('longname', result[:body])
  135. assert_match('Current User', result[:body])
  136. article = Ticket::Article.create(
  137. ticket_id: ticket.id,
  138. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  139. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  140. from: 'Zammad Feedback <feedback@zammad.org>',
  141. content_type: 'text/html',
  142. body: 'Welcome!
  143. <b>test123</b>',
  144. internal: false,
  145. updated_by_id: 1,
  146. created_by_id: 1,
  147. )
  148. changes = {
  149. state: %w[aaa bbb],
  150. group: %w[xxx yyy],
  151. }
  152. result = NotificationFactory::Mailer.template(
  153. template: 'ticket_update',
  154. locale: 'xx-us',
  155. objects: {
  156. ticket: ticket,
  157. article: article,
  158. recipient: agent1,
  159. current_user: agent_current_user,
  160. changes: changes,
  161. },
  162. )
  163. assert_match('Updated ticket', result[:subject])
  164. assert_match('Notification&lt;b&gt;xxx&lt;/b&gt;', result[:body])
  165. assert_match('has been updated by', result[:body])
  166. assert_match('<b>test123</b>', result[:body])
  167. assert_match('Manage your notification settings', result[:body])
  168. assert_no_match('Dein', result[:body])
  169. assert_no_match('longname', result[:body])
  170. assert_match('Current User', result[:body])
  171. result = NotificationFactory::Mailer.template(
  172. template: 'ticket_update',
  173. locale: 'de-de',
  174. objects: {
  175. ticket: ticket,
  176. article: article,
  177. recipient: agent1,
  178. current_user: agent_current_user,
  179. changes: changes,
  180. },
  181. )
  182. assert_match('Aktualisiertes Ticket', result[:subject])
  183. assert_match('Notification&lt;b&gt;xxx&lt;/b&gt;', result[:body])
  184. assert_match('wurde von', result[:body])
  185. assert_match('<b>test123</b>', result[:body])
  186. assert_match(Translation.translate('de-de', 'Manage your notification settings'), result[:body])
  187. assert_no_match('Your', result[:body])
  188. assert_no_match('longname', result[:body])
  189. assert_match('Current User', result[:body])
  190. Setting.set('locale_default', 'de-de')
  191. result = NotificationFactory::Mailer.template(
  192. template: 'ticket_update',
  193. objects: {
  194. ticket: ticket,
  195. article: article,
  196. recipient: agent1,
  197. current_user: agent_current_user,
  198. changes: changes,
  199. },
  200. )
  201. assert_match('Aktualisiertes Ticket', result[:subject])
  202. assert_match('Notification&lt;b&gt;xxx&lt;/b&gt;', result[:body])
  203. assert_match('wurde von', result[:body])
  204. assert_match('<b>test123</b>', result[:body])
  205. assert_match(Translation.translate('de-de', 'Manage your notification settings'), result[:body])
  206. assert_no_match('Your', result[:body])
  207. assert_no_match('longname', result[:body])
  208. assert_match('Current User', result[:body])
  209. Setting.set('locale_default', 'not_existing')
  210. result = NotificationFactory::Mailer.template(
  211. template: 'ticket_update',
  212. objects: {
  213. ticket: ticket,
  214. article: article,
  215. recipient: agent1,
  216. current_user: agent_current_user,
  217. changes: changes,
  218. },
  219. )
  220. assert_match('Updated ticket', result[:subject])
  221. assert_match('Notification&lt;b&gt;xxx&lt;/b&gt;', result[:body])
  222. assert_match('has been updated by', result[:body])
  223. assert_match('<b>test123</b>', result[:body])
  224. assert_match('Manage your notification settings', result[:body])
  225. assert_no_match('Dein', result[:body])
  226. assert_no_match('longname', result[:body])
  227. assert_match('Current User', result[:body])
  228. Setting.set('locale_default', 'pt-br')
  229. result = NotificationFactory::Mailer.template(
  230. template: 'ticket_update',
  231. objects: {
  232. ticket: ticket,
  233. article: article,
  234. recipient: agent1,
  235. current_user: agent_current_user,
  236. changes: changes,
  237. },
  238. )
  239. assert_match('atualizado', result[:subject])
  240. assert_match('Notification&lt;b&gt;xxx&lt;/b&gt;', result[:body])
  241. assert_match('foi atualizado por', result[:body])
  242. assert_match('<b>test123</b>', result[:body])
  243. assert_match(Translation.translate('pt-br', 'Manage your notification settings'), result[:body])
  244. assert_no_match('Dein', result[:body])
  245. assert_no_match('longname', result[:body])
  246. assert_match('Current User', result[:body])
  247. end
  248. end