FIX: Avoid redirection when not leaving forum. (#6419)

* FIX: Avoid redirection when not leaving forum.

* DEV: Updated click-track's tests.
This commit is contained in:
Bianca Nenciu 2018-10-04 16:06:31 +02:00 committed by Régis Hanol
parent b8a1196b6b
commit 6ad13e5ae9
3 changed files with 43 additions and 39 deletions

View File

@ -128,16 +128,18 @@ export default {
const isInternal = DiscourseURL.isInternal(href);
// If we're on the same site, use the router and track via AJAX
if (tracking && isInternal && !$link.hasClass("attachment")) {
ajax("/clicks/track", {
data: {
url: href,
post_id: postId,
topic_id: topicId,
redirect: false
},
dataType: "html"
});
if (isInternal && !$link.hasClass("attachment")) {
if (tracking) {
ajax("/clicks/track", {
data: {
url: href,
post_id: postId,
topic_id: topicId,
redirect: false
},
dataType: "html"
});
}
DiscourseURL.routeTo(href);
return false;
}

View File

@ -187,13 +187,7 @@ const DiscourseURL = Ember.Object.extend({
const pathname = path.replace(/(https?\:)?\/\/[^\/]+/, "");
const baseUri = Discourse.BaseUri;
// If we have a baseUri and an absolute URL, make sure the baseUri
// is the same. Otherwise we could be switching forums.
if (
baseUri &&
path.indexOf("http") === 0 &&
pathname.indexOf(baseUri) !== 0
) {
if (!DiscourseURL.isInternal(path)) {
return redirectTo(path);
}
@ -207,11 +201,6 @@ const DiscourseURL = Ember.Object.extend({
return;
}
// Protocol relative URLs
if (path.indexOf("//") === 0) {
return redirectTo(path);
}
// Scroll to the same page, different anchor
const m = /^#(.+)$/.exec(path);
if (m) {

View File

@ -30,7 +30,8 @@ QUnit.module("lib:click-track", {
<a class="hashtag" href="http://discuss.domain.com">#hashtag</a>
<a class="mailto" href="mailto:foo@bar.com">email-me</a>
<aside class="quote">
<a class="inside-quote" href="http://discuss.domain.com">foobar</a>
<a href="https://discuss.domain.com/t/welcome-to-meta-discourse-org/1/30">foo</a>
<a href="https://google.com">bar</a>
</aside>
</article>
</div>`
@ -45,14 +46,14 @@ var generateClickEventOn = function(selector) {
return $.Event("click", { currentTarget: fixture(selector)[0] });
};
QUnit.test("does not track clicks on lightboxes", function(assert) {
QUnit.test("does not track clicks on lightboxes", assert => {
var clickEvent = generateClickEventOn(".lightbox");
sandbox.stub(clickEvent, "preventDefault");
assert.ok(track(clickEvent));
assert.ok(!clickEvent.preventDefault.calledOnce);
});
QUnit.test("it calls preventDefault when clicking on an a", function(assert) {
QUnit.test("it calls preventDefault when clicking on an a", assert => {
var clickEvent = generateClickEventOn("a");
sandbox.stub(clickEvent, "preventDefault");
track(clickEvent);
@ -60,28 +61,40 @@ QUnit.test("it calls preventDefault when clicking on an a", function(assert) {
assert.ok(DiscourseURL.redirectTo.calledOnce);
});
QUnit.test("does not track clicks when forcibly disabled", function(assert) {
QUnit.test("does not track clicks when forcibly disabled", assert => {
assert.ok(track(generateClickEventOn(".no-track-link")));
});
QUnit.test("does not track clicks on back buttons", function(assert) {
QUnit.test("does not track clicks on back buttons", assert => {
assert.ok(track(generateClickEventOn(".back")));
});
QUnit.test("does not track clicks in quotes", function(assert) {
track(generateClickEventOn(".inside-quote"));
assert.ok(DiscourseURL.redirectTo.calledWith("http://discuss.domain.com"));
QUnit.test("does not track clicks to internal links in quotes", assert => {
sandbox.stub(DiscourseURL, "routeTo");
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
track(generateClickEventOn(".quote a:first-child"));
assert.ok(
DiscourseURL.routeTo.calledWith(
"https://discuss.domain.com/t/welcome-to-meta-discourse-org/1/30"
)
);
});
QUnit.test("does not track clicks to external links in quotes", assert => {
track(generateClickEventOn(".quote a:last-child"));
assert.ok(DiscourseURL.redirectTo.calledWith("https://google.com"));
});
QUnit.test("does not track clicks on category badges", assert => {
assert.ok(track(generateClickEventOn(".hashtag")));
});
QUnit.test("does not track clicks on mailto", function(assert) {
QUnit.test("does not track clicks on mailto", assert => {
assert.ok(track(generateClickEventOn(".mailto")));
});
QUnit.test("removes the href and put it as a data attribute", function(assert) {
QUnit.test("removes the href and put it as a data attribute", assert => {
track(generateClickEventOn("a"));
var $link = fixture("a").first();
@ -92,7 +105,7 @@ QUnit.test("removes the href and put it as a data attribute", function(assert) {
assert.ok(DiscourseURL.redirectTo.calledOnce);
});
asyncTestDiscourse("restores the href after a while", function(assert) {
asyncTestDiscourse("restores the href after a while", assert => {
assert.expect(1);
track(generateClickEventOn("a"));
@ -110,7 +123,7 @@ var badgeClickCount = function(assert, id, expected) {
assert.equal(parseInt($badge.html(), 10), expected);
};
QUnit.test("does not update badge clicks on my own link", function(assert) {
QUnit.test("does not update badge clicks on my own link", assert => {
sandbox
.stub(Discourse.User, "currentProp")
.withArgs("id")
@ -118,7 +131,7 @@ QUnit.test("does not update badge clicks on my own link", function(assert) {
badgeClickCount(assert, "with-badge", 1);
});
QUnit.test("does not update badge clicks in my own post", function(assert) {
QUnit.test("does not update badge clicks in my own post", assert => {
sandbox
.stub(Discourse.User, "currentProp")
.withArgs("id")
@ -126,14 +139,14 @@ QUnit.test("does not update badge clicks in my own post", function(assert) {
badgeClickCount(assert, "with-badge-but-not-mine", 1);
});
QUnit.test("updates badge counts correctly", function(assert) {
QUnit.test("updates badge counts correctly", assert => {
badgeClickCount(assert, "inside-onebox", 1);
badgeClickCount(assert, "inside-onebox-forced", 2);
badgeClickCount(assert, "with-badge", 2);
});
var testOpenInANewTab = function(description, clickEventModifier) {
test(description, function(assert) {
test(description, assert => {
var clickEvent = generateClickEventOn("a");
clickEventModifier(clickEvent);
sandbox.stub(clickEvent, "preventDefault");
@ -166,7 +179,7 @@ testOpenInANewTab("it opens in a new tab on middle click", function(
clickEvent.button = 2;
});
QUnit.test("tracks via AJAX if we're on the same site", function(assert) {
QUnit.test("tracks via AJAX if we're on the same site", assert => {
sandbox.stub(DiscourseURL, "routeTo");
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
@ -174,7 +187,7 @@ QUnit.test("tracks via AJAX if we're on the same site", function(assert) {
assert.ok(DiscourseURL.routeTo.calledOnce);
});
QUnit.test("does not track via AJAX for attachments", function(assert) {
QUnit.test("does not track via AJAX for attachments", assert => {
sandbox.stub(DiscourseURL, "routeTo");
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");