Martin Brennan 222c8d9b6a
FEATURE: Polymorphic bookmarks pt. 3 (reminders, imports, exports, refactors) (#16591)
A bit of a mixed bag, this addresses several edge areas of bookmarks and makes them compatible with polymorphic bookmarks (hidden behind the `use_polymorphic_bookmarks` site setting). The main ones are:

* ExportUserArchive compatibility
* SyncTopicUserBookmarked job compatibility
* Sending different notifications for the bookmark reminders based on the bookmarkable type
* Import scripts compatibility
* BookmarkReminderNotificationHandler compatibility

This PR also refactors the `register_bookmarkable` API so it accepts a class descended from a `BaseBookmarkable` class instead. This was done because we kept having to add more and more lambdas/properties inline and it was very messy, so a factory pattern is cleaner. The classes can be tested independently as well.

Some later PRs will address some other areas like the discourse narrative bot, advanced search, reports, and the .ics endpoint for bookmarks.
2022-05-09 09:37:23 +10:00

78 lines
2.2 KiB
Ruby

# frozen_string_literal: true
require_relative '../../../script/import_scripts/base'
describe ImportScripts::Base do
before do
STDOUT.stubs(:write)
end
class MockSpecImporter < ImportScripts::Base
def initialize(data)
super()
@import_data = data
end
def execute
import_users
import_posts
import_bookmarks
end
def import_users
users = @import_data[:users]
create_users(users) do |row|
{ email: row[:email], id: row[:id] }
end
end
def import_posts
posts = @import_data[:posts]
create_posts(posts) do |row|
user_id = @lookup.user_id_from_imported_user_id(row[:user_id]) || -1
{ user_id: user_id, raw: row[:raw], id: row[:id], title: "Test topic for post #{row[:id]}" }
end
end
def import_bookmarks
bookmarks = @import_data[:bookmarks]
create_bookmarks(bookmarks) do |row|
{ post_id: row[:post_id], user_id: row[:user_id] }
end
end
end
let(:import_data) do
import_file = Rack::Test::UploadedFile.new(file_from_fixtures("base-import-data.json", "json"))
ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(import_file.read))
end
it "creates bookmarks, posts, and users" do
MockSpecImporter.new(import_data).perform
expect(Bookmark.count).to eq(5)
expect(Post.count).to eq(5)
expect(User.where('id > 0').count).to eq(1)
expect(SiteSetting.purge_unactivated_users_grace_period_days).to eq(60)
end
context "when polymorphic bookmarks are enabled" do
before do
SiteSetting.use_polymorphic_bookmarks = true
end
it "creates bookmarks, posts, and users" do
MockSpecImporter.new(import_data).perform
expect(Bookmark.where(bookmarkable_type: "Post").count).to eq(5)
expect(Post.count).to eq(5)
expect(User.where('id > 0').count).to eq(1)
expect(SiteSetting.purge_unactivated_users_grace_period_days).to eq(60)
end
end
it "does not change purge unactivated users setting if disabled" do
SiteSetting.purge_unactivated_users_grace_period_days = 0
MockSpecImporter.new(import_data).perform
expect(SiteSetting.purge_unactivated_users_grace_period_days).to eq(0)
end
end