object_manager_test.rb 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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(RuntimeError) do
  65. attribute2 = 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(RuntimeError) do
  83. attribute3 = 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(RuntimeError) do
  101. attribute4 = 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(RuntimeError) do
  143. attribute6 = 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(RuntimeError) do
  188. attribute8 = 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(RuntimeError) do
  228. attribute10 = 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_raises(RuntimeError) do
  269. attribute12 = ObjectManager::Attribute.add(
  270. object: 'Ticket',
  271. name: 'test12',
  272. display: 'Test 12',
  273. data_type: 'date',
  274. data_option: {
  275. past: false,
  276. diff: 24,
  277. null: true,
  278. },
  279. active: true,
  280. screens: {},
  281. position: 20,
  282. created_by_id: 1,
  283. updated_by_id: 1,
  284. )
  285. end
  286. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  287. assert_raises(RuntimeError) do
  288. attribute13 = ObjectManager::Attribute.add(
  289. object: 'Ticket',
  290. name: 'test13|',
  291. display: 'Test 13',
  292. data_type: 'date',
  293. data_option: {
  294. future: true,
  295. past: false,
  296. diff: 24,
  297. null: true,
  298. },
  299. active: true,
  300. screens: {},
  301. position: 20,
  302. created_by_id: 1,
  303. updated_by_id: 1,
  304. )
  305. end
  306. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  307. assert_raises(RuntimeError) do
  308. attribute14 = ObjectManager::Attribute.add(
  309. object: 'Ticket',
  310. name: 'test14!',
  311. display: 'Test 14',
  312. data_type: 'date',
  313. data_option: {
  314. future: true,
  315. past: false,
  316. diff: 24,
  317. null: true,
  318. },
  319. active: true,
  320. screens: {},
  321. position: 20,
  322. created_by_id: 1,
  323. updated_by_id: 1,
  324. )
  325. end
  326. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  327. assert_raises(RuntimeError) do
  328. attribute15 = ObjectManager::Attribute.add(
  329. object: 'Ticket',
  330. name: 'test15ä',
  331. display: 'Test 15',
  332. data_type: 'date',
  333. data_option: {
  334. future: true,
  335. past: false,
  336. diff: 24,
  337. null: true,
  338. },
  339. active: true,
  340. screens: {},
  341. position: 20,
  342. created_by_id: 1,
  343. updated_by_id: 1,
  344. )
  345. end
  346. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  347. assert_raises(RuntimeError) do
  348. attribute16 = ObjectManager::Attribute.add(
  349. object: 'Ticket',
  350. name: 'test16',
  351. display: 'Test 16',
  352. data_type: 'integer',
  353. data_option: {
  354. default: 2,
  355. min: 1,
  356. max: 999,
  357. },
  358. active: true,
  359. screens: {},
  360. position: 20,
  361. created_by_id: 1,
  362. updated_by_id: 1,
  363. )
  364. end
  365. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  366. assert_raises(RuntimeError) do
  367. attribute17 = ObjectManager::Attribute.add(
  368. object: 'Ticket',
  369. name: 'test17',
  370. display: 'Test 17',
  371. data_type: 'integer',
  372. data_option: {
  373. default: 2,
  374. min: 1,
  375. },
  376. active: true,
  377. screens: {},
  378. position: 20,
  379. created_by_id: 1,
  380. updated_by_id: 1,
  381. )
  382. end
  383. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  384. assert_raises(RuntimeError) do
  385. attribute18 = ObjectManager::Attribute.add(
  386. object: 'Ticket',
  387. name: 'delete',
  388. display: 'Test 18',
  389. data_type: 'input',
  390. data_option: {
  391. maxlength: 200,
  392. type: 'text',
  393. null: false,
  394. },
  395. active: true,
  396. screens: {},
  397. position: 20,
  398. created_by_id: 1,
  399. updated_by_id: 1,
  400. )
  401. end
  402. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  403. attribute_count = ObjectManager::Attribute.count
  404. assert_raises(RuntimeError) do
  405. attribute19 = ObjectManager::Attribute.add(
  406. object: 'Ticket',
  407. name: 'updated_at',
  408. display: 'Update Time',
  409. data_type: 'datetime',
  410. data_option: {
  411. future: true,
  412. past: true,
  413. diff: 24,
  414. null: true,
  415. },
  416. active: true,
  417. screens: {},
  418. position: 20,
  419. created_by_id: 1,
  420. updated_by_id: 1,
  421. )
  422. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  423. end
  424. assert_equal(attribute_count, ObjectManager::Attribute.count)
  425. assert_raises(RuntimeError) do
  426. attribute20 = ObjectManager::Attribute.add(
  427. object: 'Ticket',
  428. name: 'updated_AT',
  429. display: 'Update Time',
  430. data_type: 'datetime',
  431. data_option: {
  432. future: true,
  433. past: true,
  434. diff: 24,
  435. null: true,
  436. },
  437. active: true,
  438. screens: {},
  439. position: 20,
  440. created_by_id: 1,
  441. updated_by_id: 1,
  442. )
  443. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  444. end
  445. assert_equal(attribute_count, ObjectManager::Attribute.count)
  446. end
  447. test 'b object manager attribute' do
  448. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  449. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  450. assert_equal(0, ObjectManager::Attribute.migrations.count)
  451. attribute1 = ObjectManager::Attribute.add(
  452. object: 'Ticket',
  453. name: 'attribute1',
  454. display: 'Attribute 1',
  455. data_type: 'input',
  456. data_option: {
  457. maxlength: 200,
  458. type: 'text',
  459. null: true,
  460. },
  461. active: true,
  462. screens: {},
  463. position: 20,
  464. created_by_id: 1,
  465. updated_by_id: 1,
  466. )
  467. assert(attribute1)
  468. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  469. assert_equal(1, ObjectManager::Attribute.where(to_migrate: true).count)
  470. assert_equal(1, ObjectManager::Attribute.migrations.count)
  471. # execute migrations
  472. assert(ObjectManager::Attribute.migration_execute)
  473. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  474. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  475. assert_equal(0, ObjectManager::Attribute.migrations.count)
  476. # create example ticket
  477. ticket1 = Ticket.create(
  478. title: 'some attribute test1',
  479. group: Group.lookup(name: 'Users'),
  480. customer_id: 2,
  481. state: Ticket::State.lookup(name: 'new'),
  482. priority: Ticket::Priority.lookup(name: '2 normal'),
  483. attribute1: 'some attribute text',
  484. updated_by_id: 1,
  485. created_by_id: 1,
  486. )
  487. assert('ticket1 created', ticket1)
  488. assert_equal('some attribute test1', ticket1.title)
  489. assert_equal('Users', ticket1.group.name)
  490. assert_equal('new', ticket1.state.name)
  491. assert_equal('some attribute text', ticket1.attribute1)
  492. # add additional attributes
  493. attribute2 = ObjectManager::Attribute.add(
  494. object: 'Ticket',
  495. name: 'attribute2',
  496. display: 'Attribute 2',
  497. data_type: 'select',
  498. data_option: {
  499. default: '2',
  500. options: {
  501. '1' => 'aa',
  502. '2' => 'bb',
  503. },
  504. null: true,
  505. },
  506. active: true,
  507. screens: {},
  508. position: 20,
  509. created_by_id: 1,
  510. updated_by_id: 1,
  511. )
  512. attribute3 = ObjectManager::Attribute.add(
  513. object: 'Ticket',
  514. name: 'attribute3',
  515. display: 'Attribute 3',
  516. data_type: 'datetime',
  517. data_option: {
  518. future: true,
  519. past: false,
  520. diff: 24,
  521. null: true,
  522. },
  523. active: true,
  524. screens: {},
  525. position: 20,
  526. created_by_id: 1,
  527. updated_by_id: 1,
  528. )
  529. attribute4 = ObjectManager::Attribute.add(
  530. object: 'Ticket',
  531. name: 'attribute4',
  532. display: 'Attribute 4',
  533. data_type: 'datetime',
  534. data_option: {
  535. future: true,
  536. past: false,
  537. diff: 24,
  538. null: true,
  539. },
  540. active: true,
  541. screens: {},
  542. position: 20,
  543. created_by_id: 1,
  544. updated_by_id: 1,
  545. )
  546. # execute migrations
  547. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  548. assert(ObjectManager::Attribute.migration_execute)
  549. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  550. # create example ticket
  551. ticket2 = Ticket.create(
  552. title: 'some attribute test2',
  553. group: Group.lookup(name: 'Users'),
  554. customer_id: 2,
  555. state: Ticket::State.lookup(name: 'new'),
  556. priority: Ticket::Priority.lookup(name: '2 normal'),
  557. attribute1: 'some attribute text',
  558. attribute2: '1',
  559. attribute3: Time.zone.parse('2016-05-12 00:59:59 UTC'),
  560. attribute4: Date.parse('2016-05-11'),
  561. updated_by_id: 1,
  562. created_by_id: 1,
  563. )
  564. assert('ticket2 created', ticket2)
  565. assert_equal('some attribute test2', ticket2.title)
  566. assert_equal('Users', ticket2.group.name)
  567. assert_equal('new', ticket2.state.name)
  568. assert_equal('some attribute text', ticket2.attribute1)
  569. assert_equal('1', ticket2.attribute2)
  570. assert_equal(Time.zone.parse('2016-05-12 00:59:59 UTC'), ticket2.attribute3)
  571. assert_equal(Date.parse('2016-05-11'), ticket2.attribute4)
  572. # update data_option null -> to_config
  573. attribute1 = ObjectManager::Attribute.add(
  574. object: 'Ticket',
  575. name: 'attribute1',
  576. display: 'Attribute 1',
  577. data_type: 'input',
  578. data_option: {
  579. maxlength: 200,
  580. type: 'text',
  581. null: false,
  582. },
  583. active: true,
  584. screens: {},
  585. position: 20,
  586. created_by_id: 1,
  587. updated_by_id: 1,
  588. )
  589. assert(attribute1)
  590. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  591. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  592. assert_equal(1, ObjectManager::Attribute.where(to_config: true).count)
  593. assert_equal(1, ObjectManager::Attribute.migrations.count)
  594. # execute migrations
  595. assert(ObjectManager::Attribute.migration_execute)
  596. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  597. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  598. assert_equal(0, ObjectManager::Attribute.where(to_config: true).count)
  599. assert_equal(0, ObjectManager::Attribute.migrations.count)
  600. # update data_option maxlength -> to_config && to_migrate
  601. attribute1 = ObjectManager::Attribute.add(
  602. object: 'Ticket',
  603. name: 'attribute1',
  604. display: 'Attribute 1',
  605. data_type: 'input',
  606. data_option: {
  607. maxlength: 250,
  608. type: 'text',
  609. null: false,
  610. },
  611. active: true,
  612. screens: {},
  613. position: 20,
  614. created_by_id: 1,
  615. updated_by_id: 1,
  616. )
  617. assert(attribute1)
  618. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  619. assert_equal(1, ObjectManager::Attribute.where(to_migrate: true).count)
  620. assert_equal(1, ObjectManager::Attribute.where(to_config: true).count)
  621. assert_equal(1, ObjectManager::Attribute.migrations.count)
  622. # execute migrations
  623. assert(ObjectManager::Attribute.migration_execute)
  624. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  625. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  626. assert_equal(0, ObjectManager::Attribute.where(to_config: true).count)
  627. assert_equal(0, ObjectManager::Attribute.migrations.count)
  628. # remove attribute
  629. ObjectManager::Attribute.remove(
  630. object: 'Ticket',
  631. name: 'attribute1',
  632. )
  633. ObjectManager::Attribute.remove(
  634. object: 'Ticket',
  635. name: 'attribute2',
  636. )
  637. ObjectManager::Attribute.remove(
  638. object: 'Ticket',
  639. name: 'attribute3',
  640. )
  641. ObjectManager::Attribute.remove(
  642. object: 'Ticket',
  643. name: 'attribute4',
  644. )
  645. assert(ObjectManager::Attribute.migration_execute)
  646. ticket2 = Ticket.find(ticket2.id)
  647. assert('ticket2 created', ticket2)
  648. assert_equal('some attribute test2', ticket2.title)
  649. assert_equal('Users', ticket2.group.name)
  650. assert_equal('new', ticket2.state.name)
  651. assert_nil(ticket2[:attribute1])
  652. assert_nil(ticket2[:attribute2])
  653. assert_nil(ticket2[:attribute3])
  654. assert_nil(ticket2[:attribute4])
  655. end
  656. test 'c object manager attribute - certain names' do
  657. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  658. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  659. assert_equal(0, ObjectManager::Attribute.migrations.count)
  660. attribute1 = ObjectManager::Attribute.add(
  661. object: 'Ticket',
  662. name: '1_a_anfrage_status',
  663. display: '1_a_anfrage_status',
  664. data_type: 'input',
  665. data_option: {
  666. maxlength: 200,
  667. type: 'text',
  668. null: true,
  669. },
  670. active: true,
  671. screens: {},
  672. position: 20,
  673. created_by_id: 1,
  674. updated_by_id: 1,
  675. )
  676. assert(attribute1)
  677. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  678. assert_equal(1, ObjectManager::Attribute.where(to_migrate: true).count)
  679. assert_equal(1, ObjectManager::Attribute.migrations.count)
  680. # execute migrations
  681. assert(ObjectManager::Attribute.migration_execute)
  682. assert_equal(false, ObjectManager::Attribute.pending_migration?)
  683. assert_equal(0, ObjectManager::Attribute.where(to_migrate: true).count)
  684. assert_equal(0, ObjectManager::Attribute.migrations.count)
  685. # create example ticket
  686. ticket1 = Ticket.create!(
  687. title: 'some attribute test3',
  688. group: Group.lookup(name: 'Users'),
  689. customer_id: 2,
  690. state: Ticket::State.lookup(name: 'new'),
  691. priority: Ticket::Priority.lookup(name: '2 normal'),
  692. '1_a_anfrage_status': 'some attribute text',
  693. updated_by_id: 1,
  694. created_by_id: 1,
  695. )
  696. assert('ticket1 created', ticket1)
  697. assert_equal('some attribute test3', ticket1.title)
  698. assert_equal('Users', ticket1.group.name)
  699. assert_equal('new', ticket1.state.name)
  700. assert_equal('some attribute text', ticket1['1_a_anfrage_status'])
  701. condition = {
  702. 'ticket.title' => {
  703. operator: 'is',
  704. value: 'some attribute test3',
  705. },
  706. }
  707. ticket_count, tickets = Ticket.selectors(condition, 10)
  708. assert_equal(ticket_count, 1)
  709. assert_equal(tickets[0].id, ticket1.id)
  710. condition = {
  711. 'ticket.1_a_anfrage_status' => {
  712. operator: 'is',
  713. value: 'some attribute text',
  714. },
  715. }
  716. ticket_count, tickets = Ticket.selectors(condition, 10)
  717. assert_equal(ticket_count, 1)
  718. assert_equal(tickets[0].id, ticket1.id)
  719. agent1 = User.create_or_update(
  720. login: 'agent1@example.com',
  721. firstname: 'Notification',
  722. lastname: 'Agent1',
  723. email: 'agent1@example.com',
  724. password: 'agentpw',
  725. active: true,
  726. roles: Role.where(name: 'Agent'),
  727. groups: Group.all,
  728. updated_by_id: 1,
  729. created_by_id: 1,
  730. )
  731. overview1 = Overview.create!(
  732. name: 'Overview1',
  733. link: 'my_overview',
  734. roles: Role.all,
  735. condition: {
  736. 'ticket.1_a_anfrage_status' => {
  737. operator: 'is',
  738. value: 'some attribute text',
  739. },
  740. },
  741. order: {
  742. by: '1_a_anfrage_status',
  743. direction: 'DESC',
  744. },
  745. group_by: '1_a_anfrage_status',
  746. view: {
  747. d: %w[title customer state created_at],
  748. s: %w[number title customer state created_at],
  749. m: %w[number title customer state created_at],
  750. view_mode_default: 's',
  751. },
  752. prio: 1,
  753. updated_by_id: 1,
  754. created_by_id: 1,
  755. )
  756. result = Ticket::Overviews.index(agent1)
  757. overview = nil
  758. result.each do |local_overview|
  759. next if local_overview[:overview][:name] != 'Overview1'
  760. overview = local_overview
  761. break
  762. end
  763. assert(overview)
  764. assert_equal(1, overview[:tickets].count)
  765. assert_equal(1, overview[:count])
  766. assert_equal(ticket1.id, overview[:tickets][0][:id])
  767. end
  768. test 'd object manager attribute - update attribute type' do
  769. attribute1 = ObjectManager::Attribute.add(
  770. object: 'Ticket',
  771. name: 'example_1',
  772. display: 'example_1',
  773. data_type: 'input',
  774. data_option: {
  775. default: '',
  776. maxlength: 200,
  777. type: 'text',
  778. null: true,
  779. options: {},
  780. },
  781. active: true,
  782. screens: {},
  783. position: 20,
  784. created_by_id: 1,
  785. updated_by_id: 1,
  786. )
  787. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  788. assert_equal(1, ObjectManager::Attribute.migrations.count)
  789. assert(ObjectManager::Attribute.migration_execute)
  790. assert_raises(RuntimeError) do
  791. ObjectManager::Attribute.add(
  792. object: 'Ticket',
  793. name: 'example_1',
  794. display: 'example_1',
  795. data_type: 'boolean',
  796. data_option: {
  797. default: true,
  798. options: {
  799. true: 'Yes',
  800. false: 'No',
  801. },
  802. null: false,
  803. },
  804. active: true,
  805. screens: {},
  806. position: 200,
  807. created_by_id: 1,
  808. updated_by_id: 1,
  809. )
  810. end
  811. attribute2 = ObjectManager::Attribute.add(
  812. object: 'Ticket',
  813. name: 'example_1',
  814. display: 'example_1',
  815. data_type: 'select',
  816. data_option: {
  817. default: '',
  818. maxlength: 200,
  819. type: 'text',
  820. null: true,
  821. options: {
  822. aa: 'aa',
  823. bb: 'bb',
  824. },
  825. },
  826. active: true,
  827. screens: {},
  828. position: 20,
  829. created_by_id: 1,
  830. updated_by_id: 1,
  831. )
  832. assert_equal(attribute1.id, attribute2.id)
  833. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  834. assert_equal(1, ObjectManager::Attribute.migrations.count)
  835. assert(ObjectManager::Attribute.migration_execute)
  836. end
  837. test 'overview any owner / no owner is set' do
  838. group = Group.create!(
  839. name: 'OverviewTest',
  840. updated_at: '2015-02-05 16:37:00',
  841. updated_by_id: 1,
  842. created_by_id: 1,
  843. )
  844. roles = Role.where(name: 'Agent')
  845. agent1 = User.create!(
  846. login: 'ticket-overview-agent1@example.com',
  847. firstname: 'Overview',
  848. lastname: 'Agent1',
  849. email: 'ticket-overview-agent1@example.com',
  850. password: 'agentpw',
  851. active: true,
  852. roles: roles,
  853. groups: [group],
  854. updated_at: '2015-02-05 16:37:00',
  855. updated_by_id: 1,
  856. created_by_id: 1,
  857. )
  858. attribute1 = ObjectManager::Attribute.add(
  859. object: 'Ticket',
  860. name: 'watcher',
  861. display: 'watcher',
  862. data_type: 'select',
  863. data_option: {
  864. default: '',
  865. maxlength: 200,
  866. type: 'text',
  867. null: true,
  868. options: {
  869. aa: 'agent a',
  870. bb: 'agent b',
  871. cc: 'agent c',
  872. },
  873. },
  874. active: true,
  875. screens: {},
  876. position: 20,
  877. created_by_id: 1,
  878. updated_by_id: 1,
  879. )
  880. assert_equal(true, ObjectManager::Attribute.pending_migration?)
  881. assert_equal(1, ObjectManager::Attribute.migrations.count)
  882. assert(ObjectManager::Attribute.migration_execute)
  883. Ticket.destroy_all
  884. Overview.destroy_all
  885. UserInfo.current_user_id = 1
  886. overview_role = Role.find_by(name: 'Agent')
  887. overview1 = Overview.create!(
  888. name: 'not watched',
  889. prio: 1000,
  890. role_ids: [overview_role.id],
  891. condition: {
  892. 'ticket.watcher' => {
  893. operator: 'is',
  894. value: '',
  895. },
  896. },
  897. order: {
  898. by: 'created_at',
  899. direction: 'ASC',
  900. },
  901. view: {
  902. d: %w[title customer group created_at],
  903. s: %w[title customer group created_at],
  904. m: %w[number title customer group created_at],
  905. view_mode_default: 's',
  906. },
  907. )
  908. overview2 = Overview.create!(
  909. name: 'not watched by somebody',
  910. prio: 2000,
  911. role_ids: [overview_role.id],
  912. condition: {
  913. 'ticket.watcher' => {
  914. operator: 'is not',
  915. value: '',
  916. },
  917. },
  918. order: {
  919. by: 'created_at',
  920. direction: 'ASC',
  921. },
  922. view: {
  923. d: %w[title customer group created_at],
  924. s: %w[title customer group created_at],
  925. m: %w[number title customer group created_at],
  926. view_mode_default: 's',
  927. },
  928. )
  929. overview3 = Overview.create!(
  930. name: 'not watched as array',
  931. prio: 3000,
  932. role_ids: [overview_role.id],
  933. condition: {
  934. 'ticket.watcher' => {
  935. operator: 'is',
  936. value: [''],
  937. },
  938. },
  939. order: {
  940. by: 'created_at',
  941. direction: 'ASC',
  942. },
  943. view: {
  944. d: %w[title customer group created_at],
  945. s: %w[title customer group created_at],
  946. m: %w[number title customer group created_at],
  947. view_mode_default: 's',
  948. },
  949. )
  950. overview4 = Overview.create!(
  951. name: 'not watched by somebody as array',
  952. prio: 4000,
  953. role_ids: [overview_role.id],
  954. condition: {
  955. 'ticket.watcher' => {
  956. operator: 'is not',
  957. value: [''],
  958. },
  959. },
  960. order: {
  961. by: 'created_at',
  962. direction: 'ASC',
  963. },
  964. view: {
  965. d: %w[title customer group created_at],
  966. s: %w[title customer group created_at],
  967. m: %w[number title customer group created_at],
  968. view_mode_default: 's',
  969. },
  970. )
  971. overview5 = Overview.create!(
  972. name: 'watched by aa',
  973. prio: 5000,
  974. role_ids: [overview_role.id],
  975. condition: {
  976. 'ticket.watcher' => {
  977. operator: 'is',
  978. value: 'aa',
  979. },
  980. },
  981. order: {
  982. by: 'created_at',
  983. direction: 'ASC',
  984. },
  985. view: {
  986. d: %w[title customer group created_at],
  987. s: %w[title customer group created_at],
  988. m: %w[number title customer group created_at],
  989. view_mode_default: 's',
  990. },
  991. )
  992. overview6 = Overview.create!(
  993. name: 'not watched by aa',
  994. prio: 6000,
  995. role_ids: [overview_role.id],
  996. condition: {
  997. 'ticket.watcher' => {
  998. operator: 'is not',
  999. value: 'aa',
  1000. },
  1001. },
  1002. order: {
  1003. by: 'created_at',
  1004. direction: 'ASC',
  1005. },
  1006. view: {
  1007. d: %w[title customer group created_at],
  1008. s: %w[title customer group created_at],
  1009. m: %w[number title customer group created_at],
  1010. view_mode_default: 's',
  1011. },
  1012. )
  1013. overview7 = Overview.create!(
  1014. name: 'watched by aa array',
  1015. prio: 7000,
  1016. role_ids: [overview_role.id],
  1017. condition: {
  1018. 'ticket.watcher' => {
  1019. operator: 'is',
  1020. value: ['aa'],
  1021. },
  1022. },
  1023. order: {
  1024. by: 'created_at',
  1025. direction: 'ASC',
  1026. },
  1027. view: {
  1028. d: %w[title customer group created_at],
  1029. s: %w[title customer group created_at],
  1030. m: %w[number title customer group created_at],
  1031. view_mode_default: 's',
  1032. },
  1033. )
  1034. overview8 = Overview.create!(
  1035. name: 'not watched by aa array',
  1036. prio: 8000,
  1037. role_ids: [overview_role.id],
  1038. condition: {
  1039. 'ticket.watcher' => {
  1040. operator: 'is not',
  1041. value: ['aa'],
  1042. },
  1043. },
  1044. order: {
  1045. by: 'created_at',
  1046. direction: 'ASC',
  1047. },
  1048. view: {
  1049. d: %w[title customer group created_at],
  1050. s: %w[title customer group created_at],
  1051. m: %w[number title customer group created_at],
  1052. view_mode_default: 's',
  1053. },
  1054. )
  1055. ticket1 = Ticket.create!(
  1056. title: 'overview test 1',
  1057. group: Group.lookup(name: 'OverviewTest'),
  1058. customer_id: 2,
  1059. owner_id: 1,
  1060. watcher: '',
  1061. state: Ticket::State.lookup(name: 'new'),
  1062. priority: Ticket::Priority.lookup(name: '2 normal'),
  1063. )
  1064. travel 2.seconds
  1065. ticket2 = Ticket.create!(
  1066. title: 'overview test 2',
  1067. group: Group.lookup(name: 'OverviewTest'),
  1068. customer_id: 2,
  1069. owner_id: nil,
  1070. watcher: nil,
  1071. state: Ticket::State.lookup(name: 'new'),
  1072. priority: Ticket::Priority.lookup(name: '2 normal'),
  1073. )
  1074. travel 2.seconds
  1075. ticket3 = Ticket.create!(
  1076. title: 'overview test 3',
  1077. group: Group.lookup(name: 'OverviewTest'),
  1078. customer_id: 2,
  1079. owner_id: agent1.id,
  1080. watcher: 'aa',
  1081. state: Ticket::State.lookup(name: 'new'),
  1082. priority: Ticket::Priority.lookup(name: '2 normal'),
  1083. )
  1084. result = Ticket::Overviews.index(agent1)
  1085. assert_equal(result[0][:overview][:id], overview1.id)
  1086. assert_equal(result[0][:overview][:name], 'not watched')
  1087. assert_equal(result[0][:overview][:view], 'not_watched')
  1088. assert_equal(result[0][:tickets].class, Array)
  1089. assert_equal(result[0][:tickets][0][:id], ticket1.id)
  1090. assert_equal(result[0][:tickets][1][:id], ticket2.id)
  1091. assert_equal(result[0][:count], 2)
  1092. assert_equal(result[1][:overview][:id], overview2.id)
  1093. assert_equal(result[1][:overview][:name], 'not watched by somebody')
  1094. assert_equal(result[1][:overview][:view], 'not_watched_by_somebody')
  1095. assert_equal(result[1][:tickets].class, Array)
  1096. assert_equal(result[1][:tickets][0][:id], ticket3.id)
  1097. assert_equal(result[1][:count], 1)
  1098. assert_equal(result[2][:overview][:id], overview3.id)
  1099. assert_equal(result[2][:overview][:name], 'not watched as array')
  1100. assert_equal(result[2][:overview][:view], 'not_watched_as_array')
  1101. assert_equal(result[2][:tickets].class, Array)
  1102. assert_equal(result[2][:tickets][0][:id], ticket1.id)
  1103. assert_equal(result[2][:tickets][1][:id], ticket2.id)
  1104. assert_equal(result[2][:count], 2)
  1105. assert_equal(result[3][:overview][:id], overview4.id)
  1106. assert_equal(result[3][:overview][:name], 'not watched by somebody as array')
  1107. assert_equal(result[3][:overview][:view], 'not_watched_by_somebody_as_array')
  1108. assert_equal(result[3][:tickets].class, Array)
  1109. assert_equal(result[3][:tickets][0][:id], ticket3.id)
  1110. assert_equal(result[3][:count], 1)
  1111. assert_equal(result[4][:overview][:id], overview5.id)
  1112. assert_equal(result[4][:overview][:name], 'watched by aa')
  1113. assert_equal(result[4][:overview][:view], 'watched_by_aa')
  1114. assert_equal(result[4][:tickets].class, Array)
  1115. assert_equal(result[4][:tickets][0][:id], ticket3.id)
  1116. assert_equal(result[4][:count], 1)
  1117. assert_equal(result[5][:overview][:id], overview6.id)
  1118. assert_equal(result[5][:overview][:name], 'not watched by aa')
  1119. assert_equal(result[5][:overview][:view], 'not_watched_by_aa')
  1120. assert_equal(result[5][:tickets].class, Array)
  1121. assert_equal(result[5][:tickets][0][:id], ticket1.id)
  1122. assert_equal(result[5][:tickets][1][:id], ticket2.id)
  1123. assert_equal(result[5][:count], 2)
  1124. assert_equal(result[6][:overview][:id], overview7.id)
  1125. assert_equal(result[6][:overview][:name], 'watched by aa array')
  1126. assert_equal(result[6][:overview][:view], 'watched_by_aa_array')
  1127. assert_equal(result[6][:tickets].class, Array)
  1128. assert_equal(result[6][:tickets][0][:id], ticket3.id)
  1129. assert_equal(result[6][:count], 1)
  1130. assert_equal(result[7][:overview][:id], overview8.id)
  1131. assert_equal(result[7][:overview][:name], 'not watched by aa array')
  1132. assert_equal(result[7][:overview][:view], 'not_watched_by_aa_array')
  1133. assert_equal(result[7][:tickets].class, Array)
  1134. assert_equal(result[7][:tickets][0][:id], ticket1.id)
  1135. assert_equal(result[7][:tickets][1][:id], ticket2.id)
  1136. assert_equal(result[7][:count], 2)
  1137. end
  1138. end