[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

   1  <?php
   2  // $Id: link.crud_browser.test,v 1.1.2.6 2010/06/14 18:14:11 jcfiala Exp $
   3  
   4  /**
   5   * @file
   6   * Testing CRUD API in the browser.
   7   */
   8  
   9  /**
  10   * Testing that users can not input bad URLs or labels
  11   */
  12  class LinkUITest extends DrupalWebTestcase {
  13  
  14    /**
  15     * Link supposed to be good
  16     */ 
  17    const LINK_INPUT_TYPE_GOOD = 0;
  18  
  19    /**
  20     * Link supposed to have a bad title
  21     */ 
  22    const LINK_INPUT_TYPE_BAD_TITLE = 1;
  23  
  24    /**
  25     * Link supposed to have a bad URL
  26     */ 
  27    const LINK_INPUT_TYPE_BAD_URL = 2;
  28  
  29    /**
  30     * Implementation of getInfo().
  31     */
  32    function getInfo() {
  33      return array(
  34        'name' => t('Link CRUD - browser test'),
  35        'description' => t('Tests the field CRUD (create, read, update, delete) API.'),
  36        'group' => t('Link'),
  37      );
  38    }
  39  
  40    /**
  41     * Implementation of setUp().
  42     */
  43    function setUp() {
  44      parent::setUp('content', 'link');
  45    }
  46  
  47    /**
  48     * Creates a link field for the "page" type and creates a page with a link.
  49     */
  50    function testLinkCreate() {
  51      //libxml_use_internal_errors(true);
  52      $account = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
  53      $this->drupalLogin($account);
  54  
  55      // create field
  56      $name = strtolower($this->randomName());
  57      $edit = array(
  58        '_add_new_field[label]' => $name,
  59        '_add_new_field[field_name]' => $name,
  60        '_add_new_field[type]' => 'link',
  61        '_add_new_field[widget_type]' => 'link',
  62      );
  63      $this->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
  64      $this->drupalPost(NULL, array('validate_url' => TRUE), t('Save field settings'));
  65  
  66      // Is field created?
  67      $this->assertRaw(t('Added field %label.', array('%label' => $name)), 'Field added');
  68  
  69      // create page form
  70      $this->drupalGet('node/add/page');
  71      $field_name = 'field_' . $name;
  72      $this->assertField($field_name . '[0][title]', 'Title found');
  73      $this->assertField($field_name . '[0][url]', 'URL found');
  74  
  75      $input_test_cases = array(
  76        array(
  77          'href' => 'http://example.com/' . $this->randomName(),
  78          'label' => $this->randomName(),
  79          'msg' => 'Link found',
  80          'type' => self::LINK_INPUT_TYPE_GOOD
  81        ),
  82        array(
  83          'href' => 'http://example.com/' . $this->randomName(),
  84          'label' => $this->randomName() . '<script>alert("hi");</script>',
  85          'msg' => 'js label',
  86          'type' => self::LINK_INPUT_TYPE_BAD_TITLE
  87        ),
  88        array(
  89          'href' => 'http://example.com/' . $this->randomName(),
  90          'label' => $this->randomName() . '<script src="http://devil.site.com"></script>',
  91          'msg' => 'js label',
  92          'type' => self::LINK_INPUT_TYPE_BAD_TITLE
  93        ),
  94        array(
  95          'href' => 'http://example.com/' . $this->randomName(),
  96          'label' => $this->randomName() . '" onmouseover="alert(\'hi\')',
  97          'msg' => 'js label',
  98          'type' => self::LINK_INPUT_TYPE_BAD_TITLE
  99        ),
 100         array(
 101          'href' => 'http://example.com/' . $this->randomName(),
 102          'label' => $this->randomName() . '\' onmouseover="alert(\'hi\')',
 103          'msg' => 'js label',
 104          'type' => self::LINK_INPUT_TYPE_BAD_TITLE
 105        ),
 106       array(
 107          'href' => 'javascript:alert("http://example.com/' . $this->randomName() . '")',
 108          'label' => $this->randomName(),
 109          'msg' => 'js url',
 110          'type' => self::LINK_INPUT_TYPE_BAD_URL
 111        ),
 112      );
 113      foreach ($input_test_cases as $input) {
 114        $this->drupalLogin($account);
 115        $this->drupalGet('node/add/page');
 116  
 117        $edit = array(
 118          'title' => $input['label'],
 119          $field_name . '[0][title]' => $input['label'],
 120          $field_name . '[0][url]' => $input['href'],
 121        );
 122        $this->drupalPost(NULL, $edit, t('Save'));
 123        if ($input['type'] == self::LINK_INPUT_TYPE_BAD_URL) {
 124          $this->assertRaw(t('Not a valid URL.'), 'Not a valid URL: ' . $input['href']);
 125          continue;
 126        }
 127        else {
 128          $this->assertRaw(t('@type %title has been created.', array('@type' => 'Page', '%title' => $edit['title'])), 'Page created: ' . $input['href']);
 129        }
 130        $url = $this->getUrl();
 131  
 132        // change to anonym user
 133        $this->drupalLogout();
 134      
 135        $this->drupalGet($url);
 136        //debug($this);
 137        // If simpletest starts using something to override the error system, this will flag
 138        // us and let us know it's broken.
 139        $this->assertFalse(libxml_use_internal_errors(TRUE));
 140        $path = '//a[@href="'. $input['href'] .'" and text()="'. $input['label'] .'"]';
 141        //$this->pass(htmlentities($path));
 142        $elements = $this->xpath($path);
 143        libxml_use_internal_errors(FALSE);
 144        $this->assertIdentical(isset($elements[0]), $input['type'] == self::LINK_INPUT_TYPE_GOOD, $input['msg']);
 145      }
 146      //libxml_use_internal_errors(FALSE);
 147    }
 148    
 149    /**
 150     * Creates a link field for the "page" type and creates a page with a link.
 151     * Just like the above test, only here we're turning off the validation on the field.
 152     */
 153    function testLinkCreate_NoValidation() {
 154      //libxml_use_internal_errors(true);
 155      $account = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
 156      $this->drupalLogin($account);
 157  
 158      // create field
 159      $name = strtolower($this->randomName());
 160      $edit = array(
 161        '_add_new_field[label]' => $name,
 162        '_add_new_field[field_name]' => $name,
 163        '_add_new_field[type]' => 'link',
 164        '_add_new_field[widget_type]' => 'link',
 165      );
 166      $this->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
 167      $this->drupalPost(NULL, array('validate_url' => FALSE), t('Save field settings'));
 168  
 169      // Is field created?
 170      $this->assertRaw(t('Added field %label.', array('%label' => $name)), 'Field added');
 171      _content_type_info(TRUE);
 172      $fields = content_fields();
 173      $this->assertTRUE(0 === $fields['field_'. $name]['validate_url'], 'Make sure validation is off.');
 174      
 175      // create page form
 176      $this->drupalGet('node/add/page');
 177      $field_name = 'field_' . $name;
 178      $this->assertField($field_name . '[0][title]', 'Title found');
 179      $this->assertField($field_name . '[0][url]', 'URL found');
 180  
 181      $input_test_cases = array(
 182        array(
 183          'href' => 'http://example.com/' . $this->randomName(),
 184          'label' => $this->randomName(),
 185          'msg' => 'Link found',
 186          'type' => self::LINK_INPUT_TYPE_GOOD
 187        ),
 188        array(
 189          'href' => 'http://example.com/' . $this->randomName(),
 190          'label' => $this->randomName() . '<script>alert("hi");</script>',
 191          'msg' => 'js label',
 192          'type' => self::LINK_INPUT_TYPE_BAD_TITLE
 193        ),
 194        array(
 195          'href' => 'http://example.com/' . $this->randomName(),
 196          'label' => $this->randomName() . '<script src="http://devil.site.com"></script>',
 197          'msg' => 'js label',
 198          'type' => self::LINK_INPUT_TYPE_BAD_TITLE
 199        ),
 200        array(
 201          'href' => 'http://example.com/' . $this->randomName(),
 202          'label' => $this->randomName() . '" onmouseover="alert(\'hi\')',
 203          'msg' => 'js label',
 204          'type' => self::LINK_INPUT_TYPE_BAD_TITLE
 205        ),
 206         array(
 207          'href' => 'http://example.com/' . $this->randomName(),
 208          'label' => $this->randomName() . '\' onmouseover="alert(\'hi\')',
 209          'msg' => 'js label',
 210          'type' => self::LINK_INPUT_TYPE_BAD_TITLE
 211        ),
 212       array(
 213          'href' => 'javascript:alert("http://example.com/' . $this->randomName() . '")',
 214          'label' => $this->randomName(),
 215          'msg' => 'js url',
 216          'type' => self::LINK_INPUT_TYPE_BAD_URL
 217        ),
 218      );
 219      foreach ($input_test_cases as $input) {
 220        $this->drupalLogin($account);
 221        $this->drupalGet('node/add/page');
 222  
 223        $edit = array(
 224          'title' => $input['label'],
 225          $field_name . '[0][title]' => $input['label'],
 226          $field_name . '[0][url]' => $input['href'],
 227        );
 228        $this->drupalPost(NULL, $edit, t('Save'));
 229        if ($input['type'] == self::LINK_INPUT_TYPE_BAD_URL) {
 230          //$this->assertRaw(t('Not a valid URL.'), 'Not a valid URL: ' . $input['href']);
 231          $this->assertNoRaw($input['href']);
 232          $this->assertRaw(t('@type %title has been created.', array('@type' => 'Page', '%title' => $edit['title'])), 'Page created: ' . $input['href']);
 233          continue;
 234        }
 235        else {
 236          $this->assertRaw(t('@type %title has been created.', array('@type' => 'Page', '%title' => $edit['title'])), 'Page created: ' . $input['href']);
 237        }
 238        $url = $this->getUrl();
 239  
 240        // change to anonym user
 241        $this->drupalLogout();
 242      
 243        $this->drupalGet($url);
 244        //debug($this);
 245        // If simpletest starts using something to override the error system, this will flag
 246        // us and let us know it's broken.
 247        $this->assertFalse(libxml_use_internal_errors(TRUE));
 248        $path = '//a[@href="'. $input['href'] .'" and text()="'. $input['label'] .'"]';
 249        //$this->pass(htmlentities($path));
 250        $elements = $this->xpath($path);
 251        libxml_use_internal_errors(FALSE);
 252        $this->assertIdentical(isset($elements[0]), $input['type'] == self::LINK_INPUT_TYPE_GOOD, $input['msg']);
 253      }
 254      //libxml_use_internal_errors(FALSE);
 255    }
 256    
 257    /**
 258     * Testing that if you use <strong> in a static title for your link, that the
 259     * title actually displays <strong>.
 260     */
 261    function testStaticLinkCreate() {
 262      $account = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
 263      $this->drupalLogin($account);
 264  
 265      // create field
 266      $name = strtolower($this->randomName());
 267      $field_name = 'field_'. $name;
 268      $edit = array(
 269        '_add_new_field[label]' => $name,
 270        '_add_new_field[field_name]' => $name,
 271        '_add_new_field[type]' => 'link',
 272        '_add_new_field[widget_type]' => 'link',
 273      );
 274      $this->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
 275      $this->drupalPost(NULL, array(
 276        'title' => 'value',
 277        'title_value' => '<strong>'. $name .'</strong>'), t('Save field settings'));
 278  
 279      // Is field created?
 280      $this->assertRaw(t('Added field %label.', array('%label' => $name)), 'Field added');
 281  
 282      // create page form
 283      $this->drupalGet('node/add/page');
 284      $this->assertField($field_name . '[0][url]', 'URL found');
 285      
 286      $input = array(
 287        'href' => 'http://example.com/' . $this->randomName()
 288      );
 289      
 290      $edit = array(
 291        'title' => $name,
 292        $field_name . '[0][url]' => $input['href'],
 293      );
 294      $this->drupalPost(NULL, $edit, t('Save'));    
 295      
 296      $url = $this->getUrl();
 297  
 298      // change to anonymous user
 299      $this->drupalLogout();    
 300      $this->drupalGet($url);
 301  
 302      $this->assertRaw(l('<strong>'. $name .'</strong>', $input['href'], array('html' => TRUE)));
 303    }
 304    
 305    /**
 306     * If we're creating a new field and just hit 'save' on the default options, we want to make
 307     * sure they are set to the expected results.
 308     */
 309    function testCRUDCreateFieldDefaults() {
 310      $account = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
 311      $this->drupalLogin($account);
 312  
 313      // create field
 314      $name = strtolower($this->randomName());
 315      $edit = array(
 316        '_add_new_field[label]' => $name,
 317        '_add_new_field[field_name]' => $name,
 318        '_add_new_field[type]' => 'link',
 319        '_add_new_field[widget_type]' => 'link',
 320      );
 321      $this->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
 322      $this->drupalPost(NULL, array(), t('Save field settings'));
 323  
 324      // Is field created?
 325      $this->assertRaw(t('Added field %label.', array('%label' => $name)), 'Field added');
 326      _content_type_info(TRUE);
 327      $fields = content_fields();
 328      $field = $fields['field_'. $name];
 329      $this->assertTrue(1 === $field['validate_url'], 'Make sure validation is on.');
 330      $this->assertFalse($field['required'], 'Make sure field is not required.');
 331      $this->assertEqual($field['title'], 'optional', 'Title should be optional by default.');
 332      $this->assertFalse($field['enable_tokens'], 'Enable Tokens should be off by default.');
 333      $this->assertEqual($field['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
 334      $this->assertEqual($field['attributes']['target'], 'default', 'Target should be "default"');
 335      $this->assertFalse($field['attributes']['rel'], 'Rel should be blank by default.');
 336      $this->assertFalse($field['attributes']['class'], 'By default, no class should be set.');
 337      $this->assertFalse($field['attributes']['title'], 'By default, no title should be set.');
 338      
 339      //$this->fail('<pre>'. print_r($fields['field_'. $name], TRUE) .'</pre>');
 340    }
 341  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7