object_manager_test.rb 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. require 'integration_test_helper'
  2. class ObjectManagerTest < ActiveSupport::TestCase
  3. test 'a object manager' do
  4. list_objects = ObjectManager.list_objects
  5. assert_equal(%w[Ticket TicketArticle User Organization Group], list_objects)
  6. list_objects = ObjectManager.list_frontend_objects
  7. assert_equal(%w[Ticket User Organization Group], list_objects)
  8. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  9. # create simple attribute
  10. attribute1 = ObjectManager::Attribute.add(
  11. object: 'Ticket',
  12. name: 'test1',
  13. display: 'Test 1',
  14. data_type: 'input',
  15. data_option: {
  16. maxlength: 200,
  17. type: 'text',
  18. null: false,
  19. },
  20. active: true,
  21. screens: {},
  22. position: 20,
  23. created_by_id: 1,
  24. updated_by_id: 1,
  25. editable: false,
  26. to_migrate: false,
  27. )
  28. assert(attribute1)
  29. assert_equal('test1', attribute1.name)
  30. assert_equal(true, attribute1.editable)
  31. assert_equal(true, attribute1.to_create)
  32. assert_equal(true, attribute1.to_migrate)
  33. assert_equal(false, attribute1.to_delete)
  34. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  35. attribute1 = ObjectManager::Attribute.get(
  36. object: 'Ticket',
  37. name: 'test1',
  38. )
  39. assert(attribute1)
  40. assert_equal('test1', attribute1.name)
  41. assert_equal(true, attribute1.editable)
  42. assert_equal(true, attribute1.to_create)
  43. assert_equal(true, attribute1.to_migrate)
  44. assert_equal(false, attribute1.to_delete)
  45. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  46. # delete attribute without execute migrations
  47. ObjectManager::Attribute.remove(
  48. object: 'Ticket',
  49. name: 'test1',
  50. )
  51. attribute1 = ObjectManager::Attribute.get(
  52. object: 'Ticket',
  53. name: 'test1',
  54. )
  55. assert_not(attribute1)
  56. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  57. assert(ObjectManager::Attribute.migration_execute)
  58. attribute1 = ObjectManager::Attribute.get(
  59. object: 'Ticket',
  60. name: 'test1',
  61. )
  62. assert_not(attribute1)
  63. # create invalid attributes
  64. assert_raises(ActiveRecord::RecordInvalid) do
  65. ObjectManager::Attribute.add(
  66. object: 'Ticket',
  67. name: 'test2_id',
  68. display: 'Test 2 with id',
  69. data_type: 'input',
  70. data_option: {
  71. maxlength: 200,
  72. type: 'text',
  73. null: false,
  74. },
  75. active: true,
  76. screens: {},
  77. position: 20,
  78. created_by_id: 1,
  79. updated_by_id: 1,
  80. )
  81. end
  82. assert_raises(ActiveRecord::RecordInvalid) do
  83. ObjectManager::Attribute.add(
  84. object: 'Ticket',
  85. name: 'test3_ids',
  86. display: 'Test 3 with id',
  87. data_type: 'input',
  88. data_option: {
  89. maxlength: 200,
  90. type: 'text',
  91. null: false,
  92. },
  93. active: true,
  94. screens: {},
  95. position: 20,
  96. created_by_id: 1,
  97. updated_by_id: 1,
  98. )
  99. end
  100. assert_raises(ActiveRecord::RecordInvalid) do
  101. ObjectManager::Attribute.add(
  102. object: 'Ticket',
  103. name: 'test4',
  104. display: 'Test 4 with missing data_option[:type]',
  105. data_type: 'input',
  106. data_option: {
  107. maxlength: 200,
  108. null: false,
  109. },
  110. active: true,
  111. screens: {},
  112. position: 20,
  113. created_by_id: 1,
  114. updated_by_id: 1,
  115. )
  116. end
  117. attribute5 = ObjectManager::Attribute.add(
  118. object: 'Ticket',
  119. name: 'test5',
  120. display: 'Test 5',
  121. data_type: 'boolean',
  122. data_option: {
  123. default: true,
  124. options: {
  125. true: 'Yes',
  126. false: 'No',
  127. },
  128. null: false,
  129. },
  130. active: true,
  131. screens: {},
  132. position: 20,
  133. created_by_id: 1,
  134. updated_by_id: 1,
  135. )
  136. assert(attribute5)
  137. assert_equal('test5', attribute5.name)
  138. ObjectManager::Attribute.remove(
  139. object: 'Ticket',
  140. name: 'test5',
  141. )
  142. assert_raises(ActiveRecord::RecordInvalid) do
  143. ObjectManager::Attribute.add(
  144. object: 'Ticket',
  145. name: 'test6',
  146. display: 'Test 6',
  147. data_type: 'boolean',
  148. data_option: {
  149. options: {
  150. true: 'Yes',
  151. false: 'No',
  152. },
  153. null: false,
  154. },
  155. active: true,
  156. screens: {},
  157. position: 20,
  158. created_by_id: 1,
  159. updated_by_id: 1,
  160. )
  161. end
  162. attribute7 = ObjectManager::Attribute.add(
  163. object: 'Ticket',
  164. name: 'test7',
  165. display: 'Test 7',
  166. data_type: 'select',
  167. data_option: {
  168. default: 1,
  169. options: {
  170. '1' => 'aa',
  171. '2' => 'bb',
  172. },
  173. null: false,
  174. },
  175. active: true,
  176. screens: {},
  177. position: 20,
  178. created_by_id: 1,
  179. updated_by_id: 1,
  180. )
  181. assert(attribute7)
  182. assert_equal('test7', attribute7.name)
  183. ObjectManager::Attribute.remove(
  184. object: 'Ticket',
  185. name: 'test7',
  186. )
  187. assert_raises(ActiveRecord::RecordInvalid) do
  188. ObjectManager::Attribute.add(
  189. object: 'Ticket',
  190. name: 'test8',
  191. display: 'Test 8',
  192. data_type: 'select',
  193. data_option: {
  194. default: 1,
  195. null: false,
  196. },
  197. active: true,
  198. screens: {},
  199. position: 20,
  200. created_by_id: 1,
  201. updated_by_id: 1,
  202. )
  203. end
  204. attribute9 = ObjectManager::Attribute.add(
  205. object: 'Ticket',
  206. name: 'test9',
  207. display: 'Test 9',
  208. data_type: 'datetime',
  209. data_option: {
  210. future: true,
  211. past: false,
  212. diff: 24,
  213. null: true,
  214. },
  215. active: true,
  216. screens: {},
  217. position: 20,
  218. created_by_id: 1,
  219. updated_by_id: 1,
  220. )
  221. assert(attribute9)
  222. assert_equal('test9', attribute9.name)
  223. ObjectManager::Attribute.remove(
  224. object: 'Ticket',
  225. name: 'test9',
  226. )
  227. assert_raises(ActiveRecord::RecordInvalid) do
  228. ObjectManager::Attribute.add(
  229. object: 'Ticket',
  230. name: 'test10',
  231. display: 'Test 10',
  232. data_type: 'datetime',
  233. data_option: {
  234. past: false,
  235. diff: 24,
  236. null: true,
  237. },
  238. active: true,
  239. screens: {},
  240. position: 20,
  241. created_by_id: 1,
  242. updated_by_id: 1,
  243. )
  244. end
  245. attribute11 = ObjectManager::Attribute.add(
  246. object: 'Ticket',
  247. name: 'test11',
  248. display: 'Test 11',
  249. data_type: 'date',
  250. data_option: {
  251. future: true,
  252. past: false,
  253. diff: 24,
  254. null: true,
  255. },
  256. active: true,
  257. screens: {},
  258. position: 20,
  259. created_by_id: 1,
  260. updated_by_id: 1,
  261. )
  262. assert(attribute11)
  263. assert_equal('test11', attribute11.name)
  264. ObjectManager::Attribute.remove(
  265. object: 'Ticket',
  266. name: 'test11',
  267. )
  268. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  269. assert_raises(ActiveRecord::RecordInvalid) do
  270. ObjectManager::Attribute.add(
  271. object: 'Ticket',
  272. name: 'test13|',
  273. display: 'Test 13',
  274. data_type: 'date',
  275. data_option: {
  276. future: true,
  277. past: false,
  278. diff: 24,
  279. null: true,
  280. },
  281. active: true,
  282. screens: {},
  283. position: 20,
  284. created_by_id: 1,
  285. updated_by_id: 1,
  286. )
  287. end
  288. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  289. assert_raises(ActiveRecord::RecordInvalid) do
  290. ObjectManager::Attribute.add(
  291. object: 'Ticket',
  292. name: 'test14!',
  293. display: 'Test 14',
  294. data_type: 'date',
  295. data_option: {
  296. future: true,
  297. past: false,
  298. diff: 24,
  299. null: true,
  300. },
  301. active: true,
  302. screens: {},
  303. position: 20,
  304. created_by_id: 1,
  305. updated_by_id: 1,
  306. )
  307. end
  308. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  309. assert_raises(ActiveRecord::RecordInvalid) do
  310. ObjectManager::Attribute.add(
  311. object: 'Ticket',
  312. name: 'test15ä',
  313. display: 'Test 15',
  314. data_type: 'date',
  315. data_option: {
  316. future: true,
  317. past: false,
  318. diff: 24,
  319. null: true,
  320. },
  321. active: true,
  322. screens: {},
  323. position: 20,
  324. created_by_id: 1,
  325. updated_by_id: 1,
  326. )
  327. end
  328. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  329. # Test case #16 invalidated after callback added to set default #data_option[:null] value
  330. assert_raises(ActiveRecord::RecordInvalid) do
  331. ObjectManager::Attribute.add(
  332. object: 'Ticket',
  333. name: 'test17',
  334. display: 'Test 17',
  335. data_type: 'integer',
  336. data_option: {
  337. default: 2,
  338. min: 1,
  339. },
  340. active: true,
  341. screens: {},
  342. position: 20,
  343. created_by_id: 1,
  344. updated_by_id: 1,
  345. )
  346. end
  347. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  348. assert_raises(ActiveRecord::RecordInvalid) do
  349. ObjectManager::Attribute.add(
  350. object: 'Ticket',
  351. name: 'delete',
  352. display: 'Test 18',
  353. data_type: 'input',
  354. data_option: {
  355. maxlength: 200,
  356. type: 'text',
  357. null: false,
  358. },
  359. active: true,
  360. screens: {},
  361. position: 20,
  362. created_by_id: 1,
  363. updated_by_id: 1,
  364. )
  365. end
  366. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  367. attribute_count = ObjectManager::Attribute.count
  368. assert_raises(ActiveRecord::RecordInvalid) do
  369. ObjectManager::Attribute.add(
  370. object: 'Ticket',
  371. name: 'updated_at',
  372. display: 'Update Time',
  373. data_type: 'datetime',
  374. data_option: {
  375. future: true,
  376. past: true,
  377. diff: 24,
  378. null: true,
  379. },
  380. active: true,
  381. screens: {},
  382. position: 20,
  383. created_by_id: 1,
  384. updated_by_id: 1,
  385. )
  386. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  387. end
  388. assert_equal(attribute_count, ObjectManager::Attribute.count)
  389. assert_raises(ActiveRecord::RecordInvalid) do
  390. ObjectManager::Attribute.add(
  391. object: 'Ticket',
  392. name: 'updated_AT',
  393. display: 'Update Time',
  394. data_type: 'datetime',
  395. data_option: {
  396. future: true,
  397. past: true,
  398. diff: 24,
  399. null: true,
  400. },
  401. active: true,
  402. screens: {},
  403. position: 20,
  404. created_by_id: 1,
  405. updated_by_id: 1,
  406. )
  407. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  408. end
  409. assert_equal(attribute_count, ObjectManager::Attribute.count)
  410. end
  411. test 'b object manager attribute' do
  412. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  413. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  414. assert_equal(0, ObjectManager::Attribute.migrations.count)
  415. attribute1 = ObjectManager::Attribute.add(
  416. object: 'Ticket',
  417. name: 'attribute1',
  418. display: 'Attribute 1',
  419. data_type: 'input',
  420. data_option: {
  421. maxlength: 200,
  422. type: 'text',
  423. null: true,
  424. },
  425. active: true,
  426. screens: {},
  427. position: 20,
  428. created_by_id: 1,
  429. updated_by_id: 1,
  430. )
  431. assert(attribute1)
  432. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  433. assert_equal(1, ObjectManager::Attribute.where(to_migrate: true).count)
  434. assert_equal(1, ObjectManager::Attribute.migrations.count)
  435. # execute migrations
  436. assert(ObjectManager::Attribute.migration_execute)
  437. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  438. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  439. assert_equal(0, ObjectManager::Attribute.migrations.count)
  440. # create example ticket
  441. ticket1 = Ticket.create(
  442. title: 'some attribute test1',
  443. group: Group.lookup(name: 'Users'),
  444. customer_id: 2,
  445. state: Ticket::State.lookup(name: 'new'),
  446. priority: Ticket::Priority.lookup(name: '2 normal'),
  447. attribute1: 'some attribute text',
  448. updated_by_id: 1,
  449. created_by_id: 1,
  450. )
  451. assert('ticket1 created', ticket1)
  452. assert_equal('some attribute test1', ticket1.title)
  453. assert_equal('Users', ticket1.group.name)
  454. assert_equal('new', ticket1.state.name)
  455. assert_equal('some attribute text', ticket1.attribute1)
  456. # add additional attributes
  457. ObjectManager::Attribute.add(
  458. object: 'Ticket',
  459. name: 'attribute2',
  460. display: 'Attribute 2',
  461. data_type: 'select',
  462. data_option: {
  463. default: '2',
  464. options: {
  465. '1' => 'aa',
  466. '2' => 'bb',
  467. },
  468. null: true,
  469. },
  470. active: true,
  471. screens: {},
  472. position: 20,
  473. created_by_id: 1,
  474. updated_by_id: 1,
  475. )
  476. ObjectManager::Attribute.add(
  477. object: 'Ticket',
  478. name: 'attribute3',
  479. display: 'Attribute 3',
  480. data_type: 'datetime',
  481. data_option: {
  482. future: true,
  483. past: false,
  484. diff: 24,
  485. null: true,
  486. },
  487. active: true,
  488. screens: {},
  489. position: 20,
  490. created_by_id: 1,
  491. updated_by_id: 1,
  492. )
  493. ObjectManager::Attribute.add(
  494. object: 'Ticket',
  495. name: 'attribute4',
  496. display: 'Attribute 4',
  497. data_type: 'datetime',
  498. data_option: {
  499. future: true,
  500. past: false,
  501. diff: 24,
  502. null: true,
  503. },
  504. active: true,
  505. screens: {},
  506. position: 20,
  507. created_by_id: 1,
  508. updated_by_id: 1,
  509. )
  510. # execute migrations
  511. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  512. assert(ObjectManager::Attribute.migration_execute)
  513. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  514. # create example ticket
  515. ticket2 = Ticket.create(
  516. title: 'some attribute test2',
  517. group: Group.lookup(name: 'Users'),
  518. customer_id: 2,
  519. state: Ticket::State.lookup(name: 'new'),
  520. priority: Ticket::Priority.lookup(name: '2 normal'),
  521. attribute1: 'some attribute text',
  522. attribute2: '1',
  523. attribute3: Time.zone.parse('2016-05-12 00:59:59 UTC'),
  524. attribute4: Date.parse('2016-05-11'),
  525. updated_by_id: 1,
  526. created_by_id: 1,
  527. )
  528. assert('ticket2 created', ticket2)
  529. assert_equal('some attribute test2', ticket2.title)
  530. assert_equal('Users', ticket2.group.name)
  531. assert_equal('new', ticket2.state.name)
  532. assert_equal('some attribute text', ticket2.attribute1)
  533. assert_equal('1', ticket2.attribute2)
  534. assert_equal(Time.zone.parse('2016-05-12 00:59:59 UTC'), ticket2.attribute3)
  535. assert_equal(Date.parse('2016-05-11'), ticket2.attribute4)
  536. # update data_option null -> to_config
  537. attribute1 = ObjectManager::Attribute.add(
  538. object: 'Ticket',
  539. name: 'attribute1',
  540. display: 'Attribute 1',
  541. data_type: 'input',
  542. data_option: {
  543. maxlength: 200,
  544. type: 'text',
  545. null: false,
  546. },
  547. active: true,
  548. screens: {},
  549. position: 20,
  550. created_by_id: 1,
  551. updated_by_id: 1,
  552. )
  553. assert(attribute1)
  554. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  555. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  556. assert_equal(1, ObjectManager::Attribute.where(to_config: true).count)
  557. assert_equal(1, ObjectManager::Attribute.migrations.count)
  558. # execute migrations
  559. assert(ObjectManager::Attribute.migration_execute)
  560. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  561. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  562. assert_equal(0, ObjectManager::Attribute.where(to_config: true).count)
  563. assert_equal(0, ObjectManager::Attribute.migrations.count)
  564. # update data_option maxlength -> to_config && to_migrate
  565. attribute1 = ObjectManager::Attribute.add(
  566. object: 'Ticket',
  567. name: 'attribute1',
  568. display: 'Attribute 1',
  569. data_type: 'input',
  570. data_option: {
  571. maxlength: 250,
  572. type: 'text',
  573. null: false,
  574. },
  575. active: true,
  576. screens: {},
  577. position: 20,
  578. created_by_id: 1,
  579. updated_by_id: 1,
  580. )
  581. assert(attribute1)
  582. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  583. assert_equal(1, ObjectManager::Attribute.where(to_migrate: true).count)
  584. assert_equal(1, ObjectManager::Attribute.where(to_config: true).count)
  585. assert_equal(1, ObjectManager::Attribute.migrations.count)
  586. # execute migrations
  587. assert(ObjectManager::Attribute.migration_execute)
  588. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  589. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  590. assert_equal(0, ObjectManager::Attribute.where(to_config: true).count)
  591. assert_equal(0, ObjectManager::Attribute.migrations.count)
  592. # remove attribute
  593. ObjectManager::Attribute.remove(
  594. object: 'Ticket',
  595. name: 'attribute1',
  596. )
  597. ObjectManager::Attribute.remove(
  598. object: 'Ticket',
  599. name: 'attribute2',
  600. )
  601. ObjectManager::Attribute.remove(
  602. object: 'Ticket',
  603. name: 'attribute3',
  604. )
  605. ObjectManager::Attribute.remove(
  606. object: 'Ticket',
  607. name: 'attribute4',
  608. )
  609. assert(ObjectManager::Attribute.migration_execute)
  610. ticket2 = Ticket.find(ticket2.id)
  611. assert('ticket2 created', ticket2)
  612. assert_equal('some attribute test2', ticket2.title)
  613. assert_equal('Users', ticket2.group.name)
  614. assert_equal('new', ticket2.state.name)
  615. assert_nil(ticket2[:attribute1])
  616. assert_nil(ticket2[:attribute2])
  617. assert_nil(ticket2[:attribute3])
  618. assert_nil(ticket2[:attribute4])
  619. end
  620. test 'c object manager attribute - certain names' do
  621. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  622. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  623. assert_equal(0, ObjectManager::Attribute.migrations.count)
  624. attribute1 = ObjectManager::Attribute.add(
  625. object: 'Ticket',
  626. name: '1_a_anfrage_status',
  627. display: '1_a_anfrage_status',
  628. data_type: 'input',
  629. data_option: {
  630. maxlength: 200,
  631. type: 'text',
  632. null: true,
  633. },
  634. active: true,
  635. screens: {},
  636. position: 20,
  637. created_by_id: 1,
  638. updated_by_id: 1,
  639. )
  640. assert(attribute1)
  641. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  642. assert_equal(1, ObjectManager::Attribute.where(to_migrate: true).count)
  643. assert_equal(1, ObjectManager::Attribute.migrations.count)
  644. # execute migrations
  645. assert(ObjectManager::Attribute.migration_execute)
  646. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  647. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  648. assert_equal(0, ObjectManager::Attribute.migrations.count)
  649. # create example ticket
  650. ticket1 = Ticket.create!(
  651. title: 'some attribute test3',
  652. group: Group.lookup(name: 'Users'),
  653. customer_id: 2,
  654. state: Ticket::State.lookup(name: 'new'),
  655. priority: Ticket::Priority.lookup(name: '2 normal'),
  656. '1_a_anfrage_status': 'some attribute text',
  657. updated_by_id: 1,
  658. created_by_id: 1,
  659. )
  660. assert('ticket1 created', ticket1)
  661. assert_equal('some attribute test3', ticket1.title)
  662. assert_equal('Users', ticket1.group.name)
  663. assert_equal('new', ticket1.state.name)
  664. assert_equal('some attribute text', ticket1['1_a_anfrage_status'])
  665. condition = {
  666. 'ticket.title' => {
  667. operator: 'is',
  668. value: 'some attribute test3',
  669. },
  670. }
  671. ticket_count, tickets = Ticket.selectors(condition, limit: 10)
  672. assert_equal(ticket_count, 1)
  673. assert_equal(tickets[0].id, ticket1.id)
  674. condition = {
  675. 'ticket.1_a_anfrage_status' => {
  676. operator: 'is',
  677. value: 'some attribute text',
  678. },
  679. }
  680. ticket_count, tickets = Ticket.selectors(condition, limit: 10)
  681. assert_equal(ticket_count, 1)
  682. assert_equal(tickets[0].id, ticket1.id)
  683. agent1 = User.create_or_update(
  684. login: 'agent1@example.com',
  685. firstname: 'Notification',
  686. lastname: 'Agent1',
  687. email: 'agent1@example.com',
  688. password: 'agentpw',
  689. active: true,
  690. roles: Role.where(name: 'Agent'),
  691. groups: Group.all,
  692. updated_by_id: 1,
  693. created_by_id: 1,
  694. )
  695. Overview.create!(
  696. name: 'Overview1',
  697. link: 'my_overview',
  698. roles: Role.all,
  699. condition: {
  700. 'ticket.1_a_anfrage_status' => {
  701. operator: 'is',
  702. value: 'some attribute text',
  703. },
  704. },
  705. order: {
  706. by: '1_a_anfrage_status',
  707. direction: 'DESC',
  708. },
  709. group_by: '1_a_anfrage_status',
  710. view: {
  711. d: %w[title customer state created_at],
  712. s: %w[number title customer state created_at],
  713. m: %w[number title customer state created_at],
  714. view_mode_default: 's',
  715. },
  716. prio: 1,
  717. updated_by_id: 1,
  718. created_by_id: 1,
  719. )
  720. result = Ticket::Overviews.index(agent1)
  721. overview = nil
  722. result.each do |local_overview|
  723. next if local_overview[:overview][:name] != 'Overview1'
  724. overview = local_overview
  725. break
  726. end
  727. assert(overview)
  728. assert_equal(1, overview[:tickets].count)
  729. assert_equal(1, overview[:count])
  730. assert_equal(ticket1.id, overview[:tickets][0][:id])
  731. end
  732. test 'd object manager attribute - update attribute type' do
  733. attribute1 = ObjectManager::Attribute.add(
  734. object: 'Ticket',
  735. name: 'example_1',
  736. display: 'example_1',
  737. data_type: 'input',
  738. data_option: {
  739. default: '',
  740. maxlength: 200,
  741. type: 'text',
  742. null: true,
  743. options: {},
  744. },
  745. active: true,
  746. screens: {},
  747. position: 20,
  748. created_by_id: 1,
  749. updated_by_id: 1,
  750. )
  751. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  752. assert_equal(1, ObjectManager::Attribute.migrations.count)
  753. assert(ObjectManager::Attribute.migration_execute)
  754. assert_raises(ActiveRecord::RecordInvalid) do
  755. ObjectManager::Attribute.add(
  756. object: 'Ticket',
  757. name: 'example_1',
  758. display: 'example_1',
  759. data_type: 'boolean',
  760. data_option: {
  761. default: true,
  762. options: {
  763. true: 'Yes',
  764. false: 'No',
  765. },
  766. null: false,
  767. },
  768. active: true,
  769. screens: {},
  770. position: 200,
  771. created_by_id: 1,
  772. updated_by_id: 1,
  773. )
  774. end
  775. attribute2 = ObjectManager::Attribute.add(
  776. object: 'Ticket',
  777. name: 'example_1',
  778. display: 'example_1',
  779. data_type: 'select',
  780. data_option: {
  781. default: '',
  782. maxlength: 200,
  783. type: 'text',
  784. null: true,
  785. options: {
  786. aa: 'aa',
  787. bb: 'bb',
  788. },
  789. },
  790. active: true,
  791. screens: {},
  792. position: 20,
  793. created_by_id: 1,
  794. updated_by_id: 1,
  795. )
  796. assert_equal(attribute1.id, attribute2.id)
  797. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  798. assert_equal(1, ObjectManager::Attribute.migrations.count)
  799. assert(ObjectManager::Attribute.migration_execute)
  800. end
  801. test 'overview any owner / no owner is set' do
  802. group = Group.create!(
  803. name: 'OverviewTest',
  804. updated_at: '2015-02-05 16:37:00',
  805. updated_by_id: 1,
  806. created_by_id: 1,
  807. )
  808. roles = Role.where(name: 'Agent')
  809. agent1 = User.create!(
  810. login: 'ticket-overview-agent1@example.com',
  811. firstname: 'Overview',
  812. lastname: 'Agent1',
  813. email: 'ticket-overview-agent1@example.com',
  814. password: 'agentpw',
  815. active: true,
  816. roles: roles,
  817. groups: [group],
  818. updated_at: '2015-02-05 16:37:00',
  819. updated_by_id: 1,
  820. created_by_id: 1,
  821. )
  822. ObjectManager::Attribute.add(
  823. object: 'Ticket',
  824. name: 'watcher',
  825. display: 'watcher',
  826. data_type: 'select',
  827. data_option: {
  828. default: '',
  829. maxlength: 200,
  830. type: 'text',
  831. null: true,
  832. options: {
  833. aa: 'agent a',
  834. bb: 'agent b',
  835. cc: 'agent c',
  836. },
  837. },
  838. active: true,
  839. screens: {},
  840. position: 20,
  841. created_by_id: 1,
  842. updated_by_id: 1,
  843. )
  844. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  845. assert_equal(1, ObjectManager::Attribute.migrations.count)
  846. assert(ObjectManager::Attribute.migration_execute)
  847. Ticket.destroy_all
  848. Overview.destroy_all
  849. UserInfo.current_user_id = 1
  850. overview_role = Role.find_by(name: 'Agent')
  851. overview1 = Overview.create!(
  852. name: 'not watched',
  853. prio: 1000,
  854. role_ids: [overview_role.id],
  855. condition: {
  856. 'ticket.watcher' => {
  857. operator: 'is',
  858. value: '',
  859. },
  860. },
  861. order: {
  862. by: 'created_at',
  863. direction: 'ASC',
  864. },
  865. view: {
  866. d: %w[title customer group created_at],
  867. s: %w[title customer group created_at],
  868. m: %w[number title customer group created_at],
  869. view_mode_default: 's',
  870. },
  871. )
  872. overview2 = Overview.create!(
  873. name: 'not watched by somebody',
  874. prio: 2000,
  875. role_ids: [overview_role.id],
  876. condition: {
  877. 'ticket.watcher' => {
  878. operator: 'is not',
  879. value: '',
  880. },
  881. },
  882. order: {
  883. by: 'created_at',
  884. direction: 'ASC',
  885. },
  886. view: {
  887. d: %w[title customer group created_at],
  888. s: %w[title customer group created_at],
  889. m: %w[number title customer group created_at],
  890. view_mode_default: 's',
  891. },
  892. )
  893. overview3 = Overview.create!(
  894. name: 'not watched as array',
  895. prio: 3000,
  896. role_ids: [overview_role.id],
  897. condition: {
  898. 'ticket.watcher' => {
  899. operator: 'is',
  900. value: [''],
  901. },
  902. },
  903. order: {
  904. by: 'created_at',
  905. direction: 'ASC',
  906. },
  907. view: {
  908. d: %w[title customer group created_at],
  909. s: %w[title customer group created_at],
  910. m: %w[number title customer group created_at],
  911. view_mode_default: 's',
  912. },
  913. )
  914. overview4 = Overview.create!(
  915. name: 'not watched by somebody as array',
  916. prio: 4000,
  917. role_ids: [overview_role.id],
  918. condition: {
  919. 'ticket.watcher' => {
  920. operator: 'is not',
  921. value: [''],
  922. },
  923. },
  924. order: {
  925. by: 'created_at',
  926. direction: 'ASC',
  927. },
  928. view: {
  929. d: %w[title customer group created_at],
  930. s: %w[title customer group created_at],
  931. m: %w[number title customer group created_at],
  932. view_mode_default: 's',
  933. },
  934. )
  935. overview5 = Overview.create!(
  936. name: 'watched by aa',
  937. prio: 5000,
  938. role_ids: [overview_role.id],
  939. condition: {
  940. 'ticket.watcher' => {
  941. operator: 'is',
  942. value: 'aa',
  943. },
  944. },
  945. order: {
  946. by: 'created_at',
  947. direction: 'ASC',
  948. },
  949. view: {
  950. d: %w[title customer group created_at],
  951. s: %w[title customer group created_at],
  952. m: %w[number title customer group created_at],
  953. view_mode_default: 's',
  954. },
  955. )
  956. overview6 = Overview.create!(
  957. name: 'not watched by aa',
  958. prio: 6000,
  959. role_ids: [overview_role.id],
  960. condition: {
  961. 'ticket.watcher' => {
  962. operator: 'is not',
  963. value: 'aa',
  964. },
  965. },
  966. order: {
  967. by: 'created_at',
  968. direction: 'ASC',
  969. },
  970. view: {
  971. d: %w[title customer group created_at],
  972. s: %w[title customer group created_at],
  973. m: %w[number title customer group created_at],
  974. view_mode_default: 's',
  975. },
  976. )
  977. overview7 = Overview.create!(
  978. name: 'watched by aa array',
  979. prio: 7000,
  980. role_ids: [overview_role.id],
  981. condition: {
  982. 'ticket.watcher' => {
  983. operator: 'is',
  984. value: ['aa'],
  985. },
  986. },
  987. order: {
  988. by: 'created_at',
  989. direction: 'ASC',
  990. },
  991. view: {
  992. d: %w[title customer group created_at],
  993. s: %w[title customer group created_at],
  994. m: %w[number title customer group created_at],
  995. view_mode_default: 's',
  996. },
  997. )
  998. overview8 = Overview.create!(
  999. name: 'not watched by aa array',
  1000. prio: 8000,
  1001. role_ids: [overview_role.id],
  1002. condition: {
  1003. 'ticket.watcher' => {
  1004. operator: 'is not',
  1005. value: ['aa'],
  1006. },
  1007. },
  1008. order: {
  1009. by: 'created_at',
  1010. direction: 'ASC',
  1011. },
  1012. view: {
  1013. d: %w[title customer group created_at],
  1014. s: %w[title customer group created_at],
  1015. m: %w[number title customer group created_at],
  1016. view_mode_default: 's',
  1017. },
  1018. )
  1019. ticket1 = Ticket.create!(
  1020. title: 'overview test 1',
  1021. group: Group.lookup(name: 'OverviewTest'),
  1022. customer_id: 2,
  1023. owner_id: 1,
  1024. watcher: '',
  1025. state: Ticket::State.lookup(name: 'new'),
  1026. priority: Ticket::Priority.lookup(name: '2 normal'),
  1027. )
  1028. travel 2.seconds
  1029. ticket2 = Ticket.create!(
  1030. title: 'overview test 2',
  1031. group: Group.lookup(name: 'OverviewTest'),
  1032. customer_id: 2,
  1033. owner_id: nil,
  1034. watcher: nil,
  1035. state: Ticket::State.lookup(name: 'new'),
  1036. priority: Ticket::Priority.lookup(name: '2 normal'),
  1037. )
  1038. travel 2.seconds
  1039. ticket3 = Ticket.create!(
  1040. title: 'overview test 3',
  1041. group: Group.lookup(name: 'OverviewTest'),
  1042. customer_id: 2,
  1043. owner_id: agent1.id,
  1044. watcher: 'aa',
  1045. state: Ticket::State.lookup(name: 'new'),
  1046. priority: Ticket::Priority.lookup(name: '2 normal'),
  1047. )
  1048. result = Ticket::Overviews.index(agent1)
  1049. assert_equal(result[0][:overview][:id], overview1.id)
  1050. assert_equal(result[0][:overview][:name], 'not watched')
  1051. assert_equal(result[0][:overview][:view], 'not_watched')
  1052. assert_equal(result[0][:tickets].class, Array)
  1053. assert_equal(result[0][:tickets][0][:id], ticket1.id)
  1054. assert_equal(result[0][:tickets][1][:id], ticket2.id)
  1055. assert_equal(result[0][:count], 2)
  1056. assert_equal(result[1][:overview][:id], overview2.id)
  1057. assert_equal(result[1][:overview][:name], 'not watched by somebody')
  1058. assert_equal(result[1][:overview][:view], 'not_watched_by_somebody')
  1059. assert_equal(result[1][:tickets].class, Array)
  1060. assert_equal(result[1][:tickets][0][:id], ticket3.id)
  1061. assert_equal(result[1][:count], 1)
  1062. assert_equal(result[2][:overview][:id], overview3.id)
  1063. assert_equal(result[2][:overview][:name], 'not watched as array')
  1064. assert_equal(result[2][:overview][:view], 'not_watched_as_array')
  1065. assert_equal(result[2][:tickets].class, Array)
  1066. assert_equal(result[2][:tickets][0][:id], ticket1.id)
  1067. assert_equal(result[2][:tickets][1][:id], ticket2.id)
  1068. assert_equal(result[2][:count], 2)
  1069. assert_equal(result[3][:overview][:id], overview4.id)
  1070. assert_equal(result[3][:overview][:name], 'not watched by somebody as array')
  1071. assert_equal(result[3][:overview][:view], 'not_watched_by_somebody_as_array')
  1072. assert_equal(result[3][:tickets].class, Array)
  1073. assert_equal(result[3][:tickets][0][:id], ticket3.id)
  1074. assert_equal(result[3][:count], 1)
  1075. assert_equal(result[4][:overview][:id], overview5.id)
  1076. assert_equal(result[4][:overview][:name], 'watched by aa')
  1077. assert_equal(result[4][:overview][:view], 'watched_by_aa')
  1078. assert_equal(result[4][:tickets].class, Array)
  1079. assert_equal(result[4][:tickets][0][:id], ticket3.id)
  1080. assert_equal(result[4][:count], 1)
  1081. assert_equal(result[5][:overview][:id], overview6.id)
  1082. assert_equal(result[5][:overview][:name], 'not watched by aa')
  1083. assert_equal(result[5][:overview][:view], 'not_watched_by_aa')
  1084. assert_equal(result[5][:tickets].class, Array)
  1085. assert_equal(result[5][:tickets][0][:id], ticket1.id)
  1086. assert_equal(result[5][:tickets][1][:id], ticket2.id)
  1087. assert_equal(result[5][:count], 2)
  1088. assert_equal(result[6][:overview][:id], overview7.id)
  1089. assert_equal(result[6][:overview][:name], 'watched by aa array')
  1090. assert_equal(result[6][:overview][:view], 'watched_by_aa_array')
  1091. assert_equal(result[6][:tickets].class, Array)
  1092. assert_equal(result[6][:tickets][0][:id], ticket3.id)
  1093. assert_equal(result[6][:count], 1)
  1094. assert_equal(result[7][:overview][:id], overview8.id)
  1095. assert_equal(result[7][:overview][:name], 'not watched by aa array')
  1096. assert_equal(result[7][:overview][:view], 'not_watched_by_aa_array')
  1097. assert_equal(result[7][:tickets].class, Array)
  1098. assert_equal(result[7][:tickets][0][:id], ticket1.id)
  1099. assert_equal(result[7][:tickets][1][:id], ticket2.id)
  1100. assert_equal(result[7][:count], 2)
  1101. end
  1102. end