WebformTestCase.test 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. <?php
  2. /**
  3. * Webform module tests.
  4. */
  5. class WebformTestCase extends DrupalWebTestCase {
  6. private $_webform_node;
  7. private $_webform_components;
  8. public $webform_users;
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function setUp($added_modules = array()) {
  13. // Enable Webform and Token modules.
  14. $modules = array('webform', 'token');
  15. parent::setUp(array_merge($modules, $added_modules));
  16. // Create a profile field to test [user:?] tokens.
  17. $field = array(
  18. 'field_name' => 'gender',
  19. 'type' => 'text',
  20. 'cardinality' => 1,
  21. );
  22. $instance = array(
  23. 'field_name' => 'gender',
  24. 'entity_type' => 'user',
  25. 'bundle' => 'user',
  26. 'label' => 'Gender',
  27. 'widget' => array(
  28. 'type' => 'text_textfield',
  29. 'label' => 'Gender',
  30. ),
  31. );
  32. field_create_field($field);
  33. field_create_instance($instance);
  34. // Create a normal user that can view their own submissions.
  35. $permissions['userAccess'] = array(
  36. 'access content',
  37. 'access own webform submissions',
  38. );
  39. // Create a normal user than can edit their own submissions.
  40. $permissions['userEdit'] = array(
  41. 'access content',
  42. 'edit own webform submissions',
  43. );
  44. // Create a webform editor to test creating and editing own content.
  45. $permissions['editor'] = array(
  46. 'access content',
  47. 'create webform content',
  48. 'edit own webform content',
  49. 'access all webform results',
  50. );
  51. // Create a webform admin that will do all node creation.
  52. $permissions['admin'] = array(
  53. 'access content',
  54. 'administer nodes',
  55. 'create webform content',
  56. 'edit any webform content',
  57. 'access all webform results',
  58. 'edit all webform submissions',
  59. 'delete all webform submissions',
  60. );
  61. foreach ($permissions as $user_key => $role_permissions) {
  62. $this->webform_users[$user_key] = $this->drupalCreateUser($role_permissions);
  63. $this->webform_users[$user_key]->gender = array(LANGUAGE_NONE => array(array('value' => 'Female')));
  64. user_save($this->webform_users[$user_key]);
  65. }
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function tearDown() {
  71. // Delete the webform admin and any created nodes.
  72. foreach ($this->webform_users as $account) {
  73. $uid = $account->uid;
  74. $result = db_select('node')
  75. ->fields('node')
  76. ->condition('uid', $uid)
  77. ->execute();
  78. foreach ($result as $node) {
  79. node_delete($node->nid);
  80. }
  81. user_cancel(array(), $uid, 'user_cancel_delete');
  82. }
  83. parent::tearDown();
  84. }
  85. /**
  86. * Reset the form.
  87. */
  88. public function webformReset() {
  89. $this->_webform_node = NULL;
  90. $this->_webform_components = NULL;
  91. }
  92. /**
  93. * Provide a list of components to test throughout the suite.
  94. *
  95. * Each component provides:
  96. * - A default configuration for the component.
  97. * - Values to try setting via POST
  98. * - Values that should match the database storage when set via POST
  99. * - Values that should match the database storage when using the default
  100. * values.
  101. *
  102. * @return array
  103. * An array of each component settings.
  104. */
  105. public function webformComponents() {
  106. if (isset($this->_webform_components)) {
  107. return $this->_webform_components;
  108. }
  109. $this->_webform_components = array(
  110. // Test date components.
  111. 'date' => array(
  112. 'component' => array(
  113. 'form_key' => 'date',
  114. 'name' => 'Date',
  115. 'type' => 'date',
  116. 'value' => '19 Nov 1978',
  117. 'extra' => array(
  118. 'timezone' => 'site',
  119. 'start_date' => '-100 years',
  120. 'end_date' => '+2 years',
  121. ),
  122. 'required' => '0',
  123. 'pid' => '0',
  124. 'weight' => '-15',
  125. ),
  126. 'sample values' => array('day' => '30', 'month' => '9', 'year' => '1982'),
  127. 'database values' => array('1982-09-30'),
  128. 'database default values' => array('1978-11-19'),
  129. // Conditionals match against the 'sample values'.
  130. 'match conditional values' => array(
  131. 'equal' => '1982/9/30',
  132. 'before' => '1982/10/1',
  133. 'before_equal' => '1982/9/30',
  134. 'after' => '1982/9/29',
  135. 'after_equal' => '1982/9/29',
  136. ),
  137. 'mismatch conditional values' => array(
  138. 'equal' => '1981/9/30',
  139. 'before' => '1982/9/30',
  140. 'before_equal' => '1982/9/29',
  141. 'after' => '1982/9/30',
  142. 'after_equal' => '1982/10/1',
  143. ),
  144. ),
  145. // Test grid components.
  146. 'grid' => array(
  147. 'component' => array(
  148. 'form_key' => 'grid',
  149. 'name' => 'Grid',
  150. 'type' => 'grid',
  151. 'value' => '',
  152. 'extra' => array(
  153. // Left side.
  154. 'questions' => "0|Ålphå\n1|ıé†å\n2|Î鬆å",
  155. // Top.
  156. 'options' => "0|øne\n1|twö\n2|ǼBƇ\n3|€Euro",
  157. ),
  158. 'required' => '0',
  159. 'pid' => '2',
  160. 'weight' => '-19',
  161. ),
  162. 'sample values' => array('0' => '0', '1' => '1', '2' => '2'),
  163. 'database values' => array('0' => '0', '1' => '1', '2' => '2'),
  164. 'database default values' => array('', '', ''),
  165. ),
  166. 'grid_keyed' => array(
  167. 'component' => array(
  168. 'form_key' => 'grid_keyed',
  169. 'name' => 'Grid Keyed',
  170. 'type' => 'grid',
  171. 'value' => '',
  172. 'extra' => array(
  173. // Left side.
  174. 'questions' => "one|What's your option?\ntwo|Agåin?|Again, right text\nthree|One more time!",
  175. // Top.
  176. 'options' => "one|Option one\ntwo|Option 2\nthree| Three is me",
  177. 'title_display' => 'internal',
  178. ),
  179. 'required' => '0',
  180. 'pid' => '0',
  181. 'weight' => '-15',
  182. ),
  183. 'sample values' => array('one' => 'one', 'two' => 'two', 'three' => 'three'),
  184. 'database values' => array('one' => 'one', 'two' => 'two', 'three' => 'three'),
  185. 'database default values' => array('one' => '', 'two' => '', 'three' => ''),
  186. ),
  187. // Test select components.
  188. 'checkboxes' => array(
  189. 'component' => array(
  190. 'form_key' => 'checkboxes',
  191. 'name' => 'Checkboxes',
  192. 'type' => 'select',
  193. 'value' => 'two',
  194. 'extra' => array(
  195. 'items' => "one|one\ntwo|two\nthree|three",
  196. 'multiple' => 1,
  197. ),
  198. 'required' => '0',
  199. 'pid' => '0',
  200. 'weight' => '-15',
  201. ),
  202. 'sample values' => array('one' => TRUE, 'two' => FALSE, 'three' => TRUE),
  203. 'database values' => array('one', 'three'),
  204. 'database default values' => array('two'),
  205. // Conditionals match against the 'sample values'.
  206. 'match conditional values' => array(
  207. // ANDed together match.
  208. 'equal' => array('one', 'three'),
  209. 'not_equal' => array('two'),
  210. ),
  211. 'mismatch conditional values' => array(
  212. 'equal' => array('one', 'two'),
  213. 'not_equal' => array('two', 'three'),
  214. ),
  215. ),
  216. 'checkboxes_zero' => array(
  217. 'component' => array(
  218. 'form_key' => 'checkboxes_zero',
  219. 'name' => 'Checkboxes zero',
  220. 'type' => 'select',
  221. 'value' => '0',
  222. 'extra' => array(
  223. 'items' => "0|zero\n1|one\n2|two",
  224. 'multiple' => 1,
  225. ),
  226. 'required' => '1',
  227. 'pid' => '0',
  228. 'weight' => '-9',
  229. ),
  230. 'sample values' => array('0' => TRUE),
  231. 'database values' => array('0'),
  232. 'database default values' => array('0'),
  233. // Conditionals match against the 'sample values'.
  234. 'match conditional values' => array(
  235. 'equal' => '0',
  236. 'not_equal' => '1',
  237. ),
  238. 'mismatch conditional values' => array(
  239. 'equal' => '1',
  240. 'not_equal' => '0',
  241. ),
  242. ),
  243. 'radios' => array(
  244. 'component' => array(
  245. 'form_key' => 'radios',
  246. 'name' => 'Radios',
  247. 'type' => 'select',
  248. 'value' => 'two',
  249. 'extra' => array(
  250. 'items' => "one|one\ntwo|two\nthree|three",
  251. ),
  252. 'required' => '1',
  253. 'pid' => '0',
  254. 'weight' => '-9',
  255. ),
  256. 'sample values' => 'one',
  257. 'database values' => array('one'),
  258. 'database default values' => array('two'),
  259. // Conditionals match against the 'sample values'.
  260. 'match conditional values' => array(
  261. 'equal' => 'one',
  262. 'not_equal' => 'two',
  263. ),
  264. 'mismatch conditional values' => array(
  265. 'equal' => 'two',
  266. 'not_equal' => 'one',
  267. ),
  268. ),
  269. 'radios_zero' => array(
  270. 'component' => array(
  271. 'form_key' => 'radios_zero',
  272. 'name' => 'Radios zero',
  273. 'type' => 'select',
  274. 'value' => '0',
  275. 'extra' => array(
  276. 'items' => "0|zero\n1|one\n2|two",
  277. ),
  278. 'required' => '1',
  279. 'pid' => '0',
  280. 'weight' => '-9',
  281. ),
  282. 'sample values' => '0',
  283. 'database values' => array('0'),
  284. 'database default values' => array('0'),
  285. // Conditionals match against the 'sample values'.
  286. 'match conditional values' => array(
  287. 'equal' => '0',
  288. 'not_equal' => '1',
  289. ),
  290. 'mismatch conditional values' => array(
  291. 'equal' => '1',
  292. 'not_equal' => '0',
  293. ),
  294. ),
  295. 'radios_relative' => array(
  296. 'component' => array(
  297. 'form_key' => 'radios_relative',
  298. 'name' => 'Radios relative',
  299. 'type' => 'select',
  300. 'value' => 'one',
  301. 'extra' => array(
  302. 'items' => "zero|Zero\none|One\ntwo|Two\nthree|Three\n",
  303. ),
  304. 'required' => '1',
  305. 'pid' => '0',
  306. 'weight' => '-9',
  307. ),
  308. 'sample values' => 'one',
  309. 'database values' => array('one'),
  310. 'database default values' => array('one'),
  311. // Conditionals match against the 'sample values'.
  312. 'match conditional values' => array(
  313. 'equal' => 'one',
  314. 'not_equal' => 'zero',
  315. 'less_than' => 'two',
  316. 'less_than_equal' => 'one',
  317. 'greater_than' => 'zero',
  318. 'greater_than_equal' => 'zero',
  319. ),
  320. 'mismatch conditional values' => array(
  321. 'equal' => 'zero',
  322. 'not_equal' => 'one',
  323. 'less_than' => 'one',
  324. 'less_than_equal' => 'zero',
  325. 'greater_than' => 'two',
  326. 'greater_than_equal' => 'two',
  327. ),
  328. ),
  329. 'select' => array(
  330. 'component' => array(
  331. 'form_key' => 'select',
  332. 'name' => 'Select',
  333. 'type' => 'select',
  334. 'value' => 'one',
  335. 'extra' => array(
  336. 'description' => 'Description here',
  337. 'items' => "one|one\ntwo|two\nthree|three\nfour|four\nfive|five\nsix|six",
  338. 'aslist' => 1,
  339. ),
  340. 'required' => '1',
  341. 'pid' => '0',
  342. 'weight' => '-15',
  343. ),
  344. 'sample values' => 'two',
  345. 'database values' => array('two'),
  346. 'database default values' => array('one'),
  347. // Conditionals match against the 'sample values'.
  348. 'match conditional values' => array(
  349. 'equal' => 'two',
  350. 'not_equal' => 'one',
  351. ),
  352. 'mismatch conditional values' => array(
  353. 'equal' => 'one',
  354. 'not_equal' => 'two',
  355. ),
  356. ),
  357. 'select_zero' => array(
  358. 'component' => array(
  359. 'form_key' => 'select_zero',
  360. 'name' => 'Select zero',
  361. 'type' => 'select',
  362. 'value' => '0',
  363. 'extra' => array(
  364. 'description' => 'Tests saving zero as a value.',
  365. 'items' => "0|zero\n1|one\n2|two",
  366. 'aslist' => 1,
  367. ),
  368. 'required' => '1',
  369. 'pid' => '0',
  370. 'weight' => '-15',
  371. ),
  372. 'sample values' => '0',
  373. 'database values' => array('0'),
  374. 'database default values' => array('0'),
  375. // Conditionals match against the 'sample values'.
  376. 'match conditional values' => array(
  377. 'equal' => '0',
  378. 'not_equal' => '1',
  379. ),
  380. 'mismatch conditional values' => array(
  381. 'equal' => '1',
  382. 'not_equal' => '0',
  383. ),
  384. ),
  385. 'select_no_default' => array(
  386. 'component' => array(
  387. 'form_key' => 'select_no_default',
  388. 'name' => 'Select no default',
  389. 'type' => 'select',
  390. 'value' => '',
  391. 'extra' => array(
  392. 'description' => 'Description here',
  393. 'items' => "one|one\ntwo|two\nthree|three\nfour|four\nfive|five\nsix|six",
  394. 'aslist' => 1,
  395. ),
  396. 'required' => '0',
  397. 'pid' => '0',
  398. 'weight' => '-15',
  399. ),
  400. 'sample values' => 'two',
  401. 'database values' => array('two'),
  402. 'database default values' => array(''),
  403. // Conditionals match against the 'sample values'.
  404. 'match conditional values' => array(
  405. 'equal' => 'two',
  406. 'not_equal' => '',
  407. ),
  408. 'mismatch conditional values' => array(
  409. 'equal' => '',
  410. 'not_equal' => 'two',
  411. ),
  412. ),
  413. 'select_no_default_zero' => array(
  414. 'component' => array(
  415. 'form_key' => 'select_no_default_zero',
  416. 'name' => 'Select no default zero',
  417. 'type' => 'select',
  418. 'value' => '',
  419. 'extra' => array(
  420. 'description' => 'Tests saving zero as a value.',
  421. 'items' => "0|zero\n1|one\n2|two",
  422. 'aslist' => 1,
  423. ),
  424. 'required' => '0',
  425. 'pid' => '0',
  426. 'weight' => '-15',
  427. ),
  428. 'sample values' => '0',
  429. 'database values' => array('0'),
  430. 'database default values' => array(''),
  431. // Conditionals match against the 'sample values'.
  432. 'match conditional values' => array(
  433. 'equal' => '0',
  434. 'not_equal' => '',
  435. ),
  436. 'mismatch conditional values' => array(
  437. 'equal' => '',
  438. 'not_equal' => '0',
  439. ),
  440. ),
  441. 'select_optgroup' => array(
  442. 'component' => array(
  443. 'form_key' => 'select_optgroup',
  444. 'name' => 'Select Optgroup',
  445. 'type' => 'select',
  446. 'value' => 'option 1-2',
  447. 'extra' => array(
  448. 'description' => 'Tests saving zero as a value.',
  449. '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",
  450. 'aslist' => 1,
  451. ),
  452. 'required' => '1',
  453. 'pid' => '0',
  454. 'weight' => '-15',
  455. ),
  456. 'sample values' => 'option 2-2',
  457. 'database values' => array('option 2-2'),
  458. 'database default values' => array('option 1-2'),
  459. ),
  460. 'select_email' => array(
  461. 'component' => array(
  462. 'form_key' => 'select_email',
  463. 'name' => 'Select e-mails',
  464. 'type' => 'select',
  465. 'value' => 'nate@localhost.localhost',
  466. 'extra' => array(
  467. 'items' => "nate@localhost.localhost|one\nadmin@localhost.localhost|two",
  468. ),
  469. 'required' => '0',
  470. 'pid' => '2',
  471. 'weight' => '-17',
  472. ),
  473. 'sample values' => 'admin@localhost.localhost',
  474. 'database values' => array('admin@localhost.localhost'),
  475. 'database default values' => array('nate@localhost.localhost'),
  476. ),
  477. 'select_multiple' => array(
  478. 'component' => array(
  479. 'form_key' => 'select_multiple',
  480. 'name' => 'Select Multiple',
  481. 'type' => 'select',
  482. 'value' => 'one,two',
  483. 'extra' => array(
  484. 'items' => "one|one\ntwo|two\nthree|three",
  485. 'multiple' => 1,
  486. 'aslist' => 1,
  487. ),
  488. 'required' => '0',
  489. 'pid' => '0',
  490. 'weight' => '-10',
  491. ),
  492. // @todo: I'd like to test a value, but SimpleTest can't set multiple values.
  493. 'sample values' => NULL,
  494. 'database values' => array('one', 'two'),
  495. 'database default values' => array('one', 'two'),
  496. ),
  497. 'select_relative' => array(
  498. 'component' => array(
  499. 'form_key' => 'select_relative',
  500. 'name' => 'Select relative',
  501. 'type' => 'select',
  502. 'value' => 'one',
  503. 'extra' => array(
  504. 'items' => "zero|Zero\none|One\ntwo|Two\nthree|Three\n",
  505. 'aslist' => 1,
  506. ),
  507. 'required' => '1',
  508. 'pid' => '0',
  509. 'weight' => '-9',
  510. ),
  511. 'sample values' => 'one',
  512. 'database values' => array('one'),
  513. 'database default values' => array('one'),
  514. // Conditionals match against the 'sample values'.
  515. 'match conditional values' => array(
  516. 'equal' => 'one',
  517. 'not_equal' => 'zero',
  518. 'less_than' => 'two',
  519. 'less_than_equal' => 'one',
  520. 'greater_than' => 'zero',
  521. 'greater_than_equal' => 'zero',
  522. ),
  523. 'mismatch conditional values' => array(
  524. 'equal' => 'zero',
  525. 'not_equal' => 'one',
  526. 'less_than' => 'one',
  527. 'less_than_equal' => 'zero',
  528. 'greater_than' => 'two',
  529. 'greater_than_equal' => 'two',
  530. ),
  531. ),
  532. // Test date components.
  533. 'date_textfield' => array(
  534. 'component' => array(
  535. 'form_key' => 'date_textfield',
  536. 'name' => 'Date Textfield',
  537. 'type' => 'date',
  538. 'value' => 'Nov 19 1978',
  539. 'extra' => array(
  540. 'timezone' => 'site',
  541. 'start_date' => '-100 years',
  542. 'end_date' => '+2 years',
  543. 'year_textfield' => 1,
  544. ),
  545. 'required' => '1',
  546. 'pid' => '0',
  547. 'weight' => '-7',
  548. ),
  549. 'sample values' => array('day' => '30', 'month' => '9', 'year' => '1982'),
  550. 'database values' => array('1982-09-30'),
  551. 'database default values' => array('1978-11-19'),
  552. // Conditionals match against the 'sample values'.
  553. 'match conditional values' => array(
  554. 'equal' => '1982/9/30',
  555. 'before' => '1982/10/1',
  556. 'before_equal' => '1982/9/30',
  557. 'after' => '1982/9/29',
  558. 'after_equal' => '1982/9/29',
  559. ),
  560. 'mismatch conditional values' => array(
  561. 'equal' => '1981/9/30',
  562. 'before' => '1982/9/30',
  563. 'before_equal' => '1982/9/29',
  564. 'after' => '1982/9/30',
  565. 'after_equal' => '1982/10/1',
  566. ),
  567. ),
  568. // Test email components.
  569. 'email' => array(
  570. 'component' => array(
  571. 'form_key' => 'email',
  572. 'name' => 'E-mail',
  573. 'type' => 'email',
  574. 'value' => '[current-user:mail]',
  575. 'required' => '0',
  576. 'extra' => array(
  577. // SimpleTest does not support type="email" input fields.
  578. 'attributes' => array('type' => 'text'),
  579. ),
  580. 'pid' => '0',
  581. 'weight' => '-5',
  582. ),
  583. 'sample values' => 'admin@localhost.localhost',
  584. 'database values' => array('admin@localhost.localhost'),
  585. 'database default values' => array($this->webform_users['admin']->mail),
  586. // Conditionals match against the 'sample values'.
  587. 'match conditional values' => array(
  588. 'equal' => 'admin@localhost.localhost',
  589. 'not_equal' => '',
  590. 'contains' => 'admin',
  591. 'does_not_contain' => 'foo',
  592. 'begins_with' => 'admin',
  593. 'ends_with' => 'localhost',
  594. 'not_empty' => TRUE,
  595. ),
  596. 'mismatch conditional values' => array(
  597. 'equal' => 'foo@localhost.localhost',
  598. 'not_equal' => 'admin@localhost.localhost',
  599. 'contains' => 'foo',
  600. 'does_not_contain' => 'admin',
  601. 'begins_with' => 'localhost',
  602. 'ends_with' => 'admin',
  603. 'empty' => TRUE,
  604. ),
  605. ),
  606. // Test hidden components.
  607. 'hidden' => array(
  608. 'component' => array(
  609. 'form_key' => 'hidden',
  610. 'name' => 'Hidden',
  611. 'type' => 'hidden',
  612. 'value' => 'default hidden value',
  613. 'required' => '1',
  614. 'pid' => '0',
  615. 'weight' => '-4',
  616. ),
  617. 'sample values' => NULL,
  618. 'database values' => array('default hidden value'),
  619. 'database default values' => array('default hidden value'),
  620. // Conditionals match against the 'sample values'.
  621. 'match conditional values' => array(
  622. 'equal' => 'default hidden value',
  623. 'not_equal' => '',
  624. 'contains' => 'hidden',
  625. 'does_not_contain' => 'foo',
  626. 'begins_with' => 'default',
  627. 'ends_with' => 'value',
  628. 'not_empty' => TRUE,
  629. ),
  630. 'mismatch conditional values' => array(
  631. 'equal' => '',
  632. 'not_equal' => 'default hidden value',
  633. 'contains' => 'foo',
  634. 'does_not_contain' => 'hidden',
  635. 'begins_with' => 'value',
  636. 'ends_with' => 'default',
  637. 'empty' => TRUE,
  638. ),
  639. ),
  640. // Test textarea components.
  641. 'textarea' => array(
  642. 'component' => array(
  643. 'form_key' => 'textarea',
  644. 'name' => 'Textarea',
  645. 'type' => 'textarea',
  646. 'value' => 'sample textarea default value',
  647. 'extra' => array(),
  648. 'required' => '0',
  649. 'pid' => '0',
  650. 'weight' => '15',
  651. ),
  652. 'sample values' => 'sample textarea value',
  653. 'database values' => array('sample textarea value'),
  654. 'database default values' => array('sample textarea default value'),
  655. // Conditionals match against the 'sample values'.
  656. 'match conditional values' => array(
  657. 'equal' => 'sample textarea value',
  658. 'not_equal' => '',
  659. 'contains' => 'sample',
  660. 'does_not_contain' => 'foo',
  661. 'begins_with' => 'sample',
  662. 'ends_with' => 'value',
  663. 'not_empty' => TRUE,
  664. ),
  665. 'mismatch conditional values' => array(
  666. 'equal' => '',
  667. 'not_equal' => 'sample textarea value',
  668. 'contains' => 'foo',
  669. 'does_not_contain' => 'sample',
  670. 'begins_with' => 'value',
  671. 'ends_with' => 'sample',
  672. 'empty' => TRUE,
  673. ),
  674. ),
  675. // Test textfield components.
  676. 'textfield' => array(
  677. 'component' => array(
  678. 'form_key' => 'textfield',
  679. 'name' => 'Textfield',
  680. 'type' => 'textfield',
  681. 'value' => '',
  682. 'required' => '0',
  683. 'pid' => '0',
  684. 'weight' => '-14',
  685. ),
  686. 'sample values' => '',
  687. 'database values' => array(''),
  688. 'database default values' => array(''),
  689. ),
  690. 'textfield_disabled' => array(
  691. 'component' => array(
  692. 'form_key' => 'textfield_disabled',
  693. 'name' => 'Textfield Disabled',
  694. 'type' => 'textfield',
  695. 'value' => '[current-page:query:foo]',
  696. 'extra' => array(
  697. 'disabled' => 1,
  698. ),
  699. 'required' => '0',
  700. 'pid' => '0',
  701. 'weight' => '-15',
  702. ),
  703. 'sample values' => NULL,
  704. 'database values' => array('bar'),
  705. 'database default values' => array('bar'),
  706. ),
  707. 'textfield_profile' => array(
  708. 'component' => array(
  709. 'form_key' => 'textfield_profile',
  710. 'name' => 'Textfield Profile',
  711. 'type' => 'textfield',
  712. 'value' => '[current-user:gender]',
  713. 'extra' => array(
  714. 'width' => '20',
  715. ),
  716. 'required' => '0',
  717. 'pid' => '0',
  718. 'weight' => '-6',
  719. ),
  720. 'sample values' => 'Female',
  721. 'database values' => array('Female'),
  722. 'database default values' => array($this->webform_users['admin']->gender[LANGUAGE_NONE][0]['value']),
  723. ),
  724. // Test time components.
  725. 'time' => array(
  726. 'component' => array(
  727. 'form_key' => 'time',
  728. 'name' => 'Time',
  729. 'type' => 'time',
  730. 'value' => '10:30pm',
  731. 'extra' => array(
  732. 'timezone' => 'site',
  733. 'hourformat' => '12-hour',
  734. ),
  735. 'required' => '0',
  736. 'pid' => '0',
  737. 'weight' => '16',
  738. ),
  739. 'sample values' => array('hour' => '12', 'minute' => '0', 'ampm' => 'pm'),
  740. 'database values' => array('12:00:00'),
  741. 'database default values' => array('22:30:00'),
  742. // Conditionals match against the 'sample values'.
  743. 'match conditional values' => array(
  744. 'equal' => '12:00pm',
  745. 'before' => '1:00pm',
  746. 'before_equal' => '12:00pm',
  747. 'after' => '11:00am',
  748. 'after_equal' => '11:00am',
  749. ),
  750. 'mismatch conditional values' => array(
  751. 'equal' => '12:00am',
  752. 'before' => '12:00pm',
  753. 'before_equal' => '11:00am',
  754. 'after' => '12:00pm',
  755. ),
  756. ),
  757. 'time_24h' => array(
  758. 'component' => array(
  759. 'form_key' => 'time_24h',
  760. 'name' => 'Time 24H',
  761. 'type' => 'time',
  762. 'value' => '10:30pm',
  763. 'extra' => array(
  764. 'timezone' => 'site',
  765. 'hourformat' => '24-hour',
  766. ),
  767. 'required' => '0',
  768. 'pid' => '0',
  769. 'weight' => '17',
  770. ),
  771. 'sample values' => array('hour' => '5', 'minute' => '0'),
  772. 'database values' => array('05:00:00'),
  773. 'database default values' => array('22:30:00'),
  774. // Conditionals match against the 'sample values'.
  775. 'match conditional values' => array(
  776. 'equal' => '5:00',
  777. 'before' => '24:00',
  778. 'before_equal' => '5:00',
  779. 'after' => '00:00',
  780. 'after_equal' => '00:00',
  781. ),
  782. 'mismatch conditional values' => array(
  783. 'equal' => '5:01',
  784. 'before' => '5:00',
  785. 'before_equal' => '4:59',
  786. 'after' => '5:00',
  787. 'after_equal' => '5:01',
  788. ),
  789. ),
  790. // Test number components.
  791. 'integer' => array(
  792. 'component' => array(
  793. 'form_key' => 'integer',
  794. 'name' => 'Integer',
  795. 'type' => 'number',
  796. 'value' => '1',
  797. 'extra' => array(
  798. 'type' => 'textfield',
  799. 'integer' => 1,
  800. 'max' => '100',
  801. // SimpleTest does not support type="number" input fields.
  802. 'attributes' => array('type' => 'text'),
  803. ),
  804. 'required' => '0',
  805. 'pid' => '0',
  806. 'weight' => '18',
  807. ),
  808. 'sample values' => '2',
  809. 'database values' => array('2'),
  810. 'database default values' => array('1'),
  811. // Conditionals match against the 'sample values'.
  812. 'match conditional values' => array(
  813. 'equal' => '2',
  814. 'not_equal' => '0',
  815. 'less_than' => '3',
  816. 'less_than_equal' => '2',
  817. 'greater_than' => '1',
  818. 'greater_than_equal' => '1',
  819. 'not_empty' => TRUE,
  820. ),
  821. 'mismatch conditional values' => array(
  822. 'equal' => '0',
  823. 'not_equal' => '2',
  824. 'less_than' => '2',
  825. 'less_than_equal' => '1',
  826. 'greater_than' => '2',
  827. 'greater_than_equal' => '3',
  828. 'empty' => TRUE,
  829. ),
  830. 'error values' => array(
  831. '1.5' => t('!name field value of @value must be an integer.', array('!name' => 'Integer', '@value' => '1.5')),
  832. '101' => t('!name field value must be less than @max.', array('!name' => 'Integer', '@max' => '100')),
  833. ),
  834. ),
  835. 'integer_range' => array(
  836. 'component' => array(
  837. 'form_key' => 'integer_range',
  838. 'name' => 'Integer Range',
  839. 'type' => 'number',
  840. 'value' => '50',
  841. 'extra' => array(
  842. 'type' => 'select',
  843. 'min' => '10',
  844. 'max' => '50',
  845. 'step' => 5,
  846. 'integer' => 1,
  847. ),
  848. 'required' => '0',
  849. 'pid' => '0',
  850. 'weight' => '19',
  851. ),
  852. 'sample values' => '10',
  853. 'database values' => array('10'),
  854. 'database default values' => array('50'),
  855. ),
  856. 'decimal_positive' => array(
  857. 'component' => array(
  858. 'form_key' => 'decimal_positive',
  859. 'name' => 'Decimal positive',
  860. 'type' => 'number',
  861. 'value' => '1',
  862. 'extra' => array(
  863. 'type' => 'textfield',
  864. 'field_prefix' => '$',
  865. 'field_suffix' => 'lbs',
  866. 'min' => '0',
  867. 'decimals' => '2',
  868. 'point' => '.',
  869. 'separator' => ',',
  870. // SimpleTest does not support type="number" input fields.
  871. 'attributes' => array('type' => 'text'),
  872. ),
  873. 'required' => '0',
  874. 'pid' => '0',
  875. 'weight' => '20',
  876. ),
  877. 'sample values' => '2.00',
  878. 'database values' => array('2.00'),
  879. 'database default values' => array('1'),
  880. // Conditionals match against the 'sample values'.
  881. 'match conditional values' => array(
  882. 'equal' => '2',
  883. 'not_equal' => '0',
  884. 'less_than' => '3.000',
  885. 'greater_than' => '1.000',
  886. 'not_empty' => TRUE,
  887. ),
  888. 'mismatch conditional values' => array(
  889. 'equal' => '0',
  890. 'not_equal' => '2',
  891. 'less_than' => '2.0',
  892. 'greater_than' => '2.00',
  893. 'empty' => TRUE,
  894. ),
  895. 'error values' => array(
  896. '-1' => t('!name field value must be greater than @min.', array('!name' => 'Decimal positive', '@min' => '0')),
  897. ),
  898. ),
  899. 'decimal_range' => array(
  900. 'component' => array(
  901. 'form_key' => 'decimal_range',
  902. 'name' => 'Decimal range',
  903. 'type' => 'number',
  904. 'value' => '1',
  905. 'extra' => array(
  906. 'type' => 'textfield',
  907. 'field_prefix' => '$',
  908. 'field_suffix' => 'lbs',
  909. 'min' => '1',
  910. 'max' => '12',
  911. 'step' => '1.5',
  912. // SimpleTest does not support type="number" input fields.
  913. 'attributes' => array('type' => 'text'),
  914. ),
  915. 'required' => '0',
  916. 'pid' => '0',
  917. 'weight' => '21',
  918. ),
  919. 'sample values' => '11.5',
  920. 'database values' => array('11.5'),
  921. 'database default values' => array('1'),
  922. 'error values' => array(
  923. '2' => t('!name field value must be @start plus a multiple of @step.', array('!name' => 'Decimal range', '@start' => '1', '@step' => '1.5')),
  924. '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')),
  925. ),
  926. ),
  927. 'decimal_range_select' => array(
  928. 'component' => array(
  929. 'form_key' => 'decimal_range_select',
  930. 'name' => 'Decimal range select',
  931. 'type' => 'number',
  932. 'value' => '1',
  933. 'extra' => array(
  934. 'type' => 'select',
  935. 'field_prefix' => '$',
  936. 'field_suffix' => 'lbs',
  937. 'min' => '1',
  938. 'max' => '12',
  939. 'step' => '1.5',
  940. ),
  941. 'required' => '0',
  942. 'pid' => '0',
  943. 'weight' => '21',
  944. ),
  945. 'sample values' => '10',
  946. 'database values' => array('10'),
  947. 'database default values' => array('1'),
  948. // Conditionals match against the 'sample values'.
  949. 'match conditional values' => array(
  950. 'equal' => '10',
  951. 'not_equal' => '2.5',
  952. 'less_than' => '11.5',
  953. 'greater_than' => '1',
  954. ),
  955. 'mismatch conditional values' => array(
  956. 'equal' => '2.5',
  957. 'not_equal' => '10',
  958. 'less_than' => '10',
  959. 'greater_than' => '11.5',
  960. ),
  961. ),
  962. );
  963. return $this->_webform_components;
  964. }
  965. /**
  966. * Create a sample Webform node.
  967. */
  968. public function webformForm() {
  969. if (isset($this->_webform_node)) {
  970. return $this->_webform_node;
  971. }
  972. $settings = array(
  973. 'type' => 'webform',
  974. 'language' => LANGUAGE_NONE,
  975. 'uid' => '1',
  976. 'status' => '1',
  977. 'promote' => '1',
  978. 'moderate' => '0',
  979. 'sticky' => '0',
  980. 'tnid' => '0',
  981. 'translate' => '0',
  982. 'title' => 'Test Webform',
  983. 'log' => '',
  984. 'format' => '1',
  985. 'webform' => array(
  986. 'confirmation' => 'Thanks!',
  987. ) + webform_node_defaults(),
  988. );
  989. $cid = 0;
  990. foreach ($this->webformComponents() as $key => $component_info) {
  991. $cid++;
  992. $settings['webform']['components'][$cid] = $component_info['component'];
  993. $settings['webform']['components'][$cid]['cid'] = $cid;
  994. $settings['webform']['components'][$cid]['pid'] = 0;
  995. }
  996. $this->_webform_node = $this->drupalCreateNode($settings);
  997. return $this->_webform_node;
  998. }
  999. /**
  1000. * Generate a list of all values that would result in a valid submission.
  1001. *
  1002. * @param $input_values
  1003. * An array of input values keyed by the component form key. If none
  1004. * are specified, the defaults will be pulled from webformComponents().
  1005. */
  1006. public function webformPost($input_values = NULL) {
  1007. $edit = array();
  1008. if (empty($input_values)) {
  1009. $input_values = array();
  1010. foreach ($this->webformComponents() as $key => $component_info) {
  1011. $input_values[$key] = $component_info['sample values'];
  1012. }
  1013. }
  1014. foreach ($input_values as $key => $values) {
  1015. if (is_array($values)) {
  1016. foreach ($values as $subkey => $value) {
  1017. $edit["submitted[$key][$subkey]"] = $value;
  1018. }
  1019. }
  1020. elseif ($values != NULL) {
  1021. $value = $values;
  1022. // Multiple selects have a funky extra empty bracket in the name.
  1023. $extra = $key == 'select_multiple' ? '[]' : '';
  1024. $edit["submitted[$key]$extra"] = $value;
  1025. }
  1026. }
  1027. return $edit;
  1028. }
  1029. /**
  1030. * Utility function to print out the current page being tested.
  1031. */
  1032. public function webformPrintPage() {
  1033. $this->verbose($this->drupalGetContent());
  1034. }
  1035. }