Projet

Général

Profil

Paste
Télécharger (23,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / piwik / piwik.test @ 147616a8

1
<?php
2

    
3
/**
4
 * @file
5
 * Test file for Piwik module.
6
 */
7
class PiwikBasicTest 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('Piwik basic tests'),
19
      'description' => t('Test basic functionality of Piwik module.'),
20
      'group' => 'Piwik',
21
    );
22
  }
23

    
24
  function setUp() {
25
    parent::setUp('piwik');
26

    
27
    $permissions = array(
28
      'access administration pages',
29
      'administer piwik',
30
    );
31

    
32
    // User to set up piwik.
33
    $this->noSnippetUser = $this->drupalCreateUser($permissions);
34
    $permissions[] = 'add JS snippets for piwik';
35
    $this->admin_user = $this->drupalCreateUser($permissions);
36
    $this->drupalLogin($this->admin_user);
37
  }
38

    
39
  function testPiwikConfiguration() {
40
    // Check for setting page's presence.
41
    $this->drupalGet('admin/config/system/piwik');
42
    $this->assertRaw(t('Piwik site ID'), '[testPiwikConfiguration]: Settings page displayed.');
43

    
44
    // Check for account code validation.
45
    $edit['piwik_site_id'] = $this->randomName(2);
46
    $this->drupalPost('admin/config/system/piwik', $edit, 'Save configuration');
47
    $this->assertRaw(t('A valid Piwik site ID is an integer only.'), '[testPiwikConfiguration]: Invalid Piwik site ID number validated.');
48

    
49
    // User should have access to code snippets.
50
    $this->assertFieldByName('piwik_codesnippet_before');
51
    $this->assertFieldByName('piwik_codesnippet_after');
52
    $this->assertNoFieldByXPath("//textarea[@name='piwik_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is enabled.');
53
    $this->assertNoFieldByXPath("//textarea[@name='piwik_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/piwik');
58

    
59
    // User should *not* have access to snippets, but create fields.
60
    $this->assertFieldByName('piwik_codesnippet_before');
61
    $this->assertFieldByName('piwik_codesnippet_after');
62
    $this->assertFieldByXPath("//textarea[@name='piwik_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is disabled.');
63
    $this->assertFieldByXPath("//textarea[@name='piwik_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is disabled.');
64
  }
65

    
66
  function testPiwikPageVisibility() {
67
    $ua_code = '1';
68
    variable_set('piwik_site_id', $ua_code);
69
    variable_get('piwik_url_http', 'http://example.com/piwik/');
70
    variable_get('piwik_url_https', 'https://example.com/piwik/');
71

    
72
    // Show tracking on "every page except the listed pages".
73
    variable_set('piwik_visibility_pages', 0);
74
    // Disable tracking one "admin*" pages only.
75
    variable_set('piwik_pages', "admin\nadmin/*");
76
    // Enable tracking only for authenticated users only.
77
    variable_set('piwik_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
78

    
79
    // Check tracking code visibility.
80
    $this->drupalGet('');
81
    $this->assertRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is displayed for authenticated users.');
82

    
83
    // Test whether tracking code is not included on pages to omit.
84
    $this->drupalGet('admin');
85
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is not displayed on admin page.');
86
    $this->drupalGet('admin/config/system/piwik');
87
    // Checking for tracking code URI here, as $ua_code is displayed in the form.
88
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is not displayed on admin subpage.');
89

    
90
    // Test whether tracking code display is properly flipped.
91
    variable_set('piwik_visibility_pages', 1);
92
    $this->drupalGet('admin');
93
    $this->assertRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is displayed on admin page.');
94
    $this->drupalGet('admin/config/system/piwik');
95
    // Checking for tracking code URI here, as $ua_code is displayed in the form.
96
    $this->assertRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is displayed on admin subpage.');
97
    $this->drupalGet('');
98
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is NOT displayed on front page.');
99

    
100
    // Test whether tracking code is not display for anonymous.
101
    $this->drupalLogout();
102
    $this->drupalGet('');
103
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is NOT displayed for anonymous.');
104

    
105
    // Switch back to every page except the listed pages.
106
    variable_set('piwik_visibility_pages', 0);
107
    // Enable tracking code for all user roles.
108
    variable_set('piwik_roles', array());
109

    
110
    // Test whether 403 forbidden tracking code is shown if user has no access.
111
    $this->drupalGet('admin');
112
    $this->assertRaw('403/URL = "', '[testPiwikPageVisibility]: 403 Forbidden tracking code shown if user has no access.');
113

    
114
    // Test whether 404 not found tracking code is shown on non-existent pages.
115
    $this->drupalGet($this->randomName(64));
116
    $this->assertRaw('404/URL = "', '[testPiwikPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
117
  }
118

    
119
  function testPiwikTrackingCode() {
120
    $ua_code = '2';
121
    variable_set('piwik_site_id', $ua_code);
122
    variable_get('piwik_url_http', 'http://example.com/piwik/');
123
    variable_get('piwik_url_https', 'https://example.com/piwik/');
124

    
125
    // Show tracking code on every page except the listed pages.
126
    variable_set('piwik_visibility_pages', 0);
127
    // Enable tracking code for all user roles.
128
    variable_set('piwik_roles', array());
129

    
130
    /* Sample JS code as added to page:
131
    <script type="text/javascript">
132
    var _paq = _paq || [];
133
    (function(){
134
        var u=(("https:" == document.location.protocol) ? "https://{$PIWIK_URL}" : "http://{$PIWIK_URL}");
135
        _paq.push(['setSiteId', {$IDSITE}]);
136
        _paq.push(['setTrackerUrl', u+'piwik.php']);
137
        _paq.push(['trackPageView']);
138
        var d=document,
139
            g=d.createElement('script'),
140
            s=d.getElementsByTagName('script')[0];
141
            g.type='text/javascript';
142
            g.defer=true;
143
            g.async=true;
144
            g.src=u+'piwik.js';
145
            s.parentNode.insertBefore(g,s);
146
    })();
147
    </script>
148
    */
149

    
150
    // Test whether tracking code uses latest JS.
151
    variable_set('piwik_cache', 0);
152
    $this->drupalGet('');
153
    $this->assertRaw('u+"piwik.php"', '[testPiwikTrackingCode]: Latest tracking code used.');
154

    
155
    // Test if tracking of User ID is enabled.
156
    variable_set('piwik_trackuserid', 1);
157
    $this->drupalGet('');
158
    $this->assertRaw('_paq.push(["setUserId", ', '[testPiwikTrackingCode]: Tracking code for User ID is enabled.');
159

    
160
    // Test if tracking of User ID is disabled.
161
    variable_set('piwik_trackuserid', 0);
162
    $this->drupalGet('');
163
    $this->assertNoRaw('_paq.push(["setUserId", ', '[testPiwikTrackingCode]: Tracking code for User ID is disabled.');
164

    
165
    // Test whether single domain tracking is active.
166
    $this->drupalGet('');
167
    $this->assertNoRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: Single domain tracking is active.');
168

    
169
    // Enable "One domain with multiple subdomains".
170
    variable_set('piwik_domain_mode', 1);
171
    $this->drupalGet('');
172

    
173
    // Test may run on localhost, an ipaddress or real domain name.
174
    // TODO: Workaround to run tests successfully. This feature cannot tested reliable.
175
    global $cookie_domain;
176
    if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
177
      $this->assertRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: One domain with multiple subdomains is active on real host.');
178
    }
179
    else {
180
      // Special cases, Localhost and IP addresses don't show 'setCookieDomain'.
181
      $this->assertNoRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
182
    }
183

    
184
    // Test whether the BEFORE and AFTER code is added to the tracker.
185
    variable_set('piwik_codesnippet_before', '_paq.push(["setLinkTrackingTimer", 250]);');
186
    variable_set('piwik_codesnippet_after', '_paq.push(["t2.setSiteId", 2]);_gaq.push(["t2.trackPageView"]);');
187
    $this->drupalGet('');
188
    $this->assertRaw('setLinkTrackingTimer', '[testPiwikTrackingCode]: Before codesnippet has been found with "setLinkTrackingTimer" set.');
189
    $this->assertRaw('t2.trackPageView', '[testPiwikTrackingCode]: After codesnippet with "t2" tracker has been found.');
190
  }
191

    
192
}
193

    
194
class PiwikCustomVariablesTest extends DrupalWebTestCase {
195

    
196
  public static function getInfo() {
197
    return array(
198
      'name' => t('Piwik Custom Variables tests'),
199
      'description' => t('Test custom variables functionality of Piwik module.'),
200
      'group' => 'Piwik',
201
      'dependencies' => array('token'),
202
    );
203
  }
204

    
205
  function setUp() {
206
    parent::setUp('piwik', 'token');
207

    
208
    $permissions = array(
209
      'access administration pages',
210
      'administer piwik',
211
    );
212

    
213
    // User to set up piwik.
214
    $this->admin_user = $this->drupalCreateUser($permissions);
215
  }
216

    
217
  function testPiwikCustomVariables() {
218
    $ua_code = '3';
219
    variable_set('piwik_site_id', $ua_code);
220

    
221
    // Basic test if the feature works.
222
    $custom_vars = array(
223
      'slots' => array(
224
        1 => array(
225
          'slot' => 1,
226
          'name' => 'Foo 1',
227
          'value' => 'Bar 1',
228
          'scope' => 3,
229
        ),
230
        2 => array(
231
          'slot' => 2,
232
          'name' => 'Foo 2',
233
          'value' => 'Bar 2',
234
          'scope' => 2,
235
        ),
236
        3 => array(
237
          'slot' => 3,
238
          'name' => 'Foo 3',
239
          'value' => 'Bar 3',
240
          'scope' => 3,
241
        ),
242
        4 => array(
243
          'slot' => 4,
244
          'name' => 'Foo 4',
245
          'value' => 'Bar 4',
246
          'scope' => 2,
247
        ),
248
        5 => array(
249
          'slot' => 5,
250
          'name' => 'Foo 5',
251
          'value' => 'Bar 5',
252
          'scope' => 1,
253
        ),
254
      )
255
    );
256
    variable_set('piwik_custom_var', $custom_vars);
257
    $this->drupalGet('');
258

    
259
    foreach ($custom_vars['slots'] as $slot) {
260
      $this->assertRaw("_paq.push(['setCustomVariable', " . $slot['slot'] . ", \"" . $slot['name'] . "\", \"" . $slot['value'] . "\", " . $slot['scope'] . "]);", '[testPiwikCustomVariables]: setCustomVariable ' . $slot['slot'] . ' is shown.');
261
    }
262

    
263
    // Test whether tokens are replaced in custom variable names.
264
    $site_slogan = $this->randomName(16);
265
    variable_set('site_slogan', $site_slogan);
266

    
267
    $custom_vars = array(
268
      'slots' => array(
269
        1 => array(
270
          'slot' => 1,
271
          'name' => 'Name: [site:slogan]',
272
          'value' => 'Value: [site:slogan]',
273
          'scope' => 3,
274
        ),
275
        2 => array(
276
          'slot' => 2,
277
          'name' => '',
278
          'value' => $this->randomName(16),
279
          'scope' => 1,
280
        ),
281
        3 => array(
282
          'slot' => 3,
283
          'name' => $this->randomName(16),
284
          'value' => '',
285
          'scope' => 2,
286
        ),
287
        4 => array(
288
          'slot' => 4,
289
          'name' => '',
290
          'value' => '',
291
          'scope' => 3,
292
        ),
293
        5 => array(
294
          'slot' => 5,
295
          'name' => '',
296
          'value' => '',
297
          'scope' => 3,
298
        ),
299
      )
300
    );
301
    variable_set('piwik_custom_var', $custom_vars);
302
    $this->verbose('<pre>' . print_r($custom_vars, TRUE) . '</pre>');
303

    
304
    $this->drupalGet('');
305
    $this->assertRaw("_paq.push(['setCustomVariable', 1, \"Name: $site_slogan\", \"Value: $site_slogan\", 3]", '[testPiwikCustomVariables]: Tokens have been replaced in custom variable.');
306
    $this->assertNoRaw("_paq.push(['setCustomVariable', 2,", '[testPiwikCustomVariables]: Value with empty name is not shown.');
307
    $this->assertNoRaw("_paq.push(['setCustomVariable', 3,", '[testPiwikCustomVariables]: Name with empty value is not shown.');
308
    $this->assertNoRaw("_paq.push(['setCustomVariable', 4,", '[testPiwikCustomVariables]: Empty name and value is not shown.');
309
    $this->assertNoRaw("_paq.push(['setCustomVariable', 5,", '[testPiwikCustomVariables]: Empty name and value is not shown.');
310
  }
311
}
312

    
313
/**
314
 * Test custom url functionality of Google Analytics module.
315
 */
316
class PiwikCustomUrls extends DrupalWebTestCase {
317

    
318
  public static function getInfo() {
319
    return array(
320
      'name' => 'Piwik custom url tests',
321
      'description' => 'Test custom url functionality of Piwik module.',
322
      'group' => 'Piwik',
323
    );
324
  }
325

    
326
  function setUp() {
327
    parent::setUp('piwik');
328

    
329
    $permissions = array(
330
      'access administration pages',
331
      'administer piwik',
332
    );
333

    
334
    // User to set up piwik.
335
    $this->admin_user = $this->drupalCreateUser($permissions);
336
  }
337

    
338
  /**
339
   * Tests if user password page urls are overridden.
340
   */
341
  public function testPiwikUserPasswordPage() {
342
    $base_path = base_path();
343
    $ua_code = '1';
344
    variable_set('piwik_site_id', $ua_code);
345
    variable_get('piwik_url_http', 'http://example.com/piwik/');
346
    variable_get('piwik_url_https', 'https://example.com/piwik/');
347

    
348
    $this->drupalGet('user/password', array('query' => array('name' => 'foo')));
349
    $this->assertRaw('_paq.push(["setCustomUrl", ' . drupal_json_encode(url('user/password')) . ']);');
350

    
351
    $this->drupalGet('user/password', array('query' => array('name' => 'foo@example.com')));
352
    $this->assertRaw('_paq.push(["setCustomUrl", ' . drupal_json_encode(url('user/password')) . ']);');
353

    
354
    $this->drupalGet('user/password');
355
    $this->assertNoRaw('_paq.push(["setCustomUrl", "', '[testPiwikCustomUrls]: Custom url not set.');
356
  }
357

    
358
}
359

    
360
class PiwikStatusMessagesTest extends DrupalWebTestCase {
361

    
362
  public static function getInfo() {
363
    return array(
364
      'name' => 'Piwik status messages tests',
365
      'description' => 'Test status messages functionality of Piwik module.',
366
      'group' => 'Piwik',
367
    );
368
  }
369

    
370
  function setUp() {
371
    parent::setUp('piwik');
372

    
373
    $permissions = array(
374
      'access administration pages',
375
      'administer piwik',
376
    );
377

    
378
    // User to set up piwik.
379
    $this->admin_user = $this->drupalCreateUser($permissions);
380
  }
381

    
382
  function testPiwikStatusMessages() {
383
    $ua_code = '1';
384
    variable_set('piwik_site_id', $ua_code);
385

    
386
    // Enable logging of errors only.
387
    variable_set('piwik_trackmessages', array('error' => 'error'));
388

    
389
    $this->drupalPost('user/login', array(), t('Log in'));
390
    $this->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Username field is required."]);', '[testPiwikStatusMessages]: trackEvent "Username field is required." is shown.');
391
    $this->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Password field is required."]);', '[testPiwikStatusMessages]: trackEvent "Password field is required." is shown.');
392

    
393
    // @todo: investigate why drupal_set_message() fails.
394
    //drupal_set_message('Example status message.', 'status');
395
    //drupal_set_message('Example warning message.', 'warning');
396
    //drupal_set_message('Example error message.', 'error');
397
    //drupal_set_message('Example error <em>message</em> with html tags and <a href="http://example.com/">link</a>.', 'error');
398
    //$this->drupalGet('');
399
    //$this->assertNoRaw('_paq.push(["trackEvent", "Messages", "Status message", "Example status message."]);', '[testPiwikStatusMessages]: Example status message is not enabled for tracking.');
400
    //$this->assertNoRaw('_paq.push(["trackEvent", "Messages", "Warning message", "Example warning message."]);', '[testPiwikStatusMessages]: Example warning message is not enabled for tracking.');
401
    //$this->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Example error message."]);', '[testPiwikStatusMessages]: Example error message is shown.');
402
    //$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.');
403
  }
404
}
405

    
406
class PiwikRolesTest extends DrupalWebTestCase {
407

    
408
  public static function getInfo() {
409
    return array(
410
      'name' => t('Piwik role tests'),
411
      'description' => t('Test roles functionality of Piwik module.'),
412
      'group' => 'Piwik',
413
    );
414
  }
415

    
416
  function setUp() {
417
    parent::setUp('piwik');
418

    
419
    $permissions = array(
420
      'access administration pages',
421
      'administer piwik',
422
    );
423

    
424
    // User to set up piwik.
425
    $this->admin_user = $this->drupalCreateUser($permissions);
426
  }
427

    
428
  function testPiwikRolesTracking() {
429
    $ua_code = '1';
430
    variable_set('piwik_site_id', $ua_code);
431
    variable_get('piwik_url_http', 'http://example.com/piwik/');
432
    variable_get('piwik_url_https', 'https://example.com/piwik/');
433

    
434
    // Test if the default settings are working as expected.
435

    
436
    // Add to the selected roles only.
437
    variable_set('piwik_visibility_roles', 0);
438
    // Enable tracking for all users.
439
    variable_set('piwik_roles', array());
440

    
441
    // Check tracking code visibility.
442
    $this->drupalGet('');
443
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.');
444
    $this->drupalGet('admin');
445
    $this->assertRaw('"403/URL = "', '[testPiwikRoleVisibility]: 403 Forbidden tracking code is displayed for anonymous users in admin section with default settings.');
446

    
447
    $this->drupalLogin($this->admin_user);
448

    
449
    $this->drupalGet('');
450
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.');
451
    $this->drupalGet('admin');
452
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.');
453

    
454
    // Test if the non-default settings are working as expected.
455

    
456
    // Enable tracking only for authenticated users.
457
    variable_set('piwik_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
458

    
459
    $this->drupalGet('');
460
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.');
461

    
462
    $this->drupalLogout();
463
    $this->drupalGet('');
464
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.');
465

    
466
    // Add to every role except the selected ones.
467
    variable_set('piwik_visibility_roles', 1);
468
    // Enable tracking for all users.
469
    variable_set('piwik_roles', array());
470

    
471
    // Check tracking code visibility.
472
    $this->drupalGet('');
473
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.');
474
    $this->drupalGet('admin');
475
    $this->assertRaw('"403/URL = "', '[testPiwikRoleVisibility]: 403 Forbidden tracking code is shown for anonymous users if every role except the selected ones is selected.');
476

    
477
    $this->drupalLogin($this->admin_user);
478

    
479
    $this->drupalGet('');
480
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.');
481
    $this->drupalGet('admin');
482
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.');
483

    
484
    // Disable tracking for authenticated users.
485
    variable_set('piwik_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
486

    
487
    $this->drupalGet('');
488
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.');
489
    $this->drupalGet('admin');
490
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.');
491

    
492
    $this->drupalLogout();
493
    $this->drupalGet('');
494
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.');
495
  }
496

    
497
}
498

    
499
class PiwikPhpFilterTest extends DrupalWebTestCase {
500

    
501
  public static function getInfo() {
502
    return array(
503
      'name' => 'Piwik php filter tests',
504
      'description' => 'Test php filter functionality of Piwik module.',
505
      'group' => 'Piwik',
506
    );
507
  }
508

    
509
  function setUp() {
510
    parent::setUp('piwik', 'php');
511

    
512
    // Administrator with all permissions.
513
    $permissions_admin_user = array(
514
      'access administration pages',
515
      'administer piwik',
516
      'use PHP for tracking visibility',
517
    );
518
    $this->admin_user = $this->drupalCreateUser($permissions_admin_user);
519

    
520
    // Administrator who cannot configure tracking visibility with PHP.
521
    $permissions_delegated_admin_user = array(
522
      'access administration pages',
523
      'administer piwik',
524
    );
525
    $this->delegated_admin_user = $this->drupalCreateUser($permissions_delegated_admin_user);
526
  }
527

    
528
  function testPiwikPhpFilter() {
529
    $ua_code = '1';
530
    $this->drupalLogin($this->admin_user);
531

    
532
    $edit = array();
533
    $edit['piwik_site_id'] = $ua_code;
534
    $edit['piwik_url_http'] = 'http://example.com/piwik/';
535
    $edit['piwik_url_https'] = 'https://example.com/piwik/';
536
    $edit['piwik_url_skiperror'] = TRUE; // Required for testing only.
537
    $edit['piwik_visibility_pages'] = 2;
538
    $edit['piwik_pages'] = '<?php return 0; ?>';
539
    $this->drupalPost('admin/config/system/piwik', $edit, t('Save configuration'));
540

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

    
545
    // Check tracking code visibility.
546
    variable_set('piwik_pages', '<?php return TRUE; ?>');
547
    $this->drupalGet('');
548
    $this->assertRaw('u+"piwik.php"', '[testPiwikPhpFilter]: Tracking is displayed on frontpage page.');
549
    $this->drupalGet('admin');
550
    $this->assertRaw('u+"piwik.php"', '[testPiwikPhpFilter]: Tracking is displayed on admin page.');
551

    
552
    variable_set('piwik_pages', '<?php return FALSE; ?>');
553
    $this->drupalGet('');
554
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikPhpFilter]: Tracking is not displayed on frontpage page.');
555

    
556
    // Test administration form.
557
    variable_set('piwik_pages', '<?php return TRUE; ?>');
558
    $this->drupalGet('admin/config/system/piwik');
559
    $this->assertRaw(t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'), '[testPiwikPhpFilter]: Permission to administer PHP for tracking visibility.');
560
    $this->assertRaw(check_plain('<?php return TRUE; ?>'), '[testPiwikPhpFilter]: PHP code snippted is displayed.');
561

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

    
568
    // Set a different value and verify that this is still the same after the post.
569
    variable_set('piwik_pages', '<?php return 0; ?>');
570

    
571
    $edit = array();
572
    $edit['piwik_site_id'] = $ua_code;
573
    $edit['piwik_url_http'] = 'http://example.com/piwik/';
574
    $edit['piwik_url_https'] = 'https://example.com/piwik/';
575
    $edit['piwik_url_skiperror'] = TRUE; // Required for testing only.
576
    $this->drupalPost('admin/config/system/piwik', $edit, t('Save configuration'));
577

    
578
    // Compare saved setting with posted setting.
579
    $piwik_visibility_pages = variable_get('piwik_visibility_pages', 0);
580
    $piwik_pages = variable_get('piwik_pages', $this->randomName(8));
581
    $this->assertEqual(2, $piwik_visibility_pages, '[testPiwikPhpFilter]: Pages on which this PHP code returns TRUE is selected.');
582
    $this->assertEqual('<?php return 0; ?>', $piwik_pages, '[testPiwikPhpFilter]: PHP code snippet is intact.');
583
  }
584

    
585
}