Projet

Général

Profil

Paste
Télécharger (18,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / link / tests / link.validate.test @ 7fe061e8

1
<?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
    $label = $this->randomName();
26
    $settings = array(
27
      'title' => $label,
28
      $field_name => array(
29
        LANGUAGE_NONE=> array(
30
          array(
31
            'title' => $label,
32
            'url' => $url,
33
          )
34
        ),
35
      ),
36
    );
37

    
38
    $node = $this->drupalCreateNode($settings);
39

    
40
    $this->assertNotNull($node, ' has been created.', 'Node created');
41

    
42
    $this->assertEqual($url, $node->{$field_name}[LANGUAGE_NONE][0]['url']);
43
  }
44
}
45

    
46
class LinkValidateTest extends LinkValidateTestCase {
47

    
48
  public static function getInfo() {
49
    return array(
50
      'name' => 'Link Validation Tests',
51
      'description' => 'Tests the field validation.',
52
      'group' => 'Link',
53
    );
54
  }
55

    
56
  function test_link_validate_basic_url() {
57
    $this->link_test_validate_url('http://www.example.com');
58
  }
59

    
60
  /**
61
   * Test if we're stopped from posting a bad url on default validation.
62
   */
63
  function test_link_validate_bad_url_validate_default() {
64
    $this->web_user = $this->drupalCreateUser(array('administer content types',
65
                                             'administer nodes',
66
                                             'administer filters',
67
                                             'access content',
68
                                             'create page content',
69
                                             'access administration pages'));
70
    $this->drupalLogin($this->web_user);
71

    
72
    // create field
73
    $name = strtolower($this->randomName());
74
    $edit = array(
75
      'fields[_add_new_field][label]' => $name,
76
      'fields[_add_new_field][field_name]' => $name,
77
      'fields[_add_new_field][type]' => 'link_field',
78
      'fields[_add_new_field][widget_type]' => 'link_field',
79
    );
80
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
81
    $this->drupalPost(NULL, array(), t('Save field settings'));
82
    $this->drupalPost(NULL, array(), t('Save settings'));
83

    
84
    // Is field created?
85
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
86
    node_types_rebuild();
87
    menu_rebuild();
88

    
89
    // create page form
90
    $this->drupalGet('node/add/page');
91
    $field_name = 'field_' . $name;
92
    $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
93
    $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
94

    
95

    
96
    $edit = array(
97
      'title' => 'Simple Title',
98
      $field_name . '[und][0][url]' => 'edik:naw',
99
    );
100

    
101
    $this->drupalPost(NULL, $edit, t('Save'));
102
    $this->assertText(t('The value @value provided for @field is not a valid URL.', array('@value' => 'edik:naw', '@field' => $name)));
103
  }
104

    
105
  /**
106
   * Test if we're stopped from posting a bad url with validation on.
107
   */
108
  function test_link_validate_bad_url_validate_on() {
109
    $this->web_user = $this->drupalCreateUser(array('administer content types',
110
                                             'administer nodes',
111
                                             'administer filters',
112
                                             'access content',
113
                                             'create page content',
114
                                             'access administration pages'));
115
    $this->drupalLogin($this->web_user);
116

    
117
    // create field
118
    $name = strtolower($this->randomName());
119
    $edit = array(
120
      'fields[_add_new_field][label]' => $name,
121
      'fields[_add_new_field][field_name]' => $name,
122
      'fields[_add_new_field][type]' => 'link_field',
123
      'fields[_add_new_field][widget_type]' => 'link_field',
124
    );
125
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
126
    $this->drupalPost(NULL, array(), t('Save field settings'));
127
    $this->drupalPost(NULL, array('instance[settings][validate_url]' => TRUE), t('Save settings'));
128

    
129
    // Is field created?
130
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
131
    node_types_rebuild();
132
    menu_rebuild();
133

    
134
    // create page form
135
    $this->drupalGet('node/add/page');
136
    $field_name = 'field_' . $name;
137
    $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
138
    $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
139

    
140

    
141
    $edit = array(
142
      'title' => 'Simple Title',
143
      $field_name . '[und][0][url]' => 'edik:naw',
144
    );
145

    
146
    $this->drupalPost(NULL, $edit, t('Save'));
147
    $this->assertText(t('The value @value provided for @field is not a valid URL.', array('@field' => $name, '@value' => 'edik:naw')));
148

    
149
  }
150

    
151
  /**
152
   * Test if we can post a bad url if the validation is expressly turned off.
153
   */
154
  function test_link_validate_bad_url_validate_off() {
155
    $this->web_user = $this->drupalCreateUser(array('administer content types',
156
                                             'administer nodes',
157
                                             'administer filters',
158
                                             'access content',
159
                                             'create page content',
160
                                             'access administration pages'));
161
    $this->drupalLogin($this->web_user);
162

    
163
    // create field
164
    $name = strtolower($this->randomName());
165
    $edit = array(
166
      'fields[_add_new_field][label]' => $name,
167
      'fields[_add_new_field][field_name]' => $name,
168
      'fields[_add_new_field][type]' => 'link_field',
169
      'fields[_add_new_field][widget_type]' => 'link_field',
170
    );
171
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
172
    $this->drupalPost(NULL, array(), t('Save field settings'));
173
    $this->drupalPost(NULL, array('instance[settings][validate_url]' => FALSE), t('Save settings'));
174

    
175
    /*$instance_details = db_query("SELECT * FROM {field_config_instance} WHERE field_name = :field_name AND bundle = 'page'", array(':field_name' => 'field_'. $name))->fetchObject();
176
    $this->fail('<pre>'. print_r($instance_details, TRUE) .'</pre>');
177
    $this->fail('<pre>'. print_r(unserialize($instance_details->data), TRUE) .'</pre>');*/
178

    
179
    // Is field created?
180
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
181
    node_types_rebuild();
182
    menu_rebuild();
183

    
184
    // create page form
185
    $this->drupalGet('node/add/page');
186
    $field_name = 'field_' . $name;
187
    $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
188
    $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
189

    
190

    
191
    $edit = array(
192
      'title' => 'Simple Title',
193
      $field_name . '[und][0][url]' => 'edik:naw',
194
    );
195

    
196
    $this->drupalPost(NULL, $edit, t('Save'));
197
    $this->assertNoText(t('The value %value provided for %field is not a valid URL.', array('%field' => $name, '%value' => 'edik:naw')));
198
  }
199

    
200
  /**
201
   * Test if a bad url can sneak through un-filtered if we play with the validation...
202
   */
203
  function x_test_link_validate_switching_between_validation_status() {
204
    $this->acquireContentTypes(1);
205
    $this->web_user = $this->drupalCreateUser(array('administer content types',
206
                                             'administer nodes',
207
                                             'access administration pages',
208
                                             'access content',
209
                                             'create ' . $this->content_types[0]->type . ' content',
210
                                             'edit any ' . $this->content_types[0]->type . ' content'));
211
    $this->drupalLogin($this->web_user);
212
    variable_set('node_options_' . $this->content_types[0]->name, array('status', 'promote'));
213
    $field_settings = array(
214
      'type' => 'link',
215
      'widget_type' => 'link',
216
      'type_name' => $this->content_types[0]->name,
217
      'attributes' => array(), // <-- This is needed or we have an error
218
      'validate_url' => 0,
219
    );
220

    
221
    $field = $this->createField($field_settings, 0);
222
    //$this->fail('<pre>'. print_r($field, TRUE) .'</pre>');
223
    $field_db_info = content_database_info($field);
224

    
225
    $this->acquireNodes(2);
226

    
227
    $node = node_load($this->nodes[0]->nid);
228

    
229
    $this->drupalGet('node/' . $this->nodes[0]->nid);
230

    
231
    $edit = array();
232
    $title = $this->randomName();
233
    $url = 'javascript:alert("http://example.com/' . $this->randomName() . '")';
234
    $edit[$field['field_name'] . '[0][url]'] = $url;
235
    $edit[$field['field_name'] . '[0][title]'] = $title;
236

    
237
    $this->drupalPost('node/' . $this->nodes[0]->nid . '/edit', $edit, t('Save'));
238
    //$this->pass($this->content);
239
    $this->assertNoText(t('The value %value provided for %field is not a valid URL.', array('%field' => $name, '%value' => trim($url))));
240

    
241
    // Make sure we get a new version!
242
    $node = node_load($this->nodes[0]->nid, NULL, TRUE);
243
    $this->assertEqual($url, $node->{$field['field_name']}[0]['url']);
244

    
245
    $this->drupalGet('node/' . $node->nid);
246
    $this->assertNoRaw($url, 'Make sure Javascript does not display.');
247

    
248
    // Turn the array validation back _on_.
249
    $edit = array('validate_url' => TRUE);
250
    $node_type_link = str_replace('_', '-', $node->type);
251
    //$this->drupalGet('admin/content/node-type/'. $node_type_link .'/fields'); ///'. $field['field_name']);
252
    //$this->fail($this->content);
253
    $this->drupalPost('admin/content/node-type/' . $node_type_link . '/fields/' . $field['field_name'], $edit, t('Save field settings'));
254

    
255
    $this->drupalGet('node/' . $node->nid);
256
    // This actually works because the display_url goes through the core
257
    // url() function.  But we should have a test that makes sure it continues
258
    // to work.
259
    $this->assertNoRaw($url, 'Make sure Javascript does not display.');
260
    //$this->fail($this->content);
261

    
262
  }
263

    
264
  // Validate that '<front>' is a valid url.
265
  function test_link_front_url() {
266
    $this->link_test_validate_url('<front>');
267
  }
268

    
269
  // Validate that an internal url would be accepted.
270
  function test_link_internal_url() {
271
    // Create the content first.
272
    $node = $this->drupalCreateNode();
273

    
274
    $link = 'node/' . $node->nid;
275
    $this->link_test_validate_url($link);
276
    $type = link_url_type($link);
277
    $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
278
  }
279

    
280
  // Validate a simple mailto.
281
  function test_link_mailto() {
282
    $this->link_test_validate_url('mailto:jcfiala@gmail.com');
283
  }
284

    
285
  function test_link_external_https() {
286
    $this->link_test_validate_url('https://www.example.com/');
287
  }
288

    
289
  function test_link_ftp() {
290
    $this->link_test_validate_url('ftp://www.example.com/');
291
  }
292
}
293

    
294
class LinkValidateTestNews extends LinkValidateTestCase {
295

    
296
  public static function getInfo() {
297
    return array(
298
      'name' => 'Link News Validation Tests',
299
      'description' => 'Tests the field validation for usenet urls.',
300
      'group' => 'Link',
301
    );
302
  }
303

    
304
  // Validate a news link to a message group
305
  function test_link_news() {
306
    $this->link_test_validate_url('news:comp.infosystems.www.misc');
307
  }
308

    
309
  // Validate a news link to a message id.  Said ID copied off of google groups.
310
  function test_link_news_message() {
311
    $this->link_test_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
312
  }
313
}
314

    
315
class LinkValidateSpecificURL extends LinkValidateTestCase {
316
  public static function getInfo() {
317
    return array(
318
      'name' => 'Link Specific URL Validation Tests',
319
      'description' => 'Tests field validation with unusual urls',
320
      'group' => 'Link',
321
    );
322
  }
323

    
324
  // Lets throw in a lot of umlouts for testing!
325
  function test_umlout_url() {
326
    $this->link_test_validate_url('http://üÜü.exämple.com/nöde');
327
  }
328

    
329
  function test_umlout_mailto() {
330
    $this->link_test_validate_url('mailto:Üser@exÅmple.com');
331
  }
332

    
333
  function test_german_b_url() {
334
    $this->link_test_validate_url('http://www.test.com/ßstuff');
335
  }
336

    
337
  function test_special_n_url() {
338
    $this->link_test_validate_url('http://www.testÑñ.com/');
339
  }
340

    
341
  function test_curly_brackets_in_query() {
342
    $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}');
343
  }
344

    
345
  /**
346
   * Here, we're testing that a very long url is stored properly in the db.
347
   *
348
   * Basically, trying to test http://drupal.org/node/376818
349
   */
350
  function testLinkURLFieldIsBig() {
351
    $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';
352
    $this->link_test_validate_url($long_url);
353
  }
354

    
355
}
356

    
357
/**
358
 * A series of tests of links, only going against the link_validate_url function in link.module.
359
 *
360
 * Validation is guided by the rules in http://tools.ietf.org/html/rfc1738 !
361
 */
362
class LinkValidateUrlLight extends DrupalWebTestCase {
363

    
364
  public static function getInfo() {
365
    return array(
366
      'name' => 'Link Light Validation Tests',
367
      'description' => 'Tests the link_validate_url() function by itself, without invoking the full drupal/cck lifecycle.',
368
      'group' => 'Link',
369
    );
370
  }
371
  
372
  function setUp() {
373
    parent::setUp('link');
374
  }
375

    
376
  /**
377
   * Translates the LINK type constants to english for display and debugging of tests
378
   */
379
  function name_Link_Type($type) {
380
    switch ($type) {
381
      case LINK_FRONT:
382
        return "Front";
383
      case LINK_EMAIL:
384
        return "Email";
385
      case LINK_NEWS:
386
        return "Newsgroup";
387
      case LINK_INTERNAL:
388
        return "Internal Link";
389
      case LINK_EXTERNAL:
390
        return "External Link";
391
      case FALSE:
392
        return "Invalid Link";
393
      default:
394
        return "Bad Value:" . $type;
395
    }
396
  }
397

    
398
  // Make sure that a link labeled <front> works.
399
  function testValidateFrontLink() {
400
    $valid = link_validate_url('<front>');
401
    $this->assertEqual(LINK_FRONT, $valid, 'Make sure that front link is verified and identified');
402
  }
403

    
404
  function testValidateEmailLink() {
405
    $valid = link_validate_url('mailto:bob@example.com');
406
    $this->assertEqual(LINK_EMAIL, $valid, "Make sure a basic mailto is verified and identified");
407
  }
408

    
409
  function testValidateEmailLinkBad() {
410
    $valid = link_validate_url(':bob@example.com');
411
    $this->assertEqual(FALSE, $valid, 'Make sure just a bad address is correctly failed');
412
  }
413

    
414
  function testValidateNewsgroupLink() {
415
    $valid = link_validate_url('news:comp.infosystems.www.misc');
416
    $this->assertEqual(LINK_NEWS, $valid, 'Make sure link to newsgroup validates as news.');
417
  }
418

    
419
  function testValidateNewsArticleLink() {
420
    $valid = link_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
421
    $this->assertEqual(LINK_NEWS, $valid, 'Make sure link to specific article validates as news.');
422
  }
423

    
424
  function testValidateBadNewsgroupLink() {
425
    $valid = link_validate_url('news:comp.bad_name.misc');
426
    $this->assertEqual(FALSE, $valid, 'newsgroup names can\'t contain underscores, so it should come back as invalid.');
427
  }
428

    
429
  function testValidateInternalLinks() {
430
    $tempfile = drupal_tempnam('public://files', 'test');
431
    $links = array(
432
      'rss.xml',
433
      file_uri_target($tempfile),
434
      drupal_realpath($tempfile),
435
    );
436
    
437
    foreach ($links as $link) {
438
      $type = link_url_type($link);
439
      $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
440
      $valid = link_validate_url($link);
441
      $this->assertTrue($valid, 'Test ' . $link . ' is valid internal link.');
442
    }
443
  }
444

    
445
  function testValidateExternalLinks() {
446
    $links = array(
447
      'http://localhost:8080/',
448
      'www.example.com',
449
      'www.example.com/',
450
      'http://username:p%40ssw0rd!@www.example.com/',
451
      'http://@www.example.com/',
452
      'http://username:@www.example.com/',
453
      'http://username:password@www.example.com:8080/',
454
      'http://127.0.0.1:80/',
455
      'http://127.173.24.255:4723/',
456
      '127.173.24.255:4723/',
457
      'http://255.255.255.255:4823/',
458
      'www.test-site.com',
459
      'http://example.com/index.php?q=node/123',
460
      'http://example.com/?first_name=Joe Bob&last_name=Smith',
461
      // Anchors
462
      'http://www.example.com/index.php#test',
463
      'http://www.example.com/index.php#this@that.',
464
      'http://www.example.com/index.php#',
465
      'http://www.cnn.com/video/#/video/politics/2008/12/09/intv.madeleine.albright.cnn',
466
      'http://www.archive.org/stream/aesopsfables00aesorich#page/n7/mode/2up',
467
      'http://www.example.com/blah/#this@that?',
468
    );
469
    // Test all of the protocols.
470
    $allowed_protocols = variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'));
471
    foreach ($allowed_protocols as $protocol) {
472
      if ($protocol !== 'news' && $protocol !== 'mailto') {
473
        $links[] = $protocol . '://www.example.com';
474
      }
475
    }
476
    foreach ($links as $link) {
477
      $type = link_url_type($link);
478
      $this->assertEqual(LINK_EXTERNAL, $type, 'Testing that ' . $link . ' is an external link.');
479
      $valid = link_validate_url($link);
480
      $this->assertTrue($valid, 'Test ' . $link . ' is valid external link.');
481
      // The following two lines are commented out and only used for comparisons.
482
      //$valid2 = valid_url($link, TRUE);
483
      //$this->assertEqual(TRUE, $valid2, "Using valid_url() on $link.");
484
    }
485
    // Test if we can make a tld valid:
486
    variable_set('link_extra_domains', array('frog'));
487
    $valid = link_validate_url('http://www.example.frog');
488
    $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.");
489
  }
490

    
491
  function testInvalidExternalLinks() {
492
    $links = array(
493
      'http://www.ex ample.com/',
494
      'http://25.0.0/', // bad ip!
495
      'http://4827.0.0.2/',
496
      '//www.example.com/',
497
      'http://www.testß.com/', // ß not allowed in domain names!
498
      'http://www.example.frog/', // Bad TLD
499
      //'http://www.-fudge.com/', // domains can't have sections starting with a dash.
500
      'http://example.com/index.php?page=this\that',
501
      'example@example.com',
502
    );
503
    foreach ($links as $link) {
504
      $valid = link_validate_url($link);
505
      $this->assertEqual(FALSE, $valid, 'Testing that ' . $link . ' is not a valid link.');
506
    }
507
  }
508
}