create_article_examples.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'mobile app: create article' do |type_label, internal: false, attachments: false, conditional: true|
  3. let(:group) { Group.find_by(name: 'Users') }
  4. let(:agent) { create(:agent, groups: [group]) }
  5. let(:customer) { create(:customer) }
  6. let(:to) { nil }
  7. let(:cc) { nil }
  8. let(:new_text) { 'This is a note' }
  9. let(:result_text) { content_type == 'text/html' ? "<p>#{new_text}</p>" : new_text }
  10. # expected variables:
  11. # ticket
  12. # type as object
  13. def open_article_dialog
  14. visit "/tickets/#{ticket.id}"
  15. find_button('Add reply').click
  16. wait_for_test_flag('ticket-article-reply.opened')
  17. end
  18. def save_article
  19. find_button('Done').click
  20. find_button('Save').click
  21. wait_for_gql('shared/entities/ticket/graphql/mutations/update.graphql')
  22. end
  23. context 'when ticket was not created as the same type', if: conditional do
  24. before do
  25. if type.name == 'note'
  26. create(:ticket_article, :inbound_email, ticket: ticket)
  27. else
  28. create(:ticket_article, ticket: ticket, type_name: 'note')
  29. end
  30. end
  31. it 'cannot create article' do
  32. open_article_dialog
  33. expect(find_select('Channel', visible: :all).open.dialog_element).to have_no_text(type_label)
  34. end
  35. end
  36. context 'when ticket was created as the same type' do
  37. before do
  38. article
  39. end
  40. # rubocop:disable RSpec/ExampleLength
  41. it "can create article #{type_label}" do
  42. open_article_dialog
  43. find_select('Channel', visible: :all).select_option(type_label)
  44. if internal
  45. expect(find_select('Visibility', visible: :all)).to have_selected_option('Internal')
  46. else
  47. expect(find_select('Visibility', visible: :all)).to have_selected_option('Public')
  48. end
  49. text = find_editor('Text')
  50. expect(text).to have_text_value('', exact: true)
  51. text.type(new_text)
  52. if to.present?
  53. find_select('To', visible: :all).search_for_option(to)
  54. end
  55. if cc.present?
  56. find_select('CC', visible: :all).search_for_option(cc)
  57. end
  58. if attachments
  59. find_field('attachments', visible: :all).attach_file('spec/fixtures/files/image/small.png')
  60. # need to wait until the file is uploaded
  61. expect(page).to have_text('small.png', wait: 60)
  62. else
  63. expect(page).to have_no_field('attachments', visible: :all)
  64. end
  65. save_article
  66. attributes = {
  67. type_id: type.id,
  68. internal: internal,
  69. content_type: content_type,
  70. }
  71. if to.present?
  72. attributes[:to] = to
  73. end
  74. if cc.present?
  75. attributes[:to] = to
  76. end
  77. attributes[:body] = result_text
  78. if attachments
  79. attributes[:attachments] = [Store.last]
  80. expect(Store.last.filename).to eq('small.png')
  81. end
  82. expect(Ticket::Article.last).to have_attributes(attributes)
  83. end
  84. # rubocop:enable RSpec/ExampleLength
  85. end
  86. end