From 815116f6a25dfca41daee590df90398b8f9402e1 Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Fri, 10 Jan 2020 17:02:36 +0200 Subject: [PATCH] FIX: Make 'findBySlugPathWithID' when URL ends with a slash (#8699) Make URLs such as 'https://discourse/c/foo/bar/' work the same way 'https://discourse/c/foo/bar' does. --- .../discourse/models/category.js.es6 | 2 +- test/javascripts/models/category-test.js.es6 | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/models/category.js.es6 b/app/assets/javascripts/discourse/models/category.js.es6 index 78832ccbf35..738819bc607 100644 --- a/app/assets/javascripts/discourse/models/category.js.es6 +++ b/app/assets/javascripts/discourse/models/category.js.es6 @@ -334,7 +334,7 @@ Category.reopenClass({ }, findBySlugPathWithID(slugPathWithID) { - let parts = slugPathWithID.split("/"); + let parts = slugPathWithID.split("/").filter(Boolean); // slugs found by star/glob pathing in emeber do not automatically url decode - ensure that these are decoded if (Discourse.SiteSettings.slug_generation_method === "encoded") { parts = parts.map(urlPart => decodeURI(urlPart)); diff --git a/test/javascripts/models/category-test.js.es6 b/test/javascripts/models/category-test.js.es6 index 9863eea36a0..cb58081417e 100644 --- a/test/javascripts/models/category-test.js.es6 +++ b/test/javascripts/models/category-test.js.es6 @@ -184,6 +184,30 @@ QUnit.test("findSingleBySlug", assert => { ); }); +QUnit.test("findBySlugPathWithID", assert => { + const store = createStore(); + + const foo = store.createRecord("category", { id: 1, slug: "foo" }); + const bar = store.createRecord("category", { + id: 2, + slug: "bar", + parentCategory: foo + }); + const baz = store.createRecord("category", { + id: 3, + slug: "baz", + parentCategory: foo + }); + + const categoryList = [foo, bar, baz]; + sandbox.stub(Category, "list").returns(categoryList); + + assert.deepEqual(Category.findBySlugPathWithID("foo"), foo); + assert.deepEqual(Category.findBySlugPathWithID("foo/bar"), bar); + assert.deepEqual(Category.findBySlugPathWithID("foo/bar/"), bar); + assert.deepEqual(Category.findBySlugPathWithID("foo/baz/3"), baz); +}); + QUnit.test("search with category name", assert => { const store = createStore(), category1 = store.createRecord("category", {