You are here

function PiwikBasicTest::testPiwikTrackingCode in Piwik Web Analytics 7.2

Same name and namespace in other branches
  1. 6.2 piwik.test \PiwikBasicTest::testPiwikTrackingCode()

File

./piwik.test, line 119
Test file for Piwik module.

Class

PiwikBasicTest
@file Test file for Piwik module.

Code

function testPiwikTrackingCode() {
  $ua_code = '2';
  variable_set('piwik_site_id', $ua_code);
  variable_get('piwik_url_http', 'http://example.com/piwik/');
  variable_get('piwik_url_https', 'https://example.com/piwik/');

  // Show tracking code on every page except the listed pages.
  variable_set('piwik_visibility_pages', 0);

  // Enable tracking code for all user roles.
  variable_set('piwik_roles', array());

  /* Sample JS code as added to page:
     <script type="text/javascript">
     var _paq = _paq || [];
     (function(){
         var u=(("https:" == document.location.protocol) ? "https://{$PIWIK_URL}" : "http://{$PIWIK_URL}");
         _paq.push(['setSiteId', {$IDSITE}]);
         _paq.push(['setTrackerUrl', u+'piwik.php']);
         _paq.push(['trackPageView']);
         var d=document,
             g=d.createElement('script'),
             s=d.getElementsByTagName('script')[0];
             g.type='text/javascript';
             g.defer=true;
             g.async=true;
             g.src=u+'piwik.js';
             s.parentNode.insertBefore(g,s);
     })();
     </script>
     */

  // Test whether tracking code uses latest JS.
  variable_set('piwik_cache', 0);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('u+"piwik.php"', '[testPiwikTrackingCode]: Latest tracking code used.');

  // Test if tracking of User ID is enabled.
  variable_set('piwik_trackuserid', 1);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('_paq.push(["setUserId", ', '[testPiwikTrackingCode]: Tracking code for User ID is enabled.');

  // Test if tracking of User ID is disabled.
  variable_set('piwik_trackuserid', 0);
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('_paq.push(["setUserId", ', '[testPiwikTrackingCode]: Tracking code for User ID is disabled.');

  // Test whether single domain tracking is active.
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: Single domain tracking is active.');

  // Enable "One domain with multiple subdomains".
  variable_set('piwik_domain_mode', 1);
  $this
    ->drupalGet('');

  // Test may run on localhost, an ipaddress or real domain name.
  // TODO: Workaround to run tests successfully. This feature cannot tested reliable.
  global $cookie_domain;
  if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
    $this
      ->assertRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: One domain with multiple subdomains is active on real host.');
  }
  else {

    // Special cases, Localhost and IP addresses don't show 'setCookieDomain'.
    $this
      ->assertNoRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
  }

  // Test whether the BEFORE and AFTER code is added to the tracker.
  variable_set('piwik_codesnippet_before', '_paq.push(["setLinkTrackingTimer", 250]);');
  variable_set('piwik_codesnippet_after', '_paq.push(["t2.setSiteId", 2]);_gaq.push(["t2.trackPageView"]);');
  $this
    ->drupalGet('');
  $this
    ->assertRaw('setLinkTrackingTimer', '[testPiwikTrackingCode]: Before codesnippet has been found with "setLinkTrackingTimer" set.');
  $this
    ->assertRaw('t2.trackPageView', '[testPiwikTrackingCode]: After codesnippet with "t2" tracker has been found.');
}