From 22bfa4418398d9adc57043201dfff8cb72ed0dac Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 10 Sep 2022 14:36:02 -0500 Subject: [PATCH] posts: fix exception when tagging post with `char:copy:foo`. Fixup for 015c6dc7d. Show a warning about failure to add a tag instead of raising an exception when trying to tag a post with `char:copy:foo`. This tries to create a tag named `copy:foo` then set the category to character, which doesn't work because `copy:foo` isn't a valid tag name. --- app/models/tag.rb | 2 +- test/unit/post_test.rb | 34 +++++++++++++++++----------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index 6cc6bf766..19a63a114 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -197,7 +197,7 @@ class Tag < ApplicationRecord tag = find_or_create_by(name: normalize_name(name)) if category.present? && current_user.present? && Pundit.policy!(current_user, tag).can_change_category? - tag.update!(category: categories.value_for(category)) + tag.update(category: categories.value_for(category)) end tag diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index ce2745427..d84a62218 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -523,28 +523,28 @@ class PostTest < ActiveSupport::TestCase end context "tagged with a metatag" do - context "for typing a tag" do - setup do - @post = FactoryBot.create(:post, tag_string: "char:hoge") - @tags = @post.tag_array + context "for a tag category prefix" do + should "set the category of a new tag" do + create(:post, tag_string: "char:hoge") + + assert_equal(Tag.categories.character, Tag.find_by_name("hoge").category) end - should "change the type" do - assert(Tag.where(name: "hoge", category: 4).exists?, "expected 'moge' tag to be created as a character") - end - end + should "change the category for an aliased tag" do + create(:tag_alias, antecedent_name: "hoge", consequent_name: "moge") + post = create(:post, tag_string: "char:hoge") - context "for typing an aliased tag" do - setup do - @alias = FactoryBot.create(:tag_alias, antecedent_name: "hoge", consequent_name: "moge") - @post = FactoryBot.create(:post, tag_string: "char:hoge") - @tags = @post.tag_array + assert_equal(["moge"], post.tag_array) + assert_equal(Tag.categories.general, Tag.find_by_name("moge").category) + assert_equal(Tag.categories.character, Tag.find_by_name("hoge").category) end - should "change the type" do - assert_equal(["moge"], @tags) - assert(Tag.where(name: "moge", category: 0).exists?, "expected 'moge' tag to be created as a character") - assert(Tag.where(name: "hoge", category: 4).exists?, "expected 'moge' tag to be created as a character") + should "not raise an exception for an invalid tag name" do + post = create(:post, tag_string: "tagme char:copy:blah") + + assert_match(/Couldn't add tag: 'copy:blah' cannot begin with 'copy:'/, post.warnings[:base].join("\n")) + assert_equal(["tagme"], post.tag_array) + assert_equal(false, Tag.exists?(name: "copy:blah")) end end