Projet

Général

Profil

Paste
Télécharger (16,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / piwik / piwik.test @ e9f59589

1
<?php
2

    
3
/**
4
 * @file
5
 * Test file for Piwik module.
6
 */
7
class PiwikBasicTest extends DrupalWebTestCase {
8

    
9
  public static function getInfo() {
10
    return array(
11
      'name' => t('Piwik basic tests'),
12
      'description' => t('Test basic functionality of Piwik module.'),
13
      'group' => 'Piwik',
14
    );
15
  }
16

    
17
  function setUp() {
18
    parent::setUp('piwik');
19

    
20
    $permissions = array(
21
      'access administration pages',
22
      'administer piwik',
23
    );
24

    
25
    // User to set up piwik.
26
    $this->admin_user = $this->drupalCreateUser($permissions);
27
    $this->drupalLogin($this->admin_user);
28
  }
29

    
30
  function testPiwikConfiguration() {
31
    // Check for setting page's presence.
32
    $this->drupalGet('admin/config/system/piwik');
33
    $this->assertRaw(t('Piwik site ID'), '[testPiwikConfiguration]: Settings page displayed.');
34

    
35
    // Check for account code validation.
36
    $edit['piwik_site_id'] = $this->randomName(2);
37
    $this->drupalPost('admin/config/system/piwik', $edit, 'Save configuration');
38
    $this->assertRaw(t('A valid Piwik site ID is an integer only.'), '[testPiwikConfiguration]: Invalid Piwik site ID number validated.');
39
  }
40

    
41
  function testPiwikPageVisibility() {
42
    $ua_code = '1';
43
    variable_set('piwik_site_id', $ua_code);
44
    variable_get('piwik_url_http', 'http://example.com/piwik/');
45
    variable_get('piwik_url_https', 'https://example.com/piwik/');
46

    
47
    // Show tracking on "every page except the listed pages".
48
    variable_set('piwik_visibility_pages', 0);
49
    // Disable tracking one "admin*" pages only.
50
    variable_set('piwik_pages', "admin\nadmin/*");
51
    // Enable tracking only for authenticated users only.
52
    variable_set('piwik_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
53

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

    
58
    // Test whether tracking code is not included on pages to omit.
59
    $this->drupalGet('admin');
60
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is not displayed on admin page.');
61
    $this->drupalGet('admin/config/system/piwik');
62
    // Checking for tracking code URI here, as $ua_code is displayed in the form.
63
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is not displayed on admin subpage.');
64

    
65
    // Test whether tracking code display is properly flipped.
66
    variable_set('piwik_visibility_pages', 1);
67
    $this->drupalGet('admin');
68
    $this->assertRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is displayed on admin page.');
69
    $this->drupalGet('admin/config/system/piwik');
70
    // Checking for tracking code URI here, as $ua_code is displayed in the form.
71
    $this->assertRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is displayed on admin subpage.');
72
    $this->drupalGet('');
73
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is NOT displayed on front page.');
74

    
75
    // Test whether tracking code is not display for anonymous.
76
    $this->drupalLogout();
77
    $this->drupalGet('');
78
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikPageVisibility]: Tracking code is NOT displayed for anonymous.');
79

    
80
    // Switch back to every page except the listed pages.
81
    variable_set('piwik_visibility_pages', 0);
82
    // Enable tracking code for all user roles.
83
    variable_set('piwik_roles', array());
84

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

    
89
    // Test whether 404 not found tracking code is shown on non-existent pages.
90
    $this->drupalGet($this->randomName(64));
91
    $this->assertRaw('"404/URL = "', '[testPiwikPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
92
  }
93

    
94
  function testPiwikTrackingCode() {
95
    $ua_code = '2';
96
    variable_set('piwik_site_id', $ua_code);
97
    variable_get('piwik_url_http', 'http://example.com/piwik/');
98
    variable_get('piwik_url_https', 'https://example.com/piwik/');
99

    
100
    // Show tracking code on every page except the listed pages.
101
    variable_set('piwik_visibility_pages', 0);
102
    // Enable tracking code for all user roles.
103
    variable_set('piwik_roles', array());
104

    
105
    /* Sample JS code as added to page:
106
    <script type="text/javascript">
107
    var _paq = _paq || [];
108
    (function(){
109
        var u=(("https:" == document.location.protocol) ? "https://{$PIWIK_URL}" : "http://{$PIWIK_URL}");
110
        _paq.push(['setSiteId', {$IDSITE}]);
111
        _paq.push(['setTrackerUrl', u+'piwik.php']);
112
        _paq.push(['trackPageView']);
113
        var d=document,
114
            g=d.createElement('script'),
115
            s=d.getElementsByTagName('script')[0];
116
            g.type='text/javascript';
117
            g.defer=true;
118
            g.async=true;
119
            g.src=u+'piwik.js';
120
            s.parentNode.insertBefore(g,s);
121
    })();
122
    </script>
123
    */
124

    
125
    // Test whether tracking code uses latest JS.
126
    variable_set('piwik_cache', 0);
127
    $this->drupalGet('');
128
    $this->assertRaw('u+"piwik.php"', '[testPiwikTrackingCode]: Latest tracking code used.');
129

    
130
    // Test whether single domain tracking is active.
131
    $this->drupalGet('');
132
    $this->assertNoRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: Single domain tracking is active.');
133

    
134
    // Enable "One domain with multiple subdomains".
135
    variable_set('piwik_domain_mode', 1);
136
    $this->drupalGet('');
137

    
138
    // Test may run on localhost, an ipaddress or real domain name.
139
    // TODO: Workaround to run tests successfully. This feature cannot tested reliable.
140
    global $cookie_domain;
141
    if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
142
      $this->assertRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: One domain with multiple subdomains is active on real host.');
143
    }
144
    else {
145
      // Special cases, Localhost and IP addresses don't show 'setCookieDomain'.
146
      $this->assertNoRaw('_paq.push(["setCookieDomain"', '[testPiwikTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
147
    }
148

    
149
    // Test whether the BEFORE and AFTER code is added to the tracker.
150
    variable_set('piwik_codesnippet_before', '_paq.push(["setLinkTrackingTimer", 250]);');
151
    variable_set('piwik_codesnippet_after', '_paq.push(["t2.setSiteId", 2]);_gaq.push(["t2.trackPageView"]);');
152
    $this->drupalGet('');
153
    $this->assertRaw('setLinkTrackingTimer', '[testPiwikTrackingCode]: Before codesnippet has been found with "setLinkTrackingTimer" set.');
154
    $this->assertRaw('t2.trackPageView', '[testPiwikTrackingCode]: After codesnippet with "t2" tracker has been found.');
155
  }
156

    
157
}
158

    
159
class PiwikCustomVariablesTest extends DrupalWebTestCase {
160

    
161
  public static function getInfo() {
162
    return array(
163
      'name' => t('Piwik Custom Variables tests'),
164
      'description' => t('Test custom variables functionality of Piwik module.'),
165
      'group' => 'Piwik',
166
      'dependencies' => array('token'),
167
    );
168
  }
169

    
170
  function setUp() {
171
    parent::setUp('piwik', 'token');
172

    
173
    $permissions = array(
174
      'access administration pages',
175
      'administer piwik',
176
    );
177

    
178
    // User to set up piwik.
179
    $this->admin_user = $this->drupalCreateUser($permissions);
180
  }
181

    
182
  function testPiwikCustomVariables() {
183
    $ua_code = '3';
184
    variable_set('piwik_site_id', $ua_code);
185

    
186
    // Basic test if the feature works.
187
    $custom_vars = array(
188
      'slots' => array(
189
        1 => array(
190
          'slot' => 1,
191
          'name' => 'Foo 1',
192
          'value' => 'Bar 1',
193
          'scope' => 3,
194
        ),
195
        2 => array(
196
          'slot' => 2,
197
          'name' => 'Foo 2',
198
          'value' => 'Bar 2',
199
          'scope' => 2,
200
        ),
201
        3 => array(
202
          'slot' => 3,
203
          'name' => 'Foo 3',
204
          'value' => 'Bar 3',
205
          'scope' => 3,
206
        ),
207
        4 => array(
208
          'slot' => 4,
209
          'name' => 'Foo 4',
210
          'value' => 'Bar 4',
211
          'scope' => 2,
212
        ),
213
        5 => array(
214
          'slot' => 5,
215
          'name' => 'Foo 5',
216
          'value' => 'Bar 5',
217
          'scope' => 1,
218
        ),
219
      )
220
    );
221
    variable_set('piwik_custom_var', $custom_vars);
222
    $this->drupalGet('');
223

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

    
228
    // Test whether tokens are replaced in custom variable names.
229
    $site_slogan = $this->randomName(16);
230
    variable_set('site_slogan', $site_slogan);
231

    
232
    $custom_vars = array(
233
      'slots' => array(
234
        1 => array(
235
          'slot' => 1,
236
          'name' => 'Name: [site:slogan]',
237
          'value' => 'Value: [site:slogan]',
238
          'scope' => 3,
239
        ),
240
        2 => array(
241
          'slot' => 2,
242
          'name' => '',
243
          'value' => $this->randomName(16),
244
          'scope' => 1,
245
        ),
246
        3 => array(
247
          'slot' => 3,
248
          'name' => $this->randomName(16),
249
          'value' => '',
250
          'scope' => 2,
251
        ),
252
        4 => array(
253
          'slot' => 4,
254
          'name' => '',
255
          'value' => '',
256
          'scope' => 3,
257
        ),
258
        5 => array(
259
          'slot' => 5,
260
          'name' => '',
261
          'value' => '',
262
          'scope' => 3,
263
        ),
264
      )
265
    );
266
    variable_set('piwik_custom_var', $custom_vars);
267
    $this->verbose('<pre>' . print_r($custom_vars, TRUE) . '</pre>');
268

    
269
    $this->drupalGet('');
270
    $this->assertRaw("_paq.push(['setCustomVariable', 1, \"Name: $site_slogan\", \"Value: $site_slogan\", 3]", '[testPiwikCustomVariables]: Tokens have been replaced in custom variable.');
271
    $this->assertNoRaw("_paq.push(['setCustomVariable', 2,", '[testPiwikCustomVariables]: Value with empty name is not shown.');
272
    $this->assertNoRaw("_paq.push(['setCustomVariable', 3,", '[testPiwikCustomVariables]: Name with empty value is not shown.');
273
    $this->assertNoRaw("_paq.push(['setCustomVariable', 4,", '[testPiwikCustomVariables]: Empty name and value is not shown.');
274
    $this->assertNoRaw("_paq.push(['setCustomVariable', 5,", '[testPiwikCustomVariables]: Empty name and value is not shown.');
275
  }
276
}
277

    
278
class PiwikStatusMessagesTest extends DrupalWebTestCase {
279

    
280
  public static function getInfo() {
281
    return array(
282
      'name' => 'Piwik status messages tests',
283
      'description' => 'Test status messages functionality of Piwik module.',
284
      'group' => 'Piwik',
285
    );
286
  }
287

    
288
  function setUp() {
289
    parent::setUp('piwik');
290

    
291
    $permissions = array(
292
      'access administration pages',
293
      'administer piwik',
294
    );
295

    
296
    // User to set up piwik.
297
    $this->admin_user = $this->drupalCreateUser($permissions);
298
  }
299

    
300
  function testPiwikStatusMessages() {
301
    $ua_code = '1';
302
    variable_set('piwik_site_id', $ua_code);
303

    
304
    // Enable logging of errors only.
305
    variable_set('piwik_trackmessages', array('error' => 'error'));
306

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

    
311
    // @todo: investigate why drupal_set_message() fails.
312
    //drupal_set_message('Example status message.', 'status');
313
    //drupal_set_message('Example warning message.', 'warning');
314
    //drupal_set_message('Example error message.', 'error');
315
    //drupal_set_message('Example error <em>message</em> with html tags and <a href="http://example.com/">link</a>.', 'error');
316
    //$this->drupalGet('');
317
    //$this->assertNoRaw('_paq.push(["trackEvent", "Messages", "Status message", "Example status message."]);', '[testPiwikStatusMessages]: Example status message is not enabled for tracking.');
318
    //$this->assertNoRaw('_paq.push(["trackEvent", "Messages", "Warning message", "Example warning message."]);', '[testPiwikStatusMessages]: Example warning message is not enabled for tracking.');
319
    //$this->assertRaw('_paq.push(["trackEvent", "Messages", "Error message", "Example error message."]);', '[testPiwikStatusMessages]: Example error message is shown.');
320
    //$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.');
321
  }
322
}
323

    
324
class PiwikRolesTest extends DrupalWebTestCase {
325

    
326
  public static function getInfo() {
327
    return array(
328
      'name' => t('Piwik role tests'),
329
      'description' => t('Test roles functionality of Piwik module.'),
330
      'group' => 'Piwik',
331
    );
332
  }
333

    
334
  function setUp() {
335
    parent::setUp('piwik');
336

    
337
    $permissions = array(
338
      'access administration pages',
339
      'administer piwik',
340
    );
341

    
342
    // User to set up piwik.
343
    $this->admin_user = $this->drupalCreateUser($permissions);
344
  }
345

    
346
  function testPiwikRolesTracking() {
347
    $ua_code = '1';
348
    variable_set('piwik_site_id', $ua_code);
349
    variable_get('piwik_url_http', 'http://example.com/piwik/');
350
    variable_get('piwik_url_https', 'https://example.com/piwik/');
351

    
352
    // Test if the default settings are working as expected.
353

    
354
    // Add to the selected roles only.
355
    variable_set('piwik_visibility_roles', 0);
356
    // Enable tracking for all users.
357
    variable_set('piwik_roles', array());
358

    
359
    // Check tracking code visibility.
360
    $this->drupalGet('');
361
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.');
362
    $this->drupalGet('admin');
363
    $this->assertRaw('"403/URL = "', '[testPiwikRoleVisibility]: 403 Forbidden tracking code is displayed for anonymous users in admin section with default settings.');
364

    
365
    $this->drupalLogin($this->admin_user);
366

    
367
    $this->drupalGet('');
368
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.');
369
    $this->drupalGet('admin');
370
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.');
371

    
372
    // Test if the non-default settings are working as expected.
373

    
374
    // Enable tracking only for authenticated users.
375
    variable_set('piwik_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
376

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

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

    
384
    // Add to every role except the selected ones.
385
    variable_set('piwik_visibility_roles', 1);
386
    // Enable tracking for all users.
387
    variable_set('piwik_roles', array());
388

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

    
395
    $this->drupalLogin($this->admin_user);
396

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

    
402
    // Disable tracking for authenticated users.
403
    variable_set('piwik_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
404

    
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

    
410
    $this->drupalLogout();
411
    $this->drupalGet('');
412
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.');
413
  }
414

    
415
}