answer.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (C) 2012-2022 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 { create(: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/upload/hello_world.txt') }
  46. end
  47. after(:create) do |answer, context|
  48. Store.add(
  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. )
  55. end
  56. end
  57. end
  58. end