mirror of
https://github.com/discourse/discourse.git
synced 2024-12-03 08:54:03 +08:00
222c8d9b6a
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.
103 lines
4.3 KiB
Ruby
103 lines
4.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
describe TopicBookmarkable do
|
|
fab!(:user) { Fabricate(:user) }
|
|
fab!(:guardian) { Guardian.new(user) }
|
|
fab!(:private_category) { Fabricate(:private_category, group: Fabricate(:group)) }
|
|
|
|
before do
|
|
SiteSetting.use_polymorphic_bookmarks = true
|
|
end
|
|
|
|
let!(:topic1) { Fabricate(:topic) }
|
|
let!(:topic2) { Fabricate(:topic) }
|
|
let!(:post) { Fabricate(:post, topic: topic1) }
|
|
let!(:bookmark1) { Fabricate(:bookmark, user: user, bookmarkable: topic1, name: "something i gotta do") }
|
|
let!(:bookmark2) { Fabricate(:bookmark, user: user, bookmarkable: topic2) }
|
|
let!(:bookmark3) { Fabricate(:bookmark) }
|
|
let!(:topic_user1) { Fabricate(:topic_user, user: user, topic: topic1) }
|
|
let!(:topic_user2) { Fabricate(:topic_user, user: user, topic: topic2) }
|
|
|
|
subject { RegisteredBookmarkable.new(TopicBookmarkable) }
|
|
|
|
describe "#perform_list_query" do
|
|
it "returns all the user's bookmarks" do
|
|
expect(subject.perform_list_query(user, guardian).map(&:id)).to match_array([bookmark1.id, bookmark2.id])
|
|
end
|
|
|
|
it "does not return bookmarks for posts where the user does not have access to the topic category" do
|
|
bookmark1.bookmarkable.update!(category: private_category)
|
|
expect(subject.perform_list_query(user, guardian).map(&:id)).to match_array([bookmark2.id])
|
|
end
|
|
|
|
it "does not return bookmarks for posts where the user does not have access to the private message" do
|
|
bookmark1.update!(bookmarkable: Fabricate(:private_message_topic))
|
|
expect(subject.perform_list_query(user, guardian).map(&:id)).to match_array([bookmark2.id])
|
|
end
|
|
end
|
|
|
|
describe "#perform_search_query" do
|
|
before do
|
|
SearchIndexer.enable
|
|
end
|
|
|
|
it "returns bookmarks that match by name" do
|
|
ts_query = Search.ts_query(term: "gotta", ts_config: "simple")
|
|
expect(subject.perform_search_query(subject.perform_list_query(user, guardian), "%gotta%", ts_query).map(&:id)).to match_array([bookmark1.id])
|
|
end
|
|
|
|
it "returns bookmarks that match by post search data (topic title or post content)" do
|
|
post.update(raw: "some post content")
|
|
topic1.update(title: "a great topic title")
|
|
|
|
ts_query = Search.ts_query(term: "post content", ts_config: "simple")
|
|
expect(subject.perform_search_query(subject.perform_list_query(user, guardian), "%post content%", ts_query).map(&:id)).to match_array([bookmark1.id])
|
|
|
|
ts_query = Search.ts_query(term: "great topic", ts_config: "simple")
|
|
expect(subject.perform_search_query(subject.perform_list_query(user, guardian), "%great topic%", ts_query).map(&:id)).to match_array([bookmark1.id])
|
|
|
|
ts_query = Search.ts_query(term: "blah", ts_config: "simple")
|
|
expect(subject.perform_search_query(subject.perform_list_query(user, guardian), "%blah%", ts_query).map(&:id)).to eq([])
|
|
end
|
|
end
|
|
|
|
describe "#can_send_reminder?" do
|
|
it "cannot send reminder if the topic is deleted" do
|
|
expect(subject.can_send_reminder?(bookmark1)).to eq(true)
|
|
bookmark1.bookmarkable.trash!
|
|
bookmark1.reload
|
|
expect(subject.can_send_reminder?(bookmark1)).to eq(false)
|
|
end
|
|
end
|
|
|
|
describe "#reminder_handler" do
|
|
it "creates a notification for the user with the correct details" do
|
|
expect { subject.send_reminder_notification(bookmark1) }.to change { Notification.count }.by(1)
|
|
notif = user.notifications.last
|
|
expect(notif.notification_type).to eq(Notification.types[:bookmark_reminder])
|
|
expect(notif.topic_id).to eq(bookmark1.bookmarkable_id)
|
|
expect(notif.post_number).to eq(1)
|
|
expect(notif.data).to eq(
|
|
{
|
|
title: bookmark1.bookmarkable.title,
|
|
display_username: bookmark1.user.username,
|
|
bookmark_name: bookmark1.name,
|
|
bookmarkable_url: bookmark1.bookmarkable.first_post.url
|
|
}.to_json
|
|
)
|
|
end
|
|
end
|
|
|
|
describe "#can_see?" do
|
|
it "returns false if the post is in a private category or private message the user cannot see" do
|
|
expect(subject.can_see?(guardian, bookmark1)).to eq(true)
|
|
bookmark1.bookmarkable.update!(category: private_category)
|
|
expect(subject.can_see?(guardian, bookmark1)).to eq(false)
|
|
bookmark1.update!(bookmarkable: Fabricate(:private_message_topic))
|
|
expect(subject.can_see?(guardian, bookmark1)).to eq(false)
|
|
end
|
|
end
|
|
end
|