webform.test 27 KB

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