DEV: Move Discourse.getURL and related functions to a module (#9966)

* DEV: Move `Discourse.getURL` and related functions to a module

* DEV: Remove `Discourse.getURL` and `Discourse.getURLWithCDN`

* FIX: `get-url` is required for server side code

* DEV: Deprecate `BaseUri` too.
This commit is contained in:
Robin Ward
2020-06-03 12:45:26 -04:00
committed by GitHub
parent d27b877a40
commit ba00cc8ec4
90 changed files with 401 additions and 260 deletions

View File

@@ -9,6 +9,7 @@ import {
url,
htmlSafe
} from "discourse/lib/computed";
import { setPrefix } from "discourse-common/lib/get-url";
QUnit.module("lib:computed", {
beforeEach() {
@@ -148,7 +149,7 @@ QUnit.test("url", assert => {
"it supports urls without a prefix"
);
Discourse.BaseUri = "/prefixed";
setPrefix("/prefixed");
t = testClass.create({ username: "eviltrout" });
assert.equal(
t.get("userUrl"),

View File

@@ -2,43 +2,6 @@ import { logIn, updateCurrentUser } from "helpers/qunit-helpers";
QUnit.module("lib:discourse");
QUnit.test("getURL on subfolder install", assert => {
Discourse.BaseUri = "/forum";
assert.equal(Discourse.getURL("/"), "/forum/", "root url has subfolder");
assert.equal(
Discourse.getURL("/u/neil"),
"/forum/u/neil",
"relative url has subfolder"
);
assert.equal(
Discourse.getURL("/svg-sprite/forum.example.com/svg-sprite.js"),
"/forum/svg-sprite/forum.example.com/svg-sprite.js",
"works when the url has the prefix in the middle"
);
assert.equal(
Discourse.getURL("/forum/t/123"),
"/forum/t/123",
"does not prefix if the URL is already prefixed"
);
});
QUnit.test("getURLWithCDN on subfolder install with S3", assert => {
Discourse.BaseUri = "/forum";
Discourse.S3CDN = "https://awesome.cdn/site";
Discourse.S3BaseUrl = "//test.s3-us-west-1.amazonaws.com/site";
let url = "//test.s3-us-west-1.amazonaws.com/site/forum/awesome.png";
let expected = "https://awesome.cdn/site/forum/awesome.png";
assert.equal(Discourse.getURLWithCDN(url), expected, "at correct path");
Discourse.S3CDN = null;
Discourse.S3BaseUrl = null;
});
QUnit.test("title counts are updated correctly", assert => {
Discourse.set("hasFocus", true);
Discourse.set("contextCount", 0);

View File

@@ -0,0 +1,88 @@
import {
default as getURL,
setupURL,
setupS3CDN,
getURLWithCDN,
isAbsoluteURL,
getAbsoluteURL,
setPrefix,
withoutPrefix
} from "discourse-common/lib/get-url";
QUnit.module("lib:get-url");
QUnit.test("isAbsoluteURL", assert => {
setupURL(null, "https://example.com", "/forum");
assert.ok(isAbsoluteURL("https://example.com/test/thing"));
assert.ok(!isAbsoluteURL("http://example.com/test/thing"));
assert.ok(!isAbsoluteURL("https://discourse.org/test/thing"));
});
QUnit.test("getAbsoluteURL", assert => {
setupURL(null, "https://example.com", "/forum");
assert.equal(getAbsoluteURL("/cool/path"), "https://example.com/cool/path");
});
QUnit.test("withoutPrefix", assert => {
setPrefix("/eviltrout");
assert.equal(withoutPrefix("/eviltrout/hello"), "/hello");
assert.equal(withoutPrefix("/eviltrout/"), "/");
assert.equal(withoutPrefix("/eviltrout"), "");
setPrefix("");
assert.equal(withoutPrefix("/eviltrout/hello"), "/eviltrout/hello");
assert.equal(withoutPrefix("/eviltrout"), "/eviltrout");
assert.equal(withoutPrefix("/"), "/");
setPrefix(null);
assert.equal(withoutPrefix("/eviltrout/hello"), "/eviltrout/hello");
assert.equal(withoutPrefix("/eviltrout"), "/eviltrout");
assert.equal(withoutPrefix("/"), "/");
});
QUnit.test("getURL with empty paths", assert => {
setupURL(null, "https://example.com", "/");
assert.equal(getURL("/"), "/");
assert.equal(getURL(""), "");
setupURL(null, "https://example.com", "");
assert.equal(getURL("/"), "/");
assert.equal(getURL(""), "");
setupURL(null, "https://example.com", undefined);
assert.equal(getURL("/"), "/");
assert.equal(getURL(""), "");
});
QUnit.test("getURL on subfolder install", assert => {
setupURL(null, "", "/forum");
assert.equal(getURL("/"), "/forum/", "root url has subfolder");
assert.equal(
getURL("/u/neil"),
"/forum/u/neil",
"relative url has subfolder"
);
assert.equal(
getURL("/svg-sprite/forum.example.com/svg-sprite.js"),
"/forum/svg-sprite/forum.example.com/svg-sprite.js",
"works when the url has the prefix in the middle"
);
assert.equal(
getURL("/forum/t/123"),
"/forum/t/123",
"does not prefix if the URL is already prefixed"
);
});
QUnit.test("getURLWithCDN on subfolder install with S3", assert => {
setupURL(null, "", "/forum");
setupS3CDN(
"//test.s3-us-west-1.amazonaws.com/site",
"https://awesome.cdn/site"
);
let url = "//test.s3-us-west-1.amazonaws.com/site/forum/awesome.png";
let expected = "https://awesome.cdn/site/forum/awesome.png";
assert.equal(getURLWithCDN(url), expected, "at correct path");
});

View File

@@ -1,4 +1,5 @@
import DiscourseURL, { userPath } from "discourse/lib/url";
import { setPrefix } from "discourse-common/lib/get-url";
QUnit.module("lib:url");
@@ -59,8 +60,8 @@ QUnit.test("userPath", assert => {
assert.equal(userPath("hp.json"), "/u/hp.json");
});
QUnit.test("userPath with BaseUri", assert => {
Discourse.BaseUri = "/forum";
QUnit.test("userPath with prefix", assert => {
setPrefix("/forum");
assert.equal(userPath(), "/forum/u");
assert.equal(userPath("eviltrout"), "/forum/u/eviltrout");
assert.equal(userPath("hp.json"), "/forum/u/hp.json");

View File

@@ -1,4 +1,5 @@
import EmailLog from "admin/models/email-log";
import { setPrefix } from "discourse-common/lib/get-url";
QUnit.module("Discourse.EmailLog");
@@ -7,7 +8,7 @@ QUnit.test("create", assert => {
});
QUnit.test("subfolder support", assert => {
Discourse.BaseUri = "/forum";
setPrefix("/forum");
const attrs = {
id: 60,
to_address: "wikiman@asdf.com",

View File

@@ -1,4 +1,5 @@
import Report from "admin/models/report";
import { setPrefix } from "discourse-common/lib/get-url";
QUnit.module("Report");
@@ -528,7 +529,7 @@ QUnit.test("computed labels", assert => {
assert.equal(computedFilesizeLabel.value, 582641);
// subfolder support
Discourse.BaseUri = "/forum";
setPrefix("/forum");
const postLink = computedLabels[5].compute(row).formatedValue;
assert.equal(

View File

@@ -151,8 +151,10 @@ QUnit.testStart(function(ctx) {
// Allow our tests to change site settings and have them reset before the next test
Discourse.SiteSettings = dup(Discourse.SiteSettingsOriginal);
Discourse.BaseUri = "";
Discourse.BaseUrl = "http://localhost:3000";
let getURL = require("discourse-common/lib/get-url");
getURL.setupURL(null, "http://localhost:3000", "");
getURL.setupS3CDN(null, null);
let User = require("discourse/models/user").default;
let Session = require("discourse/models/session").default;

View File

@@ -1,4 +1,5 @@
import { moduleForWidget, widgetTest } from "helpers/widget-test";
import { setPrefix } from "discourse-common/lib/get-url";
moduleForWidget("home-logo");
@@ -101,7 +102,7 @@ widgetTest("mobile without logo", {
widgetTest("basics, subfolder", {
template: '{{mount-widget widget="home-logo" args=args}}',
beforeEach() {
Discourse.BaseUri = "/forum";
setPrefix("/forum");
this.siteSettings.site_logo_url = bigLogo;
this.siteSettings.site_logo_small_url = smallLogo;
this.siteSettings.title = title;
@@ -118,7 +119,7 @@ widgetTest("basics, subfolder", {
widgetTest("basics, subfolder - minimized", {
template: '{{mount-widget widget="home-logo" args=args}}',
beforeEach() {
Discourse.BaseUri = "/forum";
setPrefix("/forum");
this.siteSettings.site_logo_url = bigLogo;
this.siteSettings.site_logo_small_url = smallLogo;
this.siteSettings.title = title;
@@ -135,7 +136,7 @@ widgetTest("basics, subfolder - minimized", {
widgetTest("mobile logo, subfolder", {
template: '{{mount-widget widget="home-logo" args=args}}',
beforeEach() {
Discourse.BaseUri = "/forum";
setPrefix("/forum");
this.siteSettings.site_mobile_logo_url = mobileLogo;
this.siteSettings.site_logo_small_url = smallLogo;
this.site.mobileView = true;