mollom_test.module

Tracking 6.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_entity_deleteImplements non-existing hook_entity_delete() on behalf of Mollom module.
mollom_formsImplements hook_forms().
mollom_mollom_test_presaveImplements hook_mollom_test_presave() on behalf of mollom.module.
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_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.

File

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