edit_spec.rb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 'add link to another KB answer' do
  66. def open_editor_and_add_link(input)
  67. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{draft_answer.id}/edit"
  68. find('a[data-action="link_answer"]').click
  69. within('.popover-content') do
  70. find('input').fill_in with: input
  71. first('.js-option span', text: input).click
  72. find('[type=submit]').click
  73. end
  74. end
  75. before do
  76. published_answer
  77. end
  78. it 'adds a link to an answer' do
  79. open_editor_and_add_link published_answer.translations.first.title
  80. expect(page).to have_link(href: "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{published_answer.id}/edit")
  81. end
  82. end
  83. context 'embedded video' do
  84. it 'has adding functionality' do
  85. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{published_answer.id}/edit"
  86. find('a[data-action="embed_video"]').click
  87. within('.popover-content') do
  88. find('input').fill_in with: 'https://www.youtube.com/watch?v=vTTzwJsHpU8'
  89. find('[type=submit]').click
  90. end
  91. within('.richtext-content') do
  92. expect(page).to have_text('( widget: video, provider: youtube, id: vTTzwJsHpU8 )')
  93. end
  94. end
  95. it 'loads stored' do
  96. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{published_answer_with_video.id}"
  97. iframe = find('iframe')
  98. expect(iframe['src']).to start_with('https://www.youtube.com/embed/')
  99. end
  100. end
  101. context 'tags' do
  102. before do
  103. visit "#knowledge_base/#{knowledge_base.id}/locale/#{locale_name}/answer/#{published_answer_with_tag.id}/edit"
  104. end
  105. let(:new_tag_name) { 'capybara_kb_tag' }
  106. it 'adds a new tag' do
  107. within :active_content do
  108. click '.js-newTagLabel'
  109. elem = find('.js-newTagInput')
  110. elem.fill_in with: new_tag_name
  111. elem.send_keys :return
  112. wait.until_exists { published_answer_with_tag.reload.tag_list.include? new_tag_name }
  113. expect(page).to have_link(new_tag_name, href: false, class: ['js-tag'])
  114. end
  115. end
  116. it 'triggers autocomplete after one character' do
  117. within :active_content do
  118. click '.js-newTagLabel'
  119. elem = find('.js-newTagInput')
  120. elem.fill_in with: 'e'
  121. expect(page).to have_css('ul.ui-autocomplete > li.ui-menu-item', minimum: 1)
  122. end
  123. end
  124. it 'shows an existing tag' do
  125. within :active_content do
  126. expect(page).to have_link(published_answer_tag_name, href: false, class: ['js-tag'])
  127. end
  128. end
  129. it 'deletes a tag' do
  130. within :active_content do
  131. find('.list-item', text: published_answer_tag_name)
  132. .find('.js-delete').click
  133. expect(page).to have_no_link(published_answer_tag_name, href: false, class: ['js-tag'])
  134. wait.until_exists { published_answer_with_tag.reload.tag_list.exclude? published_answer_tag_name }
  135. end
  136. end
  137. end
  138. describe 'linked tickets' do
  139. let(:ticket) { Ticket.first }
  140. it 'links a ticket' do
  141. visit "#knowledge_base/#{knowledge_base.id}/locale/#{locale_name}/answer/#{published_answer.id}/edit"
  142. within '.knowledge-base-sidebar .sidebar-linked-tickets' do
  143. click '.js-add'
  144. end
  145. in_modal do
  146. fill_in 'ticket_number', with: ticket.number
  147. click '.js-submit'
  148. end
  149. within '.knowledge-base-sidebar .sidebar-linked-tickets' do
  150. expect(page).to have_text(ticket.title)
  151. end
  152. added_link = Link.list(link_object: 'Ticket', link_object_value: ticket.id).last
  153. expect(added_link).to eq({
  154. 'link_object' => 'KnowledgeBase::Answer::Translation',
  155. 'link_object_value' => published_answer.translations.first.id,
  156. 'link_type' => 'normal',
  157. })
  158. end
  159. context 'when a linked ticket exists' do
  160. before do
  161. create(:link, from: ticket, to: published_answer.translations.first)
  162. visit "#knowledge_base/#{knowledge_base.id}/locale/#{locale_name}/answer/#{published_answer.id}/edit"
  163. end
  164. it 'shows a linked ticket' do
  165. within '.knowledge-base-sidebar .sidebar-linked-tickets' do
  166. expect(page).to have_text(ticket.title)
  167. end
  168. end
  169. it 'removes a linked ticket' do
  170. within '.knowledge-base-sidebar .sidebar-linked-tickets' do
  171. click '.js-delete'
  172. expect(page).to have_no_text(ticket.title)
  173. end
  174. expect(Link.list(link_object: 'Ticket', link_object_value: ticket.id)).to be_blank
  175. end
  176. context 'when agent has no acess to ticket', authenticated_as: -> { create(:admin) } do
  177. it 'does not show a linked ticket' do
  178. within '.knowledge-base-sidebar .sidebar-linked-tickets' do
  179. expect(page).to have_no_text(ticket.title)
  180. end
  181. end
  182. end
  183. end
  184. end
  185. context 'deleted by another user' do
  186. before do
  187. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{published_answer.id}/edit"
  188. end
  189. it 'shows not available', performs_jobs: true do
  190. find(:active_content, text: published_answer.translations.first.title)
  191. perform_enqueued_jobs do
  192. ActiveRecord::Base.transaction do
  193. published_answer.destroy
  194. end
  195. end
  196. within :active_content do
  197. expect(page).to have_text('The page is not available anymore')
  198. end
  199. end
  200. end
  201. context 'updated by another user' do
  202. before do
  203. ensure_websocket do
  204. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{published_answer.id}/edit"
  205. end
  206. travel 1.minute
  207. end
  208. it 'shows new content', performs_jobs: true do
  209. find(:active_content, text: published_answer.translations.first.title)
  210. accept_prompt do
  211. perform_enqueued_jobs do
  212. Transaction.execute do
  213. published_answer.translations.first.update! title: 'new title'
  214. end
  215. end
  216. end
  217. within :active_content do
  218. expect(page).to have_text('new title')
  219. end
  220. end
  221. end
  222. describe 'previewing' do
  223. before do
  224. visit "#knowledge_base/#{knowledge_base.id}/locale/#{primary_locale.system_locale.locale}/answer/#{draft_answer.id}/edit"
  225. end
  226. it 'opens preview' do
  227. new_window = window_opened_by { click '.icon-external' }
  228. within_window new_window do
  229. within '.main--article' do
  230. expect(page).to have_text(draft_answer.translations.first.title)
  231. end
  232. end
  233. end
  234. it 'creates a KB preview token' do
  235. expect { click('.icon-external') }
  236. .to change { Token.exists?(action: 'KnowledgeBasePreview') }
  237. .to(true)
  238. end
  239. end
  240. end