From bdf8815b71986fe2b4fc6c8a05ca9b906a36c673 Mon Sep 17 00:00:00 2001 From: Harry Wood Date: Tue, 31 Jan 2023 10:10:06 +0000 Subject: [PATCH] DEV: Add a test for create_post in import scripts (#18893) Add some testing of the `create_post` method in ImportScripts::Base Basic test of Post creation and (if enabled) the bbcode_to_md call. --- spec/script/import_scripts/base_spec.rb | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/spec/script/import_scripts/base_spec.rb b/spec/script/import_scripts/base_spec.rb index 3f637f4d8b..86a9ba42df 100644 --- a/spec/script/import_scripts/base_spec.rb +++ b/spec/script/import_scripts/base_spec.rb @@ -54,4 +54,34 @@ RSpec.describe ImportScripts::Base do MockSpecImporter.new(import_data).perform expect(SiteSetting.purge_unactivated_users_grace_period_days).to eq(0) end + + describe "#create_post" do + let(:importer) { described_class.new } + fab!(:user) { Fabricate(:user) } + let(:post_params) { + { + user_id: user.id, + raw: "Test post [b]content[/b]", + title: "Test topic for post" + } + } + + it "creates a Post" do + expect { + importer.create_post(post_params, 123) + }.to change { Post.count }.by(1) + end + + if ENV["IMPORT"] == "1" + it "uses the ruby-bbcode-to-md gem (conditional Gemfile option)" do + expect(String.method_defined?(:bbcode_to_md)).to be true + end + + it "converts bbcode to markdown when specified" do + importer.instance_variable_set(:@bbcode_to_md, true) + importer.create_post(post_params, 123) + expect(Post.first.raw).to eq "Test post **content**" + end + end + end end