2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-02 12:50:54 +08:00
|
|
|
require "import_export"
|
2017-10-27 04:29:36 +08:00
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe ImportExport::Importer do
|
2017-11-02 17:33:36 +08:00
|
|
|
before { STDOUT.stubs(:write) }
|
|
|
|
|
2017-10-27 04:29:36 +08:00
|
|
|
let(:import_data) do
|
|
|
|
import_file = Rack::Test::UploadedFile.new(file_from_fixtures("import-export.json", "json"))
|
2019-10-02 12:50:54 +08:00
|
|
|
ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(import_file.read))
|
2017-10-27 04:29:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def import(data)
|
|
|
|
ImportExport::Importer.new(data).perform
|
|
|
|
end
|
|
|
|
|
2022-07-27 18:21:10 +08:00
|
|
|
describe ".perform" do
|
2017-10-27 04:29:36 +08:00
|
|
|
it "topics and users" do
|
|
|
|
data = import_data.dup
|
|
|
|
data[:categories] = nil
|
|
|
|
data[:groups] = nil
|
|
|
|
|
2022-07-19 22:03:03 +08:00
|
|
|
expect { import(data) }.to not_change { Category.count }.and not_change {
|
|
|
|
Group.count
|
2017-10-27 04:29:36 +08:00
|
|
|
}.and change { Topic.count }.by(2).and change { User.count }.by(2)
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with categories and groups" do
|
2020-01-31 00:46:33 +08:00
|
|
|
it "works" do
|
|
|
|
data = import_data.dup
|
|
|
|
data[:topics] = nil
|
|
|
|
data[:users] = nil
|
2017-10-27 04:29:36 +08:00
|
|
|
|
2020-01-31 00:46:33 +08:00
|
|
|
expect { import(data) }.to change { Category.count }.by(6).and change { Group.count }.by(
|
2023-01-09 19:18:21 +08:00
|
|
|
2,
|
2020-01-31 00:46:33 +08:00
|
|
|
).and change { Topic.count }.by(6).and not_change { User.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works with sub-sub-categories" do
|
|
|
|
data = import_data.dup
|
|
|
|
|
|
|
|
# 11 -> 10 -> 15
|
|
|
|
data[:categories].find { |c| c[:id] == 10 }[:parent_category_id] = 11
|
|
|
|
data[:categories].find { |c| c[:id] == 15 }[:parent_category_id] = 10
|
|
|
|
|
|
|
|
expect { import(data) }.to change { Category.count }.by(6).and change {
|
|
|
|
SiteSetting.max_category_nesting
|
|
|
|
}.from(2).to(3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "fixes permissions" do
|
|
|
|
data = import_data.dup
|
|
|
|
data[:categories].find { |c| c[:id] == 10 }[:permissions_params] = { custom_group: 1 }
|
|
|
|
data[:categories].find { |c| c[:id] == 15 }[:permissions_params] = { staff: 1 }
|
|
|
|
|
|
|
|
permissions = data[:categories].find { |c| c[:id] == 10 }[:permissions_params]
|
|
|
|
|
|
|
|
expect { import(data) }.to change { Category.count }.by(6).and change {
|
|
|
|
permissions[:staff]
|
|
|
|
}.from(nil).to(1)
|
|
|
|
end
|
2017-10-27 04:29:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "categories, groups and users" do
|
|
|
|
data = import_data.dup
|
|
|
|
data[:topics] = nil
|
|
|
|
|
|
|
|
expect { import(data) }.to change { Category.count }.by(6).and change { Group.count }.by(
|
2023-01-09 19:18:21 +08:00
|
|
|
2,
|
2017-10-27 04:29:36 +08:00
|
|
|
).and change { Topic.count }.by(6).and change { User.count }.by(2)
|
|
|
|
end
|
|
|
|
|
2020-04-18 05:59:54 +08:00
|
|
|
it "groups" do
|
|
|
|
data = import_data.dup
|
|
|
|
data[:categories] = nil
|
|
|
|
data[:topics] = nil
|
|
|
|
data[:users] = nil
|
|
|
|
|
2022-07-19 22:03:03 +08:00
|
|
|
expect { import(data) }.to not_change { Category.count }.and change { Group.count }.by(
|
2023-01-09 19:18:21 +08:00
|
|
|
2,
|
2022-07-19 22:03:03 +08:00
|
|
|
).and not_change { Topic.count }.and not_change { User.count }
|
2020-04-18 05:59:54 +08:00
|
|
|
end
|
|
|
|
|
2017-10-27 04:29:36 +08:00
|
|
|
it "all" do
|
|
|
|
expect { import(import_data) }.to change { Category.count }.by(6).and change {
|
|
|
|
Group.count
|
|
|
|
}.by(2).and change { Topic.count }.by(8).and change { User.count }.by(2).and change {
|
2022-10-06 03:22:16 +08:00
|
|
|
TranslationOverride.count
|
|
|
|
}.by(1)
|
2017-10-27 04:29:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|