|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../db/migrate/20250318024953_copy_solved_topic_custom_field_to_discourse_solved_solved_topics" |
| 4 | + |
| 5 | +module DiscourseSolved |
| 6 | + describe CopySolvedTopicCustomFieldToDiscourseSolvedSolvedTopics do |
| 7 | + let(:migration) { described_class.new } |
| 8 | + |
| 9 | + it "copies accepted answer from custom fields to table" do |
| 10 | + topic = Fabricate(:topic) |
| 11 | + post = Fabricate(:post, topic: topic) |
| 12 | + acting_user = Fabricate(:user) |
| 13 | + |
| 14 | + TopicCustomField.create!( |
| 15 | + topic: topic, |
| 16 | + name: "accepted_answer_post_id", |
| 17 | + value: post.id.to_s, |
| 18 | + created_at: 1.day.ago, |
| 19 | + updated_at: 1.day.ago, |
| 20 | + ) |
| 21 | + TopicCustomField.create!(topic: topic, name: "solved_auto_close_topic_timer_id", value: "123") |
| 22 | + UserAction.create!( |
| 23 | + action_type: 15, |
| 24 | + user_id: acting_user.id, |
| 25 | + acting_user_id: acting_user.id, |
| 26 | + target_topic_id: topic.id, |
| 27 | + created_at: 1.hour.ago, |
| 28 | + ) |
| 29 | + |
| 30 | + migration.up |
| 31 | + |
| 32 | + solved_topic = DiscourseSolved::SolvedTopic.last |
| 33 | + expect(solved_topic.topic_id).to eq(topic.id) |
| 34 | + expect(solved_topic.answer_post_id).to eq(post.id) |
| 35 | + expect(solved_topic.topic_timer_id).to eq(123) |
| 36 | + expect(solved_topic.accepter_user_id).to eq(acting_user.id) |
| 37 | + end |
| 38 | + |
| 39 | + it "uses the most recent user action for accepter" do |
| 40 | + topic = Fabricate(:topic) |
| 41 | + post = Fabricate(:post, topic: topic) |
| 42 | + user1 = Fabricate(:user) |
| 43 | + user2 = Fabricate(:user) |
| 44 | + |
| 45 | + TopicCustomField.create!(topic: topic, name: "accepted_answer_post_id", value: post.id.to_s) |
| 46 | + UserAction.create!( |
| 47 | + action_type: 15, |
| 48 | + user_id: user1.id, |
| 49 | + acting_user_id: user1.id, |
| 50 | + target_topic_id: topic.id, |
| 51 | + created_at: 2.hours.ago, |
| 52 | + ) |
| 53 | + UserAction.create!( |
| 54 | + action_type: 15, |
| 55 | + user_id: user2.id, |
| 56 | + acting_user_id: user2.id, |
| 57 | + target_topic_id: topic.id, |
| 58 | + created_at: 1.hour.ago, |
| 59 | + ) |
| 60 | + |
| 61 | + migration.up |
| 62 | + |
| 63 | + expect(DiscourseSolved::SolvedTopic.last.accepter_user_id).to eq(user2.id) |
| 64 | + end |
| 65 | + end |
| 66 | +end |
0 commit comments