ticket_test.rb 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. require 'test_helper'
  2. class TicketTest < ActiveSupport::TestCase
  3. setup do
  4. Ticket.destroy_all
  5. end
  6. test 'ticket create' do
  7. ticket = Ticket.create!(
  8. title: "some title\n äöüß",
  9. group: Group.lookup(name: 'Users'),
  10. customer_id: 2,
  11. state: Ticket::State.lookup(name: 'new'),
  12. priority: Ticket::Priority.lookup(name: '2 normal'),
  13. updated_by_id: 1,
  14. created_by_id: 1,
  15. )
  16. assert(ticket, 'ticket created')
  17. assert_equal(ticket.title, 'some title äöüß', 'ticket.title verify')
  18. assert_equal(ticket.group.name, 'Users', 'ticket.group verify')
  19. assert_equal(ticket.state.name, 'new', 'ticket.state verify')
  20. # create inbound article #1
  21. article_inbound1 = Ticket::Article.create!(
  22. ticket_id: ticket.id,
  23. from: 'some_sender@example.com',
  24. to: 'some_recipient@example.com',
  25. subject: 'some subject',
  26. message_id: 'some@id',
  27. body: 'some message article_inbound1 😍😍😍',
  28. internal: false,
  29. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  30. type: Ticket::Article::Type.find_by(name: 'email'),
  31. updated_by_id: 1,
  32. created_by_id: 1,
  33. )
  34. assert_equal(article_inbound1.body, 'some message article_inbound1 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  35. ticket = Ticket.find(ticket.id)
  36. assert_equal(ticket.article_count, 1, 'ticket.article_count verify - inbound')
  37. assert_equal(ticket.last_contact_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - inbound')
  38. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  39. assert_nil(ticket.last_contact_agent_at, 'ticket.last_contact_agent_at verify - inbound')
  40. assert_nil(ticket.first_response_at, 'ticket.first_response_at verify - inbound')
  41. assert_nil(ticket.close_at, 'ticket.close_at verify - inbound')
  42. # create inbound article #2
  43. travel 2.seconds
  44. article_inbound2 = Ticket::Article.create!(
  45. ticket_id: ticket.id,
  46. from: 'some_sender@example.com',
  47. to: 'some_recipient@example.com',
  48. subject: 'some subject',
  49. message_id: 'some@id',
  50. body: 'some message article_inbound2 😍😍😍',
  51. internal: false,
  52. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  53. type: Ticket::Article::Type.find_by(name: 'email'),
  54. updated_by_id: 1,
  55. created_by_id: 1,
  56. )
  57. assert_equal(article_inbound2.body, 'some message article_inbound2 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  58. ticket = Ticket.find(ticket.id)
  59. assert_equal(ticket.article_count, 2, 'ticket.article_count verify - inbound')
  60. assert_equal(ticket.last_contact_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - inbound')
  61. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  62. assert_nil(ticket.last_contact_agent_at, 'ticket.last_contact_agent_at verify - inbound')
  63. assert_nil(ticket.first_response_at, 'ticket.first_response_at verify - inbound')
  64. assert_nil(ticket.close_at, 'ticket.close_at verify - inbound')
  65. # create note article
  66. article_note = Ticket::Article.create!(
  67. ticket_id: ticket.id,
  68. from: 'some person',
  69. subject: "some\nnote",
  70. body: "some\n message",
  71. internal: true,
  72. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  73. type: Ticket::Article::Type.find_by(name: 'note'),
  74. updated_by_id: 1,
  75. created_by_id: 1,
  76. )
  77. assert_equal(article_note.subject, 'some note', 'article_note.subject verify - inbound')
  78. assert_equal(article_note.body, "some\n message", 'article_note.body verify - inbound')
  79. ticket = Ticket.find(ticket.id)
  80. assert_equal(ticket.article_count, 3, 'ticket.article_count verify - note')
  81. assert_equal(ticket.last_contact_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - note')
  82. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - note')
  83. assert_nil(ticket.last_contact_agent_at, 'ticket.last_contact_agent_at verify - note')
  84. assert_nil(ticket.first_response_at, 'ticket.first_response_at verify - note')
  85. assert_nil(ticket.close_at, 'ticket.close_at verify - note')
  86. # create outbound article
  87. travel 2.seconds
  88. article_outbound = Ticket::Article.create!(
  89. ticket_id: ticket.id,
  90. from: 'some_recipient@example.com',
  91. to: 'some_sender@example.com',
  92. subject: 'some subject',
  93. message_id: 'some@id2',
  94. body: 'some message 2',
  95. internal: false,
  96. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  97. type: Ticket::Article::Type.find_by(name: 'email'),
  98. updated_by_id: 1,
  99. created_by_id: 1,
  100. )
  101. ticket = Ticket.find(ticket.id)
  102. assert_equal(ticket.article_count, 4, 'ticket.article_count verify - outbound')
  103. assert_equal(ticket.last_contact_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact verify - outbound')
  104. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - outbound')
  105. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - outbound')
  106. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - outbound')
  107. assert_nil(ticket.close_at, 'ticket.close_at verify - outbound')
  108. # create inbound article #3
  109. article_inbound3 = Ticket::Article.create!(
  110. ticket_id: ticket.id,
  111. from: 'some_sender@example.com',
  112. to: 'some_recipient@example.com',
  113. subject: 'some subject',
  114. message_id: 'some@id',
  115. body: 'some message article_inbound3 😍😍😍',
  116. internal: false,
  117. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  118. type: Ticket::Article::Type.find_by(name: 'email'),
  119. updated_by_id: 1,
  120. created_by_id: 1,
  121. )
  122. assert_equal(article_inbound3.body, 'some message article_inbound3 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  123. ticket = Ticket.find(ticket.id)
  124. assert_equal(ticket.article_count, 5, 'ticket.article_count verify - inbound')
  125. assert_equal(ticket.last_contact_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - inbound')
  126. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  127. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - outbound')
  128. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - outbound')
  129. assert_nil(ticket.close_at, 'ticket.close_at verify - outbound')
  130. # create inbound article #4
  131. travel 2.seconds
  132. article_inbound4 = Ticket::Article.create!(
  133. ticket_id: ticket.id,
  134. from: 'some_sender@example.com',
  135. to: 'some_recipient@example.com',
  136. subject: 'some subject',
  137. message_id: 'some@id',
  138. body: 'some message article_inbound4 😍😍😍',
  139. internal: false,
  140. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  141. type: Ticket::Article::Type.find_by(name: 'email'),
  142. updated_by_id: 1,
  143. created_by_id: 1,
  144. )
  145. assert_equal(article_inbound4.body, 'some message article_inbound4 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  146. ticket = Ticket.find(ticket.id)
  147. assert_equal(ticket.article_count, 6, 'ticket.article_count verify - inbound')
  148. assert_equal(ticket.last_contact_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - inbound')
  149. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  150. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - outbound')
  151. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - outbound')
  152. assert_nil(ticket.close_at, 'ticket.close_at verify - outbound')
  153. ticket.state_id = Ticket::State.where(name: 'closed').first.id
  154. ticket.save
  155. ticket = Ticket.find(ticket.id)
  156. assert_equal(ticket.article_count, 6, 'ticket.article_count verify - state update')
  157. assert_equal(ticket.last_contact_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - state update')
  158. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer_at verify - state update')
  159. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - state update')
  160. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - state update')
  161. assert(ticket.close_at, 'ticket.close_at verify - state update')
  162. # set pending time
  163. ticket.state_id = Ticket::State.find_by(name: 'pending reminder').id
  164. ticket.pending_time = Time.zone.parse('1977-10-27 22:00:00 +0000')
  165. ticket.save
  166. ticket = Ticket.find(ticket.id)
  167. assert_equal(ticket.state.name, 'pending reminder', 'state verify')
  168. assert_equal(ticket.pending_time, Time.zone.parse('1977-10-27 22:00:00 +0000'), 'pending_time verify')
  169. # reset pending state, should also reset pending time
  170. ticket.state_id = Ticket::State.find_by(name: 'closed').id
  171. ticket.save
  172. ticket = Ticket.find(ticket.id)
  173. assert_equal(ticket.state.name, 'closed', 'state verify')
  174. assert_nil(ticket.pending_time)
  175. # delete article
  176. article_note = Ticket::Article.create!(
  177. ticket_id: ticket.id,
  178. from: 'some person',
  179. subject: 'some note',
  180. body: 'some message',
  181. internal: true,
  182. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  183. type: Ticket::Article::Type.find_by(name: 'note'),
  184. updated_by_id: 1,
  185. created_by_id: 1,
  186. )
  187. ticket = Ticket.find(ticket.id)
  188. assert_equal(ticket.article_count, 7, 'ticket.article_count verify - note')
  189. article_note.destroy
  190. ticket = Ticket.find(ticket.id)
  191. assert_equal(ticket.article_count, 6, 'ticket.article_count verify - note')
  192. delete = ticket.destroy
  193. assert(delete, 'ticket destroy')
  194. travel_back
  195. end
  196. test 'ticket latest change' do
  197. ticket1 = Ticket.create!(
  198. title: 'latest change 1',
  199. group: Group.lookup(name: 'Users'),
  200. customer_id: 2,
  201. state: Ticket::State.lookup(name: 'new'),
  202. priority: Ticket::Priority.lookup(name: '2 normal'),
  203. updated_by_id: 1,
  204. created_by_id: 1,
  205. )
  206. assert_equal(Ticket.latest_change.to_s, ticket1.updated_at.to_s)
  207. travel 1.minute
  208. ticket2 = Ticket.create!(
  209. title: 'latest change 2',
  210. group: Group.lookup(name: 'Users'),
  211. customer_id: 2,
  212. state: Ticket::State.lookup(name: 'new'),
  213. priority: Ticket::Priority.lookup(name: '2 normal'),
  214. updated_by_id: 1,
  215. created_by_id: 1,
  216. )
  217. assert_equal(Ticket.latest_change.to_s, ticket2.updated_at.to_s)
  218. travel 1.minute
  219. ticket1.title = 'latest change 1 - 1'
  220. ticket1.save
  221. assert_equal(Ticket.latest_change.to_s, ticket1.updated_at.to_s)
  222. travel 1.minute
  223. ticket1.touch
  224. assert_equal(Ticket.latest_change.to_s, ticket1.updated_at.to_s)
  225. ticket1.destroy
  226. assert_equal(Ticket.latest_change.to_s, ticket2.updated_at.to_s)
  227. ticket2.destroy
  228. travel_back
  229. end
  230. test 'ticket process_pending' do
  231. # close all other pending close tickets first
  232. Ticket.where('pending_time IS NOT NULL').each do |ticket|
  233. ticket.state = Ticket::State.lookup(name: 'closed')
  234. ticket.save!
  235. end
  236. ticket = Ticket.create!(
  237. title: 'pending close test',
  238. group: Group.lookup(name: 'Users'),
  239. customer_id: 2,
  240. state: Ticket::State.lookup(name: 'pending close'),
  241. pending_time: Time.zone.now - 60,
  242. priority: Ticket::Priority.lookup(name: '2 normal'),
  243. updated_by_id: 1,
  244. created_by_id: 1,
  245. )
  246. lookup_ticket = Ticket.find_by('pending_time <= ?', Time.zone.now)
  247. assert_equal(lookup_ticket.id, ticket.id, 'ticket.pending_time verify')
  248. Ticket.process_pending
  249. lookup_ticket = Ticket.find_by('pending_time <= ?', Time.zone.now)
  250. assert_nil(lookup_ticket, 'ticket.pending_time processed verify')
  251. end
  252. test 'ticket subject' do
  253. ticket = Ticket.create!(
  254. title: 'subject test 1',
  255. group: Group.lookup(name: 'Users'),
  256. customer_id: 2,
  257. state: Ticket::State.lookup(name: 'new'),
  258. priority: Ticket::Priority.lookup(name: '2 normal'),
  259. updated_by_id: 1,
  260. created_by_id: 1,
  261. )
  262. assert_equal('subject test 1', ticket.title)
  263. assert_equal("ABC subject test 1 [Ticket##{ticket.number}]", ticket.subject_build('ABC subject test 1'))
  264. assert_equal("RE: ABC subject test 1 [Ticket##{ticket.number}]", ticket.subject_build('ABC subject test 1', 'reply'))
  265. assert_equal("RE: ABC subject test 1 [Ticket##{ticket.number}]", ticket.subject_build(' ABC subject test 1', 'reply'))
  266. assert_equal("RE: ABC subject test 1 [Ticket##{ticket.number}]", ticket.subject_build('ABC subject test 1 ', 'reply'))
  267. assert_equal("FWD: ABC subject test 1 [Ticket##{ticket.number}]", ticket.subject_build('ABC subject test 1 ', 'forward'))
  268. ticket.destroy
  269. Setting.set('ticket_hook_position', 'left')
  270. ticket = Ticket.create!(
  271. title: 'subject test 1',
  272. group: Group.lookup(name: 'Users'),
  273. customer_id: 2,
  274. state: Ticket::State.lookup(name: 'new'),
  275. priority: Ticket::Priority.lookup(name: '2 normal'),
  276. updated_by_id: 1,
  277. created_by_id: 1,
  278. )
  279. assert_equal('subject test 1', ticket.title)
  280. assert_equal("[Ticket##{ticket.number}] ABC subject test 1", ticket.subject_build('ABC subject test 1'))
  281. assert_equal("RE: [Ticket##{ticket.number}] ABC subject test 1", ticket.subject_build('ABC subject test 1', 'reply'))
  282. assert_equal("RE: [Ticket##{ticket.number}] ABC subject test 1", ticket.subject_build(' ABC subject test 1', 'reply'))
  283. assert_equal("RE: [Ticket##{ticket.number}] ABC subject test 1", ticket.subject_build('ABC subject test 1 ', 'reply'))
  284. assert_equal("FWD: [Ticket##{ticket.number}] ABC subject test 1", ticket.subject_build('ABC subject test 1 ', 'forward'))
  285. ticket.destroy
  286. Setting.set('ticket_hook_position', 'none')
  287. ticket = Ticket.create!(
  288. title: 'subject test 1',
  289. group: Group.lookup(name: 'Users'),
  290. customer_id: 2,
  291. state: Ticket::State.lookup(name: 'new'),
  292. priority: Ticket::Priority.lookup(name: '2 normal'),
  293. updated_by_id: 1,
  294. created_by_id: 1,
  295. )
  296. assert_equal('subject test 1', ticket.title)
  297. assert_equal('ABC subject test 1', ticket.subject_build('ABC subject test 1'))
  298. assert_equal('RE: ABC subject test 1', ticket.subject_build('ABC subject test 1', 'reply'))
  299. assert_equal('RE: ABC subject test 1', ticket.subject_build(' ABC subject test 1', 'reply'))
  300. assert_equal('RE: ABC subject test 1', ticket.subject_build('ABC subject test 1 ', 'reply'))
  301. assert_equal('FWD: ABC subject test 1', ticket.subject_build('ABC subject test 1 ', 'forward'))
  302. ticket.destroy
  303. end
  304. test 'ticket followup number check' do
  305. origin_backend = Setting.get('ticket_number')
  306. Setting.set('ticket_number', 'Ticket::Number::Increment')
  307. ticket1 = Ticket.create!(
  308. title: 'subject test 1234-1',
  309. group: Group.lookup(name: 'Users'),
  310. customer_id: 2,
  311. state: Ticket::State.lookup(name: 'new'),
  312. priority: Ticket::Priority.lookup(name: '2 normal'),
  313. updated_by_id: 1,
  314. created_by_id: 1,
  315. )
  316. assert_equal('subject test 1234-1', ticket1.title)
  317. assert_equal("ABC subject test 1 [Ticket##{ticket1.number}]", ticket1.subject_build('ABC subject test 1'))
  318. assert_equal(ticket1.id, Ticket::Number.check("Re: Help [Ticket##{ticket1.number}]").id)
  319. Setting.set('ticket_number', 'Ticket::Number::Date')
  320. ticket1 = Ticket.create!(
  321. title: 'subject test 1234-2',
  322. group: Group.lookup(name: 'Users'),
  323. customer_id: 2,
  324. state: Ticket::State.lookup(name: 'new'),
  325. priority: Ticket::Priority.lookup(name: '2 normal'),
  326. updated_by_id: 1,
  327. created_by_id: 1,
  328. )
  329. assert_equal('subject test 1234-2', ticket1.title)
  330. assert_equal("ABC subject test 1 [Ticket##{ticket1.number}]", ticket1.subject_build('ABC subject test 1'))
  331. assert_equal(ticket1.id, Ticket::Number.check("Re: Help [Ticket##{ticket1.number}]").id)
  332. Setting.set('ticket_number', origin_backend)
  333. end
  334. test 'article attachment helper 1' do
  335. ticket1 = Ticket.create!(
  336. title: 'some article helper test1',
  337. group: Group.lookup(name: 'Users'),
  338. customer_id: 2,
  339. state: Ticket::State.lookup(name: 'new'),
  340. priority: Ticket::Priority.lookup(name: '2 normal'),
  341. updated_by_id: 1,
  342. created_by_id: 1,
  343. )
  344. assert(ticket1, 'ticket created')
  345. # create inbound article #1
  346. article1 = Ticket::Article.create!(
  347. ticket_id: ticket1.id,
  348. from: 'some_sender@example.com',
  349. to: 'some_recipient@example.com',
  350. subject: 'some subject',
  351. message_id: 'some@id',
  352. content_type: 'text/html',
  353. body: 'some message article helper test1 <div><img style="width: 85.5px; height: 49.5px" src="cid:15.274327094.140938@zammad.example.com">asdasd<img src="cid:15.274327094.140939@zammad.example.com"><br>',
  354. internal: false,
  355. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  356. type: Ticket::Article::Type.find_by(name: 'email'),
  357. updated_by_id: 1,
  358. created_by_id: 1,
  359. )
  360. store1 = Store.add(
  361. object: 'Ticket::Article',
  362. o_id: article1.id,
  363. data: 'content_file1_normally_should_be_an_image',
  364. filename: 'some_file1.jpg',
  365. preferences: {
  366. 'Content-Type' => 'image/jpeg',
  367. 'Mime-Type' => 'image/jpeg',
  368. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  369. 'Content-Disposition' => 'inline'
  370. },
  371. created_by_id: 1,
  372. )
  373. store2 = Store.add(
  374. object: 'Ticket::Article',
  375. o_id: article1.id,
  376. data: 'content_file2_normally_should_be_an_image',
  377. filename: 'some_file2.jpg',
  378. preferences: {
  379. 'Content-Type' => 'image/jpeg',
  380. 'Mime-Type' => 'image/jpeg',
  381. 'Content-ID' => '15.274327094.140939@zammad.example.com',
  382. 'Content-Disposition' => 'inline'
  383. },
  384. created_by_id: 1,
  385. )
  386. store3 = Store.add(
  387. object: 'Ticket::Article',
  388. o_id: article1.id,
  389. data: 'content_file3',
  390. filename: 'some_file3.txt',
  391. preferences: {
  392. 'Content-Type' => 'text/stream',
  393. 'Mime-Type' => 'text/stream',
  394. 'Content-ID' => '15.274327094.99999@zammad.example.com',
  395. 'Content-Disposition' => 'inline'
  396. },
  397. created_by_id: 1,
  398. )
  399. article_attributes = Ticket::Article.insert_urls(article1.attributes_with_association_ids)
  400. assert_no_match('15.274327094.140938@zammad.example.com', article_attributes['body'])
  401. assert_no_match('15.274327094.140939@zammad.example.com', article_attributes['body'])
  402. assert_no_match('15.274327094.99999@zammad.example.com', article_attributes['body'])
  403. assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store1.id}", article_attributes['body'])
  404. assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store2.id}", article_attributes['body'])
  405. assert_no_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store3.id}", article_attributes['body'])
  406. article1 = Ticket::Article.find(article1.id)
  407. attachments = article1.attachments_inline
  408. assert_equal(2, attachments.length)
  409. assert_equal(store1.id, attachments.first.id)
  410. ticket1.destroy
  411. end
  412. test 'article attachment helper 2' do
  413. ticket1 = Ticket.create!(
  414. title: 'some article helper test2',
  415. group: Group.lookup(name: 'Users'),
  416. customer_id: 2,
  417. state: Ticket::State.lookup(name: 'new'),
  418. priority: Ticket::Priority.lookup(name: '2 normal'),
  419. updated_by_id: 1,
  420. created_by_id: 1,
  421. )
  422. assert(ticket1, 'ticket created')
  423. # create inbound article #1
  424. article1 = Ticket::Article.create!(
  425. ticket_id: ticket1.id,
  426. from: 'some_sender@example.com',
  427. to: 'some_recipient@example.com',
  428. subject: 'some subject',
  429. message_id: 'some@id',
  430. content_type: 'text/html',
  431. body: 'some message article helper test2 <div><img src="cid:15.274327094.140938@zammad.example.com">asdasd<img border="0" width="60" height="19" src="cid:15.274327094.140939@zammad.example.com" alt="Beschreibung: Beschreibung: efqmLogo"><br>',
  432. internal: false,
  433. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  434. type: Ticket::Article::Type.find_by(name: 'email'),
  435. updated_by_id: 1,
  436. created_by_id: 1,
  437. )
  438. store1 = Store.add(
  439. object: 'Ticket::Article',
  440. o_id: article1.id,
  441. data: 'content_file1_normally_should_be_an_image',
  442. filename: 'some_file1.jpg',
  443. preferences: {
  444. 'Content-Type' => 'image/jpeg',
  445. 'Mime-Type' => 'image/jpeg',
  446. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  447. 'Content-Disposition' => 'inline'
  448. },
  449. created_by_id: 1,
  450. )
  451. store2 = Store.add(
  452. object: 'Ticket::Article',
  453. o_id: article1.id,
  454. data: 'content_file2_normally_should_be_an_image',
  455. filename: 'some_file2.jpg',
  456. preferences: {
  457. 'Content-Type' => 'image/jpeg',
  458. 'Mime-Type' => 'image/jpeg',
  459. 'Content-ID' => '15.274327094.140939@zammad.example.com',
  460. 'Content-Disposition' => 'inline'
  461. },
  462. created_by_id: 1,
  463. )
  464. store3 = Store.add(
  465. object: 'Ticket::Article',
  466. o_id: article1.id,
  467. data: 'content_file3',
  468. filename: 'some_file3.txt',
  469. preferences: {
  470. 'Content-Type' => 'text/stream',
  471. 'Mime-Type' => 'text/stream',
  472. 'Content-ID' => '15.274327094.99999@zammad.example.com',
  473. 'Content-Disposition' => 'inline'
  474. },
  475. created_by_id: 1,
  476. )
  477. article_attributes = Ticket::Article.insert_urls(article1.attributes_with_association_ids)
  478. assert_no_match('15.274327094.140938@zammad.example.com', article_attributes['body'])
  479. assert_no_match('15.274327094.140939@zammad.example.com', article_attributes['body'])
  480. assert_no_match('15.274327094.99999@zammad.example.com', article_attributes['body'])
  481. assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store1.id}", article_attributes['body'])
  482. assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store2.id}", article_attributes['body'])
  483. assert_no_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store3.id}", article_attributes['body'])
  484. article1 = Ticket::Article.find(article1.id)
  485. attachments = article1.attachments_inline
  486. assert_equal(2, attachments.length)
  487. assert_equal(store1.id, attachments.first.id)
  488. ticket1.destroy
  489. end
  490. end