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