View source
<?php
namespace Drupal\Tests\matomo\Functional;
use Drupal\Component\Utility\Html;
use Drupal\Tests\BrowserTestBase;
class MatomoPhpFilterTest extends BrowserTestBase {
public static $modules = [
'matomo',
'php',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$permissions_admin_user = [
'access administration pages',
'administer matomo',
'use php for matomo tracking visibility',
];
$this->admin_user = $this
->drupalCreateUser($permissions_admin_user);
$permissions_delegated_admin_user = [
'access administration pages',
'administer matomo',
];
$this->delegated_admin_user = $this
->drupalCreateUser($permissions_delegated_admin_user);
}
public function testMatomoPhpFilter() {
$page = $this
->getSession()
->getPage();
$site_id = '1';
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('admin/config/system/matomo');
$page
->find('css', '[name="matomo_url_skiperror"]')
->setValue(TRUE);
$edit = [];
$edit['matomo_site_id'] = $site_id;
$edit['matomo_url_http'] = 'http://www.example.com/matomo/';
$edit['matomo_url_https'] = 'https://www.example.com/matomo/';
$edit['matomo_visibility_request_path_mode'] = 2;
$edit['matomo_visibility_request_path_pages'] = '<?php return 0; ?>';
$this
->drupalPostForm(NULL, $edit, 'Save configuration');
$matomo_visibility_request_path_pages = \Drupal::config('matomo.settings')
->get('visibility.request_path_pages');
$this
->assertEqual('<?php return 0; ?>', $matomo_visibility_request_path_pages, '[testMatomoPhpFilter]: PHP code snippet is intact.');
$this
->config('matomo.settings')
->set('visibility.request_path_pages', '<?php return TRUE; ?>')
->save();
$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.');
$this
->config('matomo.settings')
->set('visibility.request_path_pages', '<?php return FALSE; ?>')
->save();
$this
->drupalGet('');
$this
->assertNoRaw('u+"matomo.php"', '[testMatomoPhpFilter]: Tracking is not displayed on frontpage page.');
$this
->config('matomo.settings')
->set('visibility.request_path_pages', '<?php return TRUE; ?>')
->save();
$this
->drupalGet('admin/config/system/matomo');
$this
->assertRaw('Pages on which this PHP code returns <code>TRUE</code> (experts only)', '[testMatomoPhpFilter]: Permission to administer PHP for tracking visibility.');
$this
->assertRaw(Html::escape('<?php return TRUE; ?>'), '[testMatomoPhpFilter]: PHP code snippted is displayed.');
$this
->drupalLogin($this->delegated_admin_user);
$this
->drupalGet('admin/config/system/matomo');
$this
->assertNoRaw('Pages on which this PHP code returns <code>TRUE</code> (experts only)', '[testMatomoPhpFilter]: No permission to administer PHP for tracking visibility.');
$this
->assertNoRaw(Html::escape('<?php return TRUE; ?>'), '[testMatomoPhpFilter]: No permission to view PHP code snippted.');
$this
->config('matomo.settings')
->set('visibility.request_path_pages', '<?php return 0; ?>')
->save();
$this
->drupalGet('admin/config/system/matomo');
$page
->find('css', '[name="matomo_url_skiperror"]')
->setValue(TRUE);
$edit = [];
$edit['matomo_site_id'] = $site_id;
$edit['matomo_url_http'] = 'http://www.example.com/matomo/';
$edit['matomo_url_https'] = 'https://www.example.com/matomo/';
$this
->drupalPostForm(NULL, $edit, 'Save configuration');
$matomo_visibility_request_path_mode = $this
->config('matomo.settings')
->get('visibility.request_path_mode');
$matomo_visibility_request_path_pages = $this
->config('matomo.settings')
->get('visibility.request_path_pages');
$this
->assertEqual(2, $matomo_visibility_request_path_mode, '[testMatomoPhpFilter]: Pages on which this PHP code returns TRUE is selected.');
$this
->assertEqual('<?php return 0; ?>', $matomo_visibility_request_path_pages, '[testMatomoPhpFilter]: PHP code snippet is intact.');
}
}