Projet

Général

Profil

Paste
Télécharger (17,8 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / link / tests / link.validate.test @ a5572547

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Tests that exercise the validation functions in the link module.
6
 */
7
8
class LinkValidateTestCase extends LinkBaseTestClass {
9
10
  protected function createLink($url, $title, $attributes = array()) {
11
    return array(
12
      'url' => $url,
13
      'title' => $title,
14
      'attributes' => $attributes,
15
    );
16
  }
17
18
  /**
19
   * Takes a url, and sees if it can validate that the url is valid.
20
   */
21
  protected function link_test_validate_url($url) {
22
23
    $field_name = $this->createLinkField();
24
25
    $permission = 'create page content';
26
    $this->checkPermissions(array($permission), TRUE);
27
28
    $this->drupalGet('node/add/page');
29
30
    $label = $this->randomName();
31
    $edit = array(
32
      'title' => $label,
33
      $field_name . '[und][0][title]' => $label,
34
      $field_name . '[und][0][url]' => $url,
35
    );
36
    $this->drupalPost(NULL, $edit, t('Save'));
37
    $this->assertRaw(t(' has been created.'), 'Node created');
38
39
    $nid = 1; //$matches[1];
40
41
    $node = node_load($nid);
42
43
    $this->assertEqual($url, $node->{$field_name}['und'][0]['url']);
44
  }
45
}
46
47
class LinkValidateTest extends LinkValidateTestCase {
48
49
  public static function getInfo() {
50
    return array(
51
      'name' => 'Link Validation Tests',
52
      'description' => 'Tests the field validation.',
53
      'group' => 'Link',
54
    );
55
  }
56
57
  function test_link_validate_basic_url() {
58
    $this->link_test_validate_url('http://www.example.com');
59
  }
60
61
  /**
62
   * Test if we're stopped from posting a bad url on default validation.
63
   */
64
  function test_link_validate_bad_url_validate_default() {
65
    $this->web_user = $this->drupalCreateUser(array('administer content types',
66
                                             'administer nodes',
67
                                             'administer filters',
68
                                             'access content',
69
                                             'create page content',
70
                                             'access administration pages'));
71
    $this->drupalLogin($this->web_user);
72
73
    // create field
74
    $name = strtolower($this->randomName());
75
    $edit = array(
76
      'fields[_add_new_field][label]' => $name,
77
      'fields[_add_new_field][field_name]' => $name,
78
      'fields[_add_new_field][type]' => 'link_field',
79
      'fields[_add_new_field][widget_type]' => 'link_field',
80
    );
81
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
82
    $this->drupalPost(NULL, array(), t('Save field settings'));
83
    $this->drupalPost(NULL, array(), t('Save settings'));
84
85
    // Is field created?
86
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
87
    node_types_rebuild();
88
    menu_rebuild();
89
90
    // create page form
91
    $this->drupalGet('node/add/page');
92
    $field_name = 'field_' . $name;
93
    $this->assertField('edit-field-'. $name .'-und-0-title', 'Title found');
94
    $this->assertField('edit-field-'. $name .'-und-0-url', 'URL found');
95
96
97
    $edit = array(
98
      'title' => 'Simple Title',
99
      $field_name .'[und][0][url]' => 'edik:naw',
100
    );
101
102
    $this->drupalPost(NULL, $edit, t('Save'));
103
    $this->assertText(t('The value provided for @field is not a valid URL.', array('@field' => $name)));
104
  }
105
106
  /**
107
   * Test if we're stopped from posting a bad url with validation on.
108
   */
109
  function test_link_validate_bad_url_validate_on() {
110
    $this->web_user = $this->drupalCreateUser(array('administer content types',
111
                                             'administer nodes',
112
                                             'administer filters',
113
                                             'access content',
114
                                             'create page content',
115
                                             'access administration pages'));
116
    $this->drupalLogin($this->web_user);
117
118
    // create field
119
    $name = strtolower($this->randomName());
120
    $edit = array(
121
      'fields[_add_new_field][label]' => $name,
122
      'fields[_add_new_field][field_name]' => $name,
123
      'fields[_add_new_field][type]' => 'link_field',
124
      'fields[_add_new_field][widget_type]' => 'link_field',
125
    );
126
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
127
    $this->drupalPost(NULL, array(), t('Save field settings'));
128
    $this->drupalPost(NULL, array('instance[settings][validate_url]' => TRUE), t('Save settings'));
129
130
    // Is field created?
131
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
132
    node_types_rebuild();
133
    menu_rebuild();
134
135
    // create page form
136
    $this->drupalGet('node/add/page');
137
    $field_name = 'field_' . $name;
138
    $this->assertField('edit-field-'. $name .'-und-0-title', 'Title found');
139
    $this->assertField('edit-field-'. $name .'-und-0-url', 'URL found');
140
141
142
    $edit = array(
143
      'title' => 'Simple Title',
144
      $field_name .'[und][0][url]' => 'edik:naw',
145
    );
146
147
    $this->drupalPost(NULL, $edit, t('Save'));
148
    $this->assertText(t('The value provided for @field is not a valid URL.', array('@field' => $name)));
149
150
  }
151
152
  /**
153
   * Test if we can post a bad url if the validation is expressly turned off.
154
   */
155
  function test_link_validate_bad_url_validate_off() {
156
    $this->web_user = $this->drupalCreateUser(array('administer content types',
157
                                             'administer nodes',
158
                                             'administer filters',
159
                                             'access content',
160
                                             'create page content',
161
                                             'access administration pages'));
162
    $this->drupalLogin($this->web_user);
163
164
    // create field
165
    $name = strtolower($this->randomName());
166
    $edit = array(
167
      'fields[_add_new_field][label]' => $name,
168
      'fields[_add_new_field][field_name]' => $name,
169
      'fields[_add_new_field][type]' => 'link_field',
170
      'fields[_add_new_field][widget_type]' => 'link_field',
171
    );
172
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
173
    $this->drupalPost(NULL, array(), t('Save field settings'));
174
    $this->drupalPost(NULL, array('instance[settings][validate_url]' => FALSE), t('Save settings'));
175
176
    /*$instance_details = db_query("SELECT * FROM {field_config_instance} WHERE field_name = :field_name AND bundle = 'page'", array(':field_name' => 'field_'. $name))->fetchObject();
177
    $this->fail('<pre>'. print_r($instance_details, TRUE) .'</pre>');
178
    $this->fail('<pre>'. print_r(unserialize($instance_details->data), TRUE) .'</pre>');*/
179
180
    // Is field created?
181
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
182
    node_types_rebuild();
183
    menu_rebuild();
184
185
    // create page form
186
    $this->drupalGet('node/add/page');
187
    $field_name = 'field_' . $name;
188
    $this->assertField('edit-field-'. $name .'-und-0-title', 'Title found');
189
    $this->assertField('edit-field-'. $name .'-und-0-url', 'URL found');
190
191
192
    $edit = array(
193
      'title' => 'Simple Title',
194
      $field_name .'[und][0][url]' => 'edik:naw',
195
    );
196
197
    $this->drupalPost(NULL, $edit, t('Save'));
198
    $this->assertNoText(t('The value provided for @field is not a valid URL.', array('@field' => $name)));
199
  }
200
201
  /**
202
   * Test if a bad url can sneak through un-filtered if we play with the validation...
203
   */
204
  function x_test_link_validate_switching_between_validation_status() {
205
    $this->acquireContentTypes(1);
206
    $this->web_user = $this->drupalCreateUser(array('administer content types',
207
                                             'administer nodes',
208
                                             'access administration pages',
209
                                             'access content',
210
                                             'create '. $this->content_types[0]->type .' content',
211
                                             'edit any '. $this->content_types[0]->type .' content'));
212
    $this->drupalLogin($this->web_user);
213
    variable_set('node_options_'. $this->content_types[0]->name, array('status', 'promote'));
214
    $field_settings = array(
215
      'type' => 'link',
216
      'widget_type' => 'link',
217
      'type_name' => $this->content_types[0]->name,
218
      'attributes' => array(), // <-- This is needed or we have an error
219
      'validate_url' => 0,
220
    );
221
222
    $field = $this->createField($field_settings, 0);
223
    //$this->fail('<pre>'. print_r($field, TRUE) .'</pre>');
224
    $field_db_info = content_database_info($field);
225
226
    $this->acquireNodes(2);
227
228
    $node = node_load($this->nodes[0]->nid);
229
230
    $this->drupalGet('node/'. $this->nodes[0]->nid);
231
232
    $edit = array();
233
    $title = $this->randomName();
234
    $url = 'javascript:alert("http://example.com/' . $this->randomName() . '")';
235
    $edit[$field['field_name'] .'[0][url]'] = $url;
236
    $edit[$field['field_name'] .'[0][title]'] = $title;
237
238
    $this->drupalPost('node/'. $this->nodes[0]->nid .'/edit', $edit, t('Save'));
239
    //$this->pass($this->content);
240
    $this->assertNoText(t('The value provided for %field is not a valid URL.', array('%field' => $name)));
241
242
    // Make sure we get a new version!
243
    $node = node_load($this->nodes[0]->nid, NULL, TRUE);
244
    $this->assertEqual($url, $node->{$field['field_name']}[0]['url']);
245
246
    $this->drupalGet('node/'. $node->nid);
247
    $this->assertNoRaw($url, 'Make sure Javascript does not display.');
248
249
    // Turn the array validation back _on_.
250
    $edit = array('validate_url' => TRUE);
251
    $node_type_link = str_replace('_', '-', $node->type);
252
    //$this->drupalGet('admin/content/node-type/'. $node_type_link .'/fields'); ///'. $field['field_name']);
253
    //$this->fail($this->content);
254
    $this->drupalPost('admin/content/node-type/'. $node_type_link .'/fields/'. $field['field_name'], $edit, t('Save field settings'));
255
256
    $this->drupalGet('node/'. $node->nid);
257
    // This actually works because the display_url goes through the core
258
    // url() function.  But we should have a test that makes sure it continues
259
    // to work.
260
    $this->assertNoRaw($url, 'Make sure Javascript does not display.');
261
    //$this->fail($this->content);
262
263
  }
264
265
  // Validate that '<front>' is a valid url.
266
  function test_link_front_url() {
267
    $this->link_test_validate_url('<front>');
268
  }
269
270
  // Validate that an internal url would be accepted.
271
  function test_link_internal_url() {
272
    $this->link_test_validate_url('node/32');
273
  }
274
275
  // Validate a simple mailto.
276
  function test_link_mailto() {
277
    $this->link_test_validate_url('mailto:jcfiala@gmail.com');
278
  }
279
280
  function test_link_external_https() {
281
    $this->link_test_validate_url('https://www.example.com/');
282
  }
283
284
  function test_link_ftp() {
285
    $this->link_test_validate_url('ftp://www.example.com/');
286
  }
287
}
288
289
class LinkValidateTestNews extends LinkValidateTestCase {
290
291
  public static function getInfo() {
292
    return array(
293
      'name' => 'Link News Validation Tests',
294
      'description' => 'Tests the field validation for usenet urls.',
295
      'group' => 'Link',
296
    );
297
  }
298
299
  // Validate a news link to a message group
300
  function test_link_news() {
301
    $this->link_test_validate_url('news:comp.infosystems.www.misc');
302
  }
303
304
  // Validate a news link to a message id.  Said ID copied off of google groups.
305
  function test_link_news_message() {
306
    $this->link_test_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
307
  }
308
}
309
310
class LinkValidateSpecificURL extends LinkValidateTestCase {
311
  public static function getInfo() {
312
    return array(
313
      'name' => 'Link Specific URL Validation Tests',
314
      'description' => 'Tests field validation with unusual urls',
315
      'group' => 'Link',
316
    );
317
  }
318
319
  // Lets throw in a lot of umlouts for testing!
320
  function test_umlout_url() {
321
    $this->link_test_validate_url('http://üÜü.exämple.com/nöde');
322
  }
323
324
  function test_umlout_mailto() {
325
    $this->link_test_validate_url('mailto:Üser@exÅmple.com');
326
  }
327
328
  function test_german_b_url() {
329
    $this->link_test_validate_url('http://www.test.com/ßstuff');
330
  }
331
332
  function test_special_n_url() {
333
    $this->link_test_validate_url('http://www.testÑñ.com/');
334
  }
335
336
  function test_curly_brackets_in_query() {
337
    $this->link_test_validate_url('http://www.healthyteennetwork.org/index.asp?Type=B_PR&SEC={2AE1D600-4FC6-4B4D-8822-F1D5F072ED7B}&DE={235FD1E7-208D-4363-9854-4E6775EB8A4C}');
338
  }
339
340
  /**
341
   * Here, we're testing that a very long url is stored properly in the db.
342
   *
343
   * Basicly, trying to test http://drupal.org/node/376818
344
   */
345
  function testLinkURLFieldIsBig() {
346
    $long_url = 'http://th.wikipedia.org/wiki/%E0%B9%82%E0%B8%A3%E0%B8%87%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B9%80%E0%B8%9A%E0%B8%8D%E0%B8%88%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%B9%E0%B8%97%E0%B8%B4%E0%B8%A8_%E0%B8%99%E0%B8%84%E0%B8%A3%E0%B8%A8%E0%B8%A3%E0%B8%B5%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A';
347
    $this->link_test_validate_url($long_url);
348
  }
349
350
}
351
352
/**
353
 * A series of tests of links, only going against the link_validate_url function in link.module.
354
 *
355
 * Validation is guided by the rules in http://tools.ietf.org/html/rfc1738 !
356
 */
357
class LinkValidateUrlLight extends DrupalWebTestCase {
358
359
  public static function getInfo() {
360
    return array(
361
      'name' => 'Link Light Validation Tests',
362
      'description' => 'Tests the link_validate_url() function by itself, without invoking the full drupal/cck lifecycle.',
363
      'group' => 'Link',
364
    );
365
  }
366
367
  /**
368
   * Translates the LINK type constants to english for display and debugging of tests
369
   */
370
  function name_Link_Type($type) {
371
    switch ($type) {
372
      case LINK_FRONT:
373
        return "Front";
374
      case LINK_EMAIL:
375
        return "Email";
376
      case LINK_NEWS:
377
        return "Newsgroup";
378
      case LINK_INTERNAL:
379
        return "Internal Link";
380
      case LINK_EXTERNAL:
381
        return "External Link";
382
      case FALSE:
383
        return "Invalid Link";
384
      default:
385
        return "Bad Value:". $type;
386
    }
387
  }
388
389
  // Make sure that a link labelled <front> works.
390
  function testValidateFrontLink() {
391
    $valid = link_validate_url('<front>');
392
    $this->assertEqual(LINK_FRONT, $valid, 'Make sure that front link is verfied and identified');
393
  }
394
395
  function testValidateEmailLink() {
396
    $valid = link_validate_url('mailto:bob@example.com');
397
    $this->assertEqual(LINK_EMAIL, $valid, "Make sure a basic mailto is verified and identified");
398
  }
399
400
  function testValidateEmailLinkBad() {
401
    $valid = link_validate_url(':bob@example.com');
402
    $this->assertEqual(FALSE, $valid, 'Make sure just a bad address is correctly failed');
403
  }
404
405
  function testValidateNewsgroupLink() {
406
    $valid = link_validate_url('news:comp.infosystems.www.misc');
407
    $this->assertEqual(LINK_NEWS, $valid, 'Make sure link to newsgroup validates as news.');
408
  }
409
410
  function testValidateNewsArticleLink() {
411
    $valid = link_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
412
    $this->assertEqual(LINK_NEWS, $valid, 'Make sure link to specific article valiates as news.');
413
  }
414
415
  function testValidateBadNewsgroupLink() {
416
    $valid = link_validate_url('news:comp.bad_name.misc');
417
    $this->assertEqual(FALSE, $valid, 'newsgroup names can\'t contain underscores, so it should come back as invalid.');
418
  }
419
420
  function testValidateInternalLinks() {
421
    $links = array(
422
      'node/5',
423
      'rss.xml',
424
      'files/test.jpg',
425
      '/var/www/test',
426
    );
427
    
428
    foreach ($links as $link) {
429
      $valid = link_validate_url($link);
430
      $this->assertEqual(LINK_INTERNAL, $valid, 'Test ' . $link . ' internal link.');
431
    }
432
  }
433
434
  function testValidateExternalLinks() {
435
    $links = array(
436
      'http://localhost:8080/',
437
      'www.example.com',
438
      'www.example.com/',
439
      'http://username:p%40ssw0rd!@www.example.com/',
440
      'http://@www.example.com/',
441
      'http://username:@www.example.com/',
442
      'http://username:password@www.example.com:8080/',
443
      'http://127.0.0.1:80/',
444
      'http://127.173.24.255:4723/',
445
      '127.173.24.255:4723/',
446
      'http://255.255.255.255:4823/',
447
      'www.test-site.com',
448
      'http://example.com/index.php?q=node/123',
449
      'http://example.com/index.php?page=this\that',
450
      'http://example.com/?first_name=Joe Bob&last_name=Smith',
451
      // Anchors
452
      'http://www.example.com/index.php#test',
453
      'http://www.example.com/index.php#this@that.',
454
      'http://www.example.com/index.php#',
455
      'http://www.cnn.com/video/#/video/politics/2008/12/09/intv.madeleine.albright.cnn',
456
      'http://www.archive.org/stream/aesopsfables00aesorich#page/n7/mode/2up',
457
      'http://www.example.com/blah/#this@that?',
458
    );
459
    // Test all of the protocols.
460
    $allowed_protocols = variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'));
461
    foreach ($allowed_protocols as $protocol) {
462
      if ($protocol !== 'news' && $protocol !== 'mailto') {
463
        $links[] = $protocol .'://www.example.com';
464
      }
465
    }
466
    foreach ($links as $link) {
467
      $valid = link_validate_url($link);
468
      $this->assertEqual(LINK_EXTERNAL, $valid, 'Testing that '. $link .' is a valid external link.');
469
      // The following two lines are commented out and only used for comparisons.
470
      //$valid2 = valid_url($link, TRUE);
471
      //$this->assertEqual(TRUE, $valid2, "Using valid_url() on $link.");
472
    }
473
    // Test if we can make a tld valid:
474
    variable_set('link_extra_domains', array('frog'));
475
    $valid = link_validate_url('http://www.example.frog');
476
    $this->assertEqual(LINK_EXTERNAL, $valid, "Testing that http://www.example.frog is a valid external link if we've added 'frog' to the list of valid domains.");
477
  }
478
479
  function testInvalidExternalLinks() {
480
    $links = array(
481
      'http://www.ex ample.com/',
482
      'http://25.0.0/', // bad ip!
483
      'http://4827.0.0.2/',
484
      '//www.example.com/',
485
      'http://www.testß.com/', // ß not allowed in domain names!
486
      'http://www.example.frog/', // Bad TLD
487
      //'http://www.-fudge.com/', // domains can't have sections starting with a dash.
488
    );
489
    foreach ($links as $link) {
490
      $valid = link_validate_url($link);
491
      $this->assertEqual(FALSE, $valid, 'Testing that '. $link .' is not a valid link.');
492
    }
493
  }
494
}