attribute_spec.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ObjectManager::Attribute, type: :model do
  4. describe 'callbacks' do
  5. context 'for setting default values on local data options' do
  6. subject(:attr) { described_class.new }
  7. context ':null' do
  8. it 'sets nil values to true' do
  9. expect { attr.validate }
  10. .to change { attr.data_option[:null] }.to(true)
  11. end
  12. it 'does not overwrite false values' do
  13. attr.data_option[:null] = false
  14. expect { attr.validate }
  15. .not_to change { attr.data_option[:null] }
  16. end
  17. end
  18. context ':maxlength' do
  19. context 'for data_type: select / tree_select / checkbox' do
  20. subject(:attr) { described_class.new(data_type: 'select') }
  21. it 'sets nil values to 255' do
  22. expect { attr.validate }
  23. .to change { attr.data_option[:maxlength] }.to(255)
  24. end
  25. end
  26. end
  27. context ':nulloption' do
  28. context 'for data_type: select / tree_select / checkbox' do
  29. subject(:attr) { described_class.new(data_type: 'select') }
  30. it 'sets nil values to true' do
  31. expect { attr.validate }
  32. .to change { attr.data_option[:nulloption] }.to(true)
  33. end
  34. it 'does not overwrite false values' do
  35. attr.data_option[:nulloption] = false
  36. expect { attr.validate }
  37. .not_to change { attr.data_option[:nulloption] }
  38. end
  39. end
  40. end
  41. end
  42. end
  43. describe 'check name' do
  44. it 'rejects ActiveRecord reserved word "attribute"' do
  45. expect do
  46. described_class.add attributes_for :object_manager_attribute_text, name: 'attribute'
  47. end.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Name attribute is a reserved word')
  48. end
  49. ObjectManager::Attribute::RESERVED_NAMES.each do |reserved_word|
  50. it "rejects Zammad reserved word '#{reserved_word}'" do
  51. expect { described_class.add attributes_for :object_manager_attribute_text, name: reserved_word }.to raise_error(ActiveRecord::RecordInvalid, %r{is a reserved word})
  52. end
  53. end
  54. ObjectManager::Attribute::RESERVED_NAMES_PER_MODEL.each do |object, reserved_names|
  55. reserved_names.each do |reserved_word|
  56. it "rejects Zammad reserved word '#{reserved_word}' for model '#{object}'" do
  57. expect { described_class.add attributes_for :object_manager_attribute_text, name: reserved_word, object_name: object }.to raise_error(ActiveRecord::RecordInvalid, %r{is a reserved word})
  58. end
  59. end
  60. end
  61. %w[someting_id something_ids].each do |reserved_word|
  62. it "rejects word '#{reserved_word}' which is used for database references" do
  63. expect do
  64. described_class.add attributes_for :object_manager_attribute_text, name: reserved_word
  65. end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name can't be used because *_id and *_ids are not allowed")
  66. end
  67. end
  68. %w[title tags number].each do |not_editable_attribute|
  69. it "rejects '#{not_editable_attribute}' which is used" do
  70. expect do
  71. described_class.add attributes_for :object_manager_attribute_text, name: not_editable_attribute
  72. end.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Name attribute is not editable')
  73. end
  74. end
  75. %w[priority state note].each do |existing_attribute|
  76. it "rejects '#{existing_attribute}' which is used" do
  77. expect do
  78. described_class.add attributes_for :object_manager_attribute_text, name: existing_attribute
  79. end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name #{existing_attribute} already exists")
  80. end
  81. end
  82. it 'rejects duplicate attribute name of conflicting types' do
  83. attribute = attributes_for(:object_manager_attribute_text)
  84. described_class.add attribute
  85. attribute[:data_type] = 'boolean'
  86. expect do
  87. described_class.add attribute
  88. end.to raise_error ActiveRecord::RecordInvalid
  89. end
  90. it 'accepts duplicate attribute name on the same types (editing an existing attribute)' do
  91. attribute = attributes_for(:object_manager_attribute_text)
  92. described_class.add attribute
  93. expect do
  94. described_class.add attribute
  95. end.not_to raise_error
  96. end
  97. it 'accepts duplicate attribute name on compatible types (editing the type of an existing attribute)' do
  98. attribute = attributes_for(:object_manager_attribute_text)
  99. described_class.add attribute
  100. attribute[:data_type] = 'select'
  101. attribute[:data_option_new] = { default: '', options: { 'a' => 'a' } }
  102. expect do
  103. described_class.add attribute
  104. end.not_to raise_error
  105. end
  106. it 'accepts valid attribute names' do
  107. expect do
  108. described_class.add attributes_for :object_manager_attribute_text
  109. end.not_to raise_error
  110. end
  111. end
  112. describe 'validate that referenced attributes are not set as inactive' do
  113. subject(:attr) { create(:object_manager_attribute_text) }
  114. before do
  115. allow(described_class)
  116. .to receive(:attribute_used_by_references?)
  117. .with(attr.object_lookup.name, attr.name)
  118. .and_return(is_referenced)
  119. attr.active = active
  120. end
  121. context 'when is used and changing to inactive' do
  122. let(:active) { false }
  123. let(:is_referenced) { true }
  124. it { is_expected.not_to be_valid }
  125. it do
  126. attr.valid?
  127. expect(attr.errors).not_to be_blank
  128. end
  129. end
  130. context 'when is not used and changing to inactive' do
  131. let(:active) { false }
  132. let(:is_referenced) { false }
  133. it { is_expected.to be_valid }
  134. end
  135. context 'when is used and staying active and chan' do
  136. let(:active) { true }
  137. let(:is_referenced) { true }
  138. it { is_expected.to be_valid }
  139. end
  140. end
  141. describe 'Class methods:' do
  142. describe '.pending_migration?', db_strategy: :reset do
  143. it 'returns false if there are no pending migrations' do
  144. expect(described_class.pending_migration?).to be false
  145. end
  146. it 'returns true if there are pending migrations' do
  147. create(:object_manager_attribute_text)
  148. expect(described_class.pending_migration?).to be true
  149. end
  150. it 'returns false if migration was executed' do
  151. create(:object_manager_attribute_text)
  152. described_class.migration_execute
  153. expect(described_class.pending_migration?).to be false
  154. end
  155. end
  156. describe '.attribute_to_references_hash_objects' do
  157. it 'returns classes with conditions' do
  158. expect(described_class.attribute_to_references_hash_objects).to contain_exactly(Trigger, Overview, Job, Sla, Report::Profile)
  159. end
  160. end
  161. describe '.data_options_hash' do
  162. context 'when hash' do
  163. let(:check) do
  164. {
  165. 'a' => 'A',
  166. 'b' => 'B',
  167. 'c' => 'c',
  168. }
  169. end
  170. it 'does return the options as hash' do
  171. expect(described_class.data_options_hash(check)).to eq({
  172. 'a' => 'A',
  173. 'b' => 'B',
  174. 'c' => 'c',
  175. })
  176. end
  177. end
  178. context 'when array' do
  179. let(:check) do
  180. [
  181. {
  182. value: 'a',
  183. name: 'A',
  184. },
  185. {
  186. value: 'b',
  187. name: 'B',
  188. },
  189. {
  190. value: 'c',
  191. name: 'c',
  192. },
  193. ]
  194. end
  195. it 'does return the options as hash' do
  196. expect(described_class.data_options_hash(check)).to eq({
  197. 'a' => 'A',
  198. 'b' => 'B',
  199. 'c' => 'c',
  200. })
  201. end
  202. end
  203. context 'when tree array' do
  204. let(:check) do
  205. [
  206. {
  207. value: 'a',
  208. name: 'A',
  209. },
  210. {
  211. value: 'b',
  212. name: 'B',
  213. },
  214. {
  215. value: 'c',
  216. name: 'c',
  217. children: [
  218. {
  219. value: 'c::a',
  220. name: 'c sub a',
  221. },
  222. {
  223. value: 'c::b',
  224. name: 'c sub b',
  225. },
  226. {
  227. value: 'c::c',
  228. name: 'c sub c',
  229. },
  230. ],
  231. },
  232. ]
  233. end
  234. it 'does return the options as hash' do
  235. expect(described_class.data_options_hash(check)).to eq({
  236. 'a' => 'A',
  237. 'b' => 'B',
  238. 'c' => 'c',
  239. 'c::a' => 'c sub a',
  240. 'c::b' => 'c sub b',
  241. 'c::c' => 'c sub c',
  242. })
  243. end
  244. end
  245. end
  246. end
  247. describe 'Data options validation' do
  248. it 'calls ObjectManager::Attribute::DataOptionValidator' do
  249. record = described_class.new
  250. expect_any_instance_of(described_class::DataOptionValidator).to receive(:validate).with(record)
  251. record.valid?
  252. end
  253. end
  254. describe 'undefined method `to_hash` on editing select fields in the admin interface after migration to 5.1 #4027', db_strategy: :reset do
  255. let(:select_field) { create(:object_manager_attribute_select) }
  256. before do
  257. described_class.migration_execute
  258. end
  259. it 'does save the attribute with sorted options' do
  260. add = select_field.attributes.deep_symbolize_keys
  261. add[:data_option_new] = add[:data_option]
  262. add[:data_option_new][:options] = [
  263. {
  264. name: 'a',
  265. value: 'a',
  266. },
  267. {
  268. name: 'b',
  269. value: 'b',
  270. },
  271. {
  272. name: 'c',
  273. value: 'c',
  274. },
  275. ]
  276. described_class.add(add)
  277. described_class.migration_execute
  278. expect_result = {
  279. 'key_1' => 'value_1',
  280. 'key_2' => 'value_2',
  281. 'key_3' => 'value_3',
  282. 'a' => 'a',
  283. 'b' => 'b',
  284. 'c' => 'c'
  285. }
  286. expect(select_field.reload.data_option[:historical_options]).to eq(expect_result)
  287. end
  288. end
  289. describe '#add' do
  290. context 'when data is valid' do
  291. let(:attribute) do
  292. {
  293. object: 'Ticket',
  294. name: 'test1',
  295. display: 'Test 1',
  296. data_type: 'input',
  297. data_option: {
  298. maxlength: 200,
  299. type: 'text',
  300. null: false,
  301. },
  302. active: true,
  303. screens: {},
  304. position: 20,
  305. created_by_id: 1,
  306. updated_by_id: 1,
  307. editable: false,
  308. to_migrate: false,
  309. }
  310. end
  311. it 'is successful' do
  312. expect { described_class.add(attribute) }.to change(described_class, :count)
  313. expect(described_class.get(object: 'Ticket', name: 'test1')).to have_attributes(attribute)
  314. end
  315. end
  316. context 'when data is invalid' do
  317. let(:attribute) do
  318. {
  319. object: 'Ticket',
  320. name: 'test2_id',
  321. display: 'Test 2 with id',
  322. data_type: 'input',
  323. data_option: {
  324. maxlength: 200,
  325. type: 'text',
  326. null: false,
  327. },
  328. active: true,
  329. screens: {},
  330. position: 20,
  331. created_by_id: 1,
  332. updated_by_id: 1,
  333. }
  334. end
  335. it 'raises an error' do
  336. expect { described_class.add(attribute) }.to raise_error(ActiveRecord::RecordInvalid)
  337. end
  338. end
  339. context 'when adding a json field' do
  340. let(:expected_attributes) do
  341. {
  342. data_type: 'autocompletion_ajax_external_data_source',
  343. active: true,
  344. }
  345. end
  346. let(:attribute) { create(:object_manager_attribute_autocompletion_ajax_external_data_source) }
  347. it 'works on postgresql', db_adapter: :postgresql do
  348. expect { attribute }.to change(described_class, :count)
  349. expect(attribute).to have_attributes(expected_attributes)
  350. end
  351. it 'fails on mysql', db_adapter: :mysql do
  352. expect { attribute }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Data type can only be created on postgresql databases')
  353. end
  354. end
  355. end
  356. describe '#get' do
  357. context 'when attribute exists' do
  358. before do
  359. create(:object_manager_attribute_text, name: 'test3')
  360. end
  361. it 'returns the attribute' do
  362. expect(described_class.get(object: 'Ticket', name: 'test3')).to have_attributes(name: 'test3', editable: true)
  363. end
  364. end
  365. context 'when attribute does not exist' do
  366. it 'returns nil' do
  367. expect(described_class.get(object: 'Ticket', name: 'test4')).to be_nil
  368. end
  369. end
  370. end
  371. describe '#remove' do
  372. context 'when attribute exists' do
  373. before do
  374. create(:object_manager_attribute_text, name: 'test3')
  375. end
  376. it 'is successful' do
  377. expect { described_class.remove(object: 'Ticket', name: 'test3') }.to change(described_class, :count)
  378. expect(described_class.get(object: 'Ticket', name: 'test3')).to be_nil
  379. end
  380. end
  381. context 'when attribute does not exist' do
  382. it 'raises an error' do
  383. expect { described_class.remove(object: 'Ticket', name: 'test4') }.to raise_error(RuntimeError)
  384. end
  385. end
  386. end
  387. end