2013-02-15 05:51:48 +08:00
|
|
|
# encoding: utf-8
|
2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
2013-02-15 05:51:48 +08:00
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-10-17 14:44:56 +08:00
|
|
|
require_dependency 'post_creator'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe Category do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2019-03-18 15:25:45 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
it { is_expected.to validate_presence_of :user_id }
|
|
|
|
it { is_expected.to validate_presence_of :name }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it 'validates uniqueness of name' do
|
|
|
|
Fabricate(:category)
|
2019-04-18 05:41:37 +08:00
|
|
|
is_expected.to validate_uniqueness_of(:name).scoped_to(:parent_category_id).case_insensitive
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2019-03-18 15:25:45 +08:00
|
|
|
it 'validates inclusion of search_priority' do
|
|
|
|
category = Fabricate.build(:category, user: user)
|
|
|
|
|
|
|
|
expect(category.valid?).to eq(true)
|
|
|
|
|
|
|
|
category.search_priority = Searchable::PRIORITIES.values.last + 1
|
|
|
|
|
|
|
|
expect(category.valid?).to eq(false)
|
2019-04-30 14:58:18 +08:00
|
|
|
expect(category.errors.to_hash.keys).to contain_exactly(:search_priority)
|
2019-03-18 15:25:45 +08:00
|
|
|
end
|
|
|
|
|
2014-08-12 04:55:26 +08:00
|
|
|
it 'validates uniqueness in case insensitive way' do
|
|
|
|
Fabricate(:category, name: "Cats")
|
2014-12-31 22:55:03 +08:00
|
|
|
cats = Fabricate.build(:category, name: "cats")
|
|
|
|
expect(cats).to_not be_valid
|
|
|
|
expect(cats.errors[:name]).to be_present
|
2014-08-12 04:55:26 +08:00
|
|
|
end
|
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
describe "resolve_permissions" do
|
|
|
|
it "can determine read_restricted" do
|
2017-07-28 09:20:09 +08:00
|
|
|
read_restricted, resolved = Category.resolve_permissions(everyone: :full)
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(read_restricted).to be false
|
|
|
|
expect(resolved).to be_blank
|
2013-07-14 09:24:16 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-17 15:51:32 +08:00
|
|
|
describe "permissions_params" do
|
|
|
|
it "returns the right group names and permission type" do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
group = Fabricate(:group)
|
|
|
|
category_group = Fabricate(:category_group, category: category, group: group)
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(category.permissions_params).to eq("#{group.name}" => category_group.permission_type)
|
2015-09-17 15:51:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-18 05:12:32 +08:00
|
|
|
describe "#review_group_id" do
|
2019-05-10 18:59:31 +08:00
|
|
|
fab!(:group) { Fabricate(:group) }
|
|
|
|
fab!(:category) { Fabricate(:category, reviewable_by_group: group) }
|
|
|
|
fab!(:topic) { Fabricate(:topic, category: category) }
|
|
|
|
fab!(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
2019-04-18 05:12:32 +08:00
|
|
|
|
|
|
|
it "will add the group to the reviewable" do
|
|
|
|
SiteSetting.enable_category_group_review = true
|
|
|
|
reviewable = PostActionCreator.spam(user, post).reviewable
|
|
|
|
expect(reviewable.reviewable_by_group_id).to eq(group.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will add the group to the reviewable even if created manually" do
|
|
|
|
SiteSetting.enable_category_group_review = true
|
|
|
|
reviewable = ReviewableFlaggedPost.create!(
|
|
|
|
created_by: user,
|
|
|
|
payload: { raw: 'test raw' },
|
|
|
|
category: category
|
|
|
|
)
|
|
|
|
expect(reviewable.reviewable_by_group_id).to eq(group.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will not add add the group to the reviewable" do
|
|
|
|
SiteSetting.enable_category_group_review = false
|
|
|
|
reviewable = PostActionCreator.spam(user, post).reviewable
|
|
|
|
expect(reviewable.reviewable_by_group_id).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will nullify the group_id if destroyed" do
|
|
|
|
reviewable = PostActionCreator.spam(user, post).reviewable
|
|
|
|
group.destroy
|
|
|
|
expect(category.reload.reviewable_by_group).to be_blank
|
|
|
|
expect(reviewable.reload.reviewable_by_group_id).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will remove the reviewable_by_group if the category is updated" do
|
|
|
|
SiteSetting.enable_category_group_review = true
|
|
|
|
reviewable = PostActionCreator.spam(user, post).reviewable
|
|
|
|
category.reviewable_by_group_id = nil
|
|
|
|
category.save!
|
|
|
|
expect(reviewable.reload.reviewable_by_group_id).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
describe "topic_create_allowed and post_create_allowed" do
|
2019-06-26 15:41:07 +08:00
|
|
|
fab!(:group) { Fabricate(:group) }
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2019-06-26 15:41:07 +08:00
|
|
|
fab!(:user) do
|
2013-07-14 09:24:16 +08:00
|
|
|
user = Fabricate(:user)
|
|
|
|
group.add(user)
|
|
|
|
group.save
|
2019-06-26 15:41:07 +08:00
|
|
|
user
|
|
|
|
end
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2019-06-26 15:41:07 +08:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2019-06-26 15:41:07 +08:00
|
|
|
fab!(:default_category) { Fabricate(:category) }
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2019-06-26 15:41:07 +08:00
|
|
|
fab!(:full_category) do
|
|
|
|
c = Fabricate(:category)
|
|
|
|
c.set_permissions(group => :full)
|
|
|
|
c.save
|
|
|
|
c
|
|
|
|
end
|
|
|
|
|
|
|
|
fab!(:can_post_category) do
|
|
|
|
c = Fabricate(:category)
|
|
|
|
c.set_permissions(group => :create_post)
|
|
|
|
c.save
|
|
|
|
c
|
|
|
|
end
|
|
|
|
|
|
|
|
fab!(:can_read_category) do
|
|
|
|
c = Fabricate(:category)
|
|
|
|
c.set_permissions(group => :readonly)
|
|
|
|
c.save
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user_guardian) { Guardian.new(user) }
|
|
|
|
let(:admin_guardian) { Guardian.new(admin) }
|
|
|
|
let(:anon_guardian) { Guardian.new(nil) }
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2019-06-26 15:41:07 +08:00
|
|
|
context 'when disabling uncategorized' do
|
|
|
|
before do
|
|
|
|
SiteSetting.allow_uncategorized_topics = false
|
|
|
|
end
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2019-06-26 15:41:07 +08:00
|
|
|
it "allows everything to admins unconditionally" do
|
|
|
|
count = Category.count
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2019-06-26 15:41:07 +08:00
|
|
|
expect(Category.topic_create_allowed(admin_guardian).count).to eq(count)
|
|
|
|
expect(Category.post_create_allowed(admin_guardian).count).to eq(count)
|
|
|
|
expect(Category.secured(admin_guardian).count).to eq(count)
|
|
|
|
end
|
2013-07-16 13:44:07 +08:00
|
|
|
|
2019-06-26 15:41:07 +08:00
|
|
|
it "allows normal users correct access to all categories" do
|
2015-05-14 10:19:22 +08:00
|
|
|
|
2019-06-26 15:41:07 +08:00
|
|
|
# Sam: I am mixed here, once disabling uncategorized maybe users should no
|
|
|
|
# longer be allowed to know about it so all counts should go down?
|
|
|
|
expect(Category.secured(user_guardian).count).to eq(5)
|
|
|
|
expect(Category.post_create_allowed(user_guardian).count).to eq(4)
|
|
|
|
expect(Category.topic_create_allowed(user_guardian).count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows everything to admins unconditionally" do
|
|
|
|
count = Category.count
|
|
|
|
|
|
|
|
expect(Category.topic_create_allowed(admin_guardian).count).to eq(count)
|
|
|
|
expect(Category.post_create_allowed(admin_guardian).count).to eq(count)
|
|
|
|
expect(Category.secured(admin_guardian).count).to eq(count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows normal users correct access to all categories" do
|
|
|
|
expect(Category.secured(user_guardian).count).to eq(5)
|
|
|
|
expect(Category.post_create_allowed(user_guardian).count).to eq(4)
|
|
|
|
expect(Category.topic_create_allowed(user_guardian).count).to eq(3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows anon correct access" do
|
|
|
|
expect(Category.scoped_to_permissions(anon_guardian, [:readonly]).count).to eq(2)
|
|
|
|
expect(Category.post_create_allowed(anon_guardian).count).to eq(0)
|
|
|
|
expect(Category.topic_create_allowed(anon_guardian).count).to eq(0)
|
|
|
|
|
|
|
|
# nil has special semantics
|
|
|
|
expect(Category.scoped_to_permissions(nil, [:readonly]).count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles :everyone scope" do
|
2017-07-28 09:20:09 +08:00
|
|
|
can_post_category.set_permissions(everyone: :create_post)
|
2013-07-16 13:44:07 +08:00
|
|
|
can_post_category.save
|
|
|
|
|
2019-06-26 15:41:07 +08:00
|
|
|
expect(Category.post_create_allowed(user_guardian).count).to eq(4)
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2013-10-13 06:54:48 +08:00
|
|
|
# anonymous has permission to create no topics
|
2019-06-26 15:41:07 +08:00
|
|
|
expect(Category.scoped_to_permissions(user_guardian, [:readonly]).count).to eq(3)
|
2013-10-13 06:54:48 +08:00
|
|
|
end
|
2013-07-14 09:24:16 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
describe "security" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:category) { Fabricate(:category) }
|
|
|
|
fab!(:category_2) { Fabricate(:category) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2013-05-16 04:45:52 +08:00
|
|
|
it "secures categories correctly" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category.read_restricted?).to be false
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
category.set_permissions({})
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category.read_restricted?).to be true
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
category.set_permissions(everyone: :full)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category.read_restricted?).to be false
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(user.secure_categories).to be_empty
|
2013-04-29 14:33:24 +08:00
|
|
|
|
|
|
|
group.add(user)
|
|
|
|
group.save
|
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
category.set_permissions(group.id => :full)
|
2013-04-29 14:33:24 +08:00
|
|
|
category.save
|
|
|
|
|
|
|
|
user.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(user.secure_categories).to eq([category])
|
2013-05-16 04:45:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "lists all secured categories correctly" do
|
2014-07-16 03:19:17 +08:00
|
|
|
uncategorized = Category.find(SiteSetting.uncategorized_category_id)
|
2013-10-24 07:05:51 +08:00
|
|
|
|
2013-05-16 04:45:52 +08:00
|
|
|
group.add(user)
|
2013-07-14 09:24:16 +08:00
|
|
|
category.set_permissions(group.id => :full)
|
2017-08-31 12:06:56 +08:00
|
|
|
category.save!
|
2013-07-14 09:24:16 +08:00
|
|
|
category_2.set_permissions(group.id => :full)
|
2017-08-31 12:06:56 +08:00
|
|
|
category_2.save!
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Category.secured).to match_array([uncategorized])
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(Category.secured(Guardian.new(user))).to match_array([uncategorized, category, category_2])
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-31 04:48:19 +08:00
|
|
|
it "strips leading blanks" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Fabricate(:category, name: " music").name).to eq("music")
|
2013-07-31 04:48:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "strips trailing blanks" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Fabricate(:category, name: "bugs ").name).to eq("bugs")
|
2013-07-31 04:48:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "strips leading and trailing blanks" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Fabricate(:category, name: " blanks ").name).to eq("blanks")
|
2013-07-31 04:48:19 +08:00
|
|
|
end
|
|
|
|
|
2014-08-18 23:07:32 +08:00
|
|
|
it "sets name_lower" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Fabricate(:category, name: "Not MySQL").name_lower).to eq("not mysql")
|
2014-08-18 23:07:32 +08:00
|
|
|
end
|
|
|
|
|
2014-04-25 21:14:05 +08:00
|
|
|
it "has custom fields" do
|
|
|
|
category = Fabricate(:category, name: " music")
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category.custom_fields["a"]).to be_nil
|
2014-04-25 21:14:05 +08:00
|
|
|
|
|
|
|
category.custom_fields["bob"] = "marley"
|
|
|
|
category.custom_fields["jack"] = "black"
|
|
|
|
category.save
|
|
|
|
|
|
|
|
category = Category.find(category.id)
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(category.custom_fields).to eq("bob" => "marley", "jack" => "black")
|
2014-04-25 21:14:05 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe "short name" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:category) { Fabricate(:category, name: 'xx') }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "creates the category" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has one topic' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Topic.where(category_id: category.id).count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-15 05:51:48 +08:00
|
|
|
describe 'non-english characters' do
|
2015-04-13 22:50:41 +08:00
|
|
|
context 'uses ascii slug generator' do
|
|
|
|
before do
|
|
|
|
SiteSetting.slug_generation_method = 'ascii'
|
|
|
|
@category = Fabricate(:category, name: "测试")
|
|
|
|
end
|
|
|
|
after { @category.destroy }
|
|
|
|
|
|
|
|
it "creates a blank slug" do
|
|
|
|
expect(@category.slug).to be_blank
|
|
|
|
expect(@category.slug_for_url).to eq("#{@category.id}-category")
|
|
|
|
end
|
|
|
|
end
|
2013-02-15 05:51:48 +08:00
|
|
|
|
2015-04-13 22:50:41 +08:00
|
|
|
context 'uses none slug generator' do
|
|
|
|
before do
|
|
|
|
SiteSetting.slug_generation_method = 'none'
|
|
|
|
@category = Fabricate(:category, name: "测试")
|
|
|
|
end
|
|
|
|
after do
|
|
|
|
SiteSetting.slug_generation_method = 'ascii'
|
|
|
|
@category.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a blank slug" do
|
|
|
|
expect(@category.slug).to be_blank
|
|
|
|
expect(@category.slug_for_url).to eq("#{@category.id}-category")
|
|
|
|
end
|
2013-02-15 05:51:48 +08:00
|
|
|
end
|
2014-09-16 19:15:05 +08:00
|
|
|
|
2015-04-13 22:50:41 +08:00
|
|
|
context 'uses encoded slug generator' do
|
|
|
|
before do
|
|
|
|
SiteSetting.slug_generation_method = 'encoded'
|
|
|
|
@category = Fabricate(:category, name: "测试")
|
|
|
|
end
|
|
|
|
after do
|
|
|
|
SiteSetting.slug_generation_method = 'ascii'
|
|
|
|
@category.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a slug" do
|
2015-05-04 19:48:37 +08:00
|
|
|
expect(@category.slug).to eq("测试")
|
|
|
|
expect(@category.slug_for_url).to eq("测试")
|
2015-04-13 22:50:41 +08:00
|
|
|
end
|
2014-09-16 19:15:05 +08:00
|
|
|
end
|
2013-02-15 05:51:48 +08:00
|
|
|
end
|
|
|
|
|
2013-05-30 23:09:09 +08:00
|
|
|
describe 'slug would be a number' do
|
2015-04-13 22:50:41 +08:00
|
|
|
let(:category) { Fabricate.build(:category, name: "2") }
|
2013-05-30 23:09:09 +08:00
|
|
|
|
|
|
|
it 'creates a blank slug' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category.slug).to be_blank
|
|
|
|
expect(category.slug_for_url).to eq("#{category.id}-category")
|
2013-05-30 23:09:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-04 08:23:59 +08:00
|
|
|
describe 'custom slug can be provided' do
|
2015-04-13 22:50:41 +08:00
|
|
|
it 'can be sanitized' do
|
|
|
|
@c = Fabricate(:category, name: "Fun Cats", slug: "fun-cats")
|
|
|
|
@cat = Fabricate(:category, name: "love cats", slug: "love-cats")
|
2014-12-04 08:23:59 +08:00
|
|
|
|
2015-04-13 22:50:41 +08:00
|
|
|
@c.slug = ' invalid slug'
|
|
|
|
@c.save
|
|
|
|
expect(@c.slug).to eq('invalid-slug')
|
|
|
|
|
|
|
|
c = Fabricate.build(:category, name: "More Fun Cats", slug: "love-cats")
|
|
|
|
expect(c).not_to be_valid
|
|
|
|
expect(c.errors[:slug]).to be_present
|
|
|
|
|
|
|
|
@cat.slug = "#{@c.id}-category"
|
|
|
|
expect(@cat).not_to be_valid
|
|
|
|
expect(@cat.errors[:slug]).to be_present
|
2014-12-20 22:07:29 +08:00
|
|
|
|
2015-04-13 22:50:41 +08:00
|
|
|
@cat.slug = "#{@cat.id}-category"
|
|
|
|
expect(@cat).to be_valid
|
|
|
|
expect(@cat.errors[:slug]).not_to be_present
|
2014-12-04 08:23:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-22 12:48:18 +08:00
|
|
|
describe 'description_text' do
|
|
|
|
it 'correctly generates text description as needed' do
|
|
|
|
c = Category.new
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(c.description_text).to be_nil
|
2014-10-22 12:48:18 +08:00
|
|
|
c.description = "<hello <a>test</a>."
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(c.description_text).to eq("<hello test.")
|
2014-10-22 12:48:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe 'after create' do
|
|
|
|
before do
|
2013-05-08 03:52:45 +08:00
|
|
|
@category = Fabricate(:category, name: 'Amazing Category')
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic = @category.topic
|
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'is created correctly' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@category.slug).to eq('amazing-category')
|
|
|
|
expect(@category.slug_for_url).to eq(@category.slug)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@category.description).to be_blank
|
2013-02-22 07:09:56 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Topic.where(category_id: @category).count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@topic).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@topic.category).to eq(@category)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@topic).to be_visible
|
2013-03-08 01:45:49 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@topic.pinned_at).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Guardian.new(@category.user).can_delete?(@topic)).to be false
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@topic.posts.count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@category.topic_url).to be_present
|
Default values for posts/topics fields on Category
When creating categories (or, at least, subcategories), certain integer
values are set to a default of NULL: topics_week, topics_month,
topics_year, posts_week, posts_month, and posts_year. This causes
consistent exceptions when trying to visit `/categories`, with the
offending line being in
`CategoryDetailedSerializer#count_with_subcategories`. This attempts to
coerce nil into Fixnum.
A fix could be to convert to 0 in the code, but these attributes should
really never be NULL. If there are no posts or topics, they should be 0
to maintain data integrity.
Signed-off-by: David Celis <me@davidcel.is>
2014-02-07 03:56:39 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@category.posts_week).to eq(0)
|
|
|
|
expect(@category.posts_month).to eq(0)
|
|
|
|
expect(@category.posts_year).to eq(0)
|
Default values for posts/topics fields on Category
When creating categories (or, at least, subcategories), certain integer
values are set to a default of NULL: topics_week, topics_month,
topics_year, posts_week, posts_month, and posts_year. This causes
consistent exceptions when trying to visit `/categories`, with the
offending line being in
`CategoryDetailedSerializer#count_with_subcategories`. This attempts to
coerce nil into Fixnum.
A fix could be to convert to 0 in the code, but these attributes should
really never be NULL. If there are no posts or topics, they should be 0
to maintain data integrity.
Signed-off-by: David Celis <me@davidcel.is>
2014-02-07 03:56:39 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@category.topics_week).to eq(0)
|
|
|
|
expect(@category.topics_month).to eq(0)
|
|
|
|
expect(@category.topics_year).to eq(0)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-19 01:59:54 +08:00
|
|
|
it "renames the definition when renamed" do
|
2019-04-29 15:32:25 +08:00
|
|
|
@category.update(name: 'Troutfishing')
|
2014-07-19 01:59:54 +08:00
|
|
|
@topic.reload
|
2016-07-28 23:57:30 +08:00
|
|
|
expect(@topic.title).to match(/Troutfishing/)
|
2017-03-26 14:59:53 +08:00
|
|
|
expect(@topic.fancy_title).to match(/Troutfishing/)
|
2014-07-19 01:59:54 +08:00
|
|
|
end
|
|
|
|
|
2014-07-26 04:36:16 +08:00
|
|
|
it "doesn't raise an error if there is no definition topic to rename (uncategorized)" do
|
2019-04-29 15:32:25 +08:00
|
|
|
expect { @category.update(name: 'Troutfishing', topic_id: nil) }.to_not raise_error
|
2014-07-26 04:36:16 +08:00
|
|
|
end
|
|
|
|
|
2016-04-27 19:04:44 +08:00
|
|
|
it "creates permalink when category slug is changed" do
|
2019-04-29 15:32:25 +08:00
|
|
|
@category.update(slug: 'new-category')
|
2016-04-27 19:04:44 +08:00
|
|
|
expect(Permalink.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2019-03-21 06:38:59 +08:00
|
|
|
it "reuses existing permalink when category slug is changed" do
|
|
|
|
permalink = Permalink.create!(url: "c/#{@category.slug}", category_id: 42)
|
|
|
|
|
2019-04-29 15:32:25 +08:00
|
|
|
expect { @category.update(slug: 'new-slug') }.to_not change { Permalink.count }
|
2019-03-21 06:38:59 +08:00
|
|
|
expect(permalink.reload.category_id).to eq(@category.id)
|
|
|
|
end
|
|
|
|
|
2016-04-27 19:04:44 +08:00
|
|
|
it "creates permalink when sub category slug is changed" do
|
|
|
|
sub_category = Fabricate(:category, slug: 'sub-category', parent_category_id: @category.id)
|
2019-04-29 15:32:25 +08:00
|
|
|
sub_category.update(slug: 'new-sub-category')
|
2016-04-27 19:04:44 +08:00
|
|
|
expect(Permalink.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes permalink when category slug is reused" do
|
|
|
|
Fabricate(:permalink, url: "/c/bikeshed-category")
|
|
|
|
Fabricate(:category, slug: 'bikeshed-category')
|
|
|
|
expect(Permalink.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes permalink when sub category slug is reused" do
|
|
|
|
Fabricate(:permalink, url: "/c/main-category/sub-category")
|
|
|
|
main_category = Fabricate(:category, slug: 'main-category')
|
|
|
|
Fabricate(:category, slug: 'sub-category', parent_category_id: main_category.id)
|
|
|
|
expect(Permalink.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
2018-12-21 11:59:54 +08:00
|
|
|
it "correctly creates permalink when category slug is changed in subfolder install" do
|
|
|
|
GlobalSetting.stubs(:relative_url_root).returns('/forum')
|
|
|
|
Discourse.stubs(:base_uri).returns("/forum")
|
|
|
|
old_url = @category.url
|
2019-04-29 15:32:25 +08:00
|
|
|
@category.update(slug: 'new-category')
|
2018-12-21 11:59:54 +08:00
|
|
|
permalink = Permalink.last
|
|
|
|
expect(permalink.url).to eq(old_url[1..-1])
|
|
|
|
end
|
|
|
|
|
2013-12-04 07:53:40 +08:00
|
|
|
it "should not set its description topic to auto-close" do
|
2013-12-07 05:39:35 +08:00
|
|
|
category = Fabricate(:category, name: 'Closing Topics', auto_close_hours: 1)
|
2017-05-17 02:49:42 +08:00
|
|
|
expect(category.topic.public_topic_timer).to eq(nil)
|
2013-12-04 07:53:40 +08:00
|
|
|
end
|
|
|
|
|
2013-04-02 00:26:51 +08:00
|
|
|
describe "creating a new category with the same slug" do
|
2014-08-14 02:45:25 +08:00
|
|
|
it "should have a blank slug if at the same level" do
|
2014-03-25 01:36:23 +08:00
|
|
|
category = Fabricate(:category, name: "Amazing Categóry")
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category.slug).to be_blank
|
|
|
|
expect(category.slug_for_url).to eq("#{category.id}-category")
|
2013-04-02 00:26:51 +08:00
|
|
|
end
|
2014-08-14 02:45:25 +08:00
|
|
|
|
|
|
|
it "doesn't have a blank slug if not at the same level" do
|
|
|
|
parent = Fabricate(:category, name: 'Other parent')
|
|
|
|
category = Fabricate(:category, name: "Amazing Categóry", parent_category_id: parent.id)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category.slug).to eq('amazing-category')
|
|
|
|
expect(category.slug_for_url).to eq("amazing-category")
|
2014-08-14 02:45:25 +08:00
|
|
|
end
|
2013-04-02 00:26:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe "trying to change the category topic's category" do
|
|
|
|
before do
|
|
|
|
@new_cat = Fabricate(:category, name: '2nd Category', user: @category.user)
|
2014-07-17 03:39:39 +08:00
|
|
|
@topic.change_category_to_id(@new_cat.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.reload
|
|
|
|
@category.reload
|
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'does not cause changes' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@category.topic_count).to eq(0)
|
|
|
|
expect(@topic.category).to eq(@category)
|
|
|
|
expect(@category.topic).to eq(@topic)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 14:23:35 +08:00
|
|
|
describe 'new' do
|
|
|
|
subject { Fabricate.build(:category, user: Fabricate(:user)) }
|
|
|
|
|
|
|
|
it 'triggers a extensibility event' do
|
|
|
|
event = DiscourseEvent.track_events { subject.save! }.last
|
|
|
|
|
|
|
|
expect(event[:event_name]).to eq(:category_created)
|
|
|
|
expect(event[:params].first).to eq(subject)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-04 08:23:59 +08:00
|
|
|
describe "update" do
|
|
|
|
it "should enforce uniqueness of slug" do
|
|
|
|
Fabricate(:category, slug: "the-slug")
|
|
|
|
c2 = Fabricate(:category, slug: "different-slug")
|
|
|
|
c2.slug = "the-slug"
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(c2).to_not be_valid
|
|
|
|
expect(c2.errors[:slug]).to be_present
|
2014-12-04 08:23:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe 'destroy' do
|
|
|
|
before do
|
|
|
|
@category = Fabricate(:category)
|
|
|
|
@category_id = @category.id
|
|
|
|
@topic_id = @category.topic_id
|
2018-03-14 03:59:12 +08:00
|
|
|
SiteSetting.shared_drafts_category = @category.id.to_s
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'is deleted correctly' do
|
2018-03-27 14:23:35 +08:00
|
|
|
@category.destroy
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Category.exists?(id: @category_id)).to be false
|
|
|
|
expect(Topic.exists?(id: @topic_id)).to be false
|
2018-03-14 03:59:12 +08:00
|
|
|
expect(SiteSetting.shared_drafts_category).to be_blank
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2018-03-27 14:23:35 +08:00
|
|
|
|
|
|
|
it 'triggers a extensibility event' do
|
2019-04-17 02:42:47 +08:00
|
|
|
event = DiscourseEvent.track(:category_destroyed) { @category.destroy }
|
2018-03-27 14:23:35 +08:00
|
|
|
|
|
|
|
expect(event[:event_name]).to eq(:category_destroyed)
|
|
|
|
expect(event[:params].first).to eq(@category)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-10-17 14:44:56 +08:00
|
|
|
describe 'latest' do
|
|
|
|
it 'should be updated correctly' do
|
|
|
|
category = Fabricate(:category)
|
2016-12-05 20:31:43 +08:00
|
|
|
post = create_post(category: category.id)
|
2013-10-17 14:44:56 +08:00
|
|
|
|
|
|
|
category.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category.latest_post_id).to eq(post.id)
|
|
|
|
expect(category.latest_topic_id).to eq(post.topic_id)
|
2013-10-17 14:44:56 +08:00
|
|
|
|
2016-12-05 20:31:43 +08:00
|
|
|
post2 = create_post(category: category.id)
|
|
|
|
post3 = create_post(topic_id: post.topic_id, category: category.id)
|
2013-10-17 14:44:56 +08:00
|
|
|
|
|
|
|
category.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category.latest_post_id).to eq(post3.id)
|
|
|
|
expect(category.latest_topic_id).to eq(post2.topic_id)
|
2013-10-17 14:44:56 +08:00
|
|
|
|
2013-12-31 06:33:07 +08:00
|
|
|
post3.reload
|
2013-10-17 14:44:56 +08:00
|
|
|
|
|
|
|
destroyer = PostDestroyer.new(Fabricate(:admin), post3)
|
|
|
|
destroyer.destroy
|
|
|
|
|
|
|
|
category.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category.latest_post_id).to eq(post2.id)
|
2013-10-17 14:44:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe 'update_stats' do
|
|
|
|
before do
|
|
|
|
@category = Fabricate(:category)
|
2013-02-17 04:57:16 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-17 04:57:16 +08:00
|
|
|
context 'with regular topics' do
|
|
|
|
before do
|
2016-12-05 20:31:43 +08:00
|
|
|
create_post(user: @category.user, category: @category.id)
|
2013-02-17 04:57:16 +08:00
|
|
|
Category.update_stats
|
|
|
|
@category.reload
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'updates topic stats' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@category.topics_week).to eq(1)
|
|
|
|
expect(@category.topics_month).to eq(1)
|
|
|
|
expect(@category.topics_year).to eq(1)
|
|
|
|
expect(@category.topic_count).to eq(1)
|
|
|
|
expect(@category.post_count).to eq(1)
|
|
|
|
expect(@category.posts_year).to eq(1)
|
|
|
|
expect(@category.posts_month).to eq(1)
|
|
|
|
expect(@category.posts_week).to eq(1)
|
2013-03-31 19:22:05 +08:00
|
|
|
end
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-17 04:57:16 +08:00
|
|
|
context 'with deleted topics' do
|
|
|
|
before do
|
2013-02-26 00:42:20 +08:00
|
|
|
@category.topics << Fabricate(:deleted_topic,
|
2013-02-17 04:57:16 +08:00
|
|
|
user: @category.user)
|
|
|
|
Category.update_stats
|
|
|
|
@category.reload
|
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'does not count deleted topics' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@category.topics_week).to eq(0)
|
|
|
|
expect(@category.topic_count).to eq(0)
|
|
|
|
expect(@category.topics_month).to eq(0)
|
|
|
|
expect(@category.topics_year).to eq(0)
|
|
|
|
expect(@category.post_count).to eq(0)
|
|
|
|
expect(@category.posts_year).to eq(0)
|
|
|
|
expect(@category.posts_month).to eq(0)
|
|
|
|
expect(@category.posts_week).to eq(0)
|
2013-12-14 04:15:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with revised post' do
|
|
|
|
before do
|
2016-12-05 20:31:43 +08:00
|
|
|
post = create_post(user: @category.user, category: @category.id)
|
2013-12-14 04:15:51 +08:00
|
|
|
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.editing_grace_period = 1.minute
|
2014-10-28 05:06:43 +08:00
|
|
|
post.revise(post.user, { raw: 'updated body' }, revised_at: post.updated_at + 2.minutes)
|
2013-12-14 04:15:51 +08:00
|
|
|
|
|
|
|
Category.update_stats
|
|
|
|
@category.reload
|
2013-02-17 04:57:16 +08:00
|
|
|
end
|
2013-03-31 19:22:05 +08:00
|
|
|
|
2013-12-14 04:15:51 +08:00
|
|
|
it "doesn't count each version of a post" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@category.post_count).to eq(1)
|
|
|
|
expect(@category.posts_year).to eq(1)
|
|
|
|
expect(@category.posts_month).to eq(1)
|
|
|
|
expect(@category.posts_week).to eq(1)
|
2013-12-14 04:15:51 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-08-28 03:58:05 +08:00
|
|
|
|
|
|
|
context 'for uncategorized category' do
|
|
|
|
before do
|
|
|
|
@uncategorized = Category.find(SiteSetting.uncategorized_category_id)
|
2019-05-25 01:16:19 +08:00
|
|
|
create_post(user: Fabricate(:user), category: @uncategorized.id)
|
2014-08-28 03:58:05 +08:00
|
|
|
Category.update_stats
|
|
|
|
@uncategorized.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates topic stats' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@uncategorized.topics_week).to eq(1)
|
|
|
|
expect(@uncategorized.topics_month).to eq(1)
|
|
|
|
expect(@uncategorized.topics_year).to eq(1)
|
|
|
|
expect(@uncategorized.topic_count).to eq(1)
|
|
|
|
expect(@uncategorized.post_count).to eq(1)
|
|
|
|
expect(@uncategorized.posts_year).to eq(1)
|
|
|
|
expect(@uncategorized.posts_month).to eq(1)
|
|
|
|
expect(@uncategorized.posts_week).to eq(1)
|
2014-08-28 03:58:05 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-10-24 00:58:11 +08:00
|
|
|
|
2014-02-17 01:45:00 +08:00
|
|
|
describe "#url" do
|
|
|
|
it "builds a url for normal categories" do
|
|
|
|
category = Fabricate(:category, name: "cats")
|
2015-01-08 00:45:57 +08:00
|
|
|
expect(category.url).to eq "/c/cats"
|
2014-02-17 01:45:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "for subcategories" do
|
|
|
|
it "includes the parent category" do
|
|
|
|
parent_category = Fabricate(:category, name: "parent")
|
|
|
|
subcategory = Fabricate(:category, name: "child",
|
2017-07-28 09:20:09 +08:00
|
|
|
parent_category_id: parent_category.id)
|
2015-01-08 00:45:57 +08:00
|
|
|
expect(subcategory.url).to eq "/c/parent/child"
|
2014-02-17 01:45:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-24 00:58:11 +08:00
|
|
|
|
2015-12-28 14:28:16 +08:00
|
|
|
describe "#url_with_id" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:category) { Fabricate(:category, name: 'cats') }
|
2015-12-28 14:28:16 +08:00
|
|
|
|
|
|
|
it "includes the id in the URL" do
|
|
|
|
expect(category.url_with_id).to eq("/c/#{category.id}-cats")
|
|
|
|
end
|
|
|
|
|
|
|
|
context "child category" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:child_category) { Fabricate(:category, parent_category_id: category.id, name: 'dogs') }
|
2015-12-28 14:28:16 +08:00
|
|
|
|
|
|
|
it "includes the id in the URL" do
|
|
|
|
expect(child_category.url_with_id).to eq("/c/cats/dogs/#{child_category.id}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-16 03:19:17 +08:00
|
|
|
describe "uncategorized" do
|
|
|
|
let(:cat) { Category.where(id: SiteSetting.uncategorized_category_id).first }
|
|
|
|
|
|
|
|
it "reports as `uncategorized?`" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(cat).to be_uncategorized
|
2014-07-16 03:19:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot have a parent category" do
|
|
|
|
cat.parent_category_id = Fabricate(:category).id
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(cat).to_not be_valid
|
2014-07-16 03:19:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-24 00:58:11 +08:00
|
|
|
describe "parent categories" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:parent_category) { Fabricate(:category, user: user) }
|
2013-10-24 00:58:11 +08:00
|
|
|
|
|
|
|
it "can be associated with a parent category" do
|
|
|
|
sub_category = Fabricate.build(:category, parent_category_id: parent_category.id, user: user)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(sub_category).to be_valid
|
|
|
|
expect(sub_category.parent_category).to eq(parent_category)
|
2013-10-24 00:58:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot associate a category with itself" do
|
|
|
|
category = Fabricate(:category, user: user)
|
|
|
|
category.parent_category_id = category.id
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category).to_not be_valid
|
2013-10-24 00:58:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot have a category two levels deep" do
|
|
|
|
sub_category = Fabricate(:category, parent_category_id: parent_category.id, user: user)
|
|
|
|
nested_sub_category = Fabricate.build(:category, parent_category_id: sub_category.id, user: user)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(nested_sub_category).to_not be_valid
|
2013-10-24 00:58:11 +08:00
|
|
|
end
|
|
|
|
|
2014-02-09 06:10:48 +08:00
|
|
|
describe ".query_parent_category" do
|
|
|
|
it "should return the parent category id given a parent slug" do
|
|
|
|
parent_category.name = "Amazing Category"
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(parent_category.id).to eq(Category.query_parent_category(parent_category.slug))
|
2014-02-09 06:10:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".query_category" do
|
|
|
|
it "should return the category" do
|
|
|
|
category = Fabricate(:category, name: "Amazing Category", parent_category_id: parent_category.id, user: user)
|
|
|
|
parent_category.name = "Amazing Parent Category"
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(category).to eq(Category.query_category(category.slug, parent_category.id))
|
2014-02-09 06:10:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-24 00:58:11 +08:00
|
|
|
end
|
|
|
|
|
2014-07-14 22:16:24 +08:00
|
|
|
describe "find_by_email" do
|
|
|
|
it "is case insensitive" do
|
|
|
|
c1 = Fabricate(:category, email_in: 'lower@example.com')
|
|
|
|
c2 = Fabricate(:category, email_in: 'UPPER@EXAMPLE.COM')
|
|
|
|
c3 = Fabricate(:category, email_in: 'Mixed.Case@Example.COM')
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Category.find_by_email('LOWER@EXAMPLE.COM')).to eq(c1)
|
|
|
|
expect(Category.find_by_email('upper@example.com')).to eq(c2)
|
|
|
|
expect(Category.find_by_email('mixed.case@example.com')).to eq(c3)
|
|
|
|
expect(Category.find_by_email('MIXED.CASE@EXAMPLE.COM')).to eq(c3)
|
2014-07-14 22:16:24 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-15 00:38:29 +08:00
|
|
|
describe "find_by_slug" do
|
|
|
|
it "finds with category and sub category" do
|
|
|
|
category = Fabricate(:category, slug: 'awesome-category')
|
|
|
|
sub_category = Fabricate(:category, parent_category_id: category.id, slug: 'awesome-sub-category')
|
|
|
|
|
|
|
|
expect(Category.find_by_slug('awesome-category')).to eq(category)
|
|
|
|
expect(Category.find_by_slug('awesome-sub-category', 'awesome-category')).to eq(sub_category)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-28 23:57:30 +08:00
|
|
|
describe "validate email_in" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2016-07-28 23:57:30 +08:00
|
|
|
|
|
|
|
it "works with a valid email" do
|
|
|
|
expect(Category.new(name: 'test', user: user, email_in: 'test@example.com').valid?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds an error with an invalid email" do
|
|
|
|
category = Category.new(name: 'test', user: user, email_in: '<sup>test</sup>')
|
|
|
|
expect(category.valid?).to eq(false)
|
|
|
|
expect(category.errors.full_messages.join).not_to match(/<sup>/)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a duplicate email in a group" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:group) { Fabricate(:group, name: 'testgroup', incoming_email: 'test@example.com') }
|
2016-07-28 23:57:30 +08:00
|
|
|
|
|
|
|
it "adds an error with an invalid email" do
|
|
|
|
category = Category.new(name: 'test', user: user, email_in: group.incoming_email)
|
|
|
|
expect(category.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with duplicate email in a category" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:category) { Fabricate(:category, user: user, name: '<b>cool</b>', email_in: 'test@example.com') }
|
2016-07-28 23:57:30 +08:00
|
|
|
|
|
|
|
it "adds an error with an invalid email" do
|
|
|
|
category = Category.new(name: 'test', user: user, email_in: "test@example.com")
|
|
|
|
expect(category.valid?).to eq(false)
|
|
|
|
expect(category.errors.full_messages.join).not_to match(/<b>/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-07-13 10:51:08 +08:00
|
|
|
describe 'require topic/post approval' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:category) { Fabricate(:category) }
|
2018-07-13 10:51:08 +08:00
|
|
|
|
|
|
|
describe '#require_topic_approval?' do
|
|
|
|
before do
|
|
|
|
category.custom_fields[Category::REQUIRE_TOPIC_APPROVAL] = true
|
|
|
|
category.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(category.reload.require_topic_approval?).to eq(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#require_reply_approval?' do
|
|
|
|
before do
|
|
|
|
category.custom_fields[Category::REQUIRE_REPLY_APPROVAL] = true
|
|
|
|
category.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(category.reload.require_reply_approval?).to eq(true) }
|
|
|
|
end
|
|
|
|
end
|
2018-07-16 16:10:22 +08:00
|
|
|
|
|
|
|
describe 'auto bump' do
|
|
|
|
after do
|
|
|
|
RateLimiter.disable
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly automatically bump topics' do
|
|
|
|
freeze_time 1.second.ago
|
|
|
|
category = Fabricate(:category)
|
|
|
|
category.clear_auto_bump_cache!
|
|
|
|
|
2018-07-18 08:17:33 +08:00
|
|
|
freeze_time 1.second.from_now
|
|
|
|
post1 = create_post(category: category)
|
|
|
|
freeze_time 1.second.from_now
|
2018-07-16 16:10:22 +08:00
|
|
|
_post2 = create_post(category: category)
|
2018-07-18 08:17:33 +08:00
|
|
|
freeze_time 1.second.from_now
|
2018-07-16 16:10:22 +08:00
|
|
|
_post3 = create_post(category: category)
|
|
|
|
|
2018-07-17 07:33:33 +08:00
|
|
|
# no limits on post creation or category creation please
|
|
|
|
RateLimiter.enable
|
|
|
|
|
2018-07-16 16:10:22 +08:00
|
|
|
time = 1.month.from_now
|
|
|
|
freeze_time time
|
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(false)
|
|
|
|
expect(Topic.where(bumped_at: time).count).to eq(0)
|
|
|
|
|
|
|
|
category.num_auto_bump_daily = 2
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(true)
|
|
|
|
expect(Topic.where(bumped_at: time).count).to eq(1)
|
2018-07-18 08:17:33 +08:00
|
|
|
# our extra bump message
|
|
|
|
expect(post1.topic.reload.posts_count).to eq(2)
|
|
|
|
|
|
|
|
time = time + 13.hours
|
|
|
|
freeze_time time
|
2018-07-16 16:10:22 +08:00
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(true)
|
2018-07-18 08:17:33 +08:00
|
|
|
expect(Topic.where(bumped_at: time).count).to eq(1)
|
2018-07-16 16:10:22 +08:00
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(false)
|
2018-07-18 08:17:33 +08:00
|
|
|
expect(Topic.where(bumped_at: time).count).to eq(1)
|
2018-07-16 16:10:22 +08:00
|
|
|
|
2018-07-17 07:33:33 +08:00
|
|
|
time = 1.month.from_now
|
|
|
|
freeze_time time
|
|
|
|
|
|
|
|
category.auto_bump_limiter.clear!
|
|
|
|
expect(Category.auto_bump_topic!).to eq(true)
|
|
|
|
expect(Topic.where(bumped_at: time).count).to eq(1)
|
2018-07-24 14:47:55 +08:00
|
|
|
|
|
|
|
category.num_auto_bump_daily = ""
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
expect(Category.auto_bump_topic!).to eq(false)
|
2018-07-16 16:10:22 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-14 13:38:52 +08:00
|
|
|
describe "validate permissions compatibility" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
|
|
|
fab!(:group2) { Fabricate(:group) }
|
|
|
|
fab!(:parent_category) { Fabricate(:category, name: "parent") }
|
|
|
|
fab!(:subcategory) { Fabricate(:category, name: "child1", parent_category_id: parent_category.id) }
|
|
|
|
fab!(:subcategory2) { Fabricate(:category, name: "child2", parent_category_id: parent_category.id) }
|
2019-02-14 13:38:52 +08:00
|
|
|
|
|
|
|
context "when changing subcategory permissions" do
|
|
|
|
it "it is not valid if permissions are less restrictive" do
|
|
|
|
parent_category.set_permissions(group => :readonly)
|
|
|
|
parent_category.save!
|
|
|
|
|
|
|
|
subcategory.set_permissions(group => :full, group2 => :readonly)
|
|
|
|
|
|
|
|
expect(subcategory.valid?).to eq(false)
|
2019-04-01 12:34:52 +08:00
|
|
|
expect(subcategory.errors.full_messages).to contain_exactly(I18n.t("category.errors.permission_conflict", group_names: group2.name))
|
2019-02-14 13:38:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is valid if permissions are same or more restrictive" do
|
|
|
|
parent_category.set_permissions(group => :full, group2 => :create_post)
|
|
|
|
parent_category.save!
|
|
|
|
|
|
|
|
subcategory.set_permissions(group => :create_post, group2 => :full)
|
|
|
|
|
|
|
|
expect(subcategory.valid?).to eq(true)
|
|
|
|
end
|
|
|
|
|
2019-03-04 11:49:26 +08:00
|
|
|
it "is valid if everyone has access to parent category" do
|
|
|
|
parent_category.set_permissions(everyone: :readonly)
|
2019-02-14 13:38:52 +08:00
|
|
|
parent_category.save!
|
|
|
|
|
|
|
|
subcategory.set_permissions(group => :create_post, group2 => :create_post)
|
|
|
|
|
|
|
|
expect(subcategory.valid?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when changing parent category permissions" do
|
|
|
|
it "it is not valid if subcategory permissions are less restrictive" do
|
|
|
|
subcategory.set_permissions(group => :create_post)
|
|
|
|
subcategory.save!
|
|
|
|
subcategory2.set_permissions(group => :create_post, group2 => :create_post)
|
|
|
|
subcategory2.save!
|
|
|
|
|
|
|
|
parent_category.set_permissions(group => :readonly)
|
|
|
|
|
|
|
|
expect(parent_category.valid?).to eq(false)
|
2019-04-01 12:34:52 +08:00
|
|
|
expect(parent_category.errors.full_messages).to contain_exactly(I18n.t("category.errors.permission_conflict", group_names: group2.name))
|
2019-02-14 13:38:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is valid if subcategory permissions are same or more restrictive" do
|
|
|
|
subcategory.set_permissions(group => :create_post)
|
|
|
|
subcategory.save!
|
|
|
|
subcategory2.set_permissions(group => :create_post, group2 => :create_post)
|
|
|
|
subcategory2.save!
|
|
|
|
|
|
|
|
parent_category.set_permissions(group => :full, group2 => :create_post)
|
|
|
|
|
|
|
|
expect(parent_category.valid?).to eq(true)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-03-04 11:49:26 +08:00
|
|
|
it "is valid if everyone has access to parent category" do
|
2019-02-14 13:38:52 +08:00
|
|
|
subcategory.set_permissions(group => :create_post)
|
|
|
|
subcategory.save
|
2019-03-04 11:49:26 +08:00
|
|
|
parent_category.set_permissions(everyone: :readonly)
|
2019-02-14 13:38:52 +08:00
|
|
|
|
|
|
|
expect(parent_category.valid?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-06 17:30:52 +08:00
|
|
|
describe "#ensure_consistency!" do
|
|
|
|
it "creates category topic" do
|
2019-06-07 09:19:57 +08:00
|
|
|
|
|
|
|
# corrupt a category topic
|
|
|
|
uncategorized = Category.find(SiteSetting.uncategorized_category_id)
|
|
|
|
uncategorized.create_category_definition
|
|
|
|
uncategorized.topic.posts.first.destroy!
|
|
|
|
|
2019-06-07 12:56:31 +08:00
|
|
|
# make stuff extra broken
|
|
|
|
uncategorized.topic.trash!
|
|
|
|
|
2019-06-06 17:30:52 +08:00
|
|
|
category = Fabricate(:category)
|
|
|
|
category_destroyed = Fabricate(:category)
|
|
|
|
category_trashed = Fabricate(:category)
|
|
|
|
|
|
|
|
category_topic_id = category.topic.id
|
|
|
|
category_destroyed.topic.destroy!
|
|
|
|
category_trashed.topic.trash!
|
|
|
|
|
|
|
|
Category.ensure_consistency!
|
2019-06-07 09:19:57 +08:00
|
|
|
# step one fix corruption
|
|
|
|
expect(uncategorized.reload.topic_id).to eq(nil)
|
|
|
|
|
|
|
|
Category.ensure_consistency!
|
|
|
|
# step two don't create a category definition for uncategorized
|
|
|
|
expect(uncategorized.reload.topic_id).to eq(nil)
|
2019-06-06 17:30:52 +08:00
|
|
|
|
|
|
|
expect(category.reload.topic_id).to eq(category_topic_id)
|
|
|
|
expect(category_destroyed.reload.topic).to_not eq(nil)
|
|
|
|
expect(category_trashed.reload.topic).to_not eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|