0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/plugins/discourse-calendar/spec/models/topic_query_spec.rb
Martin Brennan 57779cc4be
FIX: can_create_discourse_post_event? not checking user groups correctly (#42284)
This commit fixes `can_create_discourse_post_event?` manually checking
a user's groups, which doesn't account for pseudogroups like
`logged_in_users`. We should always use `user.in_any_groups?` for this.

In addition, the opportunity is taken here for some cleanup, moving
all the guardian extensions from plugin.rb into a proper
`GuardianExtensions` file for calendar, adding extra specs along the
way.

Finally, I've added a `Group.refresh_automatic_groups_for_user!`
method as a single-user variant of `Group.refresh_automatic_groups!`
that can use in fabricators for testing, to make this more
thorough/reliable
than manually adding group user records in `after_create` in the
fabricators
2026-08-04 16:09:50 +10:00

53 lines
1.8 KiB
Ruby
Vendored

# frozen_string_literal: true
describe TopicQuery do
describe "sorts events" do
fab!(:user) { Fabricate(:user, admin: true, refresh_auto_groups: true) }
fab!(:notified_user, :user)
fab!(:topic_1) { Fabricate(:topic, user: user) }
fab!(:topic_2) { Fabricate(:topic, user: user) }
fab!(:topic_3) { Fabricate(:topic, user: user) }
fab!(:topic_4) { Fabricate(:topic, user: user) }
fab!(:post_1) { Fabricate(:post, topic: topic_1) }
fab!(:post_2) { Fabricate(:post, topic: topic_2) }
fab!(:post_3) { Fabricate(:post, topic: topic_3) }
fab!(:post_4) { Fabricate(:post, topic: topic_4) }
fab!(:future_event_1) do
DiscoursePostEvent::Event.create!(
id: post_1.id,
original_starts_at: Time.now + 5.hours,
original_ends_at: Time.now + 7.hours,
)
end
fab!(:future_event_2) do
DiscoursePostEvent::Event.create!(
id: post_2.id,
original_starts_at: Time.now + 1.hour,
original_ends_at: Time.now + 2.hours,
)
end
fab!(:past_event_1) do
DiscoursePostEvent::Event.create!(
id: post_3.id,
original_starts_at: Time.now - 10.hours,
original_ends_at: Time.now - 8.hours,
)
end
fab!(:past_event_2) do
DiscoursePostEvent::Event.create!(
id: post_4.id,
original_starts_at: Time.now - 7.hours,
original_ends_at: Time.now - 5.hours,
)
end
it "upcoming events first, sorted by ascending order. expired events last, sorted by descending order" do
ordered_topics =
TopicQuery.new(nil, order_by_event_date: [topic_1, topic_2, topic_3, topic_4]).options[
:order_by_event_date
]
expect(ordered_topics).to eq([topic_1, topic_2, topic_3, topic_4])
end
end
end