Skip to content

Node.js based forum software built for the modern web

fix: don't delete a tag when renaming it to a name that cleans up to itself (#14885)

Elias Hackradtd5376a0e9dc8
diff --git a/src/topics/tags.js b/src/topics/tags.js
index e5c08384e8..b7cc6b7e02 100644
--- a/src/topics/tags.js
+++ b/src/topics/tags.js
@@ -129,10 +129,10 @@ module.exports = function (Topics) {
};
async function renameTag(tag, newTagName) {
+ newTagName = utils.cleanUpTag(newTagName, meta.config.maximumTagLength);
if (!newTagName || tag === newTagName) {
return;
}
- newTagName = utils.cleanUpTag(newTagName, meta.config.maximumTagLength);
await Topics.createEmptyTag(newTagName);
const allCids = {};
diff --git a/test/topics.js b/test/topics.js
index 39713ae8cb..0327ee6244 100644
--- a/test/topics.js
+++ b/test/topics.js
@@ -1640,6 +1640,38 @@ describe('Topic\'s', () => {
assert.strictEqual(tags[0], 'plugins');
});
+ it('should not delete a tag when the new name cleans up to the same tag', async () => {
+ const { caseSensitiveTags } = meta.config;
+ meta.config.caseSensitiveTags = 0;
+ try {
+ const { topicData } = await topics.post({ uid: adminUid, tags: ['samename'], title: 'topic tagged with samename', content: 'topic content', cid: topic.categoryId });
+
+ await socketAdmin.tags.rename({ uid: adminUid }, [{
+ value: 'samename',
+ newName: 'SameName',
+ }]);
+
+ assert.deepStrictEqual(await topics.getTagTids('samename', 0, -1), [String(topicData.tid)]);
+ assert.strictEqual(await db.sortedSetScore('tags:topic:count', 'samename'), 1);
+ assert.deepStrictEqual(await topics.getTopicTags(topicData.tid), ['samename']);
+ } finally {
+ meta.config.caseSensitiveTags = caseSensitiveTags;
+ }
+ });
+
+ it('should rename a tag to a different case when tags are case sensitive', async () => {
+ const { topicData } = await topics.post({ uid: adminUid, tags: ['casetag'], title: 'topic tagged with casetag', content: 'topic content', cid: topic.categoryId });
+
+ await socketAdmin.tags.rename({ uid: adminUid }, [{
+ value: 'casetag',
+ newName: 'CaseTag',
+ }]);
+
+ assert.deepStrictEqual(await topics.getTagTids('casetag', 0, -1), []);
+ assert.deepStrictEqual(await topics.getTagTids('CaseTag', 0, -1), [String(topicData.tid)]);
+ assert.deepStrictEqual(await topics.getTopicTags(topicData.tid), ['CaseTag']);
+ });
+
it('should return related topics', (done) => {
const meta = require('../src/meta');
meta.config.maximumRelatedTopics = 2;