You are here

function PiwikBasicTest::testPiwikTracking in Piwik Web Analytics 7

Same name and namespace in other branches
  1. 6 tests/piwik_basic.test \PiwikBasicTest::testPiwikTracking()

File

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

Class

PiwikBasicTest
@file Test file for Piwik module.

Code

function testPiwikTracking() {

  // Set visibility to hide tracking code on admin page only,
  // track authenticated users.
  variable_set('piwik_visibility', 0);
  variable_set('piwik_pages', "admin*");
  variable_set('piwik_roles', array(
    DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
  ));
  $ua_code = '1';
  variable_set('piwik_account', $ua_code);

  // Check tracking code visibility.
  $this
    ->drupalGet('');
  $this
    ->assertRaw($ua_code, '[testPiwikTracking]: Tracking code is displayed for authenticated.');

  /* Sample JS code as added to page:
     <script type="text/javascript">
     var pkBaseURL = (("https:" == document.location.protocol) ? "https://{$piwikUrl}" : "http://{$piwikUrl}");
     document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
     </script><script type="text/javascript">
     try {
     var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", {$idSite});
     piwikTracker.trackPageView();
     piwikTracker.enableLinkTracking();
     } catch(err) {}
     </script>
     */

  // Test whether tracking code uses latest JS.
  $this
    ->drupalGet('');
  $this
    ->assertRaw('/piwik.php', '[testPiwikTracking]: Tracking code found.');

  // Test whether tracking code display is properly flipped.
  variable_set('piwik_visibility', 1);
  $this
    ->drupalGet('admin');
  $this
    ->assertRaw($ua_code, '[testPiwikTracking]: Tracking code is displayed on admin page.');
  $this
    ->drupalGet('admin/config/system/piwik');

  // Checking for tracking code URI here, as $ua_code is displayed in the form.
  $this
    ->assertRaw('/piwik.php', '[testPiwikTracking]: Tracking code is displayed on admin subpage.');
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('/piwik.php', '[testPiwikTracking]: Tracking code is not displayed on front page.');

  // Test whether tracking code is not display for anonymous.
  $this
    ->drupalGet('logout');
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('/piwik.php', '[testPiwikTracking]: Tracking code is not displayed for anonymous.');
}