object_manager_test.rb 35 KB

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