[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/link/tests/ -> link.validate.test (source)

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


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7