text_modules_examples.rb 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'text modules' do |path:|
  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. find('select[name="group_id"]').select(1)
  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. find('select[name="group_id"]').select(1)
  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. find('select[name="group_id"]').select(1)
  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. find('select[name="group_id"]').select('Users')
  58. find(:richtext).send_keys('@@FFFF1')
  59. find(:richtext).send_keys(:enter)
  60. find(:richtext).send_keys(:enter)
  61. (agent_fixed_name.firstname.length + agent_fixed_name.lastname.length + 2).times do
  62. find(:richtext).send_keys(:backspace)
  63. end
  64. expect(find(:richtext).all('a[data-mention-user-id]', visible: :all).count).to eq(0)
  65. end
  66. end
  67. it 'does delete empty mentions (issue #3636 / simulation)' do
  68. visit path
  69. within(:active_content) do
  70. find('select[name="group_id"]').select('Users')
  71. find(:richtext).send_keys('@@FFFF1')
  72. find(:richtext).send_keys(:enter)
  73. find(:richtext).send_keys(:enter)
  74. find(:richtext).send_keys('test')
  75. page.execute_script("$('a[data-mention-user-id]').first().html('<br>')")
  76. find(:richtext).send_keys(:backspace)
  77. expect(find(:richtext).all('a[data-mention-user-id]', visible: :all).count).to eq(0)
  78. end
  79. end
  80. it 'does not delete parts of the text on multiple mentions (issue #3717)' do
  81. visit path
  82. within(:active_content) do
  83. find('select[name="group_id"]').select('Users')
  84. find(:richtext).send_keys('Testing Testy')
  85. find(:richtext).send_keys('@@FFFF1')
  86. find(:richtext).send_keys(:enter)
  87. find(:richtext).send_keys(:enter)
  88. find(:richtext).send_keys('Testing Testy ')
  89. find(:richtext).send_keys('@@FFFF1')
  90. find(:richtext).send_keys(:enter)
  91. expect(find(:richtext).text).to include('Testing TestyFFFF1 GGGG1')
  92. expect(find(:richtext).text).to include('Testing Testy FFFF1 GGGG1')
  93. end
  94. end
  95. it 'does not delete line breaks of text with mentions (issue #3717)' do
  96. visit path
  97. within(:active_content) do
  98. find('select[name="group_id"]').select('Users')
  99. find(:richtext).send_keys('@@FFFF1')
  100. find(:richtext).send_keys(:enter)
  101. find(:richtext).send_keys(' Testing Testy')
  102. find(:richtext).send_keys(:enter)
  103. find(:richtext).send_keys(:enter)
  104. find(:richtext).send_keys(:backspace)
  105. find(:richtext).send_keys('@@FFFF1')
  106. find(:richtext).send_keys(:enter)
  107. expect(find(:richtext).text).to include("FFFF1 GGGG1 Testing Testy\nFFFF1 GGGG1")
  108. end
  109. end
  110. it 'supports group-dependent text modules' do
  111. visit '/'
  112. # give user access to all groups including those created
  113. # by using FactoryBot outside of the example
  114. group_names_access_map = Group.all.pluck(:name).index_with do |_group_name|
  115. 'full'.freeze
  116. end
  117. current_user do |user|
  118. user.group_names_access_map = group_names_access_map
  119. user.save!
  120. end
  121. refresh # workaround to get changed settings from db
  122. visit path
  123. within(:active_content) do
  124. find('select[name="group_id"]').select(group1.name)
  125. find(:richtext).send_keys('::')
  126. expect(page).to have_selector(:text_module, text_module_without_group1.id)
  127. expect(page).to have_selector(:text_module, text_module_without_group2.id)
  128. expect(page).to have_selector(:text_module, text_module_group1.id)
  129. expect(page).to have_no_selector(:text_module, text_module_group2.id)
  130. find('select[name="group_id"]').select(group2.name)
  131. find(:richtext).send_keys('::')
  132. expect(page).to have_selector(:text_module, text_module_without_group1.id)
  133. expect(page).to have_selector(:text_module, text_module_without_group2.id)
  134. expect(page).to have_no_selector(:text_module, text_module_group1.id)
  135. expect(page).to have_selector(:text_module, text_module_group2.id)
  136. end
  137. end
  138. it 'orders text modules by alphabet' do
  139. refresh # workaround to get changed settings from db
  140. visit path
  141. within(:active_content) do
  142. find(:richtext).send_keys('::')
  143. find(:richtext).send_keys('dummy')
  144. find('.text-modules-box')
  145. expected_order = [
  146. "#{text_module_without_group2.name}\n#{text_module_without_group2.keywords}",
  147. "#{text_module_without_group1.name}\n#{text_module_without_group1.keywords}",
  148. ]
  149. if path == 'ticket/create'
  150. expected_order = [
  151. "#{text_module_group2.name}\n#{text_module_group2.keywords}",
  152. "#{text_module_group1.name}\n#{text_module_group1.keywords}",
  153. "#{text_module_without_group2.name}\n#{text_module_without_group2.keywords}",
  154. "#{text_module_without_group1.name}\n#{text_module_without_group1.keywords}",
  155. ]
  156. end
  157. shown_text_modules = find_all(:css, '.text-modules-box li')
  158. expect(shown_text_modules.length).to eq(expected_order.length)
  159. shown_text_modules_text = shown_text_modules.map(&:text)
  160. expect(shown_text_modules_text).to eq(expected_order)
  161. end
  162. end
  163. end