You are here

function MatomoPhpFilterTest::testMatomoPhpFilter in Matomo Analytics 7.2

File

./matomo.test, line 529
Test file for Matomo module.

Class

MatomoPhpFilterTest

Code

function testMatomoPhpFilter() {
  $ua_code = '1';
  $this
    ->drupalLogin($this->admin_user);
  $edit = array();
  $edit['matomo_site_id'] = $ua_code;
  $edit['matomo_url_http'] = 'http://example.com/matomo/';
  $edit['matomo_url_https'] = 'https://example.com/matomo/';
  $edit['matomo_url_skiperror'] = TRUE;

  // Required for testing only.
  $edit['matomo_visibility_pages'] = 2;
  $edit['matomo_pages'] = '<?php return 0; ?>';
  $this
    ->drupalPost('admin/config/system/matomo', $edit, t('Save configuration'));

  // Compare saved setting with posted setting.
  $matomo_pages = variable_get('matomo_pages', $this
    ->randomName(8));
  $this
    ->assertEqual('<?php return 0; ?>', $matomo_pages, '[testMatomoPhpFilter]: PHP code snippet is intact.');

  // Check tracking code visibility.
  variable_set('matomo_pages', '<?php return TRUE; ?>');
  $this
    ->drupalGet('');
  $this
    ->assertRaw('u+"matomo.php"', '[testMatomoPhpFilter]: Tracking is displayed on frontpage page.');
  $this
    ->drupalGet('admin');
  $this
    ->assertRaw('u+"matomo.php"', '[testMatomoPhpFilter]: Tracking is displayed on admin page.');
  variable_set('matomo_pages', '<?php return FALSE; ?>');
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('u+"matomo.php"', '[testMatomoPhpFilter]: Tracking is not displayed on frontpage page.');

  // Test administration form.
  variable_set('matomo_pages', '<?php return TRUE; ?>');
  $this
    ->drupalGet('admin/config/system/matomo');
  $this
    ->assertRaw(t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'), '[testMatomoPhpFilter]: Permission to administer PHP for tracking visibility.');
  $this
    ->assertRaw(check_plain('<?php return TRUE; ?>'), '[testMatomoPhpFilter]: PHP code snippted is displayed.');

  // Login the delegated user and check if fields are visible.
  $this
    ->drupalLogin($this->delegated_admin_user);
  $this
    ->drupalGet('admin/config/system/matomo');
  $this
    ->assertNoRaw(t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'), '[testMatomoPhpFilter]: No permission to administer PHP for tracking visibility.');
  $this
    ->assertNoRaw(check_plain('<?php return TRUE; ?>'), '[testMatomoPhpFilter]: No permission to view PHP code snippted.');

  // Set a different value and verify that this is still the same after the post.
  variable_set('matomo_pages', '<?php return 0; ?>');
  $edit = array();
  $edit['matomo_site_id'] = $ua_code;
  $edit['matomo_url_http'] = 'http://example.com/matomo/';
  $edit['matomo_url_https'] = 'https://example.com/matomo/';
  $edit['matomo_url_skiperror'] = TRUE;

  // Required for testing only.
  $this
    ->drupalPost('admin/config/system/matomo', $edit, t('Save configuration'));

  // Compare saved setting with posted setting.
  $matomo_visibility_pages = variable_get('matomo_visibility_pages', 0);
  $matomo_pages = variable_get('matomo_pages', $this
    ->randomName(8));
  $this
    ->assertEqual(2, $matomo_visibility_pages, '[testMatomoPhpFilter]: Pages on which this PHP code returns TRUE is selected.');
  $this
    ->assertEqual('<?php return 0; ?>', $matomo_pages, '[testMatomoPhpFilter]: PHP code snippet is intact.');
}