mollom_test.module

Tracking 7.x-2.x branch
  1. drupal
    1. 6 contributions/mollom/tests/mollom_test.module
    2. 7 contributions/mollom/tests/mollom_test.module

Testing functionality for Mollom module.

Functions & methods

NameDescription
mollom_formsImplements hook_forms().
mollom_test_delete_formForm constructor for deleting a Mollom test item.
mollom_test_delete_form_submitForm submission handler for mollom_test_delete_form().
mollom_test_delete_multipleDeletes multiple stored {mollom_test} data records.
mollom_test_entity_infoImplements hook_entity_info().
mollom_test_formForm builder for Mollom test form.
mollom_test_form_alterImplements hook_form_alter().
mollom_test_form_field_submitForm element submit handler for mollom_test_form().
mollom_test_form_submitForm submit handler for mollom_test_form().
mollom_test_loadLoads a {mollom_test} data record by id.
mollom_test_menuImplements hook_menu().
mollom_test_mollom_form_infoImplements hook_mollom_form_info().
mollom_test_mollom_form_listImplements hook_mollom_form_list().
mollom_test_mollom_form_moderationMollom form moderation callback for a mollom_test record.
mollom_test_saveSaves a {mollom_test} data record.
mollom_test_uriEntity URI callback.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Testing functionality for Mollom module.
  5. */
  6. /**
  7. * Implements hook_entity_info().
  8. */
  9. function mollom_test_entity_info() {
  10. $info['mollom_test'] = array(
  11. 'label' => 'Mollom test record',
  12. 'base table' => 'mollom_test',
  13. 'uri callback' => 'mollom_test_uri',
  14. 'entity keys' => array(
  15. 'id' => 'mid',
  16. 'label' => 'title',
  17. ),
  18. );
  19. return $info;
  20. }
  21. /**
  22. * Entity URI callback.
  23. */
  24. function mollom_test_uri($record) {
  25. return array(
  26. 'path' => 'mollom-test/form/' . $record->mid,
  27. );
  28. }
  29. /**
  30. * Implements hook_menu().
  31. */
  32. function mollom_test_menu() {
  33. $items['mollom-test/form'] = array(
  34. 'title' => 'Mollom test form',
  35. 'page callback' => 'drupal_get_form',
  36. 'page arguments' => array('mollom_test_form'),
  37. 'access callback' => TRUE,
  38. );
  39. $items['mollom-test/form/%mollom_test/delete'] = array(
  40. 'title' => 'Delete Mollom test item',
  41. 'page callback' => 'drupal_get_form',
  42. 'page arguments' => array('mollom_test_delete_form', 2),
  43. 'access callback' => TRUE,
  44. );
  45. return $items;
  46. }
  47. /**
  48. * Implements hook_forms().
  49. */
  50. function mollom_forms() {
  51. $forms['mollom_basic_test_form'] = array(
  52. 'callback' => 'mollom_test_form',
  53. );
  54. return $forms;
  55. }
  56. /**
  57. * Implements hook_mollom_form_list().
  58. */
  59. function mollom_test_mollom_form_list() {
  60. $forms['mollom_test_form'] = array(
  61. 'title' => 'Mollom test form',
  62. 'entity' => 'mollom_test',
  63. 'moderation callback' => 'mollom_test_mollom_form_moderation',
  64. 'delete form' => 'mollom_test_delete_form',
  65. );
  66. // The basic test form is identical to the mollom_test_form, but only
  67. // registers minimal information (e.g., no entity or moderation callback) to
  68. // integrate with Mollom.
  69. $forms['mollom_basic_test_form'] = array(
  70. 'title' => 'Mollom basic test form',
  71. );
  72. // Same as above, but supports elements for text analysis.
  73. $forms['mollom_basic_elements_test_form'] = array(
  74. 'title' => 'Mollom basic elements test form',
  75. );
  76. // Same as mollom_test_form, but supports an entity delete callback.
  77. // @todo Test mollom/moderate endpoint with entity delete callback.
  78. $forms['mollom_entity_test_form'] = array(
  79. 'title' => 'Mollom entity test form',
  80. 'entity' => 'mollom_test',
  81. 'entity delete multiple callback' => 'mollom_test_delete_multiple',
  82. );
  83. return $forms;
  84. }
  85. /**
  86. * Implements hook_mollom_form_info().
  87. */
  88. function mollom_test_mollom_form_info($form_id) {
  89. if ($form_id == 'mollom_basic_test_form') {
  90. return array();
  91. }
  92. $form_info = array(
  93. 'bypass access' => array('administer mollom'),
  94. 'elements' => array(
  95. 'title' => 'Title',
  96. 'body' => 'Body',
  97. 'exclude' => 'Some other field',
  98. 'parent][child' => 'Nested element',
  99. 'field' => 'Multiple value field',
  100. ),
  101. 'mapping' => array(
  102. 'post_id' => 'mid',
  103. 'post_title' => 'title',
  104. 'author_name' => 'name',
  105. ),
  106. );
  107. return $form_info;
  108. }
  109. /**
  110. * Form builder for Mollom test form.
  111. */
  112. function mollom_test_form($form, &$form_state, $mid = NULL) {
  113. // Due to #limit_validation_errors, submitting the form with the "Add" button
  114. // will only expose validated values in the submit handler, so our storage may
  115. // be incomplete. Therefore, the default values always have to be overloaded.
  116. $form_state += array('storage' => array());
  117. if (isset($mid) && ($record = mollom_test_load($mid))) {
  118. $form_state['storage'] = (array) $record;
  119. }
  120. $form_state['storage'] += array(
  121. 'mid' => $mid,
  122. 'title' => '',
  123. 'body' => '',
  124. 'exclude' => '',
  125. 'parent' => array('child' => ''),
  126. 'field' => array(),
  127. 'status' => 1,
  128. );
  129. // Always add an empty field the user can submit.
  130. $form_state['storage']['field']['new'] = '';
  131. $form['#tree'] = TRUE;
  132. $form['mid'] = array(
  133. '#type' => 'hidden',
  134. '#value' => $form_state['storage']['mid'],
  135. );
  136. $form['title'] = array(
  137. '#type' => 'textfield',
  138. '#title' => 'Title',
  139. '#default_value' => $form_state['storage']['title'],
  140. '#required' => TRUE,
  141. );
  142. $form['body'] = array(
  143. '#type' => 'textfield',
  144. '#title' => 'Body',
  145. '#default_value' => $form_state['storage']['body'],
  146. );
  147. $form['exclude'] = array(
  148. '#type' => 'textfield',
  149. '#title' => 'Some other field',
  150. '#default_value' => $form_state['storage']['exclude'],
  151. );
  152. $form['parent']['child'] = array(
  153. '#type' => 'textfield',
  154. '#title' => 'Nested element',
  155. '#default_value' => $form_state['storage']['parent']['child'],
  156. );
  157. $form['field'] = array(
  158. '#type' => 'fieldset',
  159. '#title' => 'Field',
  160. );
  161. $weight = 0;
  162. foreach ($form_state['storage']['field'] as $delta => $value) {
  163. $form['field'][$delta] = array(
  164. '#type' => 'textfield',
  165. '#title' => 'Field ' . $delta,
  166. '#default_value' => $value,
  167. '#weight' => $weight++,
  168. );
  169. }
  170. $form['field']['new']['#weight'] = 999;
  171. $form['field']['submit'] = array(
  172. '#type' => 'submit',
  173. '#value' => 'Add',
  174. '#limit_validation_errors' => array(array('field')),
  175. '#submit' => array('mollom_test_form_field_submit'),
  176. '#weight' => 1000,
  177. );
  178. $form['status'] = array(
  179. '#type' => 'checkbox',
  180. '#title' => 'Published',
  181. '#default_value' => $form_state['storage']['status'],
  182. // For simplicity, re-use Mollom module's administration permission.
  183. '#access' => user_access('administer mollom'),
  184. );
  185. $form['actions'] = array('#type' => 'actions');
  186. $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  187. return $form;
  188. }
  189. /**
  190. * Form element submit handler for mollom_test_form().
  191. */
  192. function mollom_test_form_field_submit($form, &$form_state) {
  193. // Remove internal Form API values.
  194. form_state_values_clean($form_state);
  195. // Remove all empty values of the multiple value field.
  196. $form_state['values']['field'] = array_filter($form_state['values']['field']);
  197. // Update the storage with submitted values.
  198. $form_state['storage'] = $form_state['values'];
  199. // Store the new value and clear out the 'new' field.
  200. if (isset($form_state['values']['field']['new'])) {
  201. $form_state['storage']['field'][] = $form_state['values']['field']['new'];
  202. }
  203. unset($form_state['input']['field']['new']);
  204. $form_state['rebuild'] = TRUE;
  205. }
  206. /**
  207. * Form submit handler for mollom_test_form().
  208. */
  209. function mollom_test_form_submit($form, &$form_state) {
  210. $form_state['values']['field'][] = $form_state['values']['field']['new'];
  211. unset($form_state['values']['field']['new']);
  212. // Store submission.
  213. form_state_values_clean($form_state);
  214. $record = (object) $form_state['values'];
  215. mollom_test_save($record);
  216. $form_state['values']['mid'] = $record->mid;
  217. // Redirect to stored entry.
  218. $form_state['redirect'] = 'mollom-test/form/' . $record->mid;
  219. drupal_set_message('Successful form submission.');
  220. drupal_set_message('<pre>' . var_export($record, TRUE) . '</pre>');
  221. }
  222. /**
  223. * Implements hook_form_alter().
  224. */
  225. function mollom_test_form_alter(&$form, &$form_state, $form_id) {
  226. if (variable_get('mollom_test.disable_mollom', FALSE)) {
  227. $form_state['mollom']['require_analysis'] = FALSE;
  228. $form_state['mollom']['require_captcha'] = FALSE;
  229. }
  230. }
  231. /**
  232. * Mollom form moderation callback for a mollom_test record.
  233. */
  234. function mollom_test_mollom_form_moderation(&$form, &$form_state) {
  235. $form_state['values']['status'] = 0;
  236. }
  237. /**
  238. * Loads a {mollom_test} data record by id.
  239. */
  240. function mollom_test_load($mid) {
  241. return db_query('SELECT * FROM {mollom_test} WHERE mid = :mid', array(':mid' => $mid))->fetch();
  242. }
  243. /**
  244. * Saves a {mollom_test} data record.
  245. */
  246. function mollom_test_save($record) {
  247. // Load the stored entity, if any.
  248. if (!empty($record->mid) && !isset($record->original)) {
  249. $record->original = entity_load_unchanged('mollom_test', $record->mid);
  250. }
  251. $update = !empty($record->mid) ? array('mid') : array();
  252. $status = drupal_write_record('mollom_test', $record, $update);
  253. if ($status === SAVED_NEW) {
  254. module_invoke_all('mollom_test_insert', $record);
  255. module_invoke_all('entity_insert', $record, 'mollom_test');
  256. }
  257. else {
  258. $status = SAVED_UPDATED;
  259. module_invoke_all('mollom_test_update', $record);
  260. module_invoke_all('entity_update', $record, 'mollom_test');
  261. }
  262. return $status;
  263. }
  264. /**
  265. * Deletes multiple stored {mollom_test} data records.
  266. *
  267. * @param $mids
  268. * The mids to delete.
  269. */
  270. function mollom_test_delete_multiple(array $mids) {
  271. foreach ($mids as $mid) {
  272. $record = mollom_test_load($mid);
  273. if ($record) {
  274. module_invoke_all('entity_delete', $record, 'mollom_test');
  275. }
  276. }
  277. db_delete('mollom_test')
  278. ->condition('mid', $mids)
  279. ->execute();
  280. }
  281. /**
  282. * Form constructor for deleting a Mollom test item.
  283. */
  284. function mollom_test_delete_form($form, &$form_state, $record) {
  285. $form['mid'] = array('#type' => 'value', '#value' => $record->mid);
  286. return confirm_form($form,
  287. t('Are you sure you want to delete the %title?', array('%title' => $record->title)),
  288. 'mollom-test/form/' . $record->mid,
  289. NULL,
  290. t('Delete')
  291. );
  292. }
  293. /**
  294. * Form submission handler for mollom_test_delete_form().
  295. */
  296. function mollom_test_delete_form_submit($form, &$form_state) {
  297. mollom_test_delete_multiple(array($form_state['values']['mid']));
  298. drupal_set_message('The record has been deleted.');
  299. $form_state['redirect'] = 'mollom-test/form';
  300. }