text_modules_examples.rb 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'text modules' do |path:, ticket: nil|
  3. let!(:agent_fixed_name) { create(:agent, firstname: 'FFFF1', lastname: 'GGGG1', groups: [Group.find_by(name: 'Users')]) }
  4. let!(:group1) { create(:group) }
  5. let!(:group2) { create(:group) }
  6. let!(:text_module_without_group1) { create(:text_module, name: 'aaa', keywords: "test dummy #{Faker::Superhero.prefix}") }
  7. let!(:text_module_without_group2) { create(:text_module, name: 'bbb', keywords: "test dummy #{Faker::Superhero.prefix}") }
  8. let!(:text_module_group1) { create(:text_module, name: 'ccc', keywords: "test dummy #{Faker::Superhero.prefix}", groups: [group1]) }
  9. let!(:text_module_group2) { create(:text_module, name: 'ddd', keywords: "test dummy #{Faker::Superhero.prefix}", groups: [group2]) }
  10. it 'shows when send ::' do
  11. refresh # workaround to get new created objects from db
  12. visit path
  13. within(:active_content) do
  14. set_tree_select_value('group_id', Group.first.name)
  15. find(:richtext).send_keys(':')
  16. find(:richtext).send_keys(':')
  17. expect(page).to have_selector(:text_module, text_module_without_group1.id)
  18. end
  19. end
  20. it 'does not break after usage of Ctrl/Command+Backspace' do
  21. visit path
  22. within(:active_content) do
  23. find(:richtext).send_keys(':')
  24. find(:richtext).send_keys(':')
  25. find(:richtext).send_keys('bur')
  26. # The click is needed to get the focus back to the field for chrome.
  27. find(:richtext).click
  28. find(:richtext).send_keys([magic_key, :backspace])
  29. find(:richtext).send_keys('Some other text')
  30. find(:richtext).send_keys(:enter)
  31. expect(find(:richtext)).to have_text 'Some other text'
  32. end
  33. end
  34. it 'does not show when send :enter:' do
  35. visit path
  36. within(:active_content) do
  37. set_tree_select_value('group_id', Group.first.name)
  38. find(:richtext).send_keys(':')
  39. find(:richtext).send_keys(:enter)
  40. find(:richtext).send_keys(':')
  41. expect(page).to have_no_selector(:text_module, text_module_without_group1.id)
  42. expect(page).to have_no_selector(:text_module, text_module_without_group2.id)
  43. end
  44. end
  45. it 'does not break search on backspace' do
  46. visit path
  47. within(:active_content) do
  48. set_tree_select_value('group_id', Group.first.name)
  49. find(:richtext).send_keys('@@agen')
  50. find(:richtext).send_keys(:backspace)
  51. expect(page).to have_no_text('No results found')
  52. end
  53. end
  54. it 'does delete empty mentions (issue #3636 / FF only)' do
  55. visit path
  56. within(:active_content) do
  57. set_tree_select_value('group_id', Group.first.name)
  58. find(:richtext).send_keys('@@FFFF1')
  59. await_empty_ajax_queue
  60. find(:richtext).send_keys(:enter)
  61. find(:richtext).send_keys(:enter)
  62. (agent_fixed_name.firstname.length + agent_fixed_name.lastname.length + 2).times do
  63. find(:richtext).send_keys(:backspace)
  64. end
  65. expect(find(:richtext)).to have_no_css('a[data-mention-user-id]', visible: :all)
  66. end
  67. end
  68. it 'does delete empty mentions (issue #3636 / simulation)' do
  69. visit path
  70. within(:active_content) do
  71. set_tree_select_value('group_id', Group.first.name)
  72. find(:richtext).send_keys('@@FFFF1')
  73. await_empty_ajax_queue
  74. find(:richtext).send_keys(:enter)
  75. find(:richtext).send_keys(:enter)
  76. find(:richtext).send_keys('test')
  77. page.execute_script("$('a[data-mention-user-id]').first().html('<br>')")
  78. find(:richtext).send_keys(:backspace)
  79. expect(find(:richtext).all('a[data-mention-user-id]', visible: :all).count).to eq(0)
  80. end
  81. end
  82. it 'does not delete parts of the text on multiple mentions (issue #3717)' do
  83. visit path
  84. within(:active_content) do
  85. set_tree_select_value('group_id', Group.first.name)
  86. find(:richtext).send_keys('Testing Testy')
  87. find(:richtext).send_keys('@@FFFF1')
  88. await_empty_ajax_queue
  89. find(:richtext).send_keys(:enter)
  90. await_empty_ajax_queue
  91. find(:richtext).send_keys(:enter)
  92. find(:richtext).send_keys('Testing Testy ')
  93. find(:richtext).send_keys('@@FFFF1')
  94. await_empty_ajax_queue
  95. find(:richtext).send_keys(:enter)
  96. await_empty_ajax_queue
  97. expect(find(:richtext).text).to include('Testing TestyFFFF1 GGGG1')
  98. expect(find(:richtext).text).to include('Testing Testy FFFF1 GGGG1')
  99. end
  100. end
  101. it 'does not delete line breaks of text with mentions (issue #3717)' do
  102. visit path
  103. within(:active_content) do
  104. set_tree_select_value('group_id', Group.first.name)
  105. find(:richtext).send_keys('@@FFFF1')
  106. await_empty_ajax_queue
  107. find(:richtext).send_keys(:enter)
  108. find(:richtext).send_keys(' Testing Testy')
  109. find(:richtext).send_keys(:enter)
  110. find(:richtext).send_keys(:enter)
  111. find(:richtext).send_keys(:backspace)
  112. find(:richtext).send_keys('@@FFFF1')
  113. await_empty_ajax_queue
  114. find(:richtext).send_keys(:enter)
  115. expect(find(:richtext).text).to include("FFFF1 GGGG1 Testing Testy\nFFFF1 GGGG1")
  116. end
  117. end
  118. it 'supports group-dependent text modules' do
  119. visit '/'
  120. # give user access to all groups including those created
  121. # by using FactoryBot outside of the example
  122. group_names_access_map = Group.pluck(:name).index_with do |_group_name|
  123. 'full'.freeze
  124. end
  125. current_user do |user|
  126. user.group_names_access_map = group_names_access_map
  127. user.save!
  128. end
  129. refresh # workaround to get changed settings from db
  130. visit path
  131. within(:active_content) do
  132. set_tree_select_value('group_id', group1.name)
  133. find(:richtext).send_keys('::')
  134. expect(page).to have_selector(:text_module, text_module_without_group1.id)
  135. expect(page).to have_selector(:text_module, text_module_without_group2.id)
  136. expect(page).to have_selector(:text_module, text_module_group1.id)
  137. expect(page).to have_no_selector(:text_module, text_module_group2.id)
  138. set_tree_select_value('group_id', group2.name)
  139. find(:richtext).send_keys('::')
  140. expect(page).to have_selector(:text_module, text_module_without_group1.id)
  141. expect(page).to have_selector(:text_module, text_module_without_group2.id)
  142. expect(page).to have_no_selector(:text_module, text_module_group1.id)
  143. expect(page).to have_selector(:text_module, text_module_group2.id)
  144. end
  145. end
  146. it 'orders text modules by alphabet' do
  147. refresh # workaround to get changed settings from db
  148. visit path
  149. within(:active_content) do
  150. find(:richtext).send_keys('::')
  151. find(:richtext).send_keys('dummy')
  152. find('.text-modules-box')
  153. expected_order = [
  154. "#{text_module_without_group2.name}\n#{text_module_without_group2.keywords}",
  155. "#{text_module_without_group1.name}\n#{text_module_without_group1.keywords}",
  156. ]
  157. if path == 'ticket/create'
  158. expected_order = [
  159. "#{text_module_group2.name}\n#{text_module_group2.keywords}",
  160. "#{text_module_group1.name}\n#{text_module_group1.keywords}",
  161. "#{text_module_without_group2.name}\n#{text_module_without_group2.keywords}",
  162. "#{text_module_without_group1.name}\n#{text_module_without_group1.keywords}",
  163. ]
  164. end
  165. shown_text_modules = find_all(:css, '.text-modules-box li')
  166. expect(shown_text_modules.length).to eq(expected_order.length)
  167. shown_text_modules_text = shown_text_modules.map(&:text)
  168. expect(shown_text_modules_text).to eq(expected_order)
  169. end
  170. end
  171. context 'when text module refers external data source', authenticated_as: :authenticate, db_strategy: :reset do
  172. let(:custom_attribute) { create(:object_manager_attribute_autocompletion_ajax_external_data_source) }
  173. let(:text_module_external) do
  174. create(:text_module,
  175. name: 'external data source',
  176. content: "external \#{ticket.#{custom_attribute.name}.value}")
  177. end
  178. def authenticate
  179. text_module_external
  180. custom_attribute
  181. ObjectManager::Attribute.migration_execute
  182. true
  183. end
  184. context 'when ticket external field value is not set' do
  185. it 'inserts text module with placeholder' do
  186. visit path
  187. within(:active_content) do
  188. find(:richtext).send_keys('::ext')
  189. page.send_keys(:enter)
  190. expect(find(:richtext)).to have_text 'external -'
  191. end
  192. end
  193. end
  194. if path.starts_with? '#ticket/zoom'
  195. context 'when ticket external field value is set' do
  196. before do
  197. ticket.reload[custom_attribute.name] = { value: 'aaa', label: 'AAA' }
  198. ticket.save!
  199. end
  200. it 'inserts text module with external value' do
  201. visit path
  202. within(:active_content) do
  203. find(:richtext).send_keys('::ext')
  204. page.send_keys(:enter)
  205. expect(find(:richtext)).to have_text 'external AAA'
  206. end
  207. end
  208. end
  209. end
  210. end
  211. context 'when Group restriction for text modules not working on ticket creation (specific scenario only) #4358', authenticated_as: :authenticate do
  212. let(:agent) { create(:agent, groups: [group1]) }
  213. def authenticate
  214. agent
  215. end
  216. it 'does show group related text modules when the group is hidden' do
  217. ticket&.update(group: group1)
  218. visit path
  219. within(:active_content) do
  220. expect(page).to have_css('[data-attribute-name=group_id].is-hidden', visible: :all)
  221. find(:richtext).send_keys(':')
  222. find(:richtext).send_keys(':')
  223. expect(page).to have_selector(:text_module, text_module_group1.id)
  224. end
  225. end
  226. end
  227. end