piwik.test 24 KB

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