base_spec.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Ticket::Selector::Base, searchindex: true do
  4. let(:agent) { create(:agent, groups: [Group.first]) }
  5. let(:ticket_1) { create(:ticket, title: 'bli', group: Group.first) }
  6. let(:ticket_2) { create(:ticket, title: 'bla', group: Group.first) }
  7. let(:ticket_3) { create(:ticket, title: 'blub', group: Group.first) }
  8. before do
  9. Ticket.destroy_all
  10. ticket_1 && ticket_2 && ticket_3
  11. searchindex_model_reload([Ticket])
  12. end
  13. it 'does support AND conditions', :aggregate_failures do
  14. condition = {
  15. operator: 'AND',
  16. conditions: [
  17. {
  18. name: 'ticket.title',
  19. operator: 'contains',
  20. value: 'b',
  21. },
  22. {
  23. name: 'ticket.title',
  24. operator: 'contains',
  25. value: 'l',
  26. },
  27. {
  28. name: 'ticket.title',
  29. operator: 'contains',
  30. value: 'b',
  31. },
  32. ]
  33. }
  34. count, = Ticket.selectors(condition, { current_user: agent })
  35. expect(count).to eq(3)
  36. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  37. expect(result[:count]).to eq(3)
  38. end
  39. it 'does support NOT conditions', :aggregate_failures do
  40. condition = {
  41. operator: 'NOT',
  42. conditions: [
  43. {
  44. name: 'ticket.title',
  45. operator: 'contains',
  46. value: 'b',
  47. },
  48. {
  49. name: 'ticket.title',
  50. operator: 'contains',
  51. value: 'l',
  52. },
  53. {
  54. name: 'ticket.title',
  55. operator: 'contains',
  56. value: 'b',
  57. },
  58. ]
  59. }
  60. count, = Ticket.selectors(condition, { current_user: agent })
  61. expect(count).to eq(0)
  62. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  63. expect(result[:count]).to eq(0)
  64. end
  65. it 'does support OR conditions', :aggregate_failures do
  66. condition = {
  67. operator: 'OR',
  68. conditions: [
  69. {
  70. name: 'ticket.title',
  71. operator: 'is',
  72. value: 'bli',
  73. },
  74. {
  75. name: 'ticket.title',
  76. operator: 'is',
  77. value: 'bla',
  78. },
  79. {
  80. name: 'ticket.title',
  81. operator: 'is',
  82. value: 'blub',
  83. },
  84. ]
  85. }
  86. count, = Ticket.selectors(condition, { current_user: agent })
  87. expect(count).to eq(3)
  88. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  89. expect(result[:count]).to eq(3)
  90. end
  91. it 'does support OR conditions (one missing)', :aggregate_failures do
  92. condition = {
  93. operator: 'OR',
  94. conditions: [
  95. {
  96. name: 'ticket.title',
  97. operator: 'is',
  98. value: 'xxx',
  99. },
  100. {
  101. name: 'ticket.title',
  102. operator: 'is',
  103. value: 'bla',
  104. },
  105. {
  106. name: 'ticket.title',
  107. operator: 'is',
  108. value: 'blub',
  109. },
  110. ]
  111. }
  112. count, = Ticket.selectors(condition, { current_user: agent })
  113. expect(count).to eq(2)
  114. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  115. expect(result[:count]).to eq(2)
  116. end
  117. it 'does support OR conditions (all missing)', :aggregate_failures do
  118. condition = {
  119. operator: 'AND',
  120. conditions: [
  121. {
  122. name: 'ticket.title',
  123. operator: 'is',
  124. value: 'bli',
  125. },
  126. {
  127. name: 'ticket.title',
  128. operator: 'is',
  129. value: 'bla',
  130. },
  131. {
  132. name: 'ticket.title',
  133. operator: 'is',
  134. value: 'blub',
  135. },
  136. ]
  137. }
  138. count, = Ticket.selectors(condition, { current_user: agent })
  139. expect(count).to eq(0)
  140. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  141. expect(result[:count]).to eq(0)
  142. end
  143. it 'does support sub level conditions', :aggregate_failures do
  144. condition = {
  145. operator: 'OR',
  146. conditions: [
  147. {
  148. name: 'ticket.title',
  149. operator: 'is',
  150. value: 'bli',
  151. },
  152. {
  153. operator: 'OR',
  154. conditions: [
  155. {
  156. name: 'ticket.title',
  157. operator: 'is',
  158. value: 'bla',
  159. },
  160. {
  161. name: 'ticket.title',
  162. operator: 'is',
  163. value: 'blub',
  164. },
  165. ],
  166. }
  167. ]
  168. }
  169. count, = Ticket.selectors(condition, { current_user: agent })
  170. expect(count).to eq(3)
  171. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  172. expect(result[:count]).to eq(3)
  173. end
  174. it 'does support sub level conditions (one missing)', :aggregate_failures do
  175. condition = {
  176. operator: 'OR',
  177. conditions: [
  178. {
  179. name: 'ticket.title',
  180. operator: 'is',
  181. value: 'bli',
  182. },
  183. {
  184. operator: 'OR',
  185. conditions: [
  186. {
  187. name: 'ticket.title',
  188. operator: 'is',
  189. value: 'xxx',
  190. },
  191. {
  192. name: 'ticket.title',
  193. operator: 'is',
  194. value: 'blub',
  195. },
  196. ],
  197. }
  198. ]
  199. }
  200. count, = Ticket.selectors(condition, { current_user: agent })
  201. expect(count).to eq(2)
  202. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  203. expect(result[:count]).to eq(2)
  204. end
  205. it 'does support sub level conditions (all missing)', :aggregate_failures do
  206. condition = {
  207. operator: 'AND',
  208. conditions: [
  209. {
  210. name: 'ticket.title',
  211. operator: 'is',
  212. value: 'bli',
  213. },
  214. {
  215. operator: 'AND',
  216. conditions: [
  217. {
  218. name: 'ticket.title',
  219. operator: 'is',
  220. value: 'bla',
  221. },
  222. {
  223. name: 'ticket.title',
  224. operator: 'is',
  225. value: 'blub',
  226. },
  227. ],
  228. }
  229. ]
  230. }
  231. count, = Ticket.selectors(condition, { current_user: agent })
  232. expect(count).to eq(0)
  233. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  234. expect(result[:count]).to eq(0)
  235. end
  236. it 'does return all 3 results on empty condition', :aggregate_failures do
  237. condition = {
  238. operator: 'AND',
  239. conditions: []
  240. }
  241. count, = Ticket.selectors(condition, { current_user: agent })
  242. expect(count).to eq(3)
  243. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  244. expect(result[:count]).to eq(3)
  245. end
  246. it 'does return all 3 results on empty sub condition', :aggregate_failures do
  247. condition = {
  248. operator: 'AND',
  249. conditions: [
  250. {
  251. name: 'ticket.title',
  252. operator: 'contains',
  253. value: 'b',
  254. },
  255. {
  256. name: 'ticket.title',
  257. operator: 'contains',
  258. value: 'l',
  259. },
  260. {
  261. name: 'ticket.title',
  262. operator: 'contains',
  263. value: 'b',
  264. },
  265. {
  266. operator: 'AND',
  267. conditions: [
  268. ],
  269. }
  270. ]
  271. }
  272. count, = Ticket.selectors(condition, { current_user: agent })
  273. expect(count).to eq(3)
  274. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  275. expect(result[:count]).to eq(3)
  276. end
  277. it 'does return all 3 results on empty sub sub condition', :aggregate_failures do
  278. condition = {
  279. operator: 'AND',
  280. conditions: [
  281. {
  282. name: 'ticket.title',
  283. operator: 'contains',
  284. value: 'b',
  285. },
  286. {
  287. name: 'ticket.title',
  288. operator: 'contains',
  289. value: 'l',
  290. },
  291. {
  292. name: 'ticket.title',
  293. operator: 'contains',
  294. value: 'b',
  295. },
  296. {
  297. operator: 'AND',
  298. conditions: [
  299. {
  300. operator: 'AND',
  301. conditions: [
  302. ],
  303. }
  304. ],
  305. }
  306. ]
  307. }
  308. count, = Ticket.selectors(condition, { current_user: agent })
  309. expect(count).to eq(3)
  310. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  311. expect(result[:count]).to eq(3)
  312. end
  313. describe 'Trigger do not allow "Multi-Tree-Select" Fields on Organization and User Level as If Condition #4504', db_strategy: :reset do
  314. let(:field_name) { SecureRandom.uuid }
  315. let(:organization) { create(:organization, field_name => ['Incident', 'Incident::Hardware']) }
  316. let(:customer) { create(:customer, organization: organization, field_name => ['Incident', 'Incident::Hardware']) }
  317. let(:ticket) { create(:ticket, title: 'bli', group: Group.first, customer: customer, field_name => ['Incident', 'Incident::Hardware']) }
  318. def check_condition(attribute)
  319. condition = {
  320. operator: 'AND',
  321. conditions: [
  322. {
  323. name: attribute.to_s,
  324. operator: 'contains all',
  325. value: ['Incident', 'Incident::Hardware'],
  326. }
  327. ]
  328. }
  329. count, = Ticket.selectors(condition, { current_user: agent })
  330. expect(count).to eq(1)
  331. count, = Ticket.selectors(condition, { current_user: agent })
  332. expect(count).to eq(1)
  333. end
  334. before do
  335. create(:object_manager_attribute_multi_tree_select, object_name: 'Ticket', name: field_name)
  336. create(:object_manager_attribute_multi_tree_select, object_name: 'User', name: field_name)
  337. create(:object_manager_attribute_multi_tree_select, object_name: 'Organization', name: field_name)
  338. ObjectManager::Attribute.migration_execute
  339. ticket
  340. searchindex_model_reload([Ticket, User, Organization])
  341. end
  342. it 'does support contains one for all objects' do # rubocop:disable RSpec/NoExpectationExample
  343. check_condition("ticket.#{field_name}")
  344. check_condition("customer.#{field_name}")
  345. check_condition("organization.#{field_name}")
  346. end
  347. end
  348. describe 'Reporting profiles do not work with multi tree select #4546' do
  349. context 'when value is a string' do
  350. before do
  351. create(:tag, tag_item: create(:'tag/item', name: 'AAA'), o: ticket_1)
  352. create(:tag, tag_item: create(:'tag/item', name: 'BBB'), o: ticket_1)
  353. searchindex_model_reload([Ticket])
  354. end
  355. it 'does return ticket by contains all string value', :aggregate_failures do
  356. condition = {
  357. operator: 'AND',
  358. conditions: [
  359. {
  360. name: 'ticket.tags',
  361. operator: 'contains all',
  362. value: 'AAA, BBB',
  363. }
  364. ]
  365. }
  366. count, = Ticket.selectors(condition, { current_user: agent })
  367. expect(count).to eq(1)
  368. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  369. expect(result[:count]).to eq(1)
  370. end
  371. end
  372. context 'when value is an array', db_strategy: :reset do
  373. let(:field_name) { SecureRandom.uuid }
  374. before do
  375. create(:object_manager_attribute_multi_tree_select, name: field_name)
  376. ObjectManager::Attribute.migration_execute
  377. ticket_1.reload.update(field_name => ['Incident', 'Incident::Hardware'])
  378. searchindex_model_reload([Ticket])
  379. end
  380. it 'does return ticket by contains all array value', :aggregate_failures do
  381. condition = {
  382. operator: 'AND',
  383. conditions: [
  384. {
  385. name: "ticket.#{field_name}",
  386. operator: 'contains all',
  387. value: ['Incident', 'Incident::Hardware'],
  388. }
  389. ]
  390. }
  391. count, = Ticket.selectors(condition, { current_user: agent })
  392. expect(count).to eq(1)
  393. result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
  394. expect(result[:count]).to eq(1)
  395. end
  396. end
  397. end
  398. end