2013-02-14 15:51:48 -06:00
|
|
|
# encoding: utf-8
|
2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
2013-02-14 15:51:48 -06:00
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe Category do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:user)
|
2019-03-18 02:25:45 -05:00
|
|
|
|
2023-06-05 10:38:50 -05:00
|
|
|
it_behaves_like "it has custom fields"
|
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
it { is_expected.to validate_presence_of :user_id }
|
|
|
|
it { is_expected.to validate_presence_of :name }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2023-02-06 22:27:38 -06:00
|
|
|
it do
|
|
|
|
is_expected.to validate_numericality_of(:default_slow_mode_seconds).is_greater_than(
|
|
|
|
0,
|
|
|
|
).only_integer
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
it "validates uniqueness of name" do
|
2019-08-06 05:26:54 -05:00
|
|
|
Fabricate(:category_with_definition)
|
2019-04-17 16:41:37 -05:00
|
|
|
is_expected.to validate_uniqueness_of(:name).scoped_to(:parent_category_id).case_insensitive
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2019-03-18 02:25:45 -05: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 01:58:18 -05:00
|
|
|
expect(category.errors.to_hash.keys).to contain_exactly(:search_priority)
|
2019-03-18 02:25:45 -05:00
|
|
|
end
|
|
|
|
|
2014-08-11 15:55:26 -05:00
|
|
|
it "validates uniqueness in case insensitive way" do
|
2019-08-06 05:26:54 -05:00
|
|
|
Fabricate(:category_with_definition, name: "Cats")
|
2014-12-31 08:55:03 -06:00
|
|
|
cats = Fabricate.build(:category, name: "cats")
|
|
|
|
expect(cats).to_not be_valid
|
|
|
|
expect(cats.errors[:name]).to be_present
|
2014-08-11 15:55:26 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "Associations" do
|
2023-02-12 22:37:59 -06:00
|
|
|
it { is_expected.to have_one(:category_setting).dependent(:destroy) }
|
|
|
|
|
2023-03-09 23:45:01 -06:00
|
|
|
it "automatically creates a category setting" do
|
|
|
|
expect { Fabricate(:category) }.to change { CategorySetting.count }.by(1)
|
|
|
|
end
|
|
|
|
|
2022-06-30 01:54:20 -05:00
|
|
|
it "should delete associated sidebar_section_links when category is destroyed" do
|
|
|
|
category_sidebar_section_link = Fabricate(:category_sidebar_section_link)
|
|
|
|
category_sidebar_section_link_2 =
|
|
|
|
Fabricate(:category_sidebar_section_link, linkable: category_sidebar_section_link.linkable)
|
|
|
|
tag_sidebar_section_link = Fabricate(:tag_sidebar_section_link)
|
|
|
|
|
|
|
|
expect { category_sidebar_section_link.linkable.destroy! }.to change {
|
|
|
|
SidebarSectionLink.count
|
2023-05-03 21:14:09 -05:00
|
|
|
}.from(12).to(10)
|
|
|
|
expect(SidebarSectionLink.last).to eq(tag_sidebar_section_link)
|
2022-06-30 01:54:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-12 09:28:33 -06:00
|
|
|
describe "slug" do
|
|
|
|
it "converts to lower" do
|
|
|
|
category = Category.create!(name: "Hello World", slug: "Hello-World", user: user)
|
|
|
|
expect(category.slug).to eq("hello-world")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-13 20:24:16 -05:00
|
|
|
describe "resolve_permissions" do
|
|
|
|
it "can determine read_restricted" do
|
|
|
|
read_restricted, resolved = Category.resolve_permissions(everyone: :full)
|
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(read_restricted).to be false
|
|
|
|
expect(resolved).to be_blank
|
2013-07-13 20:24:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-17 02:51:32 -05:00
|
|
|
describe "permissions_params" do
|
|
|
|
it "returns the right group names and permission type" do
|
2019-08-06 05:26:54 -05:00
|
|
|
category = Fabricate(:category_with_definition)
|
2015-09-17 02:51:32 -05:00
|
|
|
group = Fabricate(:group)
|
|
|
|
category_group = Fabricate(:category_group, category: category, group: group)
|
|
|
|
expect(category.permissions_params).to eq("#{group.name}" => category_group.permission_type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-17 16:12:32 -05:00
|
|
|
describe "#review_group_id" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:group)
|
2019-08-06 05:26:54 -05:00
|
|
|
fab!(:category) { Fabricate(:category_with_definition, reviewable_by_group: group) }
|
2019-05-10 05:59:31 -05:00
|
|
|
fab!(:topic) { Fabricate(:topic, category: category) }
|
|
|
|
fab!(:post) { Fabricate(:post, topic: topic) }
|
2023-12-13 03:18:42 -06:00
|
|
|
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
|
2019-04-17 16:12:32 -05:00
|
|
|
|
|
|
|
it "will add the group to the reviewable" do
|
2020-07-14 11:36:19 -05:00
|
|
|
SiteSetting.enable_category_group_moderation = true
|
2019-04-17 16:12:32 -05:00
|
|
|
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
|
2020-07-14 11:36:19 -05:00
|
|
|
SiteSetting.enable_category_group_moderation = true
|
2019-04-17 16:12:32 -05:00
|
|
|
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
|
2020-07-14 11:36:19 -05:00
|
|
|
SiteSetting.enable_category_group_moderation = false
|
2019-04-17 16:12:32 -05:00
|
|
|
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
|
2020-07-14 11:36:19 -05:00
|
|
|
SiteSetting.enable_category_group_moderation = true
|
2019-04-17 16:12:32 -05:00
|
|
|
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-13 20:24:16 -05:00
|
|
|
describe "topic_create_allowed and post_create_allowed" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:group)
|
2013-07-13 20:24:16 -05:00
|
|
|
|
2019-06-26 02:41:07 -05:00
|
|
|
fab!(:user) do
|
2013-07-13 20:24:16 -05:00
|
|
|
user = Fabricate(:user)
|
|
|
|
group.add(user)
|
|
|
|
group.save
|
2019-06-26 02:41:07 -05:00
|
|
|
user
|
|
|
|
end
|
2013-07-13 20:24:16 -05:00
|
|
|
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:admin)
|
2013-07-13 20:24:16 -05:00
|
|
|
|
2019-08-06 05:26:54 -05:00
|
|
|
fab!(:default_category) { Fabricate(:category_with_definition) }
|
2013-07-13 20:24:16 -05:00
|
|
|
|
2019-06-26 02:41:07 -05:00
|
|
|
fab!(:full_category) do
|
2019-08-06 05:26:54 -05:00
|
|
|
c = Fabricate(:category_with_definition)
|
2019-06-26 02:41:07 -05:00
|
|
|
c.set_permissions(group => :full)
|
|
|
|
c.save
|
|
|
|
c
|
|
|
|
end
|
|
|
|
|
|
|
|
fab!(:can_post_category) do
|
2019-08-06 05:26:54 -05:00
|
|
|
c = Fabricate(:category_with_definition)
|
2019-06-26 02:41:07 -05:00
|
|
|
c.set_permissions(group => :create_post)
|
|
|
|
c.save
|
|
|
|
c
|
|
|
|
end
|
|
|
|
|
|
|
|
fab!(:can_read_category) do
|
2019-08-06 05:26:54 -05:00
|
|
|
c = Fabricate(:category_with_definition)
|
2019-06-26 02:41:07 -05:00
|
|
|
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-13 20:24:16 -05:00
|
|
|
|
2019-06-26 02:41:07 -05:00
|
|
|
context "when disabling uncategorized" do
|
|
|
|
before { SiteSetting.allow_uncategorized_topics = false }
|
2013-07-13 20:24:16 -05:00
|
|
|
|
2019-06-26 02:41:07 -05:00
|
|
|
it "allows everything to admins unconditionally" do
|
|
|
|
count = Category.count
|
2013-07-13 20:24:16 -05:00
|
|
|
|
2019-06-26 02:41:07 -05: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 00:44:07 -05:00
|
|
|
|
2019-06-26 02:41:07 -05:00
|
|
|
it "allows normal users correct access to all categories" do
|
|
|
|
# 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
|
2013-07-16 00:44:07 -05:00
|
|
|
can_post_category.set_permissions(everyone: :create_post)
|
|
|
|
can_post_category.save
|
|
|
|
|
2019-06-26 02:41:07 -05:00
|
|
|
expect(Category.post_create_allowed(user_guardian).count).to eq(4)
|
2013-07-13 20:24:16 -05:00
|
|
|
|
2013-10-12 17:54:48 -05:00
|
|
|
# anonymous has permission to create no topics
|
2019-06-26 02:41:07 -05:00
|
|
|
expect(Category.scoped_to_permissions(user_guardian, [:readonly]).count).to eq(3)
|
2013-10-12 17:54:48 -05:00
|
|
|
end
|
2013-07-13 20:24:16 -05:00
|
|
|
end
|
|
|
|
|
2024-04-10 09:35:42 -05:00
|
|
|
describe "with_parents" do
|
|
|
|
fab!(:category)
|
|
|
|
fab!(:subcategory) { Fabricate(:category, parent_category: category) }
|
|
|
|
|
|
|
|
it "returns parent categories and subcategories" do
|
|
|
|
expect(Category.with_parents([category.id])).to contain_exactly(category)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns only categories if top-level categories" do
|
|
|
|
expect(Category.with_parents([subcategory.id])).to contain_exactly(category, subcategory)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-29 01:33:24 -05:00
|
|
|
describe "security" do
|
2019-08-06 05:26:54 -05:00
|
|
|
fab!(:category) { Fabricate(:category_with_definition) }
|
|
|
|
fab!(:category_2) { Fabricate(:category_with_definition) }
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:user)
|
|
|
|
fab!(:group)
|
2013-04-29 01:33:24 -05:00
|
|
|
|
2013-05-15 15:45:52 -05:00
|
|
|
it "secures categories correctly" do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category.read_restricted?).to be false
|
2013-04-29 01:33:24 -05:00
|
|
|
|
2013-07-13 20:24:16 -05:00
|
|
|
category.set_permissions({})
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category.read_restricted?).to be true
|
2013-04-29 01:33:24 -05:00
|
|
|
|
2013-07-13 20:24:16 -05:00
|
|
|
category.set_permissions(everyone: :full)
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category.read_restricted?).to be false
|
2013-04-29 01:33:24 -05:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(user.secure_categories).to be_empty
|
2013-04-29 01:33:24 -05:00
|
|
|
|
|
|
|
group.add(user)
|
|
|
|
group.save
|
|
|
|
|
2013-07-13 20:24:16 -05:00
|
|
|
category.set_permissions(group.id => :full)
|
2013-04-29 01:33:24 -05:00
|
|
|
category.save
|
|
|
|
|
|
|
|
user.reload
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(user.secure_categories).to eq([category])
|
2013-05-15 15:45:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "lists all secured categories correctly" do
|
2014-07-15 14:19:17 -05:00
|
|
|
uncategorized = Category.find(SiteSetting.uncategorized_category_id)
|
2013-10-23 18:05:51 -05:00
|
|
|
|
2013-05-15 15:45:52 -05:00
|
|
|
group.add(user)
|
2013-07-13 20:24:16 -05:00
|
|
|
category.set_permissions(group.id => :full)
|
2017-08-30 23:06:56 -05:00
|
|
|
category.save!
|
2013-07-13 20:24:16 -05:00
|
|
|
category_2.set_permissions(group.id => :full)
|
2017-08-30 23:06:56 -05:00
|
|
|
category_2.save!
|
2013-04-29 01:33:24 -05:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(Category.secured).to match_array([uncategorized])
|
|
|
|
expect(Category.secured(Guardian.new(user))).to match_array(
|
|
|
|
[uncategorized, category, category_2],
|
|
|
|
)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-30 15:48:19 -05:00
|
|
|
it "strips leading blanks" do
|
2019-08-06 05:26:54 -05:00
|
|
|
expect(Fabricate(:category_with_definition, name: " music").name).to eq("music")
|
2013-07-30 15:48:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "strips trailing blanks" do
|
2019-08-06 05:26:54 -05:00
|
|
|
expect(Fabricate(:category_with_definition, name: "bugs ").name).to eq("bugs")
|
2013-07-30 15:48:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "strips leading and trailing blanks" do
|
2019-08-06 05:26:54 -05:00
|
|
|
expect(Fabricate(:category_with_definition, name: " blanks ").name).to eq("blanks")
|
2013-07-30 15:48:19 -05:00
|
|
|
end
|
|
|
|
|
2014-08-18 10:07:32 -05:00
|
|
|
it "sets name_lower" do
|
2019-08-06 05:26:54 -05:00
|
|
|
expect(Fabricate(:category_with_definition, name: "Not MySQL").name_lower).to eq("not mysql")
|
2014-08-18 10:07:32 -05:00
|
|
|
end
|
|
|
|
|
2014-04-25 08:14:05 -05:00
|
|
|
it "has custom fields" do
|
2019-08-06 05:26:54 -05:00
|
|
|
category = Fabricate(:category_with_definition, name: " music")
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category.custom_fields["a"]).to be_nil
|
2014-04-25 08:14:05 -05:00
|
|
|
|
|
|
|
category.custom_fields["bob"] = "marley"
|
|
|
|
category.custom_fields["jack"] = "black"
|
|
|
|
category.save
|
|
|
|
|
|
|
|
category = Category.find(category.id)
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category.custom_fields).to eq("bob" => "marley", "jack" => "black")
|
2014-04-25 08:14:05 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
describe "short name" do
|
2019-08-06 05:26:54 -05:00
|
|
|
fab!(:category) { Fabricate(:category_with_definition, name: "xx") }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
it "creates the category" do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category).to be_present
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has one topic" do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(Topic.where(category_id: category.id).count).to eq(1)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-14 15:51:48 -06:00
|
|
|
describe "non-english characters" do
|
2022-07-27 11:14:14 -05:00
|
|
|
context "when using ascii slug generator" do
|
2015-04-13 09:50:41 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.slug_generation_method = "ascii"
|
2019-08-06 05:26:54 -05:00
|
|
|
@category = Fabricate(:category_with_definition, name: "测试")
|
2015-04-13 09:50:41 -05:00
|
|
|
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-14 15:51:48 -06:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "when using none slug generator" do
|
2015-04-13 09:50:41 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.slug_generation_method = "none"
|
2019-08-06 05:26:54 -05:00
|
|
|
@category = Fabricate(:category_with_definition, name: "测试")
|
2015-04-13 09:50:41 -05:00
|
|
|
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-14 15:51:48 -06:00
|
|
|
end
|
2014-09-16 06:15:05 -05:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "when using encoded slug generator" do
|
2015-04-13 09:50:41 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.slug_generation_method = "encoded"
|
2019-08-06 05:26:54 -05:00
|
|
|
@category = Fabricate(:category_with_definition, name: "测试")
|
2015-04-13 09:50:41 -05:00
|
|
|
end
|
|
|
|
after do
|
|
|
|
SiteSetting.slug_generation_method = "ascii"
|
|
|
|
@category.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a slug" do
|
2019-10-11 10:38:16 -05:00
|
|
|
expect(@category.slug).to eq("%E6%B5%8B%E8%AF%95")
|
|
|
|
expect(@category.slug_for_url).to eq("%E6%B5%8B%E8%AF%95")
|
2015-04-13 09:50:41 -05:00
|
|
|
end
|
2019-10-21 12:33:19 -05:00
|
|
|
|
2019-10-21 12:35:19 -05:00
|
|
|
it "keeps the encoded slug after saving" do
|
2019-10-21 12:33:19 -05:00
|
|
|
@category.save
|
|
|
|
expect(@category.slug).to eq("%E6%B5%8B%E8%AF%95")
|
|
|
|
expect(@category.slug_for_url).to eq("%E6%B5%8B%E8%AF%95")
|
|
|
|
end
|
2014-09-16 06:15:05 -05:00
|
|
|
end
|
2013-02-14 15:51:48 -06:00
|
|
|
end
|
|
|
|
|
2013-05-30 10:09:09 -05:00
|
|
|
describe "slug would be a number" do
|
2015-04-13 09:50:41 -05:00
|
|
|
let(:category) { Fabricate.build(:category, name: "2") }
|
2013-05-30 10:09:09 -05:00
|
|
|
|
|
|
|
it "creates a blank slug" do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category.slug).to be_blank
|
|
|
|
expect(category.slug_for_url).to eq("#{category.id}-category")
|
2013-05-30 10:09:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-03 18:23:59 -06:00
|
|
|
describe "custom slug can be provided" do
|
2015-04-13 09:50:41 -05:00
|
|
|
it "can be sanitized" do
|
2019-08-06 05:26:54 -05:00
|
|
|
@c = Fabricate(:category_with_definition, name: "Fun Cats", slug: "fun-cats")
|
|
|
|
@cat = Fabricate(:category_with_definition, name: "love cats", slug: "love-cats")
|
2014-12-03 18:23:59 -06:00
|
|
|
|
2015-04-13 09:50:41 -05: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 08:07:29 -06:00
|
|
|
|
2015-04-13 09:50:41 -05:00
|
|
|
@cat.slug = "#{@cat.id}-category"
|
|
|
|
expect(@cat).to be_valid
|
|
|
|
expect(@cat.errors[:slug]).not_to be_present
|
2014-12-03 18:23:59 -06:00
|
|
|
end
|
2022-02-16 20:46:06 -06:00
|
|
|
|
|
|
|
context "if SiteSettings.slug_generation_method = ascii" do
|
|
|
|
before { SiteSetting.slug_generation_method = "ascii" }
|
|
|
|
|
|
|
|
it "fails if slug contains non-ascii characters" do
|
|
|
|
c = Fabricate.build(:category, name: "Sem acentuação", slug: "sem-acentuação")
|
|
|
|
expect(c).not_to be_valid
|
|
|
|
|
|
|
|
expect(c.errors[:slug]).to be_present
|
|
|
|
end
|
|
|
|
end
|
2014-12-03 18:23:59 -06:00
|
|
|
end
|
|
|
|
|
2014-10-21 23:48:18 -05:00
|
|
|
describe "description_text" do
|
|
|
|
it "correctly generates text description as needed" do
|
|
|
|
c = Category.new
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(c.description_text).to be_nil
|
2023-02-26 19:48:48 -06:00
|
|
|
c.description = "<hello <a>foo/bar</a>."
|
|
|
|
expect(c.description_text).to eq("<hello foo/bar.")
|
2014-10-21 23:48:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
describe "after create" do
|
|
|
|
before do
|
2019-08-06 05:26:54 -05:00
|
|
|
@category = Fabricate(:category_with_definition, name: "Amazing Category")
|
2013-02-05 13:16:51 -06:00
|
|
|
@topic = @category.topic
|
|
|
|
end
|
|
|
|
|
2013-04-29 01:33:24 -05:00
|
|
|
it "is created correctly" do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(@category.slug).to eq("amazing-category")
|
|
|
|
expect(@category.slug_for_url).to eq(@category.slug)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(@category.description).to be_blank
|
2013-02-21 17:09:56 -06:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(Topic.where(category_id: @category).count).to eq(1)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(@topic).to be_present
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(@topic.category).to eq(@category)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(@topic).to be_visible
|
2013-03-07 11:45:49 -06:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(@topic.pinned_at).to be_present
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(Guardian.new(@category.user).can_delete?(@topic)).to be false
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(@topic.posts.count).to eq(1)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-12-31 08:55:03 -06: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-06 13:56:39 -06:00
|
|
|
|
2014-12-31 08:55:03 -06: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-06 13:56:39 -06:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(@category.topics_week).to eq(0)
|
|
|
|
expect(@category.topics_month).to eq(0)
|
|
|
|
expect(@category.topics_year).to eq(0)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2020-04-20 14:51:49 -05:00
|
|
|
it "cooks the definition" do
|
|
|
|
category =
|
|
|
|
Category.create(
|
|
|
|
name: "little-test",
|
|
|
|
user_id: Discourse.system_user.id,
|
|
|
|
description: "click the link [here](https://fakeurl.com)",
|
|
|
|
)
|
|
|
|
expect(category.description.include?("[here]")).to eq(false)
|
|
|
|
expect(category.description).to eq(category.topic.first_post.cooked)
|
|
|
|
end
|
|
|
|
|
2014-07-18 12:59:54 -05:00
|
|
|
it "renames the definition when renamed" do
|
2019-04-29 02:32:25 -05:00
|
|
|
@category.update(name: "Troutfishing")
|
2014-07-18 12:59:54 -05:00
|
|
|
@topic.reload
|
2016-07-28 10:57:30 -05:00
|
|
|
expect(@topic.title).to match(/Troutfishing/)
|
2017-03-26 01:59:53 -05:00
|
|
|
expect(@topic.fancy_title).to match(/Troutfishing/)
|
2014-07-18 12:59:54 -05:00
|
|
|
end
|
|
|
|
|
2014-07-25 15:36:16 -05:00
|
|
|
it "doesn't raise an error if there is no definition topic to rename (uncategorized)" do
|
2019-04-29 02:32:25 -05:00
|
|
|
expect { @category.update(name: "Troutfishing", topic_id: nil) }.to_not raise_error
|
2014-07-25 15:36:16 -05:00
|
|
|
end
|
|
|
|
|
2016-04-27 06:04:44 -05:00
|
|
|
it "creates permalink when category slug is changed" do
|
2019-04-29 02:32:25 -05:00
|
|
|
@category.update(slug: "new-category")
|
2016-04-27 06:04:44 -05:00
|
|
|
expect(Permalink.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2019-03-20 17:38:59 -05:00
|
|
|
it "reuses existing permalink when category slug is changed" do
|
2020-06-18 03:32:14 -05:00
|
|
|
permalink = Permalink.create!(url: "c/#{@category.slug}/#{@category.id}", category_id: 42)
|
2019-03-20 17:38:59 -05:00
|
|
|
|
2019-04-29 02:32:25 -05:00
|
|
|
expect { @category.update(slug: "new-slug") }.to_not change { Permalink.count }
|
2019-03-20 17:38:59 -05:00
|
|
|
expect(permalink.reload.category_id).to eq(@category.id)
|
|
|
|
end
|
|
|
|
|
2016-04-27 06:04:44 -05:00
|
|
|
it "creates permalink when sub category slug is changed" do
|
2019-08-06 05:26:54 -05:00
|
|
|
sub_category =
|
|
|
|
Fabricate(:category_with_definition, slug: "sub-category", parent_category_id: @category.id)
|
2019-04-29 02:32:25 -05:00
|
|
|
sub_category.update(slug: "new-sub-category")
|
2016-04-27 06:04:44 -05:00
|
|
|
expect(Permalink.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes permalink when category slug is reused" do
|
|
|
|
Fabricate(:permalink, url: "/c/bikeshed-category")
|
2019-08-06 05:26:54 -05:00
|
|
|
Fabricate(:category_with_definition, slug: "bikeshed-category")
|
2016-04-27 06:04:44 -05:00
|
|
|
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")
|
2019-08-06 05:26:54 -05:00
|
|
|
main_category = Fabricate(:category_with_definition, slug: "main-category")
|
|
|
|
Fabricate(
|
|
|
|
:category_with_definition,
|
|
|
|
slug: "sub-category",
|
|
|
|
parent_category_id: main_category.id,
|
|
|
|
)
|
2016-04-27 06:04:44 -05:00
|
|
|
expect(Permalink.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
2018-12-20 21:59:54 -06:00
|
|
|
it "correctly creates permalink when category slug is changed in subfolder install" do
|
2019-11-14 23:48:24 -06:00
|
|
|
set_subfolder "/forum"
|
2018-12-20 21:59:54 -06:00
|
|
|
old_url = @category.url
|
2019-04-29 02:32:25 -05:00
|
|
|
@category.update(slug: "new-category")
|
2018-12-20 21:59:54 -06:00
|
|
|
permalink = Permalink.last
|
|
|
|
expect(permalink.url).to eq(old_url[1..-1])
|
|
|
|
end
|
|
|
|
|
2013-12-03 17:53:40 -06:00
|
|
|
it "should not set its description topic to auto-close" do
|
2019-08-06 05:26:54 -05:00
|
|
|
category = Fabricate(:category_with_definition, name: "Closing Topics", auto_close_hours: 1)
|
2017-05-16 13:49:42 -05:00
|
|
|
expect(category.topic.public_topic_timer).to eq(nil)
|
2013-12-03 17:53:40 -06:00
|
|
|
end
|
|
|
|
|
2013-04-01 11:26:51 -05:00
|
|
|
describe "creating a new category with the same slug" do
|
2014-08-13 13:45:25 -05:00
|
|
|
it "should have a blank slug if at the same level" do
|
2019-08-06 05:26:54 -05:00
|
|
|
category = Fabricate(:category_with_definition, name: "Amazing Categóry")
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category.slug).to be_blank
|
|
|
|
expect(category.slug_for_url).to eq("#{category.id}-category")
|
2013-04-01 11:26:51 -05:00
|
|
|
end
|
2014-08-13 13:45:25 -05:00
|
|
|
|
|
|
|
it "doesn't have a blank slug if not at the same level" do
|
2019-08-06 05:26:54 -05:00
|
|
|
parent = Fabricate(:category_with_definition, name: "Other parent")
|
|
|
|
category =
|
|
|
|
Fabricate(
|
|
|
|
:category_with_definition,
|
|
|
|
name: "Amazing Categóry",
|
|
|
|
parent_category_id: parent.id,
|
|
|
|
)
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category.slug).to eq("amazing-category")
|
|
|
|
expect(category.slug_for_url).to eq("amazing-category")
|
2014-08-13 13:45:25 -05:00
|
|
|
end
|
2013-04-01 11:26:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
describe "trying to change the category topic's category" do
|
|
|
|
before do
|
2019-08-06 05:26:54 -05:00
|
|
|
@new_cat = Fabricate(:category_with_definition, name: "2nd Category", user: @category.user)
|
2014-07-16 14:39:39 -05:00
|
|
|
@topic.change_category_to_id(@new_cat.id)
|
2013-02-05 13:16:51 -06:00
|
|
|
@topic.reload
|
|
|
|
@category.reload
|
|
|
|
end
|
|
|
|
|
2013-04-29 01:33:24 -05:00
|
|
|
it "does not cause changes" do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(@category.topic_count).to eq(0)
|
|
|
|
expect(@topic.category).to eq(@category)
|
|
|
|
expect(@category.topic).to eq(@topic)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 01:23:35 -05:00
|
|
|
describe "new" do
|
2023-06-21 09:00:19 -05:00
|
|
|
subject(:category) { Fabricate.build(:category, user: Fabricate(:user)) }
|
2018-03-27 01:23:35 -05:00
|
|
|
|
|
|
|
it "triggers a extensibility event" do
|
2023-06-21 09:00:19 -05:00
|
|
|
event = DiscourseEvent.track_events { category.save! }.last
|
2018-03-27 01:23:35 -05:00
|
|
|
|
|
|
|
expect(event[:event_name]).to eq(:category_created)
|
2023-06-21 09:00:19 -05:00
|
|
|
expect(event[:params].first).to eq(category)
|
2018-03-27 01:23:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-03 18:23:59 -06:00
|
|
|
describe "update" do
|
|
|
|
it "should enforce uniqueness of slug" do
|
2019-08-06 05:26:54 -05:00
|
|
|
Fabricate(:category_with_definition, slug: "the-slug")
|
|
|
|
c2 = Fabricate(:category_with_definition, slug: "different-slug")
|
2014-12-03 18:23:59 -06:00
|
|
|
c2.slug = "the-slug"
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(c2).to_not be_valid
|
|
|
|
expect(c2.errors[:slug]).to be_present
|
2014-12-03 18:23:59 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
describe "destroy" do
|
|
|
|
before do
|
2019-08-06 05:26:54 -05:00
|
|
|
@category = Fabricate(:category_with_definition)
|
2013-02-05 13:16:51 -06:00
|
|
|
@category_id = @category.id
|
|
|
|
@topic_id = @category.topic_id
|
2018-03-13 14:59:12 -05:00
|
|
|
SiteSetting.shared_drafts_category = @category.id.to_s
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-04-29 01:33:24 -05:00
|
|
|
it "is deleted correctly" do
|
2018-03-27 01:23:35 -05:00
|
|
|
@category.destroy
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(Category.exists?(id: @category_id)).to be false
|
2021-09-20 00:50:49 -05:00
|
|
|
expect(Topic.with_deleted.where.not(deleted_at: nil).exists?(id: @topic_id)).to be true
|
2018-03-13 14:59:12 -05:00
|
|
|
expect(SiteSetting.shared_drafts_category).to be_blank
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2018-03-27 01:23:35 -05:00
|
|
|
|
2023-01-19 20:29:49 -06:00
|
|
|
it "deletes related embeddable host" do
|
|
|
|
embeddable_host = Fabricate(:embeddable_host, category: @category)
|
|
|
|
@category.destroy!
|
|
|
|
expect { embeddable_host.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
|
2018-03-27 01:23:35 -05:00
|
|
|
it "triggers a extensibility event" do
|
2019-04-16 13:42:47 -05:00
|
|
|
event = DiscourseEvent.track(:category_destroyed) { @category.destroy }
|
2018-03-27 01:23:35 -05:00
|
|
|
|
|
|
|
expect(event[:event_name]).to eq(:category_destroyed)
|
|
|
|
expect(event[:params].first).to eq(@category)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-10-17 01:44:56 -05:00
|
|
|
describe "latest" do
|
|
|
|
it "should be updated correctly" do
|
2020-03-10 16:13:17 -05:00
|
|
|
category = freeze_time(1.minute.ago) { Fabricate(:category_with_definition) }
|
|
|
|
post = create_post(category: category.id, created_at: 15.seconds.ago)
|
2013-10-17 01:44:56 -05:00
|
|
|
|
|
|
|
category.reload
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category.latest_post_id).to eq(post.id)
|
|
|
|
expect(category.latest_topic_id).to eq(post.topic_id)
|
2013-10-17 01:44:56 -05:00
|
|
|
|
2020-03-10 16:13:17 -05:00
|
|
|
post2 = create_post(category: category.id, created_at: 10.seconds.ago)
|
|
|
|
post3 = create_post(topic_id: post.topic_id, category: category.id, created_at: 5.seconds.ago)
|
2013-10-17 01:44:56 -05:00
|
|
|
|
|
|
|
category.reload
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category.latest_post_id).to eq(post3.id)
|
|
|
|
expect(category.latest_topic_id).to eq(post2.topic_id)
|
2013-10-17 01:44:56 -05:00
|
|
|
|
2013-12-30 16:33:07 -06:00
|
|
|
post3.reload
|
2013-10-17 01:44:56 -05:00
|
|
|
|
|
|
|
destroyer = PostDestroyer.new(Fabricate(:admin), post3)
|
|
|
|
destroyer.destroy
|
|
|
|
|
|
|
|
category.reload
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category.latest_post_id).to eq(post2.id)
|
2013-10-17 01:44:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
describe "update_stats" do
|
2024-01-25 00:28:26 -06:00
|
|
|
before do
|
|
|
|
@category =
|
|
|
|
Fabricate(:category_with_definition, user: Fabricate(:user, refresh_auto_groups: true))
|
|
|
|
end
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2013-02-16 14:57:16 -06:00
|
|
|
context "with regular topics" do
|
|
|
|
before do
|
2016-12-05 06:31:43 -06:00
|
|
|
create_post(user: @category.user, category: @category.id)
|
2013-02-16 14:57:16 -06:00
|
|
|
Category.update_stats
|
|
|
|
@category.reload
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-04-29 01:33:24 -05:00
|
|
|
it "updates topic stats" do
|
2014-12-31 08:55:03 -06: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 06:22:05 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2013-02-16 14:57:16 -06:00
|
|
|
context "with deleted topics" do
|
|
|
|
before do
|
2013-02-25 10:42:20 -06:00
|
|
|
@category.topics << Fabricate(:deleted_topic, user: @category.user)
|
2013-02-16 14:57:16 -06:00
|
|
|
Category.update_stats
|
|
|
|
@category.reload
|
|
|
|
end
|
|
|
|
|
2013-04-29 01:33:24 -05:00
|
|
|
it "does not count deleted topics" do
|
2014-12-31 08:55:03 -06: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-13 14:15:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with revised post" do
|
|
|
|
before do
|
2016-12-05 06:31:43 -06:00
|
|
|
post = create_post(user: @category.user, category: @category.id)
|
2013-12-13 14:15:51 -06:00
|
|
|
|
2017-07-07 01:09:14 -05:00
|
|
|
SiteSetting.editing_grace_period = 1.minute
|
2014-10-27 16:06:43 -05:00
|
|
|
post.revise(post.user, { raw: "updated body" }, revised_at: post.updated_at + 2.minutes)
|
2013-12-13 14:15:51 -06:00
|
|
|
|
|
|
|
Category.update_stats
|
|
|
|
@category.reload
|
2013-02-16 14:57:16 -06:00
|
|
|
end
|
2013-03-31 06:22:05 -05:00
|
|
|
|
2013-12-13 14:15:51 -06:00
|
|
|
it "doesn't count each version of a post" do
|
2014-12-31 08:55:03 -06: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-13 14:15:51 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2014-08-27 14:58:05 -05:00
|
|
|
|
|
|
|
context "for uncategorized category" do
|
|
|
|
before do
|
|
|
|
@uncategorized = Category.find(SiteSetting.uncategorized_category_id)
|
2023-12-12 21:50:13 -06:00
|
|
|
create_post(user: Fabricate(:user, refresh_auto_groups: true), category: @uncategorized.id)
|
2014-08-27 14:58:05 -05:00
|
|
|
Category.update_stats
|
|
|
|
@uncategorized.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates topic stats" do
|
2014-12-31 08:55:03 -06: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-27 14:58:05 -05:00
|
|
|
end
|
|
|
|
end
|
2019-12-30 05:20:44 -06:00
|
|
|
|
|
|
|
context "when there are no topics left" do
|
|
|
|
let!(:topic) { create_post(user: @category.user, category: @category.id).reload.topic }
|
|
|
|
|
|
|
|
it "can update the topic count to zero" do
|
|
|
|
@category.reload
|
|
|
|
expect(@category.topic_count).to eq(1)
|
|
|
|
expect(@category.topics.count).to eq(2)
|
|
|
|
topic.delete # Delete so the post trash/destroy hook doesn't fire
|
|
|
|
|
|
|
|
Category.update_stats
|
|
|
|
@category.reload
|
|
|
|
expect(@category.topics.count).to eq(1)
|
|
|
|
expect(@category.topic_count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-10-23 11:58:11 -05:00
|
|
|
|
2014-02-16 11:45:00 -06:00
|
|
|
describe "#url" do
|
2020-03-19 09:56:28 -05:00
|
|
|
before_all { SiteSetting.max_category_nesting = 3 }
|
2014-02-16 11:45:00 -06:00
|
|
|
|
2020-03-19 09:56:28 -05:00
|
|
|
fab!(:category) { Fabricate(:category, name: "root") }
|
|
|
|
|
|
|
|
fab!(:sub_category) { Fabricate(:category, name: "child", parent_category_id: category.id) }
|
|
|
|
|
|
|
|
fab!(:sub_sub_category) do
|
|
|
|
Fabricate(:category, name: "child_of_child", parent_category_id: sub_category.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "for normal categories" do
|
|
|
|
it "builds a url" do
|
2020-06-18 03:32:14 -05:00
|
|
|
expect(category.url).to eq("/c/root/#{category.id}")
|
2020-03-19 09:56:28 -05:00
|
|
|
end
|
|
|
|
end
|
2019-08-06 05:26:54 -05:00
|
|
|
|
2020-03-19 09:56:28 -05:00
|
|
|
describe "for subcategories" do
|
|
|
|
it "builds a url" do
|
2020-06-18 03:32:14 -05:00
|
|
|
expect(sub_category.url).to eq("/c/root/child/#{sub_category.id}")
|
2020-03-19 09:56:28 -05:00
|
|
|
end
|
|
|
|
end
|
2019-08-06 05:26:54 -05:00
|
|
|
|
2020-03-19 09:56:28 -05:00
|
|
|
describe "for sub-sub-categories" do
|
|
|
|
it "builds a url" do
|
2020-06-18 03:32:14 -05:00
|
|
|
expect(sub_sub_category.url).to eq("/c/root/child/child-of-child/#{sub_sub_category.id}")
|
2015-12-28 00:28:16 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-15 14:19:17 -05:00
|
|
|
describe "uncategorized" do
|
|
|
|
let(:cat) { Category.where(id: SiteSetting.uncategorized_category_id).first }
|
|
|
|
|
|
|
|
it "reports as `uncategorized?`" do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(cat).to be_uncategorized
|
2014-07-15 14:19:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot have a parent category" do
|
2019-08-06 05:26:54 -05:00
|
|
|
cat.parent_category_id = Fabricate(:category_with_definition).id
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(cat).to_not be_valid
|
2014-07-15 14:19:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-23 11:58:11 -05:00
|
|
|
describe "parent categories" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:user)
|
2019-08-06 05:26:54 -05:00
|
|
|
fab!(:parent_category) { Fabricate(:category_with_definition, user: user) }
|
2013-10-23 11:58:11 -05: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 08:55:03 -06:00
|
|
|
expect(sub_category).to be_valid
|
|
|
|
expect(sub_category.parent_category).to eq(parent_category)
|
2013-10-23 11:58:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot associate a category with itself" do
|
2019-08-06 05:26:54 -05:00
|
|
|
category = Fabricate(:category_with_definition, user: user)
|
2013-10-23 11:58:11 -05:00
|
|
|
category.parent_category_id = category.id
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category).to_not be_valid
|
2013-10-23 11:58:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot have a category two levels deep" do
|
2019-08-06 05:26:54 -05:00
|
|
|
sub_category =
|
|
|
|
Fabricate(:category_with_definition, parent_category_id: parent_category.id, user: user)
|
2013-10-23 11:58:11 -05:00
|
|
|
nested_sub_category =
|
|
|
|
Fabricate.build(:category, parent_category_id: sub_category.id, user: user)
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(nested_sub_category).to_not be_valid
|
2013-10-23 11:58:11 -05:00
|
|
|
end
|
|
|
|
|
2014-02-08 16:10:48 -06: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 08:55:03 -06:00
|
|
|
expect(parent_category.id).to eq(Category.query_parent_category(parent_category.slug))
|
2014-02-08 16:10:48 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".query_category" do
|
|
|
|
it "should return the category" do
|
2019-08-06 05:26:54 -05:00
|
|
|
category =
|
|
|
|
Fabricate(
|
|
|
|
:category_with_definition,
|
|
|
|
name: "Amazing Category",
|
|
|
|
parent_category_id: parent_category.id,
|
|
|
|
user: user,
|
|
|
|
)
|
2014-02-08 16:10:48 -06:00
|
|
|
parent_category.name = "Amazing Parent Category"
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(category).to eq(Category.query_category(category.slug, parent_category.id))
|
2014-02-08 16:10:48 -06:00
|
|
|
end
|
|
|
|
end
|
2013-10-23 11:58:11 -05:00
|
|
|
end
|
|
|
|
|
2014-07-14 09:16:24 -05:00
|
|
|
describe "find_by_email" do
|
|
|
|
it "is case insensitive" do
|
2019-08-06 05:26:54 -05:00
|
|
|
c1 = Fabricate(:category_with_definition, email_in: "lower@example.com")
|
|
|
|
c2 = Fabricate(:category_with_definition, email_in: "UPPER@EXAMPLE.COM")
|
|
|
|
c3 = Fabricate(:category_with_definition, email_in: "Mixed.Case@Example.COM")
|
2014-12-31 08:55:03 -06: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 09:16:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-14 11:38:29 -05:00
|
|
|
describe "find_by_slug" do
|
2019-10-15 10:39:09 -05:00
|
|
|
fab!(:category) { Fabricate(:category_with_definition, slug: "awesome-category") }
|
|
|
|
|
|
|
|
fab!(:subcategory) do
|
|
|
|
Fabricate(
|
|
|
|
:category_with_definition,
|
|
|
|
parent_category_id: category.id,
|
|
|
|
slug: "awesome-sub-category",
|
|
|
|
)
|
|
|
|
end
|
2016-03-14 11:38:29 -05:00
|
|
|
|
2019-10-15 10:39:09 -05:00
|
|
|
it "finds a category that exists" do
|
2016-03-14 11:38:29 -05:00
|
|
|
expect(Category.find_by_slug("awesome-category")).to eq(category)
|
2019-10-15 10:39:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "finds a subcategory that exists" do
|
|
|
|
expect(Category.find_by_slug("awesome-sub-category", "awesome-category")).to eq(subcategory)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "produces nil if the parent doesn't exist" do
|
|
|
|
expect(Category.find_by_slug("awesome-sub-category", "no-such-category")).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "produces nil if the parent doesn't exist and the requested category is a root category" do
|
|
|
|
expect(Category.find_by_slug("awesome-category", "no-such-category")).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "produces nil if the subcategory doesn't exist" do
|
|
|
|
expect(Category.find_by_slug("no-such-category", "awesome-category")).to eq(nil)
|
2016-03-14 11:38:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-28 10:57:30 -05:00
|
|
|
describe "validate email_in" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:user)
|
2016-07-28 10:57:30 -05: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-06 22:12:20 -05:00
|
|
|
fab!(:group) { Fabricate(:group, name: "testgroup", incoming_email: "test@example.com") }
|
2016-07-28 10:57:30 -05: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-08-06 05:26:54 -05:00
|
|
|
fab!(:category) do
|
|
|
|
Fabricate(
|
|
|
|
:category_with_definition,
|
|
|
|
user: user,
|
|
|
|
name: "<b>cool</b>",
|
|
|
|
email_in: "test@example.com",
|
|
|
|
)
|
2023-01-09 05:18:21 -06:00
|
|
|
end
|
2016-07-28 10:57:30 -05: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-12 21:51:08 -05:00
|
|
|
describe "require topic/post approval" do
|
2019-08-06 05:26:54 -05:00
|
|
|
fab!(:category) { Fabricate(:category_with_definition) }
|
2018-07-12 21:51:08 -05:00
|
|
|
|
2023-09-11 20:51:49 -05:00
|
|
|
it "delegates methods to category settings" do
|
|
|
|
expect(category).to delegate_method(:require_reply_approval).to(:category_setting)
|
|
|
|
expect(category).to delegate_method(:require_reply_approval=).with_arguments(true).to(
|
|
|
|
:category_setting,
|
|
|
|
)
|
|
|
|
expect(category).to delegate_method(:require_reply_approval?).to(:category_setting)
|
2018-07-12 21:51:08 -05:00
|
|
|
|
2023-09-11 20:51:49 -05:00
|
|
|
expect(category).to delegate_method(:require_topic_approval).to(:category_setting)
|
|
|
|
expect(category).to delegate_method(:require_topic_approval=).with_arguments(true).to(
|
|
|
|
:category_setting,
|
|
|
|
)
|
|
|
|
expect(category).to delegate_method(:require_topic_approval?).to(:category_setting)
|
2018-07-12 21:51:08 -05:00
|
|
|
end
|
|
|
|
end
|
2018-07-16 03:10:22 -05:00
|
|
|
|
|
|
|
describe "auto bump" do
|
|
|
|
it "should correctly automatically bump topics" do
|
2020-03-10 16:13:17 -05:00
|
|
|
freeze_time
|
|
|
|
category = Fabricate(:category_with_definition, created_at: 1.minute.ago)
|
2018-07-16 03:10:22 -05:00
|
|
|
category.clear_auto_bump_cache!
|
|
|
|
|
2020-03-10 16:13:17 -05:00
|
|
|
post1 = create_post(category: category, created_at: 15.seconds.ago)
|
|
|
|
_post2 = create_post(category: category, created_at: 10.seconds.ago)
|
|
|
|
_post3 = create_post(category: category, created_at: 5.seconds.ago)
|
2018-07-16 03:10:22 -05:00
|
|
|
|
2018-07-16 18:33:33 -05:00
|
|
|
# no limits on post creation or category creation please
|
|
|
|
RateLimiter.enable
|
|
|
|
|
2020-03-10 16:13:17 -05:00
|
|
|
time = freeze_time 1.month.from_now
|
2018-07-16 03:10:22 -05:00
|
|
|
|
|
|
|
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-17 19:17:33 -05:00
|
|
|
# our extra bump message
|
|
|
|
expect(post1.topic.reload.posts_count).to eq(2)
|
|
|
|
|
2020-03-10 16:13:17 -05:00
|
|
|
time = freeze_time 13.hours.from_now
|
2018-07-16 03:10:22 -05:00
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(true)
|
2018-07-17 19:17:33 -05:00
|
|
|
expect(Topic.where(bumped_at: time).count).to eq(1)
|
2018-07-16 03:10:22 -05:00
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(false)
|
2018-07-17 19:17:33 -05:00
|
|
|
expect(Topic.where(bumped_at: time).count).to eq(1)
|
2018-07-16 03:10:22 -05:00
|
|
|
|
2020-03-10 16:13:17 -05:00
|
|
|
time = freeze_time 1.month.from_now
|
2018-07-16 18:33:33 -05:00
|
|
|
|
|
|
|
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 01:47:55 -05:00
|
|
|
|
|
|
|
category.num_auto_bump_daily = ""
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
expect(Category.auto_bump_topic!).to eq(false)
|
2018-07-16 03:10:22 -05:00
|
|
|
end
|
2019-11-19 08:24:18 -06:00
|
|
|
|
2023-03-09 23:45:01 -06:00
|
|
|
it "should not auto-bump the same topic within the cooldown" do
|
|
|
|
freeze_time
|
|
|
|
category =
|
|
|
|
Fabricate(
|
|
|
|
:category_with_definition,
|
|
|
|
created_at: 1.minute.ago,
|
|
|
|
category_setting_attributes: {
|
|
|
|
auto_bump_cooldown_days: 1,
|
2023-08-03 21:53:22 -05:00
|
|
|
num_auto_bump_daily: 2,
|
2023-03-09 23:45:01 -06:00
|
|
|
},
|
|
|
|
)
|
|
|
|
category.clear_auto_bump_cache!
|
|
|
|
|
|
|
|
post1 = create_post(category: category, created_at: 15.seconds.ago)
|
|
|
|
|
|
|
|
# no limits on post creation or category creation please
|
|
|
|
RateLimiter.enable
|
|
|
|
|
|
|
|
time = freeze_time 1.month.from_now
|
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(true)
|
|
|
|
expect(Topic.where(bumped_at: time).count).to eq(1)
|
|
|
|
|
|
|
|
time = freeze_time 13.hours.from_now
|
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(false)
|
|
|
|
expect(Topic.where(bumped_at: time).count).to eq(0)
|
|
|
|
|
|
|
|
time = freeze_time 13.hours.from_now
|
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(true)
|
|
|
|
expect(Topic.where(bumped_at: time).count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2019-11-19 08:24:18 -06:00
|
|
|
it "should not automatically bump topics with a bump scheduled" do
|
2020-03-10 16:13:17 -05:00
|
|
|
freeze_time
|
|
|
|
category = Fabricate(:category_with_definition, created_at: 1.second.ago)
|
2019-11-19 08:24:18 -06:00
|
|
|
category.clear_auto_bump_cache!
|
|
|
|
|
|
|
|
post1 = create_post(category: category)
|
|
|
|
|
|
|
|
# no limits on post creation or category creation please
|
|
|
|
RateLimiter.enable
|
|
|
|
|
2020-03-10 16:13:17 -05:00
|
|
|
time = freeze_time 1.month.from_now
|
2019-11-19 08:24:18 -06:00
|
|
|
|
|
|
|
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!
|
|
|
|
|
|
|
|
topic = Topic.find_by_id(post1.topic_id)
|
|
|
|
|
|
|
|
TopicTimer.create!(
|
2023-12-06 17:04:45 -06:00
|
|
|
user_id: Discourse::SYSTEM_USER_ID,
|
2019-11-19 08:24:18 -06:00
|
|
|
topic: topic,
|
|
|
|
execute_at: 1.hour.from_now,
|
|
|
|
status_type: TopicTimer.types[:bump],
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
Topic.joins(:topic_timers).where(topic_timers: { status_type: 6, deleted_at: nil }).count,
|
|
|
|
).to eq(1)
|
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(false)
|
|
|
|
expect(Topic.where(bumped_at: time).count).to eq(0)
|
|
|
|
# does not include a bump message
|
|
|
|
expect(post1.topic.reload.posts_count).to eq(1)
|
|
|
|
end
|
2018-07-16 03:10:22 -05:00
|
|
|
end
|
|
|
|
|
2019-02-13 23:38:52 -06:00
|
|
|
describe "validate permissions compatibility" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:group)
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:group2) { Fabricate(:group) }
|
2019-08-06 05:26:54 -05:00
|
|
|
fab!(:parent_category) { Fabricate(:category_with_definition, name: "parent") }
|
|
|
|
fab!(:subcategory) do
|
|
|
|
Fabricate(:category_with_definition, name: "child1", parent_category_id: parent_category.id)
|
2023-01-09 05:18:21 -06:00
|
|
|
end
|
2019-02-13 23:38:52 -06:00
|
|
|
|
|
|
|
context "when changing subcategory permissions" do
|
|
|
|
it "it is not valid if permissions are less restrictive" do
|
2019-10-14 12:47:15 -05:00
|
|
|
subcategory.set_permissions(group => :readonly)
|
|
|
|
subcategory.save!
|
|
|
|
|
2019-02-13 23:38:52 -06:00
|
|
|
parent_category.set_permissions(group => :readonly)
|
|
|
|
parent_category.save!
|
|
|
|
|
|
|
|
subcategory.set_permissions(group => :full, group2 => :readonly)
|
|
|
|
|
|
|
|
expect(subcategory.valid?).to eq(false)
|
2019-03-31 23:34:52 -05:00
|
|
|
expect(subcategory.errors.full_messages).to contain_exactly(
|
|
|
|
I18n.t("category.errors.permission_conflict", group_names: group2.name),
|
|
|
|
)
|
2019-02-13 23:38:52 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is valid if permissions are same or more restrictive" do
|
2019-10-14 12:47:15 -05:00
|
|
|
subcategory.set_permissions(group => :full, group2 => :create_post)
|
|
|
|
subcategory.save!
|
|
|
|
|
2019-02-13 23:38:52 -06:00
|
|
|
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-03 21:49:26 -06:00
|
|
|
it "is valid if everyone has access to parent category" do
|
|
|
|
parent_category.set_permissions(everyone: :readonly)
|
2019-02-13 23:38:52 -06: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
|
2019-10-14 12:47:15 -05:00
|
|
|
fab!(:subcategory2) do
|
|
|
|
Fabricate(:category_with_definition, name: "child2", parent_category_id: parent_category.id)
|
2023-01-09 05:18:21 -06:00
|
|
|
end
|
2019-10-14 12:47:15 -05:00
|
|
|
|
|
|
|
it "is not valid if subcategory permissions are less restrictive" do
|
2019-02-13 23:38:52 -06:00
|
|
|
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-03-31 23:34:52 -05:00
|
|
|
expect(parent_category.errors.full_messages).to contain_exactly(
|
|
|
|
I18n.t("category.errors.permission_conflict", group_names: group2.name),
|
|
|
|
)
|
2019-02-13 23:38:52 -06:00
|
|
|
end
|
|
|
|
|
2019-10-14 12:47:15 -05:00
|
|
|
it "is not valid if the subcategory has no category groups, but the parent does" do
|
|
|
|
parent_category.set_permissions(group => :readonly)
|
|
|
|
|
|
|
|
expect(parent_category).not_to be_valid
|
|
|
|
end
|
|
|
|
|
2019-02-13 23:38:52 -06:00
|
|
|
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-03 21:49:26 -06:00
|
|
|
it "is valid if everyone has access to parent category" do
|
2019-02-13 23:38:52 -06:00
|
|
|
subcategory.set_permissions(group => :create_post)
|
|
|
|
subcategory.save
|
2019-03-03 21:49:26 -06:00
|
|
|
parent_category.set_permissions(everyone: :readonly)
|
2019-02-13 23:38:52 -06:00
|
|
|
|
|
|
|
expect(parent_category.valid?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-11 07:31:58 -05:00
|
|
|
describe "tree metrics" do
|
|
|
|
fab!(:category) { Category.create!(user: user, name: "foo") }
|
|
|
|
|
|
|
|
fab!(:subcategory) { Category.create!(user: user, name: "bar", parent_category: category) }
|
|
|
|
|
|
|
|
context "with a self-parent" do
|
|
|
|
before_all { DB.exec(<<-SQL, id: category.id) }
|
|
|
|
UPDATE categories
|
|
|
|
SET parent_category_id = :id
|
|
|
|
WHERE id = :id
|
|
|
|
SQL
|
|
|
|
|
2022-07-27 05:21:10 -05:00
|
|
|
describe "#depth_of_descendants" do
|
2019-10-11 07:31:58 -05:00
|
|
|
it "should produce max_depth" do
|
|
|
|
expect(category.depth_of_descendants(3)).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 05:21:10 -05:00
|
|
|
describe "#height_of_ancestors" do
|
2019-10-11 07:31:58 -05:00
|
|
|
it "should produce max_height" do
|
|
|
|
expect(category.height_of_ancestors(3)).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a prospective self-parent" do
|
|
|
|
before { category.parent_category_id = category.id }
|
|
|
|
|
2022-07-27 05:21:10 -05:00
|
|
|
describe "#depth_of_descendants" do
|
2019-10-11 07:31:58 -05:00
|
|
|
it "should produce max_depth" do
|
|
|
|
expect(category.depth_of_descendants(3)).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 05:21:10 -05:00
|
|
|
describe "#height_of_ancestors" do
|
2019-10-11 07:31:58 -05:00
|
|
|
it "should produce max_height" do
|
|
|
|
expect(category.height_of_ancestors(3)).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a prospective loop" do
|
|
|
|
before { category.parent_category_id = subcategory.id }
|
|
|
|
|
2022-07-27 05:21:10 -05:00
|
|
|
describe "#depth_of_descendants" do
|
2019-10-11 07:31:58 -05:00
|
|
|
it "should produce max_depth" do
|
|
|
|
expect(category.depth_of_descendants(3)).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 05:21:10 -05:00
|
|
|
describe "#height_of_ancestors" do
|
2019-10-11 07:31:58 -05:00
|
|
|
it "should produce max_height" do
|
|
|
|
expect(category.height_of_ancestors(3)).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 05:21:10 -05:00
|
|
|
describe "#depth_of_descendants" do
|
2019-10-11 07:31:58 -05:00
|
|
|
it "should be 0 when the category has no descendants" do
|
|
|
|
expect(subcategory.depth_of_descendants).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be 1 when the category has a descendant" do
|
|
|
|
expect(category.depth_of_descendants).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 05:21:10 -05:00
|
|
|
describe "#height_of_ancestors" do
|
2019-10-11 07:31:58 -05:00
|
|
|
it "should be 0 when the category has no ancestors" do
|
|
|
|
expect(category.height_of_ancestors).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be 1 when the category has an ancestor" do
|
|
|
|
expect(subcategory.height_of_ancestors).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-24 02:07:52 -05:00
|
|
|
describe "messageBus" do
|
|
|
|
it "does not publish notification level when publishing to /categories" do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
category.name = "Amazing category"
|
|
|
|
messages = MessageBus.track_publish("/categories") { category.save! }
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
category_hash = message.data[:categories].first
|
|
|
|
|
|
|
|
expect(category_hash[:name]).to eq(category.name)
|
|
|
|
expect(category_hash.key?(:notification_level)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-06 04:30:52 -05:00
|
|
|
describe "#ensure_consistency!" do
|
|
|
|
it "creates category topic" do
|
2019-06-06 20:19:57 -05:00
|
|
|
# corrupt a category topic
|
|
|
|
uncategorized = Category.find(SiteSetting.uncategorized_category_id)
|
|
|
|
uncategorized.create_category_definition
|
|
|
|
uncategorized.topic.posts.first.destroy!
|
|
|
|
|
2019-06-06 23:56:31 -05:00
|
|
|
# make stuff extra broken
|
|
|
|
uncategorized.topic.trash!
|
|
|
|
|
2019-08-06 05:26:54 -05:00
|
|
|
category = Fabricate(:category_with_definition)
|
|
|
|
category_destroyed = Fabricate(:category_with_definition)
|
|
|
|
category_trashed = Fabricate(:category_with_definition)
|
2019-06-06 04:30:52 -05:00
|
|
|
|
|
|
|
category_topic_id = category.topic.id
|
|
|
|
category_destroyed.topic.destroy!
|
|
|
|
category_trashed.topic.trash!
|
|
|
|
|
|
|
|
Category.ensure_consistency!
|
2019-06-06 20:19:57 -05: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 04:30:52 -05: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
|
|
|
|
|
2020-12-18 08:05:01 -06:00
|
|
|
describe "#find_by_slug_path" do
|
|
|
|
it "works for categories with slugs" do
|
|
|
|
category = Fabricate(:category, slug: "cat1")
|
|
|
|
|
|
|
|
expect(Category.find_by_slug_path(["cat1"])).to eq(category)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works for categories without slugs" do
|
|
|
|
SiteSetting.slug_generation_method = "none"
|
|
|
|
|
|
|
|
category = Fabricate(:category, slug: "cat1")
|
|
|
|
|
|
|
|
expect(Category.find_by_slug_path(["#{category.id}-category"])).to eq(category)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works for subcategories with slugs" do
|
|
|
|
category = Fabricate(:category, slug: "cat1")
|
|
|
|
subcategory = Fabricate(:category, slug: "cat2", parent_category: category)
|
|
|
|
|
|
|
|
expect(Category.find_by_slug_path(%w[cat1 cat2])).to eq(subcategory)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works for subcategories without slugs" do
|
|
|
|
SiteSetting.slug_generation_method = "none"
|
|
|
|
|
|
|
|
category = Fabricate(:category, slug: "cat1")
|
|
|
|
subcategory = Fabricate(:category, slug: "cat2", parent_category: category)
|
|
|
|
|
|
|
|
expect(Category.find_by_slug_path(["cat1", "#{subcategory.id}-category"])).to eq(subcategory)
|
|
|
|
expect(
|
|
|
|
Category.find_by_slug_path(["#{category.id}-category", "#{subcategory.id}-category"]),
|
|
|
|
).to eq(subcategory)
|
|
|
|
end
|
|
|
|
end
|
2021-10-13 04:53:23 -05:00
|
|
|
|
|
|
|
describe "#cannot_delete_reason" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:admin)
|
2021-10-13 04:53:23 -05:00
|
|
|
let(:guardian) { Guardian.new(admin) }
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:category)
|
2021-10-13 04:53:23 -05:00
|
|
|
|
|
|
|
describe "when category is uncategorized" do
|
|
|
|
it "should return the reason" do
|
|
|
|
category = Category.find(SiteSetting.uncategorized_category_id)
|
|
|
|
|
|
|
|
expect(category.cannot_delete_reason).to eq(I18n.t("category.cannot_delete.uncategorized"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when category has subcategories" do
|
|
|
|
it "should return the right reason" do
|
|
|
|
category.subcategories << Fabricate(:category)
|
|
|
|
|
|
|
|
expect(category.cannot_delete_reason).to eq(
|
|
|
|
I18n.t("category.cannot_delete.has_subcategories"),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when category has topics" do
|
|
|
|
it "should return the right reason" do
|
|
|
|
topic =
|
|
|
|
Fabricate(
|
|
|
|
:topic,
|
|
|
|
title: "</a><script>alert(document.cookie);</script><a>",
|
|
|
|
category: category,
|
|
|
|
)
|
|
|
|
|
|
|
|
category.reload
|
|
|
|
|
|
|
|
expect(category.cannot_delete_reason).to eq(
|
|
|
|
I18n.t(
|
|
|
|
"category.cannot_delete.topic_exists",
|
|
|
|
count: 1,
|
|
|
|
topic_link:
|
|
|
|
"<a href=\"#{topic.url}\"></a><script>alert(document.cookie);</script><a></a>",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-10-12 12:09:45 -05:00
|
|
|
|
|
|
|
describe "#deleting the general category" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:category)
|
2022-10-12 12:09:45 -05:00
|
|
|
|
|
|
|
it "should empty out the general_category_id site_setting" do
|
|
|
|
SiteSetting.general_category_id = category.id
|
|
|
|
category.destroy
|
|
|
|
|
|
|
|
expect(SiteSetting.general_category_id).to_not eq(category.id)
|
|
|
|
expect(SiteSetting.general_category_id).to be < 1
|
|
|
|
end
|
|
|
|
end
|
2023-04-03 05:36:59 -05:00
|
|
|
|
|
|
|
describe ".ids_from_slugs" do
|
|
|
|
fab!(:category) { Fabricate(:category, slug: "category") }
|
|
|
|
fab!(:category2) { Fabricate(:category, slug: "category2") }
|
|
|
|
fab!(:subcategory) { Fabricate(:category, parent_category: category, slug: "subcategory") }
|
|
|
|
fab!(:subcategory2) { Fabricate(:category, parent_category: category2, slug: "subcategory") }
|
|
|
|
|
|
|
|
it "returns [] when inputs is []" do
|
|
|
|
expect(Category.ids_from_slugs([])).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the ids of category when input is ["category"]' do
|
|
|
|
expect(Category.ids_from_slugs(%w[category])).to contain_exactly(category.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the ids of subcategory when input is ["category:subcategory"]' do
|
|
|
|
expect(Category.ids_from_slugs(%w[category:subcategory])).to contain_exactly(subcategory.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the ids of subcategory2 when input is ["category2:subcategory"]' do
|
|
|
|
expect(Category.ids_from_slugs(%w[category2:subcategory])).to contain_exactly(subcategory2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the ids of category and category2 when input is ['category', 'category2']" do
|
|
|
|
expect(Category.ids_from_slugs(%w[category category2])).to contain_exactly(
|
|
|
|
category.id,
|
|
|
|
category2.id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the ids of subcategory and subcategory2 when input is ['category:subcategory', 'category2:subcategory']" do
|
|
|
|
expect(
|
|
|
|
Category.ids_from_slugs(%w[category:subcategory category2:subcategory]),
|
|
|
|
).to contain_exactly(subcategory.id, subcategory2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the ids of subcategory when input is ['category:subcategory', 'invalid:subcategory']" do
|
|
|
|
expect(
|
|
|
|
Category.ids_from_slugs(%w[category:subcategory invalid:subcategory]),
|
|
|
|
).to contain_exactly(subcategory.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the ids of sub-subcategory when input is ["category:subcategory:sub-subcategory"] and maximum category nesting is 3' do
|
|
|
|
SiteSetting.max_category_nesting = 3
|
|
|
|
sub_subcategory = Fabricate(:category, parent_category: subcategory, slug: "sub-subcategory")
|
|
|
|
|
|
|
|
expect(Category.ids_from_slugs(%w[category:subcategory:sub-subcategory])).to contain_exactly(
|
|
|
|
sub_subcategory.id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil when input is ["category:invalid-slug:sub-subcategory"] and maximum category nesting is 3' do
|
|
|
|
SiteSetting.max_category_nesting = 3
|
|
|
|
sub_subcategory = Fabricate(:category, parent_category: subcategory, slug: "sub-subcategory")
|
|
|
|
|
|
|
|
expect(Category.ids_from_slugs(%w[category:invalid-slug:sub-subcategory])).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the ids of subcategory when input is ["category:subcategory:sub-subcategory"] but maximum category nesting is 2' do
|
|
|
|
SiteSetting.max_category_nesting = 2
|
|
|
|
|
|
|
|
expect(Category.ids_from_slugs(%w[category:subcategory:sub-subcategory])).to contain_exactly(
|
|
|
|
subcategory.id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the ids of subcategory and subcategory2 when input is ["subcategory"]' do
|
|
|
|
expect(Category.ids_from_slugs(%w[subcategory])).to contain_exactly(
|
|
|
|
subcategory.id,
|
|
|
|
subcategory2.id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2023-04-18 12:01:11 -05:00
|
|
|
|
|
|
|
describe "allowed_tags=" do
|
|
|
|
let(:category) { Fabricate(:category) }
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:tag)
|
2023-04-18 12:01:11 -05:00
|
|
|
fab!(:tag2) { Fabricate(:tag) }
|
|
|
|
|
|
|
|
before { SiteSetting.tagging_enabled = true }
|
|
|
|
|
|
|
|
it "can use existing tags for category tags" do
|
|
|
|
category.allowed_tags = [tag.name]
|
|
|
|
expect_same_tag_names(category.reload.tags, [tag])
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with synonyms" do
|
|
|
|
fab!(:synonym) { Fabricate(:tag, name: "synonym", target_tag: tag) }
|
|
|
|
|
|
|
|
it "can use existing tags for category tags" do
|
|
|
|
category.allowed_tags = [tag.name, synonym.name]
|
|
|
|
category.reload
|
|
|
|
category.allowed_tags = [tag.name, synonym.name, tag2.name]
|
|
|
|
expect_same_tag_names(category.reload.tags, [tag.name, synonym.name, tag2.name])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-08-07 20:18:55 -05:00
|
|
|
|
|
|
|
describe "#slug_ref" do
|
|
|
|
fab!(:category) { Fabricate(:category, slug: "foo") }
|
|
|
|
|
|
|
|
it "returns the slug for categories without parents" do
|
|
|
|
expect(category.slug_ref).to eq("foo")
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for category with parent" do
|
|
|
|
fab!(:subcategory) { Fabricate(:category, parent_category: category, slug: "bar") }
|
|
|
|
|
|
|
|
it "returns the parent and child slug ref with separator" do
|
|
|
|
expect(subcategory.slug_ref).to eq("foo#{Category::SLUG_REF_SEPARATOR}bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for category with multiple parents" do
|
|
|
|
let(:subcategory_1) { Fabricate(:category, parent_category: category, slug: "bar") }
|
|
|
|
let(:subcategory_2) { Fabricate(:category, parent_category: subcategory_1, slug: "boo") }
|
|
|
|
|
|
|
|
before { SiteSetting.max_category_nesting = 3 }
|
|
|
|
|
|
|
|
it "returns the parent and child slug ref with separator" do
|
|
|
|
expect(subcategory_2.slug_ref(depth: 2)).to eq(
|
|
|
|
"foo#{Category::SLUG_REF_SEPARATOR}bar#{Category::SLUG_REF_SEPARATOR}boo",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows limiting depth" do
|
|
|
|
expect(subcategory_2.slug_ref(depth: 1)).to eq("bar#{Category::SLUG_REF_SEPARATOR}boo")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-02-20 11:24:30 -06:00
|
|
|
|
|
|
|
describe ".ancestors_of" do
|
|
|
|
fab!(:category)
|
|
|
|
fab!(:subcategory) { Fabricate(:category, parent_category: category) }
|
|
|
|
|
|
|
|
fab!(:sub_subcategory) do
|
|
|
|
SiteSetting.max_category_nesting = 3
|
|
|
|
Fabricate(:category, parent_category: subcategory)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "finds the parent" do
|
|
|
|
expect(Category.ancestors_of([subcategory.id]).to_a).to eq([category])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "finds the grandparent" do
|
|
|
|
expect(Category.ancestors_of([sub_subcategory.id]).to_a).to contain_exactly(
|
|
|
|
category,
|
|
|
|
subcategory,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "respects the relation it's called on" do
|
|
|
|
expect(Category.where.not(id: category.id).ancestors_of([sub_subcategory.id]).to_a).to eq(
|
|
|
|
[subcategory],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|