From b562ded89964307f83379c8b4e6b2366b77208b9 Mon Sep 17 00:00:00 2001 From: mattab Date: Tue, 26 Feb 2013 14:06:25 +1300 Subject: [PATCH] Refs #1845 Adding failing JS test for use case discussed Goal: . make it hard to misuse API Bug: if trackPageView is pushed to async before setTrackerUrl, the request will not be tracked. Solution: setTrackerUrl should be executed first even if i was async "pushed" at a later stage (Benaka has patch). --- tests/javascript/index.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/tests/javascript/index.php b/tests/javascript/index.php index 8a22a6d940e..083de2e88c7 100644 --- a/tests/javascript/index.php +++ b/tests/javascript/index.php @@ -29,10 +29,22 @@ function getToken() { if ($sqlite) { echo ' var _paq = _paq || []; -_paq.push(["setSiteId", 1]); -_paq.push(["setTrackerUrl", "piwik.php"]); -_paq.push(["setCustomData", { "token" : getToken() }]); -_paq.push(["trackPageView", "Asynchronous tracker"]);'; + +function testCallingTrackPageViewBeforeSetTrackerUrlWorks() { + _paq.push(["setSiteId", 1]); + _paq.push(["setCustomData", { "token" : getToken() }]); + _paq.push(["trackPageView", "Asynchronous Tracker ONE"]); + _paq.push(["setTrackerUrl", "piwik.php"]); +} + +function testTrackPageViewAsync() { + _paq.push(["trackPageView", "Asynchronous tracking TWO"]); +} + +testCallingTrackPageViewBeforeSetTrackerUrlWorks(); +testTrackPageViewAsync(); + +'; } ?> @@ -1050,11 +1062,14 @@ function wait(msecs) xhr.open("GET", "piwik.php?requests=" + getToken(), false); xhr.send(null); results = xhr.responseText; - equal( (/([0-9]+)\<\/span\>/.exec(results))[1], "24", "count tracking events" ); + equal( (/([0-9]+)\<\/span\>/.exec(results))[1], "26", "count tracking events" ); + + alert(results); // tracking requests ok( /PiwikTest/.test( results ), "trackPageView(), setDocumentTitle()" ); - ok( /Asynchronous/.test( results ), "async trackPageView()" ); + ok( results.indexOf("tests/javascript/piwik.php?action_name=Asynchronous%20Tracker%20ONE&idsite=1&rec=1") >= 0 , "async trackPageView() called before setTrackerUrl() should work" ); + ok( /Asynchronous%20tracking%20TWO/.test( results ), "async trackPageView() called after another trackPageView()" ); ok( /CustomTitleTest/.test( results ), "trackPageView(customTitle)" ); ok( ! /click.example.com/.test( results ), "click: ignore href=javascript" ); ok( /example.ca/.test( results ), "trackLink()" );