matomo.test 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <?php
  2. /**
  3. * @file
  4. * Test file for Matomo module.
  5. */
  6. class MatomoBasicTest extends DrupalWebTestCase {
  7. /**
  8. * User without permissions to edit snippets.
  9. *
  10. * @var \StdClass
  11. */
  12. protected $noSnippetUser;
  13. public static function getInfo() {
  14. return array(
  15. 'name' => t('Matomo basic tests'),
  16. 'description' => t('Test basic functionality of Matomo module.'),
  17. 'group' => 'Matomo',
  18. );
  19. }
  20. function setUp() {
  21. parent::setUp('matomo');
  22. $permissions = array(
  23. 'access administration pages',
  24. 'administer matomo',
  25. );
  26. // User to set up matomo.
  27. $this->noSnippetUser = $this->drupalCreateUser($permissions);
  28. $permissions[] = 'add js snippets for matomo';
  29. $this->admin_user = $this->drupalCreateUser($permissions);
  30. $this->drupalLogin($this->admin_user);
  31. }
  32. function testMatomoConfiguration() {
  33. // Check for setting page's presence.
  34. $this->drupalGet('admin/config/system/matomo');
  35. $this->assertRaw(t('Matomo site ID'), '[testMatomoConfiguration]: Settings page displayed.');
  36. // Check for account code validation.
  37. $edit['matomo_site_id'] = $this->randomName(2);
  38. $this->drupalPost('admin/config/system/matomo', $edit, 'Save configuration');
  39. $this->assertRaw(t('A valid Matomo site ID is an integer only.'), '[testMatomoConfiguration]: Invalid Matomo site ID number validated.');
  40. // User should have access to code snippets.
  41. $this->assertFieldByName('matomo_codesnippet_before');
  42. $this->assertFieldByName('matomo_codesnippet_after');
  43. $this->assertNoFieldByXPath("//textarea[@name='matomo_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is enabled.');
  44. $this->assertNoFieldByXPath("//textarea[@name='matomo_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is enabled.');
  45. // Login as user without JS permissions.
  46. $this->drupalLogin($this->noSnippetUser);
  47. $this->drupalGet('admin/config/system/matomo');
  48. // User should *not* have access to snippets, but create fields.
  49. $this->assertFieldByName('matomo_codesnippet_before');
  50. $this->assertFieldByName('matomo_codesnippet_after');
  51. $this->assertFieldByXPath("//textarea[@name='matomo_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is disabled.');
  52. $this->assertFieldByXPath("//textarea[@name='matomo_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is disabled.');
  53. }
  54. function testMatomoPageVisibility() {
  55. $ua_code = '1';
  56. variable_set('matomo_site_id', $ua_code);
  57. variable_get('matomo_url_http', 'http://example.com/matomo/');
  58. variable_get('matomo_url_https', 'https://example.com/matomo/');
  59. // Show tracking on "every page except the listed pages".
  60. variable_set('matomo_visibility_pages', 0);
  61. // Disable tracking one "admin*" pages only.
  62. variable_set('matomo_pages', "admin\nadmin/*");
  63. // Enable tracking only for authenticated users only.
  64. variable_set('matomo_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
  65. // Check tracking code visibility.
  66. $this->drupalGet('');
  67. $this->assertRaw('/matomo/matomo.js', '[testMatomoPageVisibility]: Custom tracking script is is displayed for authenticated users.');
  68. $this->assertRaw('u+"piwik.php"', '[testMatomoPageVisibility]: Tracking code is displayed for authenticated users.');
  69. // Test whether tracking code is not included on pages to omit.
  70. $this->drupalGet('admin');
  71. $this->assertNoRaw('u+"piwik.php"', '[testMatomoPageVisibility]: Tracking code is not displayed on admin page.');
  72. $this->drupalGet('admin/config/system/matomo');
  73. // Checking for tracking code URI here, as $ua_code is displayed in the form.
  74. $this->assertNoRaw('u+"piwik.php"', '[testMatomoPageVisibility]: Tracking code is not displayed on admin subpage.');
  75. // Test whether tracking code display is properly flipped.
  76. variable_set('matomo_visibility_pages', 1);
  77. $this->drupalGet('admin');
  78. $this->assertRaw('u+"piwik.php"', '[testMatomoPageVisibility]: Tracking code is displayed on admin page.');
  79. $this->drupalGet('admin/config/system/matomo');
  80. // Checking for tracking code URI here, as $ua_code is displayed in the form.
  81. $this->assertRaw('u+"piwik.php"', '[testMatomoPageVisibility]: Tracking code is displayed on admin subpage.');
  82. $this->drupalGet('');
  83. $this->assertNoRaw('u+"piwik.php"', '[testMatomoPageVisibility]: Tracking code is NOT displayed on front page.');
  84. // Test whether tracking code is not display for anonymous.
  85. $this->drupalLogout();
  86. $this->drupalGet('');
  87. $this->assertNoRaw('u+"piwik.php"', '[testMatomoPageVisibility]: Tracking code is NOT displayed for anonymous.');
  88. // Switch back to every page except the listed pages.
  89. variable_set('matomo_visibility_pages', 0);
  90. // Enable tracking code for all user roles.
  91. variable_set('matomo_roles', array());
  92. // Test whether 403 forbidden tracking code is shown if user has no access.
  93. $this->drupalGet('admin');
  94. $this->assertRaw('403/URL = "', '[testMatomoPageVisibility]: 403 Forbidden tracking code shown if user has no access.');
  95. // Test whether 404 not found tracking code is shown on non-existent pages.
  96. $this->drupalGet($this->randomName(64));
  97. $this->assertRaw('404/URL = "', '[testMatomoPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
  98. }
  99. function testMatomoTrackingCode() {
  100. $ua_code = '2';
  101. variable_set('matomo_site_id', $ua_code);
  102. variable_get('matomo_url_http', 'http://example.com/matomo/');
  103. variable_get('matomo_url_https', 'https://example.com/matomo/');
  104. // Show tracking code on every page except the listed pages.
  105. variable_set('matomo_visibility_pages', 0);
  106. // Enable tracking code for all user roles.
  107. variable_set('matomo_roles', array());
  108. /* Sample JS code as added to page:
  109. <script type="text/javascript">
  110. var _paq = _paq || [];
  111. (function(){
  112. var u=(("https:" == document.location.protocol) ? "https://{$MATOMO_URL}" : "http://{$MATOMO_URL}");
  113. _paq.push(['setSiteId', {$IDSITE}]);
  114. _paq.push(['setTrackerUrl', u+'piwik.php']);
  115. _paq.push(['trackPageView']);
  116. var d=document,
  117. g=d.createElement('script'),
  118. s=d.getElementsByTagName('script')[0];
  119. g.type='text/javascript';
  120. g.defer=true;
  121. g.async=true;
  122. g.src=u+'matomo.js';
  123. s.parentNode.insertBefore(g,s);
  124. })();
  125. </script>
  126. */
  127. // Test whether tracking code uses latest JS.
  128. variable_set('matomo_cache', 0);
  129. $this->drupalGet('');
  130. $this->assertRaw('u+"piwik.php"', '[testMatomoTrackingCode]: Latest tracking code used.');
  131. // Test if tracking of User ID is enabled.
  132. variable_set('matomo_trackuserid', 1);
  133. $this->drupalGet('');
  134. $this->assertRaw('_paq.push(["setUserId", ', '[testMatomoTrackingCode]: Tracking code for User ID is enabled.');
  135. // Test if tracking of User ID is disabled.
  136. variable_set('matomo_trackuserid', 0);
  137. $this->drupalGet('');
  138. $this->assertNoRaw('_paq.push(["setUserId", ', '[testMatomoTrackingCode]: Tracking code for User ID is disabled.');
  139. // Test whether single domain tracking is active.
  140. $this->drupalGet('');
  141. $this->assertNoRaw('_paq.push(["setCookieDomain"', '[testMatomoTrackingCode]: Single domain tracking is active.');
  142. // Enable "One domain with multiple subdomains".
  143. variable_set('matomo_domain_mode', 1);
  144. $this->drupalGet('');
  145. // Test may run on localhost, an ipaddress or real domain name.
  146. // TODO: Workaround to run tests successfully. This feature cannot tested reliable.
  147. global $cookie_domain;
  148. if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
  149. $this->assertRaw('_paq.push(["setCookieDomain"', '[testMatomoTrackingCode]: One domain with multiple subdomains is active on real host.');
  150. }
  151. else {
  152. // Special cases, Localhost and IP addresses don't show 'setCookieDomain'.
  153. $this->assertNoRaw('_paq.push(["setCookieDomain"', '[testMatomoTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
  154. }
  155. // Test whether the BEFORE and AFTER code is added to the tracker.
  156. variable_set('matomo_codesnippet_before', '_paq.push(["setLinkTrackingTimer", 250]);');
  157. variable_set('matomo_codesnippet_after', '_paq.push(["t2.setSiteId", 2]);_gaq.push(["t2.trackPageView"]);');
  158. $this->drupalGet('');
  159. $this->assertRaw('setLinkTrackingTimer', '[testMatomoTrackingCode]: Before codesnippet has been found with "setLinkTrackingTimer" set.');
  160. $this->assertRaw('t2.trackPageView', '[testMatomoTrackingCode]: After codesnippet with "t2" tracker has been found.');
  161. }
  162. }
  163. class MatomoCustomVariablesTest extends DrupalWebTestCase {
  164. public static function getInfo() {
  165. return array(
  166. 'name' => t('Matomo Custom Variables tests'),
  167. 'description' => t('Test custom variables functionality of Matomo module.'),
  168. 'group' => 'Matomo',
  169. 'dependencies' => array('token'),
  170. );
  171. }
  172. function setUp() {
  173. parent::setUp('matomo', 'token');
  174. $permissions = array(
  175. 'access administration pages',
  176. 'administer matomo',
  177. );
  178. // User to set up matomo.
  179. $this->admin_user = $this->drupalCreateUser($permissions);
  180. }
  181. function testMatomoCustomVariables() {
  182. $ua_code = '3';
  183. variable_set('matomo_site_id', $ua_code);
  184. // Basic test if the feature works.
  185. $custom_vars = array(
  186. 'slots' => array(
  187. 1 => array(
  188. 'slot' => 1,
  189. 'name' => 'Foo 1',
  190. 'value' => 'Bar 1',
  191. 'scope' => 3,
  192. ),
  193. 2 => array(
  194. 'slot' => 2,
  195. 'name' => 'Foo 2',
  196. 'value' => 'Bar 2',
  197. 'scope' => 2,
  198. ),
  199. 3 => array(
  200. 'slot' => 3,
  201. 'name' => 'Foo 3',
  202. 'value' => 'Bar 3',
  203. 'scope' => 3,
  204. ),
  205. 4 => array(
  206. 'slot' => 4,
  207. 'name' => 'Foo 4',
  208. 'value' => 'Bar 4',
  209. 'scope' => 2,
  210. ),
  211. 5 => array(
  212. 'slot' => 5,
  213. 'name' => 'Foo 5',
  214. 'value' => 'Bar 5',
  215. 'scope' => 1,
  216. ),
  217. )
  218. );
  219. variable_set('matomo_custom_var', $custom_vars);
  220. $this->drupalGet('');
  221. foreach ($custom_vars['slots'] as $slot) {
  222. $this->assertRaw("_paq.push(['setCustomVariable', " . $slot['slot'] . ", \"" . $slot['name'] . "\", \"" . $slot['value'] . "\", " . $slot['scope'] . "]);", '[testMatomoCustomVariables]: setCustomVariable ' . $slot['slot'] . ' is shown.');
  223. }
  224. // Test whether tokens are replaced in custom variable names.
  225. $site_slogan = $this->randomName(16);
  226. variable_set('site_slogan', $site_slogan);
  227. $custom_vars = array(
  228. 'slots' => array(
  229. 1 => array(
  230. 'slot' => 1,
  231. 'name' => 'Name: [site:slogan]',
  232. 'value' => 'Value: [site:slogan]',
  233. 'scope' => 3,
  234. ),
  235. 2 => array(
  236. 'slot' => 2,
  237. 'name' => '',
  238. 'value' => $this->randomName(16),
  239. 'scope' => 1,
  240. ),
  241. 3 => array(
  242. 'slot' => 3,
  243. 'name' => $this->randomName(16),
  244. 'value' => '',
  245. 'scope' => 2,
  246. ),
  247. 4 => array(
  248. 'slot' => 4,
  249. 'name' => '',
  250. 'value' => '',
  251. 'scope' => 3,
  252. ),
  253. 5 => array(
  254. 'slot' => 5,
  255. 'name' => '',
  256. 'value' => '',
  257. 'scope' => 3,
  258. ),
  259. )
  260. );
  261. variable_set('matomo_custom_var', $custom_vars);
  262. $this->verbose('<pre>' . print_r($custom_vars, TRUE) . '</pre>');
  263. $this->drupalGet('');
  264. $this->assertRaw("_paq.push(['setCustomVariable', 1, \"Name: $site_slogan\", \"Value: $site_slogan\", 3]", '[testMatomoCustomVariables]: Tokens have been replaced in custom variable.');
  265. $this->assertNoRaw("_paq.push(['setCustomVariable', 2,", '[testMatomoCustomVariables]: Value with empty name is not shown.');
  266. $this->assertNoRaw("_paq.push(['setCustomVariable', 3,", '[testMatomoCustomVariables]: Name with empty value is not shown.');
  267. $this->assertNoRaw("_paq.push(['setCustomVariable', 4,", '[testMatomoCustomVariables]: Empty name and value is not shown.');
  268. $this->assertNoRaw("_paq.push(['setCustomVariable', 5,", '[testMatomoCustomVariables]: Empty name and value is not shown.');
  269. }
  270. }
  271. /**
  272. * Test custom url functionality of Matomo module.
  273. */
  274. class MatomoCustomUrls extends DrupalWebTestCase {
  275. public static function getInfo() {
  276. return array(
  277. 'name' => 'Matomo custom url tests',
  278. 'description' => 'Test custom url functionality of Matomo module.',
  279. 'group' => 'Matomo',
  280. );
  281. }
  282. function setUp() {
  283. parent::setUp('matomo');
  284. $permissions = array(
  285. 'access administration pages',
  286. 'administer matomo',
  287. );
  288. // User to set up matomo.
  289. $this->admin_user = $this->drupalCreateUser($permissions);
  290. }
  291. /**
  292. * Tests if user password page urls are overridden.
  293. */
  294. public function testMatomoUserPasswordPage() {
  295. $base_path = base_path();
  296. $ua_code = '1';
  297. variable_set('matomo_site_id', $ua_code);
  298. variable_get('matomo_url_http', 'http://example.com/matomo/');
  299. variable_get('matomo_url_https', 'https://example.com/matomo/');
  300. $this->drupalGet('user/password', array('query' => array('name' => 'foo')));
  301. $this->assertRaw('_paq.push(["setCustomUrl", ' . drupal_json_encode(url('user/password')) . ']);');
  302. $this->drupalGet('user/password', array('query' => array('name' => 'foo@example.com')));
  303. $this->assertRaw('_paq.push(["setCustomUrl", ' . drupal_json_encode(url('user/password')) . ']);');
  304. $this->drupalGet('user/password');
  305. $this->assertNoRaw('_paq.push(["setCustomUrl", "', '[testMatomoCustomUrls]: Custom url not set.');
  306. }
  307. }
  308. class MatomoStatusMessagesTest extends DrupalWebTestCase {
  309. public static function getInfo() {
  310. return array(
  311. 'name' => 'Matomo status messages tests',
  312. 'description' => 'Test status messages functionality of Matomo module.',
  313. 'group' => 'Matomo',
  314. );
  315. }
  316. function setUp() {
  317. parent::setUp('matomo');
  318. $permissions = array(
  319. 'access administration pages',
  320. 'administer matomo',
  321. );
  322. // User to set up matomo.
  323. $this->admin_user = $this->drupalCreateUser($permissions);
  324. }
  325. function testMatomoStatusMessages() {
  326. $ua_code = '1';
  327. variable_set('matomo_site_id', $ua_code);
  328. // Enable logging of errors only.
  329. variable_set('matomo_trackmessages', array('error' => 'error'));
  330. $this->drupalPost('user/login', array(), t('Log in'));
  331. $this->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Username field is required."]);', '[testMatomoStatusMessages]: trackEvent "Username field is required." is shown.');
  332. $this->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Password field is required."]);', '[testMatomoStatusMessages]: trackEvent "Password field is required." is shown.');
  333. // @todo: investigate why drupal_set_message() fails.
  334. //drupal_set_message('Example status message.', 'status');
  335. //drupal_set_message('Example warning message.', 'warning');
  336. //drupal_set_message('Example error message.', 'error');
  337. //drupal_set_message('Example error <em>message</em> with html tags and <a href="https://example.com/">link</a>.', 'error');
  338. //$this->drupalGet('');
  339. //$this->assertNoRaw('_paq.push(["trackEvent", "Messages", "Status message", "Example status message."]);', '[testMatomoStatusMessages]: Example status message is not enabled for tracking.');
  340. //$this->assertNoRaw('_paq.push(["trackEvent", "Messages", "Warning message", "Example warning message."]);', '[testMatomoStatusMessages]: Example warning message is not enabled for tracking.');
  341. //$this->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Example error message."]);', '[testMatomoStatusMessages]: Example error message is shown.');
  342. //$this->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Example error message with html tags and link."]);', '[testMatomoStatusMessages]: HTML has been stripped successful from Example error message with html tags and link.');
  343. }
  344. }
  345. class MatomoRolesTest extends DrupalWebTestCase {
  346. public static function getInfo() {
  347. return array(
  348. 'name' => t('Matomo role tests'),
  349. 'description' => t('Test roles functionality of Matomo module.'),
  350. 'group' => 'Matomo',
  351. );
  352. }
  353. function setUp() {
  354. parent::setUp('matomo');
  355. $permissions = array(
  356. 'access administration pages',
  357. 'administer matomo',
  358. );
  359. // User to set up matomo.
  360. $this->admin_user = $this->drupalCreateUser($permissions);
  361. }
  362. function testMatomoRolesTracking() {
  363. $ua_code = '1';
  364. variable_set('matomo_site_id', $ua_code);
  365. variable_get('matomo_url_http', 'http://example.com/matomo/');
  366. variable_get('matomo_url_https', 'https://example.com/matomo/');
  367. // Test if the default settings are working as expected.
  368. // Add to the selected roles only.
  369. variable_set('matomo_visibility_roles', 0);
  370. // Enable tracking for all users.
  371. variable_set('matomo_roles', array());
  372. // Check tracking code visibility.
  373. $this->drupalGet('');
  374. $this->assertRaw('u+"piwik.php"', '[testMatomoRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.');
  375. $this->drupalGet('admin');
  376. $this->assertRaw('"403/URL = "', '[testMatomoRoleVisibility]: 403 Forbidden tracking code is displayed for anonymous users in admin section with default settings.');
  377. $this->drupalLogin($this->admin_user);
  378. $this->drupalGet('');
  379. $this->assertRaw('u+"piwik.php"', '[testMatomoRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.');
  380. $this->drupalGet('admin');
  381. $this->assertNoRaw('u+"piwik.php"', '[testMatomoRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.');
  382. // Test if the non-default settings are working as expected.
  383. // Enable tracking only for authenticated users.
  384. variable_set('matomo_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
  385. $this->drupalGet('');
  386. $this->assertRaw('u+"piwik.php"', '[testMatomoRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.');
  387. $this->drupalLogout();
  388. $this->drupalGet('');
  389. $this->assertNoRaw('u+"piwik.php"', '[testMatomoRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.');
  390. // Add to every role except the selected ones.
  391. variable_set('matomo_visibility_roles', 1);
  392. // Enable tracking for all users.
  393. variable_set('matomo_roles', array());
  394. // Check tracking code visibility.
  395. $this->drupalGet('');
  396. $this->assertRaw('u+"piwik.php"', '[testMatomoRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.');
  397. $this->drupalGet('admin');
  398. $this->assertRaw('"403/URL = "', '[testMatomoRoleVisibility]: 403 Forbidden tracking code is shown for anonymous users if every role except the selected ones is selected.');
  399. $this->drupalLogin($this->admin_user);
  400. $this->drupalGet('');
  401. $this->assertRaw('u+"piwik.php"', '[testMatomoRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.');
  402. $this->drupalGet('admin');
  403. $this->assertNoRaw('u+"piwik.php"', '[testMatomoRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.');
  404. // Disable tracking for authenticated users.
  405. variable_set('matomo_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
  406. $this->drupalGet('');
  407. $this->assertNoRaw('u+"piwik.php"', '[testMatomoRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.');
  408. $this->drupalGet('admin');
  409. $this->assertNoRaw('u+"piwik.php"', '[testMatomoRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.');
  410. $this->drupalLogout();
  411. $this->drupalGet('');
  412. $this->assertRaw('u+"piwik.php"', '[testMatomoRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.');
  413. }
  414. }
  415. class MatomoPhpFilterTest extends DrupalWebTestCase {
  416. public static function getInfo() {
  417. return array(
  418. 'name' => 'Matomo php filter tests',
  419. 'description' => 'Test php filter functionality of Matomo module.',
  420. 'group' => 'Matomo',
  421. );
  422. }
  423. function setUp() {
  424. parent::setUp('matomo', 'php');
  425. // Administrator with all permissions.
  426. $permissions_admin_user = array(
  427. 'access administration pages',
  428. 'administer matomo',
  429. 'use php for matomo tracking visibility',
  430. );
  431. $this->admin_user = $this->drupalCreateUser($permissions_admin_user);
  432. // Administrator who cannot configure tracking visibility with PHP.
  433. $permissions_delegated_admin_user = array(
  434. 'access administration pages',
  435. 'administer matomo',
  436. );
  437. $this->delegated_admin_user = $this->drupalCreateUser($permissions_delegated_admin_user);
  438. }
  439. function testMatomoPhpFilter() {
  440. $ua_code = '1';
  441. $this->drupalLogin($this->admin_user);
  442. $edit = array();
  443. $edit['matomo_site_id'] = $ua_code;
  444. $edit['matomo_url_http'] = 'http://example.com/matomo/';
  445. $edit['matomo_url_https'] = 'https://example.com/matomo/';
  446. $edit['matomo_url_skiperror'] = TRUE; // Required for testing only.
  447. $edit['matomo_visibility_pages'] = 2;
  448. $edit['matomo_pages'] = '<?php return 0; ?>';
  449. $this->drupalPost('admin/config/system/matomo', $edit, t('Save configuration'));
  450. // Compare saved setting with posted setting.
  451. $matomo_pages = variable_get('matomo_pages', $this->randomName(8));
  452. $this->assertEqual('<?php return 0; ?>', $matomo_pages, '[testMatomoPhpFilter]: PHP code snippet is intact.');
  453. // Check tracking code visibility.
  454. variable_set('matomo_pages', '<?php return TRUE; ?>');
  455. $this->drupalGet('');
  456. $this->assertRaw('u+"piwik.php"', '[testMatomoPhpFilter]: Tracking is displayed on frontpage page.');
  457. $this->drupalGet('admin');
  458. $this->assertRaw('u+"piwik.php"', '[testMatomoPhpFilter]: Tracking is displayed on admin page.');
  459. variable_set('matomo_pages', '<?php return FALSE; ?>');
  460. $this->drupalGet('');
  461. $this->assertNoRaw('u+"piwik.php"', '[testMatomoPhpFilter]: Tracking is not displayed on frontpage page.');
  462. // Test administration form.
  463. variable_set('matomo_pages', '<?php return TRUE; ?>');
  464. $this->drupalGet('admin/config/system/matomo');
  465. $this->assertRaw(t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'), '[testMatomoPhpFilter]: Permission to administer PHP for tracking visibility.');
  466. $this->assertRaw(check_plain('<?php return TRUE; ?>'), '[testMatomoPhpFilter]: PHP code snippted is displayed.');
  467. // Login the delegated user and check if fields are visible.
  468. $this->drupalLogin($this->delegated_admin_user);
  469. $this->drupalGet('admin/config/system/matomo');
  470. $this->assertNoRaw(t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'), '[testMatomoPhpFilter]: No permission to administer PHP for tracking visibility.');
  471. $this->assertNoRaw(check_plain('<?php return TRUE; ?>'), '[testMatomoPhpFilter]: No permission to view PHP code snippted.');
  472. // Set a different value and verify that this is still the same after the post.
  473. variable_set('matomo_pages', '<?php return 0; ?>');
  474. $edit = array();
  475. $edit['matomo_site_id'] = $ua_code;
  476. $edit['matomo_url_http'] = 'http://example.com/matomo/';
  477. $edit['matomo_url_https'] = 'https://example.com/matomo/';
  478. $edit['matomo_url_skiperror'] = TRUE; // Required for testing only.
  479. $this->drupalPost('admin/config/system/matomo', $edit, t('Save configuration'));
  480. // Compare saved setting with posted setting.
  481. $matomo_visibility_pages = variable_get('matomo_visibility_pages', 0);
  482. $matomo_pages = variable_get('matomo_pages', $this->randomName(8));
  483. $this->assertEqual(2, $matomo_visibility_pages, '[testMatomoPhpFilter]: Pages on which this PHP code returns TRUE is selected.');
  484. $this->assertEqual('<?php return 0; ?>', $matomo_pages, '[testMatomoPhpFilter]: PHP code snippet is intact.');
  485. }
  486. }