search_spec.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Search', type: :system, authenticated: true, searchindex: true do
  4. let(:group_1) { create :group }
  5. let(:group_2) { create :group }
  6. let(:macro_without_group) { create :macro }
  7. let(:macro_note) { create :macro, name: 'Macro note', perform: { 'article.note'=>{ 'body' => 'macro body', 'internal' => 'true', 'subject' => 'macro note' } } }
  8. let(:macro_group1) { create :macro, groups: [group_1] }
  9. let(:macro_group2) { create :macro, groups: [group_2] }
  10. let(:ticket_1) { create :ticket, title: 'Testing Ticket 1', group: group_1 }
  11. let(:ticket_2) { create :ticket, title: 'Testing Ticket 2', group: group_2 }
  12. let(:note) { 'Test note' }
  13. before do
  14. ticket_1 && ticket_2 && configure_elasticsearch(required: true, rebuild: true)
  15. end
  16. it 'shows default widgets' do
  17. fill_in id: 'global-search', with: '"Welcome"'
  18. click_on 'Show Search Details'
  19. within '#navigation .tasks a[data-key=Search]' do
  20. expect(page).to have_content '"Welcome"'
  21. end
  22. end
  23. context 'with ticket search result', authenticated_as: :authenticate do
  24. let(:agent) { create(:agent, groups: Group.all) }
  25. def authenticate
  26. ticket_1 && ticket_2
  27. agent
  28. end
  29. before do
  30. fill_in id: 'global-search', with: 'Testing'
  31. click_on 'Show Search Details'
  32. find('[data-tab-content=Ticket]').click
  33. end
  34. context 'checkbox' do
  35. it 'has checkbox for each ticket records' do
  36. within '.detail-search table.table' do
  37. expect(page).to have_xpath(".//td[contains(@class, 'js-checkbox-field')]//input[@type='checkbox']", visible: :all, minimum: 2)
  38. end
  39. end
  40. it 'has select all checkbox' do
  41. within '.detail-search table.table' do
  42. expect(page).to have_xpath(".//th//input[@type='checkbox' and @name='bulk_all']", visible: :all, count: 1)
  43. end
  44. end
  45. it 'shows bulkform when checkbox is checked' do
  46. within '.detail-search table.table' do
  47. find("tr[data-id='#{ticket_1.id}']").check('bulk', allow_label_click: true)
  48. end
  49. expect(page).to have_selector('.bulkAction.no-sidebar')
  50. expect(page).to have_no_selector('.bulkAction.no-sidebar.hide', visible: :all)
  51. end
  52. it 'shows bulkform when all checkbox is checked' do
  53. within '.detail-search table.table' do
  54. find('th.table-checkbox').check('bulk_all', allow_label_click: true)
  55. end
  56. expect(page).to have_selector('.bulkAction.no-sidebar')
  57. expect(page).to have_no_selector('.bulkAction.no-sidebar.hide', visible: :all)
  58. end
  59. it 'hides bulkform when checkbox is unchecked' do
  60. within '.detail-search table.table' do
  61. find('th.table-checkbox').check('bulk_all', allow_label_click: true)
  62. all('.js-tableBody tr.item').each { |row| row.uncheck('bulk', allow_label_click: true) }
  63. end
  64. expect(page).to have_selector('.bulkAction.no-sidebar.hide', visible: :hide)
  65. end
  66. end
  67. context 'with bulkform activated' do
  68. before do
  69. find('th.table-checkbox').check('bulk_all', allow_label_click: true)
  70. end
  71. it 'has group label' do
  72. within '.bulkAction .bulkAction-form' do
  73. expect(page).to have_content 'GROUP'
  74. end
  75. end
  76. it 'has owner label' do
  77. within '.bulkAction .bulkAction-form' do
  78. expect(page).to have_content 'OWNER'
  79. end
  80. end
  81. it 'has state label' do
  82. within '.bulkAction .bulkAction-form' do
  83. expect(page).to have_content 'STATE'
  84. end
  85. end
  86. it 'has priority label' do
  87. within '.bulkAction .bulkAction-form' do
  88. expect(page).to have_content 'PRIORITY'
  89. end
  90. end
  91. end
  92. context 'bulk note' do
  93. it 'adds note to selected ticket' do
  94. within :active_content do
  95. find("tr[data-id='#{ticket_1.id}']").check('bulk', allow_label_click: true)
  96. click '.js-confirm'
  97. find('.js-confirm-step textarea').fill_in with: note
  98. click '.js-submit'
  99. end
  100. expect do
  101. wait.until { ticket_1.articles.last&.body == note }
  102. end.not_to raise_error
  103. end
  104. end
  105. context 'with drag and drop' do
  106. context 'when checked tickets are dragged' do
  107. it 'shows the batch actions' do
  108. within(:active_content, '.main .table') do
  109. # get element to move
  110. element = page.find(:table_row, ticket_1.id).native
  111. click_and_hold(element)
  112. # move element a bit to display batch actions
  113. move_mouse_by(0, 5)
  114. # move mouse again to trigger the event for chrome
  115. move_mouse_by(0, 7)
  116. end
  117. expect(page).to have_selector('.batch-overlay-circle--top.js-batch-macro-circle')
  118. .and(have_selector('.batch-overlay-circle--bottom.js-batch-assign-circle'))
  119. end
  120. end
  121. end
  122. end
  123. context 'with ticket search result for macros bulk action', authenticated_as: :authenticate do
  124. let(:group_3) { create :group }
  125. let(:search_query) { 'Testing' }
  126. let(:ticket_3) { create :ticket, title: 'Testing Ticket 3', group: group_3 }
  127. let(:agent) { create(:agent, groups: Group.all) }
  128. before do
  129. fill_in id: 'global-search', with: search_query
  130. click_on 'Show Search Details'
  131. find('[data-tab-content=Ticket]').click
  132. end
  133. describe 'group-dependent macros' do
  134. def authenticate
  135. ticket_1 && ticket_2 && ticket_3
  136. macro_without_group && macro_group1 && macro_group2
  137. agent
  138. end
  139. it 'shows only non-group macro when ticket does not match any group macros' do
  140. within(:active_content) do
  141. display_macro_batches ticket_3
  142. expect(page).to have_selector(:macro_batch, macro_without_group.id)
  143. .and(have_no_selector(:macro_batch, macro_group1.id))
  144. .and(have_no_selector(:macro_batch, macro_group2.id))
  145. end
  146. end
  147. it 'shows non-group and matching group macros for matching ticket' do
  148. within(:active_content) do
  149. display_macro_batches ticket_1
  150. expect(page).to have_selector(:macro_batch, macro_without_group.id)
  151. .and(have_selector(:macro_batch, macro_group1.id))
  152. .and(have_no_selector(:macro_batch, macro_group2.id))
  153. end
  154. end
  155. end
  156. context 'with macro batch overlay' do
  157. shared_examples "adding 'small' class to macro element" do
  158. it 'adds a "small" class to the macro element' do
  159. within(:active_content) do
  160. display_macro_batches ticket_1
  161. expect(page).to have_selector('.batch-overlay-macro-entry.small')
  162. end
  163. end
  164. end
  165. shared_examples "not adding 'small' class to macro element" do
  166. it 'does not add a "small" class to the macro element' do
  167. within(:active_content) do
  168. display_macro_batches ticket_1
  169. expect(page).to have_no_selector('.batch-overlay-macro-entry.small')
  170. end
  171. end
  172. end
  173. shared_examples 'showing all macros' do
  174. it 'shows all macros' do
  175. within(:active_content) do
  176. display_macro_batches ticket_1
  177. expect(page).to have_selector('.batch-overlay-macro-entry', count: all)
  178. end
  179. end
  180. end
  181. shared_examples 'showing some macros' do |count|
  182. it 'shows all macros' do
  183. within(:active_content) do
  184. display_macro_batches ticket_1
  185. expect(page).to have_selector('.batch-overlay-macro-entry', count: count)
  186. end
  187. end
  188. end
  189. def authenticate
  190. ticket_1 && ticket_2
  191. Macro.destroy_all && (create_list :macro, all)
  192. agent
  193. end
  194. context 'with few macros' do
  195. let(:all) { 15 }
  196. context 'when on large screen', screen_size: :desktop do
  197. it_behaves_like 'showing all macros'
  198. it_behaves_like "not adding 'small' class to macro element"
  199. end
  200. context 'when on small screen', screen_size: :tablet do
  201. it_behaves_like 'showing all macros'
  202. it_behaves_like "not adding 'small' class to macro element"
  203. end
  204. end
  205. context 'with many macros' do
  206. let(:all) { 50 }
  207. context 'when on large screen', screen_size: :desktop do
  208. it_behaves_like 'showing some macros', 32
  209. end
  210. context 'when on small screen', screen_size: :tablet do
  211. it_behaves_like 'showing some macros', 24
  212. it_behaves_like "adding 'small' class to macro element"
  213. end
  214. end
  215. end
  216. end
  217. context 'Organization members', authenticated_as: :authenticate do
  218. let(:organization) { create(:organization) }
  219. let(:members) { organization.members.order(id: :asc) }
  220. def authenticate
  221. create_list(:customer, 50, organization: organization)
  222. true
  223. end
  224. before do
  225. fill_in id: 'global-search', with: organization.name.to_s
  226. end
  227. it 'shows only first 10 members' do
  228. expect(page).to have_text(organization.name)
  229. popover_on_hover(first('a.nav-tab.organization'))
  230. expect(page).to have_text(members[9].fullname, wait: 30)
  231. expect(page).to have_no_text(members[10].fullname)
  232. end
  233. end
  234. context 'inactive user and organizations' do
  235. before do
  236. create(:organization, name: 'Example Inc.', active: true)
  237. create(:organization, name: 'Example Inactive Inc.', active: false)
  238. create(:customer, firstname: 'Firstname', lastname: 'Active', active: true)
  239. create(:customer, firstname: 'Firstname', lastname: 'Inactive', active: false)
  240. configure_elasticsearch(rebuild: true)
  241. end
  242. it 'check that inactive organizations are marked correctly' do
  243. fill_in id: 'global-search', with: '"Example"'
  244. expect(page).to have_css('.nav-tab--search.organization', minimum: 2)
  245. expect(page).to have_css('.nav-tab--search.organization.is-inactive', count: 1)
  246. end
  247. it 'check that inactive users are marked correctly' do
  248. fill_in id: 'global-search', with: '"Firstname"'
  249. expect(page).to have_css('.nav-tab--search.user', minimum: 2)
  250. expect(page).to have_css('.nav-tab--search.user.is-inactive', count: 1)
  251. end
  252. it 'check that inactive users are also marked in the popover for the quick search result' do
  253. fill_in id: 'global-search', with: '"Firstname"'
  254. popover_on_hover(find('.nav-tab--search.user.is-inactive'))
  255. expect(page).to have_css('.popover-title .is-inactive', count: 1)
  256. end
  257. end
  258. describe 'Search is not triggered/updated if url of search is updated new search item or new search is triggered via global search #3873', authenticated_as: :authenticate do
  259. let(:agent) { create(:agent, groups: Group.all) }
  260. def authenticate
  261. ticket_1 && ticket_2
  262. agent
  263. end
  264. context 'when search changed via input box' do
  265. before do
  266. visit '#search'
  267. end
  268. it 'does switch search results properly' do
  269. page.find('.js-search').fill_in(with: '"Testing Ticket 1"', fill_options: { clear: :backspace })
  270. expect(page.find('.js-tableBody')).to have_text('Testing Ticket 1')
  271. expect(page.find('.js-tableBody')).to have_no_text('Testing Ticket 2')
  272. expect(current_url).to include('Testing%20Ticket%201')
  273. # switch by global search
  274. page.find('.js-search').fill_in(with: '"Testing Ticket 2"', fill_options: { clear: :backspace })
  275. expect(page.find('.js-tableBody')).to have_text('Testing Ticket 2')
  276. expect(page.find('.js-tableBody')).to have_no_text('Testing Ticket 1')
  277. expect(current_url).to include('Testing%20Ticket%202')
  278. end
  279. end
  280. context 'when search changed via global search' do
  281. before do
  282. fill_in id: 'global-search', with: '"Testing Ticket 1"'
  283. click_on 'Show Search Details'
  284. end
  285. it 'does switch search results properly' do
  286. expect(page.find('.js-tableBody')).to have_text('Testing Ticket 1')
  287. expect(page.find('.js-tableBody')).to have_no_text('Testing Ticket 2')
  288. expect(current_url).to include('Testing%20Ticket%201')
  289. # switch by global search
  290. fill_in id: 'global-search', with: '"Testing Ticket 2"'
  291. click_on 'Show Search Details'
  292. expect(page.find('.js-tableBody')).to have_text('Testing Ticket 2')
  293. expect(page.find('.js-tableBody')).to have_no_text('Testing Ticket 1')
  294. expect(current_url).to include('Testing%20Ticket%202')
  295. end
  296. end
  297. context 'when search is changed via url' do
  298. before do
  299. visit '#search/"Testing Ticket 1"'
  300. end
  301. it 'does switch search results properly' do
  302. expect(page.find('.js-tableBody')).to have_text('Testing Ticket 1')
  303. expect(page.find('.js-tableBody')).to have_no_text('Testing Ticket 2')
  304. expect(current_url).to include('Testing%20Ticket%201')
  305. # switch by url
  306. visit '#search/"Testing Ticket 2"'
  307. expect(page.find('.js-tableBody')).to have_text('Testing Ticket 2')
  308. expect(page.find('.js-tableBody')).to have_no_text('Testing Ticket 1')
  309. expect(current_url).to include('Testing%20Ticket%202')
  310. end
  311. end
  312. end
  313. context 'Assign user to multiple organizations #1573', authenticated_as: :authenticate do
  314. let(:organizations) { create_list(:organization, 20) }
  315. let(:customer) { create(:customer, organization: organizations[0], organizations: organizations[1..]) }
  316. context 'when agent' do
  317. def authenticate
  318. customer
  319. true
  320. end
  321. before do
  322. fill_in id: 'global-search', with: customer.firstname.to_s
  323. end
  324. it 'shows only first 3 organizations' do
  325. expect(page).to have_text(customer.firstname)
  326. popover_on_hover(first('a.nav-tab.user'))
  327. within '.popover' do
  328. expect(page).to have_text(organizations[2].name, wait: 30)
  329. expect(page).to have_no_text(organizations[10].name)
  330. end
  331. end
  332. end
  333. context 'when customer', authenticated_as: :customer do
  334. before do
  335. fill_in id: 'global-search', with: organizations[0].name.to_s
  336. end
  337. it 'does not show any organizations in global search because only agents have access to it' do
  338. within '.global-search-result' do
  339. expect(page).to have_no_text(organizations[0].name)
  340. end
  341. end
  342. end
  343. end
  344. describe 'Searches display all groups and owners on bulk selections #4054', authenticated_as: :authenticate do
  345. let(:group_1) { create(:group) }
  346. let(:group_2) { create(:group) }
  347. let(:agent_1) { create(:agent, groups: [group_1]) }
  348. let(:agent_2) { create(:agent, groups: [group_2]) }
  349. let(:agent_all) { create(:agent, groups: [group_1, group_2]) }
  350. let(:ticket_1) { create(:ticket, group: group_1, title: '4054 group 1') }
  351. let(:ticket_2) { create(:ticket, group: group_2, title: '4054 group 2') }
  352. def authenticate
  353. agent_1 && agent_2 && agent_all
  354. ticket_1 && ticket_2
  355. agent_all
  356. end
  357. def check_owner_empty
  358. expect(page).to have_select('owner_id', text: '-', visible: :all)
  359. expect(page).to have_no_select('owner_id', text: agent_1.fullname, visible: :all)
  360. expect(page).to have_no_select('owner_id', text: agent_2.fullname, visible: :all)
  361. end
  362. def click_ticket(ticket)
  363. page.find(".js-tableBody tr.item[data-id='#{ticket.id}'] td.js-checkbox-field").click
  364. end
  365. def check_owner_agent1_shown
  366. expect(page).to have_select('owner_id', text: agent_1.fullname)
  367. expect(page).to have_no_select('owner_id', text: agent_2.fullname)
  368. end
  369. def check_owner_agent2_shown
  370. expect(page).to have_no_select('owner_id', text: agent_1.fullname)
  371. expect(page).to have_select('owner_id', text: agent_2.fullname)
  372. end
  373. def check_owner_field
  374. check_owner_empty
  375. click_ticket(ticket_1)
  376. check_owner_agent1_shown
  377. click_ticket(ticket_1)
  378. click_ticket(ticket_2)
  379. check_owner_agent2_shown
  380. end
  381. context 'when search is used' do
  382. before do
  383. visit '#search/4054'
  384. end
  385. it 'does show the correct owner selection for each bulk action' do
  386. check_owner_field
  387. end
  388. end
  389. context 'when ticket overview is used' do
  390. before do
  391. visit '#ticket/view/all_unassigned'
  392. end
  393. it 'does show the correct owner selection for each bulk action' do
  394. check_owner_field
  395. end
  396. end
  397. end
  398. end