Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In piwik.updatePeriodParamsFromUrl() get period/date values from hash or URL. #13268

Merged
merged 7 commits into from Aug 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion core/Url.php
Expand Up @@ -572,7 +572,14 @@ public static function isLocalHost($host)
return false;
}

return in_array($host, Url::getLocalHostnames(), true);
// remove port
$hostWithoutPort = explode(':', $host);
array_pop($hostWithoutPort);
$hostWithoutPort = implode(':', $hostWithoutPort);

$localHostnames = Url::getLocalHostnames();
return in_array($host, $localHostnames, true)
|| in_array($hostWithoutPort, $localHostnames, true);
}

public static function getTrustedHostsFromConfig()
Expand Down
5 changes: 2 additions & 3 deletions plugins/CoreHome/angularjs/common/services/piwik.js
Expand Up @@ -23,9 +23,8 @@
}

function updatePeriodParamsFromUrl() {
var date = piwik.broadcast.getValueFromHash('date');
var period = piwik.broadcast.getValueFromHash('period');

var date = piwik.broadcast.getValueFromHash('date') || piwik.broadcast.getValueFromUrl('date');
var period = piwik.broadcast.getValueFromHash('period') || piwik.broadcast.getValueFromUrl('period');
if (!isValidPeriod(period, date)) {
// invalid data in URL
return;
Expand Down
138 changes: 138 additions & 0 deletions plugins/CoreHome/angularjs/common/services/piwik.spec.js
Expand Up @@ -34,5 +34,143 @@
expect(piwikService.piwik_url).to.eql('http://localhost/');
});
});

describe('#updatePeriodParamsFromUrl()', function() {
DATE_PERIODS_TO_TEST = [
{
date: '2012-01-02',
period: 'day',
expected: {
currentDateString: '2012-01-02',
period: 'day',
startDateString: '2012-01-02',
endDateString: '2012-01-02'
}
},
{
date: '2012-01-02',
period: 'week',
expected: {
currentDateString: '2012-01-02',
period: 'week',
startDateString: '2012-01-02',
endDateString: '2012-01-08'
}
},
{
date: '2012-01-02',
period: 'month',
expected: {
currentDateString: '2012-01-02',
period: 'month',
startDateString: '2012-01-01',
endDateString: '2012-01-31'
}
},
{
date: '2012-01-02',
period: 'year',
expected: {
currentDateString: '2012-01-02',
period: 'year',
startDateString: '2012-01-01',
endDateString: '2012-12-31'
}
},
{
date: '2012-01-02,2012-02-03',
period: 'range',
expected: {
currentDateString: '2012-01-02,2012-02-03',
period: 'range',
startDateString: '2012-01-02',
endDateString: '2012-02-03'
}
},
// invalid
{
date: '2012-01-02',
period: 'range',
expected: {
currentDateString: undefined,
period: undefined,
startDateString: undefined,
endDateString: undefined
}
},
{
date: 'sldfjkdslkfj',
period: 'month',
expected: {
currentDateString: undefined,
period: undefined,
startDateString: undefined,
endDateString: undefined
}
},
{
date: '2012-01-02',
period: 'sflkjdslkfj',
expected: {
currentDateString: undefined,
period: undefined,
startDateString: undefined,
endDateString: undefined
}
}
];

DATE_PERIODS_TO_TEST.forEach(function (test) {
var date = test.date,
period = test.period,
expected = test.expected;

it('should parse the period in the URL correctly when date=' + date + ' and period=' + period, function () {
delete piwikService.currentDateString;
delete piwikService.period;
delete piwikService.startDateString;
delete piwikService.endDateString;

history.pushState(null, null, '?date=' + date + '&period=' + period);

piwikService.updatePeriodParamsFromUrl();

expect(piwikService.currentDateString).to.equal(expected.currentDateString);
expect(piwikService.period).to.equal(expected.period);
expect(piwikService.startDateString).to.equal(expected.startDateString);
expect(piwikService.endDateString).to.equal(expected.endDateString);
});

it('should parse the period in the URL hash correctly when date=' + date + ' and period=' + period, function () {
delete piwikService.currentDateString;
delete piwikService.period;
delete piwikService.startDateString;
delete piwikService.endDateString;

history.pushState(null, null, '?someparam=somevalue#?date=' + date + '&period=' + period);

piwikService.updatePeriodParamsFromUrl();

expect(piwikService.currentDateString).to.equal(expected.currentDateString);
expect(piwikService.period).to.equal(expected.period);
expect(piwikService.startDateString).to.equal(expected.startDateString);
expect(piwikService.endDateString).to.equal(expected.endDateString);
});
});

it('should not change object values if the current date/period is the same as the URL date/period', function () {
piwik.period = 'range';
piwik.currentDateString = '2012-01-01,2012-01-02';
piwik.startDateString = 'shouldnotchange';
piwik.endDateString = 'shouldnotchangeeither';

history.pushState(null, null, '?someparam=somevalue#?date=' + piwik.currentDateString + '&period=' + piwik.period);

piwikService.updatePeriodParamsFromUrl();

expect(piwikService.startDateString).to.equal('shouldnotchange');
expect(piwikService.endDateString).to.equal('shouldnotchangeeither');
});
});
});
})();
1 change: 0 additions & 1 deletion plugins/Morpheus/javascripts/ajaxHelper.js
Expand Up @@ -532,7 +532,6 @@ function ajaxHelper() {
var defaultParams = {
idSite: piwik.idSite || broadcast.getValueFromUrl('idSite'),
period: piwik.period || broadcast.getValueFromUrl('period'),
date: piwik.date || broadcast.getValueFromUrl('date'),
segment: broadcast.getValueFromHash('segment', window.location.href.split('#')[1])
};

Expand Down
10 changes: 10 additions & 0 deletions plugins/MultiSites/tests/UI/MultiSites_spec.js
Expand Up @@ -11,6 +11,7 @@ describe("MultiSitesTest", function () {
this.timeout(0);

var generalParams = 'idSite=1&period=year&date=2012-08-09';
var rangeParams = 'idSite=1&period=range&date=2012-08-05,2012-08-15';
var selector = '#multisites,.expandDataTableFooterDrawer';

var createdSiteId = null;
Expand Down Expand Up @@ -69,4 +70,13 @@ describe("MultiSitesTest", function () {
}, done);
});

it('should load the all websites dashboard correctly when period is range', function (done) {
this.retries(3);

expect.screenshot('all_websites_range').to.be.captureSelector(selector, function (page) {
page.load("?" + rangeParams + "&module=MultiSites&action=index");
page.wait(3000);
}, done);
});

});
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions tests/PHPUnit/Unit/UrlTest.php
Expand Up @@ -185,6 +185,14 @@ public function getIsLocalHost()
array($isLocal = true, '127.0.0.1'),
array($isLocal = true, '::1'),
array($isLocal = true, '[::1]'),

// with port
array($isLocal = false, '172.30.1.1:80'),
array($isLocal = false, '3ffe:1900:4545:3:200:f8ff:fe21:67cf:1005'),
array($isLocal = true, 'localhost:3000'),
array($isLocal = true, '127.0.0.1:213424'),
array($isLocal = true, '::1:345'),
array($isLocal = true, '[::1]:443'),
);
}

Expand Down