2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe DirectoryItem do
|
2016-01-08 18:53:52 +08:00
|
|
|
describe '#period_types' do
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when verifying enum sequence" do
|
2016-01-08 18:53:52 +08:00
|
|
|
before do
|
|
|
|
@period_types = DirectoryItem.period_types
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'all' should be at 1st position" do
|
|
|
|
expect(@period_types[:all]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'quarterly' should be at 6th position" do
|
|
|
|
expect(@period_types[:quarterly]).to eq(6)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe 'inactive and silenced users' do
|
2017-12-19 09:53:59 +08:00
|
|
|
it 'removes silenced users correctly' do
|
|
|
|
post = create_post
|
|
|
|
DirectoryItem.refresh_period!(:daily)
|
|
|
|
|
|
|
|
count = DirectoryItem.where(user_id: post.user_id).count
|
|
|
|
expect(count).to eq(1)
|
|
|
|
|
|
|
|
post.user.update_columns(active: false)
|
|
|
|
DirectoryItem.refresh_period!(:daily)
|
|
|
|
|
|
|
|
count = DirectoryItem.where(user_id: post.user_id).count
|
|
|
|
expect(count).to eq(0)
|
|
|
|
|
|
|
|
post.user.update_columns(active: true)
|
|
|
|
DirectoryItem.refresh_period!(:daily)
|
|
|
|
|
|
|
|
count = DirectoryItem.where(user_id: post.user_id).count
|
|
|
|
expect(count).to eq(1)
|
|
|
|
|
|
|
|
post.user.update_columns(silenced_till: 1.year.from_now)
|
|
|
|
DirectoryItem.refresh_period!(:daily)
|
|
|
|
|
|
|
|
count = DirectoryItem.where(user_id: post.user_id).count
|
|
|
|
expect(count).to eq(0)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe '.refresh!' do
|
2016-03-02 15:23:29 +08:00
|
|
|
before do
|
2019-10-04 09:11:03 +08:00
|
|
|
freeze_time DateTime.parse('2017-02-02 12:00')
|
2019-01-04 01:03:01 +08:00
|
|
|
UserActionManager.enable
|
2016-03-02 15:23:29 +08:00
|
|
|
end
|
|
|
|
|
2017-11-20 13:43:53 +08:00
|
|
|
it "creates the record for the user and handles likes" do
|
2017-08-10 03:57:35 +08:00
|
|
|
post = create_post
|
2017-11-20 13:43:53 +08:00
|
|
|
_post2 = create_post(topic_id: post.topic_id, user: post.user)
|
|
|
|
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(user2, post)
|
2017-11-20 13:43:53 +08:00
|
|
|
|
2015-03-17 03:14:33 +08:00
|
|
|
DirectoryItem.refresh!
|
2017-11-20 13:43:53 +08:00
|
|
|
|
|
|
|
item1 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: post.user_id)
|
|
|
|
item2 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: user2.id)
|
|
|
|
|
|
|
|
expect(item1.topic_count).to eq(1)
|
|
|
|
expect(item1.likes_received).to eq(1)
|
|
|
|
expect(item1.post_count).to eq(1)
|
|
|
|
|
|
|
|
expect(item2.likes_given).to eq(1)
|
|
|
|
|
|
|
|
post.topic.trash!
|
|
|
|
|
|
|
|
DirectoryItem.refresh!
|
|
|
|
|
|
|
|
item1 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: post.user_id)
|
|
|
|
item2 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: user2.id)
|
|
|
|
|
|
|
|
expect(item1.likes_given).to eq(0)
|
|
|
|
expect(item1.likes_received).to eq(0)
|
|
|
|
expect(item1.post_count).to eq(0)
|
|
|
|
expect(item1.topic_count).to eq(0)
|
2015-03-17 03:14:33 +08:00
|
|
|
end
|
|
|
|
|
2017-08-10 03:57:35 +08:00
|
|
|
it "handles users with no activity" do
|
|
|
|
post = nil
|
|
|
|
|
2019-10-04 09:11:03 +08:00
|
|
|
freeze_time(2.years.ago)
|
|
|
|
|
|
|
|
post = create_post
|
|
|
|
# Create records for that activity
|
|
|
|
DirectoryItem.refresh!
|
|
|
|
|
|
|
|
freeze_time(2.years.from_now)
|
2017-08-10 03:57:35 +08:00
|
|
|
|
|
|
|
DirectoryItem.refresh!
|
|
|
|
[:yearly, :monthly, :weekly, :daily, :quarterly].each do |period|
|
|
|
|
directory_item = DirectoryItem
|
|
|
|
.where(period_type: DirectoryItem.period_types[period])
|
|
|
|
.where(user_id: post.user.id)
|
|
|
|
.first
|
|
|
|
expect(directory_item.topic_count).to eq(0)
|
|
|
|
expect(directory_item.post_count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
directory_item = DirectoryItem
|
|
|
|
.where(period_type: DirectoryItem.period_types[:all])
|
|
|
|
.where(user_id: post.user.id)
|
|
|
|
.first
|
|
|
|
expect(directory_item.topic_count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2018-11-12 22:30:05 +08:00
|
|
|
it "creates directory item with correct activity count per period_type" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
UserVisit.create(user_id: user.id, visited_at: 1.minute.ago, posts_read: 1, mobile: false, time_read: 1)
|
|
|
|
UserVisit.create(user_id: user.id, visited_at: 2.days.ago, posts_read: 1, mobile: false, time_read: 1)
|
|
|
|
UserVisit.create(user_id: user.id, visited_at: 1.week.ago, posts_read: 1, mobile: false, time_read: 1)
|
|
|
|
UserVisit.create(user_id: user.id, visited_at: 1.month.ago, posts_read: 1, mobile: false, time_read: 1)
|
|
|
|
|
|
|
|
DirectoryItem.refresh!
|
|
|
|
|
|
|
|
daily_directory_item = DirectoryItem
|
|
|
|
.where(period_type: DirectoryItem.period_types[:daily])
|
|
|
|
.where(user_id: user.id)
|
|
|
|
.first
|
|
|
|
|
|
|
|
expect(daily_directory_item.days_visited).to eq(1)
|
|
|
|
|
|
|
|
weekly_directory_item = DirectoryItem
|
|
|
|
.where(period_type: DirectoryItem.period_types[:weekly])
|
|
|
|
.where(user_id: user.id)
|
|
|
|
.first
|
|
|
|
|
|
|
|
expect(weekly_directory_item.days_visited).to eq(2)
|
|
|
|
|
|
|
|
monthly_directory_item = DirectoryItem
|
|
|
|
.where(period_type: DirectoryItem.period_types[:monthly])
|
|
|
|
.where(user_id: user.id)
|
|
|
|
.first
|
|
|
|
|
|
|
|
expect(monthly_directory_item.days_visited).to eq(3)
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context 'when must_approve_users is true' do
|
2019-09-12 03:18:17 +08:00
|
|
|
before do
|
|
|
|
SiteSetting.must_approve_users = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't include user who hasn't been approved" do
|
|
|
|
user = Fabricate(:user, approved: false)
|
|
|
|
DirectoryItem.refresh!
|
|
|
|
expect(DirectoryItem.where(user_id: user.id).count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
2015-03-17 03:14:33 +08:00
|
|
|
end
|
|
|
|
end
|