object_manager_test.rb 33 KB

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