answer.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. FactoryBot.define do
  3. factory 'knowledge_base/answer', aliases: %i[knowledge_base_answer] do
  4. transient do
  5. add_translation { true }
  6. translation_traits { [] }
  7. translation_attributes { {} }
  8. knowledge_base { nil }
  9. end
  10. category { association :knowledge_base_category, **{ knowledge_base: knowledge_base }.compact }
  11. before(:create) do |answer, context|
  12. next if answer.translations.present?
  13. answer.translations << build(:'knowledge_base/answer/translation', *context.translation_traits, answer: answer, **context.translation_attributes)
  14. end
  15. trait :draft # empty placeholder for better readability
  16. trait :internal do
  17. internal_at { 1.week.ago }
  18. end
  19. trait :published do
  20. published_at { 1.week.ago }
  21. end
  22. trait :archived do
  23. archived_at { 1.week.ago }
  24. end
  25. trait :with_video do
  26. transient do
  27. translation_traits { [:with_video] }
  28. end
  29. end
  30. trait :with_image do
  31. transient do
  32. translation_traits { [:with_image] }
  33. end
  34. end
  35. trait :with_tag do
  36. transient do
  37. tag_names { %w[example_kb_tag] }
  38. end
  39. after(:create) do |answer, context|
  40. context.tag_names.each { |tag| answer.tag_add tag }
  41. end
  42. end
  43. trait :with_attachment do
  44. transient do
  45. attachment { File.open('spec/fixtures/files/upload/hello_world.txt') }
  46. end
  47. after(:create) do |answer, context|
  48. create(:store,
  49. object: answer.class.name,
  50. o_id: answer.id,
  51. data: context.attachment.read,
  52. filename: File.basename(context.attachment.path),
  53. preferences: {})
  54. end
  55. end
  56. end
  57. end