[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

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

   1  <?php
   2  // $Id$
   3  
   4  /**
   5   * @file
   6   * Webform module tests.
   7   */
   8  
   9  class WebformTestCase extends DrupalWebTestCase {
  10    private $_webform_node;
  11    private $_webform_components;
  12    public $webform_users;
  13  
  14    /**
  15     * Implementation of setUp().
  16     */
  17    function setUp() {
  18      // Enable Webform.
  19      parent::setUp('webform', 'profile');
  20  
  21      // Create a profile field to test %profile tokens.
  22      db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, register, visibility, autocomplete, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, %d, '%s', '%s')", 'Gender', 'profile_gender', '', 'Profile', 'textfield', 0, 0, 0, 2, 0, '', '');
  23  
  24      // Create a normal user that can view their own submissions.
  25      $permissions['userAccess'] = array(
  26        'access content',
  27        'access own webform submissions',
  28      );
  29  
  30      // Create a normal user than can edit their own submissions.
  31      $permissions['userEdit'] = array(
  32        'access content',
  33        'edit own webform submissions',
  34      );
  35  
  36      // Create a webform editor to test creating and editing own content.
  37      $permissions['editor'] = array(
  38        'access content',
  39        'create webform content',
  40        'edit own webform content',
  41        'access all webform results',
  42      );
  43  
  44      // Create a webform admin that will do all node creation.
  45      $permissions['admin'] = array(
  46        'access content',
  47        'administer nodes',
  48        'create webform content',
  49        'edit any webform content',
  50        'access all webform results',
  51        'edit all webform submissions',
  52        'delete all webform submissions',
  53      );
  54  
  55      foreach ($permissions as $user_key => $role_permissions) {
  56        $this->webform_users[$user_key] = $this->drupalCreateUser($role_permissions);
  57        $profile = array('profile_gender' => 'Female');
  58        $this->webform_users[$user_key]->profile_gender = 'Female';
  59        profile_save_profile($profile, $this->webform_users[$user_key], 'Profile');
  60      }
  61    }
  62  
  63    /**
  64     * Implemenation of tearDown().
  65     */
  66    function tearDown() {
  67      // Delete the webform admin and any created nodes.
  68      foreach ($this->webform_users as $account) {
  69        $uid = $account->uid;
  70        $result = db_query('SELECT nid FROM {node} WHERE uid = %d', $uid);
  71        while ($node = db_fetch_array($result)) {
  72          node_delete($node['nid']);
  73        }
  74        user_delete(array(), $uid);
  75      }
  76  
  77      parent::tearDown();
  78    }
  79  
  80    /**
  81     *
  82     */
  83    function webformReset() {
  84      $this->_webform_node = NULL;
  85      $this->_webform_components = NULL;
  86    }
  87  
  88    /**
  89     * Provide a list of components to test throughout the suite.
  90     *
  91     * Each component provides:
  92     *   - A default configuration for the component.
  93     *   - Values to try setting via POST
  94     *   - Values that should match the database storage when set via POST
  95     *   - Values that should match the database storage when using the default values.
  96     *
  97     * @return array
  98     *   An array of each component settings.
  99     */
 100    function testWebformComponents() {
 101      if (isset($this->_webform_components)) {
 102        return $this->_webform_components;
 103      }
 104  
 105      $this->_webform_components = array(
 106        // Test date components.
 107        'date' => array(
 108          'component' => array(
 109            'form_key' => 'date',
 110            'name' => 'Date',
 111            'type' => 'date',
 112            'value' => '19 Nov 1978',
 113            'extra' => array(
 114              'timezone' => 'site',
 115              'year_start' => '1950',
 116              'year_end' => '2020',
 117            ),
 118            'mandatory' => '0',
 119            'email' => '1',
 120            'pid' => '0',
 121            'weight' => '-15',
 122            'page_num' => 1,
 123          ),
 124          'sample values' => array('day' => '30', 'month' => '9', 'year' => '1982'),
 125          'database values' => array('1982-09-30'),
 126          'database default values' => array('1978-11-19'),
 127        ),
 128  
 129        // Test grid components.
 130        'grid' => array(
 131          'component' => array(
 132            'form_key' => 'grid',
 133            'name' => 'Grid',
 134            'type' => 'grid',
 135            'value' => '',
 136            'extra' => array(
 137              'questions' => "0|Ålphå\n1|ıé†å\n2|Î鬆å", // Left side
 138              'options' => "0|øne\n1|twö\n2|ǼBƇ\n3|€Euro", // Top
 139            ),
 140            'mandatory' => '0',
 141            'email' => '1',
 142            'pid' => '2',
 143            'weight' => '-19',
 144            'page_num' => 1,
 145          ),
 146          'sample values' => array('0' => '0', '1' => '1', '2' => '2'),
 147          'database values' => array('0' => '0', '1' => '1', '2' => '2'),
 148          'database default values' => array('', '', ''),
 149        ),
 150        'grid_keyed' => array(
 151          'component' => array(
 152            'form_key' => 'grid_keyed',
 153            'name' => 'Grid Keyed',
 154            'type' => 'grid',
 155            'value' => '',
 156            'extra' => array(
 157              'questions' => "one|What's your option?\ntwo|Agåin?\nthree|One more time!", // Left side.
 158              'options' => "one|Option one\ntwo|Option 2\nthree| Three is me", // Top
 159            ),
 160            'mandatory' => '0',
 161            'email' => '1',
 162            'pid' => '0',
 163            'weight' => '-15',
 164            'page_num' => 1,
 165          ),
 166          'sample values' => array('one' => 'one', 'two' => 'two', 'three' => 'three'),
 167          'database values' => array('one' => 'one', 'two' => 'two', 'three' => 'three'),
 168          'database default values' => array('one' => '', 'two' => '', 'three' => ''),
 169        ),
 170  
 171        // Test select components.
 172        'checkboxes' => array(
 173          'component' => array(
 174            'form_key' => 'checkboxes',
 175            'name' => 'Checkboxes',
 176            'type' => 'select',
 177            'value' => 'two',
 178            'extra' => array(
 179              'items' => "one|one\ntwo|two\nthree|three",
 180              'multiple' => 1,
 181              'email' => 0,
 182            ),
 183            'mandatory' => '0',
 184            'email' => '1',
 185            'pid' => '0',
 186            'weight' => '-15',
 187            'page_num' => 1,
 188          ),
 189          'sample values' => array('one' => TRUE, 'two' => FALSE, 'three' => TRUE),
 190          'database values' => array('one', 'three'),
 191          'database default values' => array('two'),
 192        ),
 193        'checkboxes_zero' => array(
 194          'component' => array(
 195            'form_key' => 'checkboxes_zero',
 196            'name' => 'Checkboxes zero',
 197            'type' => 'select',
 198            'value' => '0',
 199            'extra' => array(
 200              'items' => "0|zero\n1|one\n2|two",
 201              'multiple' => 1,
 202              'email' => 0,
 203            ),
 204            'mandatory' => '1',
 205            'email' => '1',
 206            'pid' => '0',
 207            'weight' => '-9',
 208            'page_num' => 1,
 209          ),
 210          'sample values' => array('0' => TRUE),
 211          'database values' => array('0'),
 212          'database default values' => array('0'),
 213        ),
 214        'radios' => array(
 215          'component' => array(
 216            'form_key' => 'radios',
 217            'name' => 'Radios',
 218            'type' => 'select',
 219            'value' => 'two',
 220            'extra' => array(
 221              'items' => "one|one\ntwo|two\nthree|three",
 222              'email' => 0,
 223            ),
 224            'mandatory' => '1',
 225            'email' => '1',
 226            'pid' => '0',
 227            'weight' => '-9',
 228            'page_num' => 1,
 229          ),
 230          'sample values' => 'one',
 231          'database values' => array('one'),
 232          'database default values' => array('two'),
 233        ),
 234        'radios_zero' => array(
 235          'component' => array(
 236            'form_key' => 'radios_zero',
 237            'name' => 'Radios zero',
 238            'type' => 'select',
 239            'value' => '0',
 240            'extra' => array(
 241              'items' => "0|zero\n1|one\n2|two",
 242              'email' => 0,
 243            ),
 244            'mandatory' => '1',
 245            'email' => '1',
 246            'pid' => '0',
 247            'weight' => '-9',
 248            'page_num' => 1,
 249          ),
 250          'sample values' => '0',
 251          'database values' => array('0'),
 252          'database default values' => array('0'),
 253        ),
 254        'select' => array(
 255          'component' => array(
 256            'form_key' => 'select',
 257            'name' => 'Select',
 258            'type' => 'select',
 259            'value' => 'one',
 260            'extra' => array(
 261              'description' => '<p>Description here</p>',
 262              'items' => "one|one\ntwo|two\nthree|three\nfour|four\nfive|five\nsix|six",
 263              'aslist' => 1,
 264              'email' => 0,
 265            ),
 266            'mandatory' => '1',
 267            'email' => '1',
 268            'pid' => '0',
 269            'weight' => '-15',
 270            'page_num' => 1,
 271          ),
 272          'sample values' => 'two',
 273          'database values' => array('two'),
 274          'database default values' => array('one'),
 275        ),
 276        'select_zero' => array(
 277          'component' => array(
 278            'form_key' => 'select_zero',
 279            'name' => 'Select',
 280            'type' => 'select',
 281            'value' => '0',
 282            'extra' => array(
 283              'description' => '<p>Tests saving zero as a value.</p>',
 284              'items' => "0|zero\n1|one\n2|two",
 285              'aslist' => 1,
 286              'email' => 0,
 287            ),
 288            'mandatory' => '1',
 289            'email' => '1',
 290            'pid' => '0',
 291            'weight' => '-15',
 292            'page_num' => 1,
 293          ),
 294          'sample values' => '0',
 295          'database values' => array('0'),
 296          'database default values' => array('0'),
 297        ),
 298        'select_optgroup' => array(
 299          'component' => array(
 300            'form_key' => 'select_optgroup',
 301            'name' => 'Select Optgroup',
 302            'type' => 'select',
 303            'value' => 'option 1-2',
 304            'extra' => array(
 305              'description' => '<p>Tests saving zero as a value.</p>',
 306              'items' => "<Group 1>\noption 1-1|option 1-1\noption 1-2|option 1-2\noption 1-3|option 1-3\n<Group 2>\noption 2-1|option 2-1\noption 2-2|option 2-2\noption 2-3|option 2-3",
 307              'aslist' => 1,
 308              'email' => 0,
 309            ),
 310            'mandatory' => '1',
 311            'email' => '1',
 312            'pid' => '0',
 313            'weight' => '-15',
 314            'page_num' => 1,
 315          ),
 316          'sample values' => 'option 2-2',
 317          'database values' => array('option 2-2'),
 318          'database default values' => array('option 1-2'),
 319        ),
 320        'select_email' => array(
 321          'component' => array(
 322            'form_key' => 'select_email',
 323            'name' => 'Select e-mails',
 324            'type' => 'select',
 325            'value' => 'nate@localhost.localhost',
 326            'extra' => array(
 327              'items' => "nate@localhost.localhost|one\nadmin@localhost.localhost|two",
 328              'email' => 3,
 329            ),
 330            'mandatory' => '0',
 331            'email' => '1',
 332            'pid' => '2',
 333            'weight' => '-17',
 334            'page_num' => 1,
 335          ),
 336          'sample values' => 'admin@localhost.localhost',
 337          'database values' => array('admin@localhost.localhost'),
 338          'database default values' => array('nate@localhost.localhost'),
 339        ),
 340        'select_multiple' => array(
 341          'component' => array(
 342            'form_key' => 'select_multiple',
 343            'name' => 'Select Multiple',
 344            'type' => 'select',
 345            'value' => 'one,two',
 346            'extra' => array(
 347              'items' => "one|one\ntwo|two\nthree|three",
 348              'multiple' => 1,
 349              'aslist' => 1,
 350              'email' => 0,
 351            ),
 352            'mandatory' => '0',
 353            'email' => '1',
 354            'pid' => '0',
 355            'weight' => '-10',
 356            'page_num' => 1,
 357          ),
 358          // TODO: I'd like to test a value, but SimpleTest can't set multiple values.
 359          'sample values' => NULL,
 360          'database values' => array('one', 'two'),
 361          'database default values' => array('one', 'two'),
 362        ),
 363  
 364        // Test date components.
 365        'date_textfield' => array(
 366          'component' => array(
 367            'form_key' => 'date_textfield',
 368            'name' => 'Date Textfield',
 369            'type' => 'date',
 370            'value' => 'Nov 19 1978',
 371            'extra' => array(
 372              'timezone' => 'site',
 373              'year_start' => '1900',
 374              'year_end' => '2050',
 375              'year_textfield' => 1,
 376            ),
 377            'mandatory' => '1',
 378            'email' => '1',
 379            'pid' => '0',
 380            'weight' => '-7',
 381            'page_num' => 1,
 382          ),
 383          'sample values' => array('day' => '30', 'month' => '9', 'year' => '1982'),
 384          'database values' => array('1982-09-30'),
 385          'database default values' => array('1978-11-19'),
 386        ),
 387  
 388        // Test email components.
 389        'email' => array(
 390          'component' => array(
 391            'form_key' => 'email',
 392            'name' => 'E-mail',
 393            'type' => 'email',
 394            'value' => '%useremail',
 395            'extra' => array(
 396              'email' => 19,
 397            ),
 398            'mandatory' => '0',
 399            'email' => '1',
 400            'pid' => '0',
 401            'weight' => '-5',
 402            'page_num' => 1,
 403          ),
 404          'sample values' => 'admin@localhost.localhost',
 405          'database values' => array('admin@localhost.localhost'),
 406          'database default values' => array($this->webform_users['admin']->mail),
 407        ),
 408  
 409        // Test hidden components.
 410        'hidden' => array(
 411          'component' => array(
 412            'form_key' => 'hidden',
 413            'name' => 'Hidden',
 414            'type' => 'hidden',
 415            'value' => 'default hidden value',
 416            'extra' => array(
 417              'email' => 0,
 418            ),
 419            'mandatory' => '1',
 420            'email' => '1',
 421            'pid' => '0',
 422            'weight' => '-4',
 423            'page_num' => 1,
 424          ),
 425          'sample values' => NULL,
 426          'database values' => array('default hidden value'),
 427          'database default values' => array('default hidden value'),
 428        ),
 429  
 430        // Test textarea components.
 431        'textarea' => array(
 432          'component' => array(
 433            'form_key' => 'textarea',
 434            'name' => 'Textarea',
 435            'type' => 'textarea',
 436            'value' => 'sample textarea default value',
 437            'extra' => array(),
 438            'mandatory' => '0',
 439            'email' => '1',
 440            'pid' => '0',
 441            'weight' => '15',
 442            'page_num' => 1,
 443          ),
 444          'sample values' => 'sample textarea value',
 445          'database values' => array('sample textarea value'),
 446          'database default values' => array('sample textarea default value'),
 447        ),
 448  
 449        // Test textfield components.
 450        'textfield_disabled' => array(
 451          'component' => array(
 452            'form_key' => 'textfield_disabled',
 453            'name' => 'Textfield Disabled',
 454            'type' => 'textfield',
 455            'value' => '%get[foo]',
 456            'extra' => array(
 457              'disabled' => 1,
 458            ),
 459            'mandatory' => '0',
 460            'email' => '1',
 461            'pid' => '0',
 462            'weight' => '-15',
 463            'page_num' => 1,
 464          ),
 465          'sample values' => NULL,
 466          'database values' => array('bar'),
 467          'database default values' => array('bar'),
 468        ),
 469        'textfield_profile' => array(
 470          'component' => array(
 471            'form_key' => 'textfield_profile',
 472            'name' => 'Textfield Profile',
 473            'type' => 'textfield',
 474            'value' => '%profile[profile_gender]',
 475            'extra' => array(
 476              'width' => '20',
 477            ),
 478            'mandatory' => '0',
 479            'email' => '1',
 480            'pid' => '0',
 481            'weight' => '-6',
 482            'page_num' => 1,
 483          ),
 484          'sample values' => 'Female',
 485          'database values' => array('Female'),
 486          'database default values' => array($this->webform_users['admin']->profile_gender),
 487        ),
 488  
 489        // Test time components.
 490        'time' => array(
 491          'component' => array(
 492            'form_key' => 'time',
 493            'name' => 'Time',
 494            'type' => 'time',
 495            'value' => '10:30pm',
 496            'extra' => array(
 497              'timezone' => 'site',
 498              'hourformat' => '12-hour',
 499            ),
 500            'mandatory' => '0',
 501            'email' => '1',
 502            'pid' => '0',
 503            'weight' => '16',
 504            'page_num' => 1,
 505          ),
 506          'sample values' => array('hour' => '5', 'minute' => '0', 'ampm' => 'am'),
 507          'database values' => array('05:00:00'),
 508          'database default values' => array('22:30:00'),
 509        ),
 510        'time_24h' => array(
 511          'component' => array(
 512            'form_key' => 'time_24h',
 513            'name' => 'Time 24H',
 514            'type' => 'time',
 515            'value' => '10:30pm',
 516            'extra' => array(
 517              'timezone' => 'site',
 518              'hourformat' => '24-hour',
 519            ),
 520            'mandatory' => '0',
 521            'email' => '1',
 522            'pid' => '0',
 523            'weight' => '17',
 524            'page_num' => 1,
 525          ),
 526          'sample values' => array('hour' => '5', 'minute' => '0'),
 527          'database values' => array('05:00:00'),
 528          'database default values' => array('22:30:00'),
 529        ),
 530      );
 531  
 532      return $this->_webform_components;
 533    }
 534  
 535    function testWebformForm() {
 536      if (isset($this->_webform_node)) {
 537        return $this->_webform_node;
 538      }
 539  
 540      $settings = array(
 541       'type' => 'webform',
 542       'language' => '',
 543       'uid' => '1',
 544       'status' => '1',
 545       'promote' => '1',
 546       'moderate' => '0',
 547       'sticky' => '0',
 548       'tnid' => '0',
 549       'translate' => '0',
 550       'title' => 'Test Webform',
 551       'body' => 'Donec placerat. Nullam nibh dolor, blandit sed, fermentum id, imperdiet sit amet, neque. Nam mollis ultrices justo. Sed tempor. Sed vitae tellus. Etiam sem arcu, eleifend sit amet, gravida eget, porta at, wisi. Nam non lacus vitae ipsum viverra pretium. Phasellus massa. Fusce magna sem, gravida in, feugiat ac, molestie eget, wisi. Fusce consectetuer luctus ipsum. Vestibulum nunc. Suspendisse dignissim adipiscing libero. Integer leo. Sed pharetra ligula a dui. Quisque ipsum nibh, ullamcorper eget, pulvinar sed, posuere vitae, nulla. Sed varius nibh ut lacus. Curabitur fringilla. Nunc est ipsum, pretium quis, dapibus sed, varius non, lectus. Proin a quam. Praesent lacinia, eros quis aliquam porttitor, urna lacus volutpat urna, ut fermentum neque mi egestas dolor.',
 552       'teaser' => 'Donec placerat. Nullam nibh dolor, blandit sed, fermentum id, imperdiet sit amet, neque. Nam mollis ultrices justo. Sed tempor. Sed vitae tellus. Etiam sem arcu, eleifend sit amet, gravida eget, porta at, wisi. Nam non lacus vitae ipsum viverra pretium. Phasellus massa. Fusce magna sem, gravida in, feugiat ac, molestie eget, wisi. Fusce consectetuer luctus ipsum. Vestibulum nunc. Suspendisse dignissim adipiscing libero. Integer leo. Sed pharetra ligula a dui. Quisque ipsum nibh, ullamcorper eget, pulvinar sed, posuere vitae, nulla. Sed varius nibh ut lacus. Curabitur fringilla.',
 553       'log' => '',
 554       'format' => '1',
 555       'webform' => array(
 556          'confirmation' => 'Thanks!',
 557          'confirmation_format' => FILTER_FORMAT_DEFAULT,
 558          'redirect_url' => '',
 559          'teaser' => '0',
 560          'allow_draft' => '1',
 561          'submit_text' => '',
 562          'submit_limit' => '-1',
 563          'submit_interval' => '-1',
 564          'submit_notice' => '1',
 565          'roles' => array('1', '2'),
 566          'components' => array(),
 567          'emails' => array(),
 568        ),
 569      );
 570  
 571      $cid = 0;
 572      foreach ($this->testWebformComponents() as $key => $component_info) {
 573        $cid++;
 574        $settings['webform']['components'][$cid] = $component_info['component'];
 575        $settings['webform']['components'][$cid]['cid'] = $cid;
 576        $settings['webform']['components'][$cid]['pid'] = 0;
 577      }
 578  
 579      $this->_webform_node = $this->drupalCreateNode($settings);
 580  
 581      return $this->_webform_node;
 582    }
 583  
 584  
 585    function testWebformPost() {
 586      $edit = array();
 587      foreach ($this->testWebformComponents() as $key => $component_info) {
 588        if (is_array($component_info['sample values'])) {
 589          foreach ($component_info['sample values'] as $subkey => $value) {
 590            $edit["submitted[$key][$subkey]"] = $value;
 591          }
 592        }
 593        elseif ($component_info['sample values'] != NULL) {
 594          $value = $component_info['sample values'];
 595          // Multiple selects have a funky extra empty bracket in the name.
 596          $extra = $key == 'select_multiple' ? '[]' : '';
 597          $edit["submitted[$key]$extra"] = $value;
 598        }
 599      }
 600      return $edit;
 601    }
 602  
 603    /**
 604     * Utility function to print out the current page being tested.
 605     */
 606    function webformPrintPage() {
 607      exit($this->_browser->_page->getRaw());
 608    }
 609  }
 610  


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