template_spec.rb 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'system/examples/pagination_examples'
  4. RSpec.describe 'Manage > Templates', type: :system do
  5. context 'with ajax pagination' do
  6. include_examples 'pagination', model: :template, klass: Template, path: 'manage/templates'
  7. end
  8. context 'when creating a new template' do
  9. before do
  10. visit '#manage/templates'
  11. within(:active_content) do
  12. find('[data-type=new]').click
  13. end
  14. end
  15. it 'with default attributes' do
  16. in_modal do
  17. fill_in('name', with: 'template 1')
  18. click_on('Submit')
  19. end
  20. end
  21. it 'with inactive state' do
  22. in_modal do
  23. fill_in('name', with: Faker::Name.unique.name)
  24. find_field('active').select('inactive')
  25. click_on('Submit')
  26. await_empty_ajax_queue
  27. expect(Template.last.active).to be(false)
  28. end
  29. end
  30. it 'with cc attribute #4421' do
  31. cc_recipients = Array.new(2) { Faker::Internet.unique.email }.join(', ')
  32. in_modal do
  33. fill_in('name', with: Faker::Name.unique.name)
  34. find('.js-attributeSelector select').find(:option, 'CC').select_option
  35. fill_in('options::article.cc::value', with: cc_recipients)
  36. click_on('Submit')
  37. await_empty_ajax_queue
  38. end
  39. expect(Template.last.options['article.cc']['value']).to eq(cc_recipients)
  40. end
  41. end
  42. context 'when editing an existing template' do
  43. let(:template) { create(:template, :dummy_data) }
  44. before do
  45. # Wait until the test users from the template have been created.
  46. # This is needed for the autocomplete fields to work correctly.
  47. if template.options['ticket.customer_id'] || template.options['ticket.owner_id']
  48. visit '/'
  49. wait.until { User.find(template.options['ticket.customer_id']['value']) } if template.options['ticket.customer_id']
  50. wait.until { User.find(template.options['ticket.owner_id']['value']) } if template.options['ticket.owner_id']
  51. end
  52. visit '#manage/templates'
  53. within(:active_content) do
  54. find(".js-tableBody tr.item[data-id='#{template.id}']").click
  55. end
  56. end
  57. it 'restores stored attributes' do
  58. in_modal do
  59. check_input_field_value('name', template.name)
  60. check_select_field_value('options::ticket.formSenderType::value', template.options['ticket.formSenderType']['value'])
  61. check_input_field_value('options::ticket.title::value', template.options['ticket.title']['value'])
  62. check_editor_field_value('options::article.body::value', template.options['article.body']['value'])
  63. check_tree_select_field_value('options::ticket.group_id::value', template.options['ticket.group_id']['value'].to_s)
  64. check_input_field_value('options::ticket.customer_id::value_completion', template.options['ticket.customer_id']['value_completion'])
  65. check_input_field_value('options::ticket.owner_id::value', template.options['ticket.owner_id']['value'].to_s, visible: :all)
  66. end
  67. end
  68. context 'with custom attributes' do
  69. let(:template) do
  70. create(:template,
  71. options: {
  72. 'ticket.title': {
  73. value: 'Test',
  74. },
  75. 'ticket.foo': {
  76. value: 'bar',
  77. },
  78. 'ticket.baz': {
  79. value: nil,
  80. },
  81. 'ticket.qux': nil,
  82. })
  83. end
  84. it 'ignores unknown or invalid attributes (#4316)' do
  85. in_modal do
  86. check_input_field_value('options::ticket.title::value', 'Test')
  87. expect(find_all('select.form-control').length).to eq(2)
  88. end
  89. end
  90. end
  91. context 'with pending till attribute (#4318)' do
  92. let(:template) do
  93. create(:template,
  94. options: {
  95. 'ticket.state_id': {
  96. value: Ticket::State.find_by(name: 'pending reminder').id.to_s
  97. },
  98. 'ticket.pending_time': pending_time,
  99. })
  100. end
  101. context 'with static operator', time_zone: 'Europe/London' do
  102. let(:date) { 1.day.from_now }
  103. let(:pending_time) do
  104. {
  105. operator: 'static',
  106. value: date.to_datetime.to_s,
  107. }
  108. end
  109. it 'restores correct values' do
  110. in_modal do
  111. check_select_field_value('options::ticket.state_id::value', template.options['ticket.state_id']['value'])
  112. check_select_field_value('options::ticket.pending_time::operator', template.options['ticket.pending_time']['operator'])
  113. check_date_field_value('options::ticket.pending_time::value', date.strftime('%m/%d/%Y'))
  114. check_time_field_value('options::ticket.pending_time::value', date.strftime('%H:%M'))
  115. end
  116. end
  117. end
  118. context 'with relative operator' do
  119. let(:pending_time) do
  120. {
  121. operator: 'relative',
  122. value: '3',
  123. range: 'day',
  124. }
  125. end
  126. it 'restores correct values' do
  127. in_modal do
  128. check_select_field_value('options::ticket.state_id::value', template.options['ticket.state_id']['value'])
  129. check_select_field_value('options::ticket.pending_time::operator', template.options['ticket.pending_time']['operator'])
  130. check_select_field_value('options::ticket.pending_time::value', template.options['ticket.pending_time']['value'])
  131. check_select_field_value('options::ticket.pending_time::range', template.options['ticket.pending_time']['range'])
  132. end
  133. end
  134. end
  135. end
  136. context 'with tags attribute' do
  137. let(:template) do
  138. create(:template,
  139. options: {
  140. 'ticket.tags': tags,
  141. })
  142. end
  143. context 'with add operator' do
  144. let(:tags) do
  145. {
  146. operator: 'add',
  147. value: 'foo, bar',
  148. }
  149. end
  150. it 'restores correct values' do
  151. check_select_field_value('options::ticket.tags::operator', template.options['ticket.tags']['operator'])
  152. check_input_field_value('options::ticket.tags::value', template.options['ticket.tags']['value'], visible: :all)
  153. end
  154. end
  155. context 'with remove operator' do
  156. let(:tags) do
  157. {
  158. operator: 'remove',
  159. value: 'foo, bar',
  160. }
  161. end
  162. it 'restores correct values' do
  163. check_select_field_value('options::ticket.tags::operator', template.options['ticket.tags']['operator'])
  164. check_input_field_value('options::ticket.tags::value', template.options['ticket.tags']['value'], visible: :all)
  165. end
  166. end
  167. context 'without operator' do
  168. let(:tags) do
  169. {
  170. value: 'foo, bar',
  171. }
  172. end
  173. it 'defaults to add operator' do
  174. check_select_field_value('options::ticket.tags::operator', 'add')
  175. check_input_field_value('options::ticket.tags::value', template.options['ticket.tags']['value'], visible: :all)
  176. end
  177. end
  178. end
  179. context 'with cc attribute #4421' do
  180. let(:template) do
  181. create(:template,
  182. options: {
  183. 'article.cc': {
  184. value: Array.new(3) { Faker::Internet.unique.email }.join(', '),
  185. },
  186. })
  187. end
  188. it 'restores correct value' do
  189. check_input_field_value('options::article.cc::value', template.options['article.cc']['value'])
  190. end
  191. end
  192. context 'with active field' do
  193. let(:template) do
  194. create(:template,
  195. :dummy_data,
  196. active: false)
  197. end
  198. it 'shows inactive selection' do
  199. check_select_field_value('active', 'false')
  200. end
  201. it 'supports modifying active state' do
  202. find_field('active').select('active')
  203. click_on('Submit')
  204. await_empty_ajax_queue
  205. expect(template.reload.active).to be(true)
  206. end
  207. end
  208. end
  209. end