[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Webform module tests.
   6   */
   7  
   8  class WebformTestCase extends DrupalWebTestCase {
   9    private $_webform_node;
  10    private $_webform_components;
  11    public $webform_users;
  12  
  13    /**
  14     * Implements setUp().
  15     */
  16    function setUp() {
  17      // Enable Webform.
  18      parent::setUp('webform', 'profile');
  19  
  20      // Create a profile field to test %profile tokens.
  21      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, '', '');
  22  
  23      // Create a normal user that can view their own submissions.
  24      $permissions['userAccess'] = array(
  25        'access content',
  26        'access own webform submissions',
  27      );
  28  
  29      // Create a normal user than can edit their own submissions.
  30      $permissions['userEdit'] = array(
  31        'access content',
  32        'edit own webform submissions',
  33      );
  34  
  35      // Create a webform editor to test creating and editing own content.
  36      $permissions['editor'] = array(
  37        'access content',
  38        'create webform content',
  39        'edit own webform content',
  40        'access all webform results',
  41      );
  42  
  43      // Create a webform admin that will do all node creation.
  44      $permissions['admin'] = array(
  45        'access content',
  46        'administer nodes',
  47        'create webform content',
  48        'edit any webform content',
  49        'access all webform results',
  50        'edit all webform submissions',
  51        'delete all webform submissions',
  52      );
  53  
  54      foreach ($permissions as $user_key => $role_permissions) {
  55        $this->webform_users[$user_key] = $this->drupalCreateUser($role_permissions);
  56        $profile = array('profile_gender' => 'Female');
  57        $this->webform_users[$user_key]->profile_gender = 'Female';
  58        profile_save_profile($profile, $this->webform_users[$user_key], 'Profile');
  59      }
  60    }
  61  
  62    /**
  63     * Implemenation of tearDown().
  64     */
  65    function tearDown() {
  66      // Delete the webform admin and any created nodes.
  67      foreach ($this->webform_users as $account) {
  68        $uid = $account->uid;
  69        $result = db_query('SELECT nid FROM {node} WHERE uid = %d', $uid);
  70        while ($node = db_fetch_array($result)) {
  71          node_delete($node['nid']);
  72        }
  73        user_delete(array(), $uid);
  74      }
  75  
  76      parent::tearDown();
  77    }
  78  
  79    /**
  80     *
  81     */
  82    function webformReset() {
  83      $this->_webform_node = NULL;
  84      $this->_webform_components = NULL;
  85    }
  86  
  87    /**
  88     * Provide a list of components to test throughout the suite.
  89     *
  90     * Each component provides:
  91     *   - A default configuration for the component.
  92     *   - Values to try setting via POST
  93     *   - Values that should match the database storage when set via POST
  94     *   - Values that should match the database storage when using the default values.
  95     *
  96     * @return array
  97     *   An array of each component settings.
  98     */
  99    function testWebformComponents() {
 100      if (isset($this->_webform_components)) {
 101        return $this->_webform_components;
 102      }
 103  
 104      $this->_webform_components = array(
 105        // Test date components.
 106        'date' => array(
 107          'component' => array(
 108            'form_key' => 'date',
 109            'name' => 'Date',
 110            'type' => 'date',
 111            'value' => '19 Nov 1978',
 112            'extra' => array(
 113              'timezone' => 'site',
 114              'start_date' => '-100 years',
 115              'end_date' => '+2 years',
 116            ),
 117            'mandatory' => '0',
 118            'pid' => '0',
 119            'weight' => '-15',
 120          ),
 121          'sample values' => array('day' => '30', 'month' => '9', 'year' => '1982'),
 122          'database values' => array('1982-09-30'),
 123          'database default values' => array('1978-11-19'),
 124        ),
 125  
 126        // Test grid components.
 127        'grid' => array(
 128          'component' => array(
 129            'form_key' => 'grid',
 130            'name' => 'Grid',
 131            'type' => 'grid',
 132            'value' => '',
 133            'extra' => array(
 134              'questions' => "0|Ålphå\n1|ıé†å\n2|Î鬆å", // Left side
 135              'options' => "0|øne\n1|twö\n2|ǼBƇ\n3|€Euro", // Top
 136            ),
 137            'mandatory' => '0',
 138            'pid' => '2',
 139            'weight' => '-19',
 140          ),
 141          'sample values' => array('0' => '0', '1' => '1', '2' => '2'),
 142          'database values' => array('0' => '0', '1' => '1', '2' => '2'),
 143          'database default values' => array('', '', ''),
 144        ),
 145        'grid_keyed' => array(
 146          'component' => array(
 147            'form_key' => 'grid_keyed',
 148            'name' => 'Grid Keyed',
 149            'type' => 'grid',
 150            'value' => '',
 151            'extra' => array(
 152              'questions' => "one|What's your option?\ntwo|Agåin?\nthree|One more time!", // Left side.
 153              'options' => "one|Option one\ntwo|Option 2\nthree| Three is me", // Top
 154            ),
 155            'mandatory' => '0',
 156            'pid' => '0',
 157            'weight' => '-15',
 158          ),
 159          'sample values' => array('one' => 'one', 'two' => 'two', 'three' => 'three'),
 160          'database values' => array('one' => 'one', 'two' => 'two', 'three' => 'three'),
 161          'database default values' => array('one' => '', 'two' => '', 'three' => ''),
 162        ),
 163  
 164        // Test select components.
 165        'checkboxes' => array(
 166          'component' => array(
 167            'form_key' => 'checkboxes',
 168            'name' => 'Checkboxes',
 169            'type' => 'select',
 170            'value' => 'two',
 171            'extra' => array(
 172              'items' => "one|one\ntwo|two\nthree|three",
 173              'multiple' => 1,
 174            ),
 175            'mandatory' => '0',
 176            'pid' => '0',
 177            'weight' => '-15',
 178          ),
 179          'sample values' => array('one' => TRUE, 'two' => FALSE, 'three' => TRUE),
 180          'database values' => array('one', 'three'),
 181          'database default values' => array('two'),
 182        ),
 183        'checkboxes_zero' => array(
 184          'component' => array(
 185            'form_key' => 'checkboxes_zero',
 186            'name' => 'Checkboxes zero',
 187            'type' => 'select',
 188            'value' => '0',
 189            'extra' => array(
 190              'items' => "0|zero\n1|one\n2|two",
 191              'multiple' => 1,
 192            ),
 193            'mandatory' => '1',
 194            'pid' => '0',
 195            'weight' => '-9',
 196          ),
 197          'sample values' => array('0' => TRUE),
 198          'database values' => array('0'),
 199          'database default values' => array('0'),
 200        ),
 201        'radios' => array(
 202          'component' => array(
 203            'form_key' => 'radios',
 204            'name' => 'Radios',
 205            'type' => 'select',
 206            'value' => 'two',
 207            'extra' => array(
 208              'items' => "one|one\ntwo|two\nthree|three",
 209            ),
 210            'mandatory' => '1',
 211            'pid' => '0',
 212            'weight' => '-9',
 213          ),
 214          'sample values' => 'one',
 215          'database values' => array('one'),
 216          'database default values' => array('two'),
 217        ),
 218        'radios_zero' => array(
 219          'component' => array(
 220            'form_key' => 'radios_zero',
 221            'name' => 'Radios zero',
 222            'type' => 'select',
 223            'value' => '0',
 224            'extra' => array(
 225              'items' => "0|zero\n1|one\n2|two",
 226            ),
 227            'mandatory' => '1',
 228            'pid' => '0',
 229            'weight' => '-9',
 230          ),
 231          'sample values' => '0',
 232          'database values' => array('0'),
 233          'database default values' => array('0'),
 234        ),
 235        'select' => array(
 236          'component' => array(
 237            'form_key' => 'select',
 238            'name' => 'Select',
 239            'type' => 'select',
 240            'value' => 'one',
 241            'extra' => array(
 242              'description' => 'Description here',
 243              'items' => "one|one\ntwo|two\nthree|three\nfour|four\nfive|five\nsix|six",
 244              'aslist' => 1,
 245            ),
 246            'mandatory' => '1',
 247            'pid' => '0',
 248            'weight' => '-15',
 249          ),
 250          'sample values' => 'two',
 251          'database values' => array('two'),
 252          'database default values' => array('one'),
 253        ),
 254        'select_zero' => array(
 255          'component' => array(
 256            'form_key' => 'select_zero',
 257            'name' => 'Select zero',
 258            'type' => 'select',
 259            'value' => '0',
 260            'extra' => array(
 261              'description' => 'Tests saving zero as a value.',
 262              'items' => "0|zero\n1|one\n2|two",
 263              'aslist' => 1,
 264            ),
 265            'mandatory' => '1',
 266            'pid' => '0',
 267            'weight' => '-15',
 268          ),
 269          'sample values' => '0',
 270          'database values' => array('0'),
 271          'database default values' => array('0'),
 272        ),
 273        'select_no_default' => array(
 274          'component' => array(
 275            'form_key' => 'select_no_default',
 276            'name' => 'Select no default',
 277            'type' => 'select',
 278            'value' => '',
 279            'extra' => array(
 280              'description' => 'Description here',
 281              'items' => "one|one\ntwo|two\nthree|three\nfour|four\nfive|five\nsix|six",
 282              'aslist' => 1,
 283            ),
 284            'mandatory' => '0',
 285            'pid' => '0',
 286            'weight' => '-15',
 287          ),
 288          'sample values' => 'two',
 289          'database values' => array('two'),
 290          'database default values' => array(''),
 291        ),
 292        'select_no_default_zero' => array(
 293          'component' => array(
 294            'form_key' => 'select_no_default_zero',
 295            'name' => 'Select no default zero',
 296            'type' => 'select',
 297            'value' => '',
 298            'extra' => array(
 299              'description' => 'Tests saving zero as a value.',
 300              'items' => "0|zero\n1|one\n2|two",
 301              'aslist' => 1,
 302            ),
 303            'mandatory' => '0',
 304            'pid' => '0',
 305            'weight' => '-15',
 306          ),
 307          'sample values' => '0',
 308          'database values' => array('0'),
 309          'database default values' => array(''),
 310        ),
 311        'select_optgroup' => array(
 312          'component' => array(
 313            'form_key' => 'select_optgroup',
 314            'name' => 'Select Optgroup',
 315            'type' => 'select',
 316            'value' => 'option 1-2',
 317            'extra' => array(
 318              'description' => 'Tests saving zero as a value.',
 319              '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",
 320              'aslist' => 1,
 321            ),
 322            'mandatory' => '1',
 323            'pid' => '0',
 324            'weight' => '-15',
 325          ),
 326          'sample values' => 'option 2-2',
 327          'database values' => array('option 2-2'),
 328          'database default values' => array('option 1-2'),
 329        ),
 330        'select_email' => array(
 331          'component' => array(
 332            'form_key' => 'select_email',
 333            'name' => 'Select e-mails',
 334            'type' => 'select',
 335            'value' => 'nate@localhost.localhost',
 336            'extra' => array(
 337              'items' => "nate@localhost.localhost|one\nadmin@localhost.localhost|two",
 338            ),
 339            'mandatory' => '0',
 340            'pid' => '2',
 341            'weight' => '-17',
 342          ),
 343          'sample values' => 'admin@localhost.localhost',
 344          'database values' => array('admin@localhost.localhost'),
 345          'database default values' => array('nate@localhost.localhost'),
 346        ),
 347        'select_multiple' => array(
 348          'component' => array(
 349            'form_key' => 'select_multiple',
 350            'name' => 'Select Multiple',
 351            'type' => 'select',
 352            'value' => 'one,two',
 353            'extra' => array(
 354              'items' => "one|one\ntwo|two\nthree|three",
 355              'multiple' => 1,
 356              'aslist' => 1,
 357            ),
 358            'mandatory' => '0',
 359            'pid' => '0',
 360            'weight' => '-10',
 361          ),
 362          // TODO: I'd like to test a value, but SimpleTest can't set multiple values.
 363          'sample values' => NULL,
 364          'database values' => array('one', 'two'),
 365          'database default values' => array('one', 'two'),
 366        ),
 367  
 368        // Test date components.
 369        'date_textfield' => array(
 370          'component' => array(
 371            'form_key' => 'date_textfield',
 372            'name' => 'Date Textfield',
 373            'type' => 'date',
 374            'value' => 'Nov 19 1978',
 375            'extra' => array(
 376              'timezone' => 'site',
 377              'start_date' => '-100 years',
 378              'end_date' => '+2 years',
 379              'year_textfield' => 1,
 380            ),
 381            'mandatory' => '1',
 382            'pid' => '0',
 383            'weight' => '-7',
 384          ),
 385          'sample values' => array('day' => '30', 'month' => '9', 'year' => '1982'),
 386          'database values' => array('1982-09-30'),
 387          'database default values' => array('1978-11-19'),
 388        ),
 389  
 390        // Test email components.
 391        'email' => array(
 392          'component' => array(
 393            'form_key' => 'email',
 394            'name' => 'E-mail',
 395            'type' => 'email',
 396            'value' => '%useremail',
 397            'mandatory' => '0',
 398            'extra' => array(
 399              // SimpleTest does not support type="email" input fields.
 400              'attributes' => array('type' => 'text'),
 401            ),
 402            'pid' => '0',
 403            'weight' => '-5',
 404          ),
 405          'sample values' => 'admin@localhost.localhost',
 406          'database values' => array('admin@localhost.localhost'),
 407          'database default values' => array($this->webform_users['admin']->mail),
 408        ),
 409  
 410        // Test hidden components.
 411        'hidden' => array(
 412          'component' => array(
 413            'form_key' => 'hidden',
 414            'name' => 'Hidden',
 415            'type' => 'hidden',
 416            'value' => 'default hidden value',
 417            'mandatory' => '1',
 418            'pid' => '0',
 419            'weight' => '-4',
 420          ),
 421          'sample values' => NULL,
 422          'database values' => array('default hidden value'),
 423          'database default values' => array('default hidden value'),
 424        ),
 425  
 426        // Test textarea components.
 427        'textarea' => array(
 428          'component' => array(
 429            'form_key' => 'textarea',
 430            'name' => 'Textarea',
 431            'type' => 'textarea',
 432            'value' => 'sample textarea default value',
 433            'extra' => array(),
 434            'mandatory' => '0',
 435            'pid' => '0',
 436            'weight' => '15',
 437          ),
 438          'sample values' => 'sample textarea value',
 439          'database values' => array('sample textarea value'),
 440          'database default values' => array('sample textarea default value'),
 441        ),
 442  
 443        // Test textfield components.
 444        'textfield_disabled' => array(
 445          'component' => array(
 446            'form_key' => 'textfield_disabled',
 447            'name' => 'Textfield Disabled',
 448            'type' => 'textfield',
 449            'value' => '%get[foo]',
 450            'extra' => array(
 451              'disabled' => 1,
 452            ),
 453            'mandatory' => '0',
 454            'pid' => '0',
 455            'weight' => '-15',
 456          ),
 457          'sample values' => NULL,
 458          'database values' => array('bar'),
 459          'database default values' => array('bar'),
 460        ),
 461        'textfield_profile' => array(
 462          'component' => array(
 463            'form_key' => 'textfield_profile',
 464            'name' => 'Textfield Profile',
 465            'type' => 'textfield',
 466            'value' => '%profile[profile_gender]',
 467            'extra' => array(
 468              'width' => '20',
 469            ),
 470            'mandatory' => '0',
 471            'pid' => '0',
 472            'weight' => '-6',
 473          ),
 474          'sample values' => 'Female',
 475          'database values' => array('Female'),
 476          'database default values' => array($this->webform_users['admin']->profile_gender),
 477        ),
 478  
 479        // Test time components.
 480        'time' => array(
 481          'component' => array(
 482            'form_key' => 'time',
 483            'name' => 'Time',
 484            'type' => 'time',
 485            'value' => '10:30pm',
 486            'extra' => array(
 487              'timezone' => 'site',
 488              'hourformat' => '12-hour',
 489            ),
 490            'mandatory' => '0',
 491            'pid' => '0',
 492            'weight' => '16',
 493          ),
 494          'sample values' => array('hour' => '5', 'minute' => '0', 'ampm' => 'am'),
 495          'database values' => array('05:00:00'),
 496          'database default values' => array('22:30:00'),
 497        ),
 498        'time_24h' => array(
 499          'component' => array(
 500            'form_key' => 'time_24h',
 501            'name' => 'Time 24H',
 502            'type' => 'time',
 503            'value' => '10:30pm',
 504            'extra' => array(
 505              'timezone' => 'site',
 506              'hourformat' => '24-hour',
 507            ),
 508            'mandatory' => '0',
 509            'pid' => '0',
 510            'weight' => '17',
 511          ),
 512          'sample values' => array('hour' => '5', 'minute' => '0'),
 513          'database values' => array('05:00:00'),
 514          'database default values' => array('22:30:00'),
 515        ),
 516  
 517        // Test number components.
 518        'integer' => array(
 519          'component' => array(
 520            'form_key' => 'integer',
 521            'name' => 'Integer',
 522            'type' => 'number',
 523            'value' => '1',
 524            'extra' => array(
 525              'type' => 'textfield',
 526              'integer' => 1,
 527              'max' => '100',
 528              // SimpleTest does not support type="number" input fields.
 529              'attributes' => array('type' => 'text'),
 530            ),
 531            'mandatory' => '0',
 532            'pid' => '0',
 533            'weight' => '18',
 534          ),
 535          'sample values' => '2',
 536          'database values' => array('2'),
 537          'database default values' => array('1'),
 538          'error values' => array(
 539            '1.5' => t('%name field value of @value must be an integer.', array('%name' => 'Integer', '@value' => '1.5')),
 540            '101' => t('%name field value must be less than @max.', array('%name' => 'Integer', '@max' => '100')),
 541          ),
 542        ),
 543        'integer_range' => array(
 544          'component' => array(
 545            'form_key' => 'integer_range',
 546            'name' => 'Integer Range',
 547            'type' => 'number',
 548            'value' => '50',
 549            'extra' => array(
 550              'type' => 'select',
 551              'min' => '10',
 552              'max' => '50',
 553              'step' => 5,
 554              'integer' => 1,
 555            ),
 556            'mandatory' => '0',
 557            'pid' => '0',
 558            'weight' => '19',
 559          ),
 560          'sample values' => '10',
 561          'database values' => array('10'),
 562          'database default values' => array('50'),
 563        ),
 564        'decimal_positive' => array(
 565          'component' => array(
 566            'form_key' => 'decimal_positive',
 567            'name' => 'Decimal positive',
 568            'type' => 'number',
 569            'value' => '1',
 570            'extra' => array(
 571              'type' => 'textfield',
 572              'field_prefix' => '$',
 573              'field_suffix' => 'lbs',
 574              'min' => '0',
 575              'decimals' => '2',
 576              'point' => '.',
 577              'separator' => ',',
 578              // SimpleTest does not support type="number" input fields.
 579              'attributes' => array('type' => 'text'),
 580            ),
 581            'mandatory' => '0',
 582            'pid' => '0',
 583            'weight' => '20',
 584          ),
 585          'sample values' => '2.00',
 586          'database values' => array('2.00'),
 587          'database default values' => array('1'),
 588          'error values' => array(
 589            '-1' => t('%name field value must be greater than @min.', array('%name' => 'Decimal positive', '@min' => '0')),
 590          ),
 591        ),
 592        'decimal_range' => array(
 593          'component' => array(
 594            'form_key' => 'decimal_range',
 595            'name' => 'Decimal range',
 596            'type' => 'number',
 597            'value' => '1',
 598            'extra' => array(
 599              'type' => 'textfield',
 600              'field_prefix' => '$',
 601              'field_suffix' => 'lbs',
 602              'min' => '1',
 603              'max' => '12',
 604              'step' => '1.5',
 605              // SimpleTest does not support type="number" input fields.
 606              'attributes' => array('type' => 'text'),
 607            ),
 608            'mandatory' => '0',
 609            'pid' => '0',
 610            'weight' => '21',
 611          ),
 612          'sample values' => '11.5',
 613          'database values' => array('11.5'),
 614          'database default values' => array('1'),
 615          'error values' => array(
 616            '2' => t('%name field value must be @start plus a multiple of @step.', array('%name' => 'Decimal range', '@start' => '1', '@step' => '1.5')),
 617            '13' => t('%name field value of @value should be in the range @min to @max.', array('%name' => 'Decimal range', '@value' => '13', '@min' => '1', '@max' => '12')),
 618          ),
 619        ),
 620        'decimal_range_select' => array(
 621          'component' => array(
 622            'form_key' => 'decimal_range_select',
 623            'name' => 'Decimal range select',
 624            'type' => 'number',
 625            'value' => '1',
 626            'extra' => array(
 627              'type' => 'select',
 628              'field_prefix' => '$',
 629              'field_suffix' => 'lbs',
 630              'min' => '1',
 631              'max' => '12',
 632              'step' => '1.5',
 633            ),
 634            'mandatory' => '0',
 635            'pid' => '0',
 636            'weight' => '21',
 637          ),
 638          'sample values' => '11.5',
 639          'database values' => array('11.5'),
 640          'database default values' => array('1'),
 641        ),
 642      );
 643  
 644      return $this->_webform_components;
 645    }
 646  
 647    function testWebformForm() {
 648      if (isset($this->_webform_node)) {
 649        return $this->_webform_node;
 650      }
 651  
 652      $settings = array(
 653       'type' => 'webform',
 654       'language' => '',
 655       'uid' => '1',
 656       'status' => '1',
 657       'promote' => '1',
 658       'moderate' => '0',
 659       'sticky' => '0',
 660       'tnid' => '0',
 661       'translate' => '0',
 662       'title' => 'Test Webform',
 663       '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.',
 664       '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.',
 665       'log' => '',
 666       'format' => '1',
 667       'webform' => array(
 668          'confirmation' => 'Thanks!',
 669          'confirmation_format' => FILTER_FORMAT_DEFAULT,
 670          'redirect_url' => '<confirmation>',
 671          'teaser' => '0',
 672          'allow_draft' => '1',
 673          'submit_text' => '',
 674          'submit_limit' => '-1',
 675          'submit_interval' => '-1',
 676          'submit_notice' => '1',
 677          'roles' => array('1', '2'),
 678          'components' => array(),
 679          'emails' => array(),
 680        ),
 681      );
 682  
 683      $cid = 0;
 684      foreach ($this->testWebformComponents() as $key => $component_info) {
 685        $cid++;
 686        $settings['webform']['components'][$cid] = $component_info['component'];
 687        $settings['webform']['components'][$cid]['cid'] = $cid;
 688        $settings['webform']['components'][$cid]['pid'] = 0;
 689      }
 690  
 691      $this->_webform_node = $this->drupalCreateNode($settings);
 692  
 693      return $this->_webform_node;
 694    }
 695  
 696    /**
 697     * Generate a list of all values that would result in a valid submission.
 698     */
 699    function testWebformPost() {
 700      $edit = array();
 701      foreach ($this->testWebformComponents() as $key => $component_info) {
 702        if (is_array($component_info['sample values'])) {
 703          foreach ($component_info['sample values'] as $subkey => $value) {
 704            $edit["submitted[$key][$subkey]"] = $value;
 705          }
 706        }
 707        elseif ($component_info['sample values'] != NULL) {
 708          $value = $component_info['sample values'];
 709          // Multiple selects have a funky extra empty bracket in the name.
 710          $extra = $key == 'select_multiple' ? '[]' : '';
 711          $edit["submitted[$key]$extra"] = $value;
 712        }
 713      }
 714      return $edit;
 715    }
 716  
 717    /**
 718     * Utility function to print out the current page being tested.
 719     */
 720    function webformPrintPage() {
 721      $this->verbose($this->drupalGetContent());
 722    }
 723  }
 724  
 725  /**
 726   * Test general functionality of Webform.
 727   */
 728  class WebformGeneralTestCase extends WebformTestCase {
 729    /**
 730     * Implements getInfo().
 731     */
 732    public static function getInfo() {
 733      return array(
 734        'name' => t('Webform'),
 735        'description' => t('Checks global Webform settings and content types.'),
 736        'group' => t('Webform'),
 737      );
 738    }
 739  
 740    /**
 741     * Test creating a new Webform node.
 742     */
 743    function testWebformCreate() {
 744      $settings = array(
 745        'title' => 'Test webform, no components',
 746        'type' => 'webform',
 747      );
 748      $node = $this->drupalCreateNode($settings);
 749  
 750      // Because this is a "webform" type node, it should have an entry in the
 751      // database even though it's using the default settings.
 752      $this->assertTrue($this->webformRecordExists($node->nid), t('Webform record made in the database for the new webform node.'));
 753  
 754      // Make a change to the node, ensure that the record stays intact.
 755      $node->title .= '!';
 756      node_save($node);
 757      $this->assertTrue($this->webformRecordExists($node->nid), t('Webform record still in the database after modifying webform node.'));
 758    }
 759  
 760    /**
 761     * Test webform-enabling a different node type and testing behavior.
 762     */
 763    function testWebformCreateNewType() {
 764      // Enable webforms on the page content type.
 765      variable_set('webform_node_types', array('webform', 'page'));
 766  
 767      $settings = array(
 768        'title' => 'Test webform-enabled page',
 769        'type' => 'page',
 770      );
 771      $node = $this->drupalCreateNode($settings);
 772  
 773      // Because this is a webform-enabled type node but does not yet have any
 774      // components, it should not have an entry in the database because it is
 775      // using the default settings.
 776      $this->assertFalse($this->webformRecordExists($node->nid), t('Webform record not in the database for the new page node.'));
 777  
 778      // Make a change to the node, ensure that the record stays empty.
 779      $node->title .= '!';
 780      node_save($node);
 781      $this->assertFalse($this->webformRecordExists($node->nid), t('Webform record still not in the database after modifying page node.'));
 782  
 783      // Add a new component to the node and check that a record is made in the
 784      // webform table.
 785      $components = $this->testWebformComponents();
 786      $textarea = $components['textarea'];
 787      $textarea['type'] = 'textarea';
 788      $textarea['form_key'] = 'textarea';
 789      $textarea['cid'] = 1;
 790      $textarea['pid'] = 0;
 791      $textarea = array_merge(webform_component_invoke('textarea', 'defaults'), $textarea);
 792      $node->webform['components'][1] = $textarea;
 793      node_save($node);
 794      $this->assertTrue($this->webformRecordExists($node->nid), t('Webform record now exists after adding a new component.'));
 795  
 796      // Remove the new component and ensure that the record is deleted.
 797      $node->webform['components'] = array();
 798      node_save($node);
 799      $this->assertFalse($this->webformRecordExists($node->nid), t('Webform record deleted after deleting last component.'));
 800    }
 801  
 802    function webformRecordExists($nid) {
 803      return (bool) db_result(db_query("SELECT nid FROM {webform} WHERE nid = %d", $nid));
 804    }
 805  }


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