summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/common.test
blob: b7ebac4ff26d19354667820d88ee9c36bcbb4266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<?php
// $Id$

class CommonFormatSizeTestCase extends DrupalWebTestCase {

  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Format size test'),
      'description' => t('Parse a predefined amount of bytes and compare the output with the expected value.'),
      'group' => t('System')
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    $this->exact_test_cases = array(
      '1 byte'    => 1, // byte
      '1 KB'      => 1000, // kilobyte
      '1 MB'      => 1000000, // megabyte
      '1 GB'      => 1000000000, // gigabyte
      '1 TB'      => 1000000000000, // terabyte
      '1 PB'      => 1000000000000000, // petabyte
      '1 EB'      => 1000000000000000000, // exabyte
      '1 ZB'      => 1000000000000000000000, // zettabyte
      '1 YB'      => 1000000000000000000000000, // yottabyte
    );
    $this->rounded_test_cases = array(
      '2 bytes'   => 2, // bytes
      '1 MB'      => 999999, // 1 MB (not 1000 kilobyte!)
      '3.62 MB'   => 3623651, // megabytes
      '67.23 PB'  => 67234178751368124, // petabytes
      '235.35 YB' => 235346823821125814962843827, // yottabytes
    );
    parent::setUp();
  }

  /**
   * testCommonFormatSize
   */
  function testCommonFormatSize() {
    foreach (array($this->exact_test_cases, $this->rounded_test_cases) as $test_cases) {
      foreach ($test_cases as $expected => $size) {
        $this->assertTrue(
          ($result = format_size($size, NULL)) == $expected,
          $expected . " == " . $result . " (" . $size . " bytes) %s"
        );
      }
    }
  }
}

/**
 * Test drupal_explode_tags() and drupal_implode_tags().
 */
class DrupalTagsHandlingTestCase extends DrupalWebTestCase {
  var $validTags = array(
    'Drupal' => 'Drupal',
    'Drupal with some spaces' => 'Drupal with some spaces',
    '"Legendary Drupal mascot of doom: ""Druplicon"""' => 'Legendary Drupal mascot of doom: "Druplicon"',
    '"Drupal, although it rhymes with sloopal, is as awesome as a troopal!"' => 'Drupal, although it rhymes with sloopal, is as awesome as a troopal!',
  );

  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Drupal tags handling'),
      'description' => t("Performs tests on Drupal's handling of tags, both explosion and implosion tactics used."),
      'group' => t('System')
    );
  }

  /**
   * Explode a series of tags.
   */
  function testDrupalExplodeTags() {
    $string = implode(', ', array_keys($this->validTags));
    $tags = drupal_explode_tags($string);
    $this->assertTags($tags);
  }

  /**
   * Implode a series of tags.
   */
  function testDrupalImplodeTags() {
    $tags = array_values($this->validTags);
    // Let's explode and implode to our heart's content.
    for ($i = 0; $i < 10; $i++) {
      $string = drupal_implode_tags($tags);
      $tags = drupal_explode_tags($string);
    }
    $this->assertTags($tags);
  }

  /**
   * Helper function: asserts that the ending array of tags is what we wanted.
   */
  function assertTags($tags) {
    $original = $this->validTags;
    foreach ($tags as $tag) {
      $key = array_search($tag, $original);
      $this->_assert($key !== FALSE, t('Make sure tag %tag shows up in the final tags array (originally %original)', array('%tag' => $tag, '%original' => $key)));
      unset($original[$key]);
    }
    foreach ($original as $leftover) {
      $this->_assert(FALSE, t('Leftover tag %leftover was left over.', array('%leftover' => $leftover)));
    }
  }
}

/**
 * Test the Drupal CSS system.
 */
class CascadingStylesheetsTestCase extends DrupalWebTestCase {
  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Cascading stylesheets'),
      'description' => t('Tests adding various cascading stylesheets to the page.'),
      'group' => t('System')
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp();
    // Reset drupal_add_css() before each test.
    drupal_add_css(NULL, NULL, TRUE);
  }

  /**
   * Check default stylesheets as empty.
   */
  function testDefault() {
    $this->assertEqual(array(), drupal_add_css(), t('Default CSS is empty.'));
  }

  /**
   * Tests adding a file stylesheet.
   */
  function testAddFile() {
    $path = drupal_get_path('module', 'simpletest') . '/simpletest.css';
    $css = drupal_add_css($path);
    $this->assertEqual($css['all']['module'][$path], TRUE, t('Adding a CSS file caches it properly.'));
  }

  /**
   * Tests rendering the stylesheets.
   */
  function testRenderFile() {
    $css = drupal_get_path('module', 'simpletest') . '/simpletest.css';
    drupal_add_css($css);
    $this->assertTrue(strpos(drupal_get_css(), $css) > 0, t('Rendered CSS includes the added stylesheet.'));
  }
}

/**
 * Test drupal_http_request().
 */
class DrupalHTTPRequestTestCase extends DrupalWebTestCase {
  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Drupal HTTP request'),
      'description' => t("Performs tests on Drupal's HTTP request mechanism."),
      'group' => t('System')
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp('system_test');
  }

  function testDrupalHTTPRequest() {
    // Parse URL schema.
    $missing_scheme = drupal_http_request('example.com/path');
    $this->assertEqual($missing_scheme->error, 'missing schema', t('Returned with "missing schema" error.'));

    $unable_to_parse = drupal_http_request('http:///path');
    $this->assertEqual($unable_to_parse->error, 'unable to parse URL', t('Returned with "unable to parse URL" error.'));

    // Fetch page.
    $result = drupal_http_request(url('node', array('absolute' => TRUE)));
    $this->assertEqual($result->code, 200, t('Fetched page successfully.'));
    $this->drupalSetContent($result->data);
    $this->assertTitle(variable_get('site_name', 'Drupal'), t('Site title matches.'));
  }

  function testDrupalHTTPRequestBasicAuth() {
    $username = $this->randomName();
    $password = $this->randomName();
    $url = url('system-test/auth', array('absolute' => TRUE));

    $auth = str_replace('http://', 'http://' . $username . ':' . $password .'@', $url);
    $result = drupal_http_request($auth);

    // We use strpos directly.
    // assertRaw() cannot be used because we are getting the data
    // in a variable instead of $this->_content.
    $this->assertTrue(strpos($result->data, $username) !== FALSE, t('$_SERVER[\'PHP_AUTH_USER\'] is passed correctly.'));
    $this->assertTrue(strpos($result->data, $password) !== FALSE, t('$_SERVER[\'PHP_AUTH_PW\'] is passed correctly.'));
  }

  function testDrupalHTTPRequestRedirect() {
    $redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
    $this->assertEqual($redirect_301->redirect_code, 301, t('drupal_http_request follows the 301 redirect.'));

    $redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array(), 'GET', NULL, 0);
    $this->assertFalse(isset($redirect_301->redirect_code), t('drupal_http_request does not follow 301 redirect if $retry = 0.'));

    $redirect_invalid = drupal_http_request(url('system-test/redirect-noscheme', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
    $this->assertEqual($redirect_invalid->error, 'missing schema', t('301 redirect to invalid URL returned with error "!error".', array('!error' => $redirect_invalid->error)));

    $redirect_invalid = drupal_http_request(url('system-test/redirect-noparse', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
    $this->assertEqual($redirect_invalid->error, 'unable to parse URL', t('301 redirect to invalid URL returned with error "!error".', array('!error' => $redirect_invalid->error)));

    $redirect_invalid = drupal_http_request(url('system-test/redirect-invalid-scheme', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
    $this->assertEqual($redirect_invalid->error, 'invalid schema ftp', t('301 redirect to invalid URL returned with error "!error".', array('!error' => $redirect_invalid->error)));

    $redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
    $this->assertEqual($redirect_302->redirect_code, 302, t('drupal_http_request follows the 302 redirect.'));

    $redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array(), 'GET', NULL, 0);
    $this->assertFalse(isset($redirect_302->redirect_code), t('drupal_http_request does not follow 302 redirect if $retry = 0.'));

    $redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
    $this->assertEqual($redirect_307->redirect_code, 307, t('drupal_http_request follows the 307 redirect.'));

    $redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array(), 'GET', NULL, 0);
    $this->assertFalse(isset($redirect_307->redirect_code), t('drupal_http_request does not follow 307 redirect if $retry = 0.'));
  }

  function testDrupalGetDestination() {
    $query = $this->randomName(10);
    $url = url('system-test/destination', array('absolute' => TRUE, 'query' => $query));
    $this->drupalGet($url);
    $this->assertText($query, t('The query passed to the page is correctly represented by drupal_get_detination().'));
  }
}

/**
 * Testing drupal_set_content and drupal_get_content.
 */
class DrupalSetContentTestCase extends DrupalWebTestCase {
  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Drupal set/get content'),
      'description' => t("Performs tests on setting and retrieiving content from theme regions."),
      'group' => t('System')
    );
  }


  /**
   * Test setting and retrieving content for theme regions.
   */
  function testRegions() {
    global $theme_key;

    $block_regions = array_keys(system_region_list($theme_key));
    $delimiter = $this->randomName(32);
    $values = array();
    // Set some random content for each region available.
    foreach ($block_regions as $region) {
      $first_chunk = $this->randomName(32);
      drupal_set_content($region, $first_chunk);
      $second_chunk = $this->randomName(32);
      drupal_set_content($region, $second_chunk);
      // Store the expected result for a drupal_get_content call for this region.
      $values[$region] = $first_chunk . $delimiter . $second_chunk;
    }

    // Ensure drupal_get_content returns expected results when fetching all regions.
    $content = drupal_get_content(NULL, $delimiter);
    foreach ($content as $region => $region_content) {
      $this->assertEqual($region_content, $values[$region], t('@region region text verified when fetching all regions', array('@region' => $region)));
    }

    // Ensure drupal_get_content returns expected results when fetching a single region.
    foreach ($block_regions as $region) {
      $region_content = drupal_get_content($region, $delimiter);
      $this->assertEqual($region_content, $values[$region], t('@region region text verified when fetching single region.', array('@region' => $region)));
    }
  }
}

/**
 * Tests for the JavaScript system.
 */
class JavaScriptTestCase extends DrupalWebTestCase {
  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('JavaScript'),
      'description' => t("Tests the JavaScript system."),
      'group' => t('System')
    );
  }
  
  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp();
    // Reset drupal_add_js() before each test.
    drupal_add_js(NULL, NULL, TRUE);
  }
  
  /**
   * Test default JavaScript is empty.
   */
  function testDefault() {
    $this->assertEqual(array(), drupal_add_js(), t('Default JavaScript is empty.'));
  }
  
  /**
   * Test adding a JavaScript file.
   */
  function testAddFile() {
    drupal_add_js('misc/collapse.js');
    $javascript = drupal_add_js();
    $this->assertTrue(array_key_exists('misc/jquery.js', $javascript['header']['core']), t('jQuery is added when a file is added.'));
    $this->assertTrue(array_key_exists('misc/drupal.js', $javascript['header']['core']), t('Drupal.js is added when file is added.'));
    $this->assertTrue(array_key_exists('misc/collapse.js', $javascript['header']['module']), t('JavaScript files are correctly added.'));
    $this->assertEqual(base_path(), $javascript['header']['setting'][0]['basePath'], t('Base path JavaScript setting is correctly set.'));
  }
  
  /**
   * Test adding settings.
   */
  function testAddSetting() {
    drupal_add_js(array('drupal' => 'rocks', 'dries' => 280342800), 'setting');
    $javascript = drupal_add_js();
    $this->assertEqual(280342800, $javascript['header']['setting'][1]['dries'], t('JavaScript setting is set correctly.'));
    $this->assertEqual('rocks', $javascript['header']['setting'][1]['drupal'], t('The other JavaScript setting is set correctly.'));
  }
  
  /**
   * Test drupal_get_js() for JavaScript settings.
   */
  function testHeaderSetting() {
    drupal_add_js(array('testSetting' => 'testValue'), 'setting');
    $javascript = drupal_get_js('header');
    $this->assertTrue(strpos($javascript, 'basePath') > 0, t('Rendered JavaScript header returns basePath setting.'));
    $this->assertTrue(strpos($javascript, 'testSetting') > 0, t('Rendered JavaScript header returns custom setting.'));
    $this->assertTrue(strpos($javascript, 'misc/jquery.js') > 0, t('Rendered JavaScript header includes jQuery.'));
  }
  
  /**
   * Test to see if resetting the JavaScript empties the cache.
   */
  function testReset() {
    drupal_add_js('misc/collapse.js');
    drupal_add_js(NULL, NULL, TRUE);    
    $this->assertEqual(array(), drupal_add_js(), t('Resetting the JavaScript correctly empties the cache.'));
  }
  
  /**
   * Test adding inline scripts.
   */
  function testAddInline() {
    $inline = '$(document).ready(function(){});';
    drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
    $javascript = drupal_add_js();
    $this->assertTrue(array_key_exists('misc/jquery.js', $javascript['header']['core']), t('jQuery is added when inline scripts are added.'));
    $this->assertEqual($inline, $javascript['footer']['inline'][0]['code'], t('Inline JavaScript is correctly added to the footer.'));
  }
  
  /**
   * Test drupal_get_js() with a footer scope.
   */
  function testFooterHTML() {
    $inline = '$(document).ready(function(){});';
    drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
    $javascript = drupal_get_js('footer');
    $this->assertTrue(strpos($javascript, $inline) > 0, t('Rendered JavaScript footer returns the inline code.'));
  }
  
  /**
   * Test drupal_add_js() sets preproccess to false when cache is set to false.
   */
  function testNoCache() {
    drupal_add_js('misc/collapse.js', array('cache' => FALSE));
    $javascript = drupal_add_js();
    $this->assertTrue(!$javascript['header']['module']['misc/collapse.js']['preprocess'], t('Setting cache to FALSE sets proprocess to FALSE when adding JavaScript.'));
  }
}

/**
 * Tests Drupal error and exception handlers.
 */
class DrupalErrorHandlerUnitTest extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('Drupal error handlers'),
      'description' => t("Performs tests on the Drupal error and exception handler."),
      'group' => t('System'),
    );
  }

  function setUp() {
    parent::setUp('system_test');
  }

  /**
   * Test the error handler.
   */
  function testErrorHandler() {
    $this->drupalGet('system-test/generate-warnings');

    $this->assertErrorMessage('Notice', 'system_test.module', 'system_test_generate_warnings() ', 'Undefined variable');
    $this->assertErrorMessage('Warning', 'system_test.module', 'system_test_generate_warnings() ', 'Division by zero');
    $this->assertErrorMessage('User notice', 'system_test.module', 'system_test_generate_warnings() ', 'Drupal is awesome');
  }

  /**
   * Test the exception handler.
   */
  function testExceptionHandler() {
    $this->drupalGet('system-test/trigger-exception');
    $this->assertErrorMessage('Exception', 'system_test.module', 'system_test_trigger_exception()', 'Drupal is awesome');

    $this->drupalGet('system-test/trigger-pdo-exception');
    $this->assertErrorMessage('PDOException', 'system_test.module', 'system_test_trigger_pdo_exception()', 'Base table or view not found');
  }

  /**
   * Helper function: assert that the logged message is correct.
   */
  function assertErrorMessage($type, $file, $function, $message) {
    $this->assertText($type, t("Found '%type' in error page.", array('%type' => $type)));
    $this->assertText($file, t("Found '%file' in error page.", array('%file' => $file)));
    $this->assertText($function, t("Found '%function' in error page.", array('%function' => $function)));
    $this->assertText($message, t("Found '%message' in error page.", array('%message' => $message)));
  }
}