1. 7 modules/simpletest/tests/ajax_forms_test.module
  2. 8 core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module

Simpletest mock module for Ajax forms testing.

File

modules/simpletest/tests/ajax_forms_test.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Simpletest mock module for Ajax forms testing.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function ajax_forms_test_menu() {
  10. $items = array();
  11. $items['ajax_forms_test_get_form'] = array(
  12. 'title' => 'AJAX forms simple form test',
  13. 'page callback' => 'drupal_get_form',
  14. 'page arguments' => array('ajax_forms_test_simple_form'),
  15. 'access callback' => TRUE,
  16. );
  17. $items['ajax_forms_test_ajax_commands_form'] = array(
  18. 'title' => 'AJAX forms AJAX commands test',
  19. 'page callback' => 'drupal_get_form',
  20. 'page arguments' => array('ajax_forms_test_ajax_commands_form'),
  21. 'access callback' => TRUE,
  22. );
  23. $items['ajax_validation_test'] = array(
  24. 'title' => 'AJAX Validation Test',
  25. 'page callback' => 'drupal_get_form',
  26. 'page arguments' => array('ajax_forms_test_validation_form'),
  27. 'access callback' => TRUE,
  28. );
  29. $items['ajax_forms_test_lazy_load_form'] = array(
  30. 'title' => 'AJAX forms lazy load test',
  31. 'page callback' => 'drupal_get_form',
  32. 'page arguments' => array('ajax_forms_test_lazy_load_form'),
  33. 'access callback' => TRUE,
  34. );
  35. return $items;
  36. }
  37. /**
  38. * A basic form used to test form_state['values'] during callback.
  39. */
  40. function ajax_forms_test_simple_form($form, &$form_state) {
  41. $form = array();
  42. $form['select'] = array(
  43. '#type' => 'select',
  44. '#options' => array(
  45. 'red' => 'red',
  46. 'green' => 'green',
  47. 'blue' => 'blue'),
  48. '#ajax' => array(
  49. 'callback' => 'ajax_forms_test_simple_form_select_callback',
  50. ),
  51. '#suffix' => '<div id="ajax_selected_color">No color yet selected</div>',
  52. );
  53. $form['checkbox'] = array(
  54. '#type' => 'checkbox',
  55. '#title' => t('Test checkbox'),
  56. '#ajax' => array(
  57. 'callback' => 'ajax_forms_test_simple_form_checkbox_callback',
  58. ),
  59. '#suffix' => '<div id="ajax_checkbox_value">No action yet</div>',
  60. );
  61. $form['submit'] = array(
  62. '#type' => 'submit',
  63. '#value' => t('submit'),
  64. );
  65. return $form;
  66. }
  67. /**
  68. * Ajax callback triggered by select.
  69. */
  70. function ajax_forms_test_simple_form_select_callback($form, $form_state) {
  71. $commands = array();
  72. $commands[] = ajax_command_html('#ajax_selected_color', $form_state['values']['select']);
  73. $commands[] = ajax_command_data('#ajax_selected_color', 'form_state_value_select', $form_state['values']['select']);
  74. return array('#type' => 'ajax', '#commands' => $commands);
  75. }
  76. /**
  77. * Ajax callback triggered by checkbox.
  78. */
  79. function ajax_forms_test_simple_form_checkbox_callback($form, $form_state) {
  80. $commands = array();
  81. $commands[] = ajax_command_html('#ajax_checkbox_value', (int) $form_state['values']['checkbox']);
  82. $commands[] = ajax_command_data('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state['values']['checkbox']);
  83. return array('#type' => 'ajax', '#commands' => $commands);
  84. }
  85. /**
  86. * Form to display the Ajax Commands.
  87. */
  88. function ajax_forms_test_ajax_commands_form($form, &$form_state) {
  89. $form = array();
  90. // Shows the 'after' command with a callback generating commands.
  91. $form['after_command_example'] = array(
  92. '#value' => t("AJAX 'After': Click to put something after the div"),
  93. '#type' => 'submit',
  94. '#ajax' => array(
  95. 'callback' => 'ajax_forms_test_advanced_commands_after_callback',
  96. ),
  97. '#suffix' => '<div id="after_div">Something can be inserted after this</div>',
  98. );
  99. // Shows the 'alert' command.
  100. $form['alert_command_example'] = array(
  101. '#value' => t("AJAX 'Alert': Click to alert"),
  102. '#type' => 'submit',
  103. '#ajax' => array(
  104. 'callback' => 'ajax_forms_test_advanced_commands_alert_callback',
  105. ),
  106. );
  107. // Shows the 'append' command.
  108. $form['append_command_example'] = array(
  109. '#value' => t("AJAX 'Append': Click to append something"),
  110. '#type' => 'submit',
  111. '#ajax' => array(
  112. 'callback' => 'ajax_forms_test_advanced_commands_append_callback',
  113. ),
  114. '#suffix' => '<div id="append_div">Append inside this div</div>',
  115. );
  116. // Shows the 'before' command.
  117. $form['before_command_example'] = array(
  118. '#value' => t("AJAX 'before': Click to put something before the div"),
  119. '#type' => 'submit',
  120. '#ajax' => array(
  121. 'callback' => 'ajax_forms_test_advanced_commands_before_callback',
  122. ),
  123. '#suffix' => '<div id="before_div">Insert something before this.</div>',
  124. );
  125. // Shows the 'changed' command without asterisk.
  126. $form['changed_command_example'] = array(
  127. '#value' => t("AJAX changed: Click to mark div changed."),
  128. '#type' => 'submit',
  129. '#ajax' => array(
  130. 'callback' => 'ajax_forms_test_advanced_commands_changed_callback',
  131. ),
  132. '#suffix' => '<div id="changed_div"> <div id="changed_div_mark_this">This div can be marked as changed or not.</div></div>',
  133. );
  134. // Shows the 'changed' command adding the asterisk.
  135. $form['changed_command_asterisk_example'] = array(
  136. '#value' => t("AJAX changed: Click to mark div changed with asterisk."),
  137. '#type' => 'submit',
  138. '#ajax' => array(
  139. 'callback' => 'ajax_forms_test_advanced_commands_changed_asterisk_callback',
  140. ),
  141. );
  142. // Shows the Ajax 'css' command.
  143. $form['css_command_example'] = array(
  144. '#value' => t("Set the '#box' div to be blue."),
  145. '#type' => 'submit',
  146. '#ajax' => array(
  147. 'callback' => 'ajax_forms_test_advanced_commands_css_callback',
  148. ),
  149. '#suffix' => '<div id="css_div" style="height: 50px; width: 50px; border: 1px solid black"> box</div>',
  150. );
  151. // Shows the Ajax 'data' command. But there is no use of this information,
  152. // as this would require a javascript client to use the data.
  153. $form['data_command_example'] = array(
  154. '#value' => t("AJAX data command: Issue command."),
  155. '#type' => 'submit',
  156. '#ajax' => array(
  157. 'callback' => 'ajax_forms_test_advanced_commands_data_callback',
  158. ),
  159. '#suffix' => '<div id="data_div">Data attached to this div.</div>',
  160. );
  161. // Shows the Ajax 'invoke' command.
  162. $form['invoke_command_example'] = array(
  163. '#value' => t("AJAX invoke command: Invoke addClass() method."),
  164. '#type' => 'submit',
  165. '#ajax' => array(
  166. 'callback' => 'ajax_forms_test_advanced_commands_invoke_callback',
  167. ),
  168. '#suffix' => '<div id="invoke_div">Original contents</div>',
  169. );
  170. // Shows the Ajax 'html' command.
  171. $form['html_command_example'] = array(
  172. '#value' => t("AJAX html: Replace the HTML in a selector."),
  173. '#type' => 'submit',
  174. '#ajax' => array(
  175. 'callback' => 'ajax_forms_test_advanced_commands_html_callback',
  176. ),
  177. '#suffix' => '<div id="html_div">Original contents</div>',
  178. );
  179. // Shows the Ajax 'insert' command.
  180. $form['insert_command_example'] = array(
  181. '#value' => t("AJAX insert: Let client insert based on #ajax['method']."),
  182. '#type' => 'submit',
  183. '#ajax' => array(
  184. 'callback' => 'ajax_forms_test_advanced_commands_insert_callback',
  185. 'method' => 'prepend',
  186. ),
  187. '#suffix' => '<div id="insert_div">Original contents</div>',
  188. );
  189. // Shows the Ajax 'prepend' command.
  190. $form['prepend_command_example'] = array(
  191. '#value' => t("AJAX 'prepend': Click to prepend something"),
  192. '#type' => 'submit',
  193. '#ajax' => array(
  194. 'callback' => 'ajax_forms_test_advanced_commands_prepend_callback',
  195. ),
  196. '#suffix' => '<div id="prepend_div">Something will be prepended to this div. </div>',
  197. );
  198. // Shows the Ajax 'remove' command.
  199. $form['remove_command_example'] = array(
  200. '#value' => t("AJAX 'remove': Click to remove text"),
  201. '#type' => 'submit',
  202. '#ajax' => array(
  203. 'callback' => 'ajax_forms_test_advanced_commands_remove_callback',
  204. ),
  205. '#suffix' => '<div id="remove_div"><div id="remove_text">text to be removed</div></div>',
  206. );
  207. // Shows the Ajax 'restripe' command.
  208. $form['restripe_command_example'] = array(
  209. '#type' => 'submit',
  210. '#value' => t("AJAX 'restripe' command"),
  211. '#ajax' => array(
  212. 'callback' => 'ajax_forms_test_advanced_commands_restripe_callback',
  213. ),
  214. '#suffix' => '<div id="restripe_div">
  215. <table id="restripe_table" style="border: 1px solid black" >
  216. <tr id="table-first"><td>first row</td></tr>
  217. <tr ><td>second row</td></tr>
  218. </table>
  219. </div>',
  220. );
  221. // Demonstrates the Ajax 'settings' command. The 'settings' command has
  222. // nothing visual to "show", but it can be tested via SimpleTest and via
  223. // Firebug.
  224. $form['settings_command_example'] = array(
  225. '#type' => 'submit',
  226. '#value' => t("AJAX 'settings' command"),
  227. '#ajax' => array(
  228. 'callback' => 'ajax_forms_test_advanced_commands_settings_callback',
  229. ),
  230. );
  231. // Shows the Ajax 'add_css' command.
  232. $form['add_css_command_example'] = array(
  233. '#type' => 'submit',
  234. '#value' => t("AJAX 'add_css' command"),
  235. '#ajax' => array(
  236. 'callback' => 'ajax_forms_test_advanced_commands_add_css_callback',
  237. ),
  238. );
  239. $form['submit'] = array(
  240. '#type' => 'submit',
  241. '#value' => t('Submit'),
  242. );
  243. return $form;
  244. }
  245. /**
  246. * Ajax callback for 'after'.
  247. */
  248. function ajax_forms_test_advanced_commands_after_callback($form, $form_state) {
  249. $selector = '#after_div';
  250. $commands = array();
  251. $commands[] = ajax_command_after($selector, "This will be placed after");
  252. return array('#type' => 'ajax', '#commands' => $commands);
  253. }
  254. /**
  255. * Ajax callback for 'alert'.
  256. */
  257. function ajax_forms_test_advanced_commands_alert_callback($form, $form_state) {
  258. $commands = array();
  259. $commands[] = ajax_command_alert("Alert");
  260. return array('#type' => 'ajax', '#commands' => $commands);
  261. }
  262. /**
  263. * Ajax callback for 'append'.
  264. */
  265. function ajax_forms_test_advanced_commands_append_callback($form, $form_state) {
  266. $selector = '#append_div';
  267. $commands = array();
  268. $commands[] = ajax_command_append($selector, "Appended text");
  269. return array('#type' => 'ajax', '#commands' => $commands);
  270. }
  271. /**
  272. * Ajax callback for 'before'.
  273. */
  274. function ajax_forms_test_advanced_commands_before_callback($form, $form_state) {
  275. $selector = '#before_div';
  276. $commands = array();
  277. $commands[] = ajax_command_before($selector, "Before text");
  278. return array('#type' => 'ajax', '#commands' => $commands);
  279. }
  280. /**
  281. * Ajax callback for 'changed'.
  282. */
  283. function ajax_forms_test_advanced_commands_changed_callback($form, $form_state) {
  284. $commands[] = ajax_command_changed('#changed_div');
  285. return array('#type' => 'ajax', '#commands' => $commands);
  286. }
  287. /**
  288. * Ajax callback for 'changed' with asterisk marking inner div.
  289. */
  290. function ajax_forms_test_advanced_commands_changed_asterisk_callback($form, $form_state) {
  291. $commands = array();
  292. $commands[] = ajax_command_changed('#changed_div', '#changed_div_mark_this');
  293. return array('#type' => 'ajax', '#commands' => $commands);
  294. }
  295. /**
  296. * Ajax callback for 'css'.
  297. */
  298. function ajax_forms_test_advanced_commands_css_callback($form, $form_state) {
  299. $selector = '#css_div';
  300. $color = 'blue';
  301. $commands = array();
  302. $commands[] = ajax_command_css($selector, array('background-color' => $color));
  303. return array('#type' => 'ajax', '#commands' => $commands);
  304. }
  305. /**
  306. * Ajax callback for 'data'.
  307. */
  308. function ajax_forms_test_advanced_commands_data_callback($form, $form_state) {
  309. $selector = '#data_div';
  310. $commands = array();
  311. $commands[] = ajax_command_data($selector, 'testkey', 'testvalue');
  312. return array('#type' => 'ajax', '#commands' => $commands);
  313. }
  314. /**
  315. * Ajax callback for 'invoke'.
  316. */
  317. function ajax_forms_test_advanced_commands_invoke_callback($form, $form_state) {
  318. $commands = array();
  319. $commands[] = ajax_command_invoke('#invoke_div', 'addClass', array('error'));
  320. return array('#type' => 'ajax', '#commands' => $commands);
  321. }
  322. /**
  323. * Ajax callback for 'html'.
  324. */
  325. function ajax_forms_test_advanced_commands_html_callback($form, $form_state) {
  326. $commands = array();
  327. $commands[] = ajax_command_html('#html_div', 'replacement text');
  328. return array('#type' => 'ajax', '#commands' => $commands);
  329. }
  330. /**
  331. * Ajax callback for 'insert'.
  332. */
  333. function ajax_forms_test_advanced_commands_insert_callback($form, $form_state) {
  334. $commands = array();
  335. $commands[] = ajax_command_insert('#insert_div', 'insert replacement text');
  336. return array('#type' => 'ajax', '#commands' => $commands);
  337. }
  338. /**
  339. * Ajax callback for 'prepend'.
  340. */
  341. function ajax_forms_test_advanced_commands_prepend_callback($form, $form_state) {
  342. $commands = array();
  343. $commands[] = ajax_command_prepend('#prepend_div', "prepended text");
  344. return array('#type' => 'ajax', '#commands' => $commands);
  345. }
  346. /**
  347. * Ajax callback for 'remove'.
  348. */
  349. function ajax_forms_test_advanced_commands_remove_callback($form, $form_state) {
  350. $commands = array();
  351. $commands[] = ajax_command_remove('#remove_text');
  352. return array('#type' => 'ajax', '#commands' => $commands);
  353. }
  354. /**
  355. * Ajax callback for 'restripe'.
  356. */
  357. function ajax_forms_test_advanced_commands_restripe_callback($form, $form_state) {
  358. $commands = array();
  359. $commands[] = ajax_command_restripe('#restripe_table');
  360. return array('#type' => 'ajax', '#commands' => $commands);
  361. }
  362. /**
  363. * Ajax callback for 'settings'.
  364. */
  365. function ajax_forms_test_advanced_commands_settings_callback($form, $form_state) {
  366. $commands = array();
  367. $setting['ajax_forms_test']['foo'] = 42;
  368. $commands[] = ajax_command_settings($setting);
  369. return array('#type' => 'ajax', '#commands' => $commands);
  370. }
  371. /**
  372. * Ajax callback for 'add_css'.
  373. */
  374. function ajax_forms_test_advanced_commands_add_css_callback($form, $form_state) {
  375. $commands = array();
  376. $commands[] = ajax_command_add_css('my/file.css');
  377. return array('#type' => 'ajax', '#commands' => $commands);
  378. }
  379. /**
  380. * This form and its related submit and callback functions demonstrate
  381. * not validating another form element when a single Ajax element is triggered.
  382. *
  383. * The "drivertext" element is an Ajax-enabled textfield, free-form.
  384. * The "required_field" element is a textfield marked required.
  385. *
  386. * The correct behavior is that the Ajax-enabled drivertext element should
  387. * be able to trigger without causing validation of the "required_field".
  388. */
  389. function ajax_forms_test_validation_form($form, &$form_state) {
  390. $form['drivertext'] = array(
  391. '#title' => t('AJAX-enabled textfield.'),
  392. '#description' => t("When this one AJAX-triggers and the spare required field is empty, you should not get an error."),
  393. '#type' => 'textfield',
  394. '#default_value' => !empty($form_state['values']['drivertext']) ? $form_state['values']['drivertext'] : "",
  395. '#ajax' => array(
  396. 'callback' => 'ajax_forms_test_validation_form_callback',
  397. 'wrapper' => 'message_area',
  398. 'method' => 'replace',
  399. ),
  400. '#suffix' => '<div id="message_area"></div>',
  401. );
  402. $form['spare_required_field'] = array(
  403. '#title' => t("Spare Required Field"),
  404. '#type' => 'textfield',
  405. '#required' => TRUE,
  406. );
  407. $form['submit'] = array(
  408. '#type' => 'submit',
  409. '#value' => t('Submit'),
  410. );
  411. return $form;
  412. }
  413. /**
  414. * Submit handler for the validation form.
  415. */
  416. function ajax_forms_test_validation_form_submit($form, $form_state) {
  417. drupal_set_message(t("Validation form submitted"));
  418. }
  419. /**
  420. * Ajax callback for the 'drivertext' element of the validation form.
  421. */
  422. function ajax_forms_test_validation_form_callback($form, $form_state) {
  423. drupal_set_message("ajax_forms_test_validation_form_callback invoked");
  424. drupal_set_message(t("Callback: drivertext=%drivertext, spare_required_field=%spare_required_field", array('%drivertext' => $form_state['values']['drivertext'], '%spare_required_field' => $form_state['values']['spare_required_field'])));
  425. return '<div id="message_area">ajax_forms_test_validation_form_callback at ' . date('c') . '</div>';
  426. }
  427. /**
  428. * Form builder: Builds a form that triggers a simple AJAX callback.
  429. */
  430. function ajax_forms_test_lazy_load_form($form, &$form_state) {
  431. $form['add_files'] = array(
  432. '#type' => 'checkbox',
  433. '#default_value' => FALSE,
  434. );
  435. $form['submit'] = array(
  436. '#type' => 'submit',
  437. '#value' => t('Submit'),
  438. '#ajax' => array(
  439. 'callback' => 'ajax_forms_test_lazy_load_form_ajax',
  440. ),
  441. );
  442. return $form;
  443. }
  444. /**
  445. * Form submit handler: Adds JavaScript and CSS that wasn't on the original form.
  446. */
  447. function ajax_forms_test_lazy_load_form_submit($form, &$form_state) {
  448. if ($form_state['values']['add_files']) {
  449. drupal_add_js(array('ajax_forms_test_lazy_load_form_submit' => 'executed'), 'setting');
  450. drupal_add_css(drupal_get_path('module', 'system') . '/system.admin.css');
  451. drupal_add_js(drupal_get_path('module', 'system') . '/system.js');
  452. }
  453. $form_state['rebuild'] = TRUE;
  454. }
  455. /**
  456. * AJAX callback for the ajax_forms_test_lazy_load_form() form.
  457. *
  458. * This function returns nothing, because all we're interested in testing is
  459. * ajax_render() adding commands for JavaScript and CSS added during the page
  460. * request, such as the ones added in ajax_forms_test_lazy_load_form_submit().
  461. */
  462. function ajax_forms_test_lazy_load_form_ajax($form, &$form_state) {
  463. return NULL;
  464. }

Functions

Namesort descending Description
ajax_forms_test_advanced_commands_add_css_callback Ajax callback for 'add_css'.
ajax_forms_test_advanced_commands_after_callback Ajax callback for 'after'.
ajax_forms_test_advanced_commands_alert_callback Ajax callback for 'alert'.
ajax_forms_test_advanced_commands_append_callback Ajax callback for 'append'.
ajax_forms_test_advanced_commands_before_callback Ajax callback for 'before'.
ajax_forms_test_advanced_commands_changed_asterisk_callback Ajax callback for 'changed' with asterisk marking inner div.
ajax_forms_test_advanced_commands_changed_callback Ajax callback for 'changed'.
ajax_forms_test_advanced_commands_css_callback Ajax callback for 'css'.
ajax_forms_test_advanced_commands_data_callback Ajax callback for 'data'.
ajax_forms_test_advanced_commands_html_callback Ajax callback for 'html'.
ajax_forms_test_advanced_commands_insert_callback Ajax callback for 'insert'.
ajax_forms_test_advanced_commands_invoke_callback Ajax callback for 'invoke'.
ajax_forms_test_advanced_commands_prepend_callback Ajax callback for 'prepend'.
ajax_forms_test_advanced_commands_remove_callback Ajax callback for 'remove'.
ajax_forms_test_advanced_commands_restripe_callback Ajax callback for 'restripe'.
ajax_forms_test_advanced_commands_settings_callback Ajax callback for 'settings'.
ajax_forms_test_ajax_commands_form Form to display the Ajax Commands.
ajax_forms_test_lazy_load_form Form builder: Builds a form that triggers a simple AJAX callback.
ajax_forms_test_lazy_load_form_ajax AJAX callback for the ajax_forms_test_lazy_load_form() form.
ajax_forms_test_lazy_load_form_submit Form submit handler: Adds JavaScript and CSS that wasn't on the original form.
ajax_forms_test_menu Implements hook_menu().
ajax_forms_test_simple_form A basic form used to test form_state['values'] during callback.
ajax_forms_test_simple_form_checkbox_callback Ajax callback triggered by checkbox.
ajax_forms_test_simple_form_select_callback Ajax callback triggered by select.
ajax_forms_test_validation_form This form and its related submit and callback functions demonstrate not validating another form element when a single Ajax element is triggered.
ajax_forms_test_validation_form_callback Ajax callback for the 'drivertext' element of the validation form.
ajax_forms_test_validation_form_submit Submit handler for the validation form.