diff --git a/app/models/category.rb b/app/models/category.rb index ad5a662a356..fc9d1377570 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -353,6 +353,8 @@ class Category < ActiveRecord::Base if self.slug.blank? errors.add(:slug, :invalid) + elsif SiteSetting.slug_generation_method == 'ascii' && !CGI.unescape(self.slug).ascii_only? + errors.add(:slug, I18n.t("category.errors.slug_contains_non_ascii_chars")) elsif duplicate_slug? errors.add(:slug, 'is already in use') end @@ -803,11 +805,7 @@ class Category < ActiveRecord::Base return nil if slug_path.size > SiteSetting.max_category_nesting slug_path.map! do |slug| - if SiteSetting.slug_generation_method == "encoded" - CGI.escape(slug.downcase) - else - slug.downcase - end + CGI.escape(slug.downcase) end query = diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 24fe3a11b7f..18abca279ca 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -704,6 +704,7 @@ en: permission_conflict: "Any group that is allowed to access a subcategory must also be allowed to access the parent category. The following groups have access to one of the subcategories, but no access to parent category: %{group_names}." disallowed_topic_tags: "This topic has tags not allowed by this category: '%{tags}'" disallowed_tags_generic: "This topic has disallowed tags." + slug_contains_non_ascii_chars: "contains non-ascii characters" cannot_delete: uncategorized: "This category is special. It is intended as a holding area for topics that have no category; it cannot be deleted." has_subcategories: "Can't delete this category because it has sub-categories." diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 85ba20b150c..d12aa8a66e6 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -362,6 +362,19 @@ describe Category do expect(@cat).to be_valid expect(@cat.errors[:slug]).not_to be_present end + + context 'if SiteSettings.slug_generation_method = ascii' do + before do + SiteSetting.slug_generation_method = 'ascii' + end + + 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 end describe 'description_text' do