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