Projet

Général

Profil

Paste
Télécharger (14,3 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / piwik / piwik.test @ a5572547

1 85ad3d82 Assos Assos
<?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 PiwikRolesTest extends DrupalWebTestCase {
279
280
  public static function getInfo() {
281
    return array(
282
      'name' => t('Piwik role tests'),
283
      'description' => t('Test roles 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 testPiwikRolesTracking() {
301
    $ua_code = '1';
302
    variable_set('piwik_site_id', $ua_code);
303
    variable_get('piwik_url_http', 'http://example.com/piwik/');
304
    variable_get('piwik_url_https', 'https://example.com/piwik/');
305
306
    // Test if the default settings are working as expected.
307
308
    // Add to the selected roles only.
309
    variable_set('piwik_visibility_roles', 0);
310
    // Enable tracking for all users.
311
    variable_set('piwik_roles', array());
312
313
    // Check tracking code visibility.
314
    $this->drupalGet('');
315
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.');
316
    $this->drupalGet('admin');
317
    $this->assertRaw('"403/URL = "', '[testPiwikRoleVisibility]: 403 Forbidden tracking code is displayed for anonymous users in admin section with default settings.');
318
319
    $this->drupalLogin($this->admin_user);
320
321
    $this->drupalGet('');
322
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.');
323
    $this->drupalGet('admin');
324
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.');
325
326
    // Test if the non-default settings are working as expected.
327
328
    // Enable tracking only for authenticated users.
329
    variable_set('piwik_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
330
331
    $this->drupalGet('');
332
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.');
333
334
    $this->drupalLogout();
335
    $this->drupalGet('');
336
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.');
337
338
    // Add to every role except the selected ones.
339
    variable_set('piwik_visibility_roles', 1);
340
    // Enable tracking for all users.
341
    variable_set('piwik_roles', array());
342
343
    // Check tracking code visibility.
344
    $this->drupalGet('');
345
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.');
346
    $this->drupalGet('admin');
347
    $this->assertRaw('"403/URL = "', '[testPiwikRoleVisibility]: 403 Forbidden tracking code is shown for anonymous users if every role except the selected ones is selected.');
348
349
    $this->drupalLogin($this->admin_user);
350
351
    $this->drupalGet('');
352
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.');
353
    $this->drupalGet('admin');
354
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.');
355
356
    // Disable tracking for authenticated users.
357
    variable_set('piwik_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));
358
359
    $this->drupalGet('');
360
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.');
361
    $this->drupalGet('admin');
362
    $this->assertNoRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.');
363
364
    $this->drupalLogout();
365
    $this->drupalGet('');
366
    $this->assertRaw('u+"piwik.php"', '[testPiwikRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.');
367
  }
368
369
}