edit_spec.rb 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Knowledge Base Locale Answer Edit', type: :system do
  4. include_context 'basic Knowledge Base'
  5. before do
  6. published_answer && draft_answer && internal_answer
  7. end
  8. it 'wraps long texts' do
  9. long_string = '3KKFA9DAWE9VJYNNnpYRRtMwfa168O1yvpD2t9QXsfb3cppGV6KZ12q0UUJIy5r4Exfk18GnWPR0A3SoDsjxIHz1Gcu4aCEVzenilSOu4gAfxnB6k3mSBUOGIfdgChEBYhcHGgiCmV2EoXu4gG7GAJxKJhM2d4NUiL5RZttGtMXYYFr2Jsg7MV7xXGcygnsLMYqnwzOJxBK0vH3fzhdIZd6YrqR3fggaY0RyKtVigOBZ2SETC8s238Z9eDL4gfUW'
  10. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{draft_answer.id}/edit"
  11. within(:active_content) do
  12. find('.richtext-content').send_keys long_string
  13. expect(page).to have_css('.js-submit') { |elem| !elem.obscured? }
  14. expect(page).to have_css('.page-header-title') { |elem| !elem.obscured? }
  15. end
  16. end
  17. context 'when image is added via button' do
  18. before do
  19. image = Rszr::Image.load('spec/fixtures/files/image/large.png')
  20. image.resize!(:auto, 30_000)
  21. image.save('tmp/really-large.png')
  22. end
  23. def open_editor_and_add_image
  24. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{draft_answer.id}/edit"
  25. find('a[data-action="insert_image"]').click
  26. within('.popover-content') do
  27. find('input[name="link"]', visible: :all).set(Rails.root.join('tmp/really-large.png'))
  28. find('[type=submit]').click
  29. end
  30. end
  31. it 'can use big inline image' do
  32. open_editor_and_add_image
  33. click '.js-submit'
  34. await_empty_ajax_queue
  35. click_on 'Edit'
  36. expect(page).to have_css("img[src='/api/v1/attachments/#{draft_answer.reload.translations.first.content.attachments.first.id}']")
  37. end
  38. end
  39. context 'add weblink' do
  40. def open_editor_and_add_link(input)
  41. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{draft_answer.id}/edit"
  42. find('a[data-action="link"]').click
  43. within('.popover-content') do
  44. find('input').fill_in with: input
  45. find('[type=submit]').click
  46. end
  47. end
  48. it 'allows mailto links' do
  49. open_editor_and_add_link 'mailto:test@example.com'
  50. expect(page).to have_link(href: 'mailto:test@example.com')
  51. end
  52. it 'allows link with a protocol' do
  53. open_editor_and_add_link 'protocol://example.org'
  54. expect(page).to have_link(href: 'protocol://example.org')
  55. end
  56. it 'allows relative link' do
  57. open_editor_and_add_link '/path'
  58. expect(page).to have_link(href: '/path')
  59. end
  60. it 'allows non-protocol URL and prepends default protocol' do
  61. open_editor_and_add_link 'example.com'
  62. expect(page).to have_link(href: 'http://example.com')
  63. end
  64. end
  65. context 'embedded video' do
  66. it 'has adding functionality' do
  67. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{published_answer.id}/edit"
  68. find('a[data-action="embed_video"]').click
  69. within('.popover-content') do
  70. find('input').fill_in with: 'https://www.youtube.com/watch?v=vTTzwJsHpU8'
  71. find('[type=submit]').click
  72. end
  73. within('.richtext-content') do
  74. expect(page).to have_text('( widget: video, provider: youtube, id: vTTzwJsHpU8 )')
  75. end
  76. end
  77. it 'loads stored' do
  78. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{published_answer_with_video.id}"
  79. iframe = find('iframe')
  80. expect(iframe['src']).to start_with('https://www.youtube.com/embed/')
  81. end
  82. end
  83. context 'tags' do
  84. before do
  85. visit "#knowledge_base/#{knowledge_base.id}/locale/#{locale_name}/answer/#{published_answer_with_tag.id}/edit"
  86. end
  87. let(:new_tag_name) { 'capybara_kb_tag' }
  88. it 'adds a new tag' do
  89. within :active_content do
  90. click '.js-newTagLabel'
  91. elem = find('.js-newTagInput')
  92. elem.fill_in with: new_tag_name
  93. elem.send_keys :return
  94. wait.until_exists { published_answer_with_tag.reload.tag_list.include? new_tag_name }
  95. expect(page).to have_link(new_tag_name, href: false, class: ['js-tag'])
  96. end
  97. end
  98. it 'triggers autocomplete after one character' do
  99. within :active_content do
  100. click '.js-newTagLabel'
  101. elem = find('.js-newTagInput')
  102. elem.fill_in with: 'e'
  103. expect(page).to have_css('ul.ui-autocomplete > li.ui-menu-item', minimum: 1)
  104. end
  105. end
  106. it 'shows an existing tag' do
  107. within :active_content do
  108. expect(page).to have_link(published_answer_tag_name, href: false, class: ['js-tag'])
  109. end
  110. end
  111. it 'deletes a tag' do
  112. within :active_content do
  113. find('.list-item', text: published_answer_tag_name)
  114. .find('.js-delete').click
  115. expect(page).to have_no_link(published_answer_tag_name, href: false, class: ['js-tag'])
  116. wait.until_exists { published_answer_with_tag.reload.tag_list.exclude? published_answer_tag_name }
  117. end
  118. end
  119. end
  120. describe 'linked tickets' do
  121. let(:ticket) { Ticket.first }
  122. it 'links a ticket' do
  123. visit "#knowledge_base/#{knowledge_base.id}/locale/#{locale_name}/answer/#{published_answer.id}/edit"
  124. within '.knowledge-base-sidebar .sidebar-linked-tickets' do
  125. click '.js-add'
  126. end
  127. in_modal do
  128. fill_in 'ticket_number', with: ticket.number
  129. click '.js-submit'
  130. end
  131. within '.knowledge-base-sidebar .sidebar-linked-tickets' do
  132. expect(page).to have_text(ticket.title)
  133. end
  134. added_link = Link.list(link_object: 'Ticket', link_object_value: ticket.id).last
  135. expect(added_link).to eq({
  136. 'link_object' => 'KnowledgeBase::Answer::Translation',
  137. 'link_object_value' => published_answer.translations.first.id,
  138. 'link_type' => 'normal',
  139. })
  140. end
  141. context 'when a linked ticket exists' do
  142. before do
  143. create(:link, from: ticket, to: published_answer.translations.first)
  144. visit "#knowledge_base/#{knowledge_base.id}/locale/#{locale_name}/answer/#{published_answer.id}/edit"
  145. end
  146. it 'shows a linked ticket' do
  147. within '.knowledge-base-sidebar .sidebar-linked-tickets' do
  148. expect(page).to have_text(ticket.title)
  149. end
  150. end
  151. it 'removes a linked ticket' do
  152. within '.knowledge-base-sidebar .sidebar-linked-tickets' do
  153. click '.js-delete'
  154. expect(page).to have_no_text(ticket.title)
  155. end
  156. expect(Link.list(link_object: 'Ticket', link_object_value: ticket.id)).to be_blank
  157. end
  158. context 'when agent has no acess to ticket', authenticated_as: -> { create(:admin) } do
  159. it 'does not show a linked ticket' do
  160. within '.knowledge-base-sidebar .sidebar-linked-tickets' do
  161. expect(page).to have_no_text(ticket.title)
  162. end
  163. end
  164. end
  165. end
  166. end
  167. context 'deleted by another user' do
  168. before do
  169. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{published_answer.id}/edit"
  170. end
  171. it 'shows not available', performs_jobs: true do
  172. find(:active_content, text: published_answer.translations.first.title)
  173. perform_enqueued_jobs do
  174. ActiveRecord::Base.transaction do
  175. published_answer.destroy
  176. end
  177. end
  178. within :active_content do
  179. expect(page).to have_text('The page is not available anymore')
  180. end
  181. end
  182. end
  183. context 'updated by another user' do
  184. before do
  185. ensure_websocket do
  186. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{published_answer.id}/edit"
  187. end
  188. travel 1.minute
  189. end
  190. it 'shows new content', performs_jobs: true do
  191. find(:active_content, text: published_answer.translations.first.title)
  192. accept_prompt do
  193. perform_enqueued_jobs do
  194. Transaction.execute do
  195. published_answer.translations.first.update! title: 'new title'
  196. end
  197. end
  198. end
  199. within :active_content do
  200. expect(page).to have_text('new title')
  201. end
  202. end
  203. end
  204. describe 'previewing' do
  205. before do
  206. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{draft_answer.id}/edit"
  207. end
  208. it 'opens preview' do
  209. new_window = window_opened_by { click '.icon-external' }
  210. within_window new_window do
  211. within '.main--article' do
  212. expect(page).to have_text(draft_answer.translations.first.title)
  213. end
  214. end
  215. end
  216. it 'creates a KB preview token' do
  217. expect { click('.icon-external') }
  218. .to change { Token.exists?(action: 'KnowledgeBasePreview') }
  219. .to(true)
  220. end
  221. end
  222. end