You are here

public function MatomoBasicTest::testMatomoTrackingCode in Matomo Analytics 8

Tests if tracking code is properly added to the page.

File

tests/src/Functional/MatomoBasicTest.php, line 154

Class

MatomoBasicTest
Test basic functionality of Matomo module.

Namespace

Drupal\Tests\matomo\Functional

Code

public function testMatomoTrackingCode() {
  $site_id = '2';
  $this
    ->config('matomo.settings')
    ->set('site_id', $site_id)
    ->save();
  $this
    ->config('matomo.settings')
    ->set('url_http', 'http://www.example.com/matomo/')
    ->save();
  $this
    ->config('matomo.settings')
    ->set('url_https', 'https://www.example.com/matomo/')
    ->save();

  // Show tracking code on every page except the listed pages.
  $this
    ->config('matomo.settings')
    ->set('visibility.request_path_mode', 0)
    ->save();

  // Enable tracking code for all user roles.
  $this
    ->config('matomo.settings')
    ->set('visibility.user_role_roles', [])
    ->save();

  // @codingStandardsIgnoreStart

  /* Sample JS code as added to page:
     <script type="text/javascript">
     var _paq = _paq || [];
     (function(){
         var u=(("https:" == document.location.protocol) ? "https://{$MATOMO_URL}" : "http://{$MATOMO_URL}");
         _paq.push(['setSiteId', {$IDSITE}]);
         _paq.push(['setTrackerUrl', u+'matomo.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+'matomo.js';
             s.parentNode.insertBefore(g,s);
     })();
     </script>
     */

  // @codingStandardsIgnoreEnd
  // Test whether tracking code uses latest JS.
  $this
    ->config('matomo.settings')
    ->set('cache', 0)
    ->save();
  $this
    ->drupalGet('');
  $this
    ->assertRaw('u+"matomo.php"', '[testMatomoTrackingCode]: Latest tracking code used.');

  // Test if tracking of User ID is enabled.
  $this
    ->config('matomo.settings')
    ->set('track.userid', 1)
    ->save();
  $this
    ->drupalGet('');
  $this
    ->assertRaw('_paq.push(["setUserId", ', '[testMatomoTrackingCode]: Tracking code for User ID is enabled.');

  // Test if tracking of User ID is disabled.
  $this
    ->config('matomo.settings')
    ->set('track.userid', 0)
    ->save();
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('_paq.push(["setUserId", ', '[testMatomoTrackingCode]: Tracking code for User ID is disabled.');

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

  // Enable "One domain with multiple subdomains".
  $this
    ->config('matomo.settings')
    ->set('domain_mode', 1)
    ->save();
  $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"', '[testMatomoTrackingCode]: 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"', '[testMatomoTrackingCode]: 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.
  $this
    ->config('matomo.settings')
    ->set('codesnippet.before', '_paq.push(["setLinkTrackingTimer", 250]);')
    ->save();
  $this
    ->config('matomo.settings')
    ->set('codesnippet.after', '_paq.push(["t2.setSiteId", 2]);if(1 == 1 && 2 < 3 && 2 > 1){console.log("Matomo: Custom condition works.");}_gaq.push(["t2.trackPageView"]);')
    ->save();
  $this
    ->drupalGet('');
  $this
    ->assertRaw('setLinkTrackingTimer', '[testMatomoTrackingCode]: Before codesnippet has been found with "setLinkTrackingTimer" set.');
  $this
    ->assertRaw('t2.trackPageView', '[testMatomoTrackingCode]: After codesnippet with "t2" tracker has been found.');
  $this
    ->assertRaw('if(1 == 1 && 2 < 3 && 2 > 1){console.log("Matomo: Custom condition works.");}', '[testMatomoTrackingCode]: JavaScript code is not HTML escaped.');

  // Test disable cookie setting.
  $this
    ->assertNoRaw('_paq.push(["disableCookies"]);', '[testMatomoTrackingCode]: Cookies not disabled by default.');
  $this
    ->config('matomo.settings')
    ->set('privacy.disablecookies', TRUE)
    ->save();
  $this
    ->drupalGet('');
  $this
    ->assertRaw('_paq.push(["disableCookies"]);', '[testMatomoTrackingCode]: Cookies disabled.');
}