PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/ajax/DeleteComment.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests
PHP | 355 lines | 173 code | 60 blank | 122 comment | 3 complexity | 3bddab1acd82f7280a53cb7673297ad5 MD5 | raw file
  1. <?php
  2. /**
  3. * Admin ajax functions to be tested
  4. */
  5. require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
  6. /**
  7. * Testing ajax comment functionality
  8. *
  9. * @package WordPress
  10. * @subpackage UnitTests
  11. * @since 3.4.0
  12. * @group ajax
  13. */
  14. class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
  15. /**
  16. * List of comments
  17. * @var array
  18. */
  19. protected $_comments = array();
  20. /**
  21. * Set up the test fixture
  22. */
  23. public function setUp() {
  24. parent::setUp();
  25. $post_id = $this->factory->post->create();
  26. $this->_comments = $this->factory->comment->create_post_comments( $post_id, 15 );
  27. $this->_comments = array_map( 'get_comment', $this->_comments );
  28. }
  29. /**
  30. * Clear the POST actions in between requests
  31. */
  32. protected function _clear_post_action() {
  33. unset($_POST['trash']);
  34. unset($_POST['untrash']);
  35. unset($_POST['spam']);
  36. unset($_POST['unspam']);
  37. unset($_POST['delete']);
  38. $this->_last_response = '';
  39. }
  40. /***********************************************************/
  41. /** Test prototype
  42. /***********************************************************/
  43. /**
  44. * Test as a privilged user (administrator)
  45. * Expects test to pass
  46. * @param mixed $comment Comment object
  47. * @param string action trash, untrash, etc.
  48. * @return void
  49. */
  50. public function _test_as_admin( $comment, $action ) {
  51. // Reset request
  52. $this->_clear_post_action();
  53. // Become an administrator
  54. $this->_setRole( 'administrator' );
  55. // Set up a default request
  56. $_POST['id'] = $comment->comment_ID;
  57. $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment->comment_ID );
  58. $_POST[$action] = 1;
  59. $_POST['_total'] = count( $this->_comments );
  60. $_POST['_per_page'] = 100;
  61. $_POST['_page'] = 1;
  62. $_POST['_url'] = admin_url( 'edit-comments.php' );
  63. // Make the request
  64. try {
  65. $this->_handleAjax( 'delete-comment' );
  66. } catch ( WPAjaxDieContinueException $e ) {
  67. unset( $e );
  68. }
  69. // Get the response
  70. $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
  71. // Ensure everything is correct
  72. $this->assertEquals( $comment->comment_ID, (string) $xml->response[0]->comment['id'] );
  73. $this->assertEquals( 'delete-comment_' . $comment->comment_ID, (string) $xml->response['action'] );
  74. $this->assertGreaterThanOrEqual( time() - 10, (int) $xml->response[0]->comment[0]->supplemental[0]->time[0] );
  75. $this->assertLessThanOrEqual( time(), (int) $xml->response[0]->comment[0]->supplemental[0]->time[0] );
  76. // trash, spam, delete should make the total go down
  77. if ( in_array( $action, array( 'trash', 'spam', 'delete' ) ) ) {
  78. $total = $_POST['_total'] - 1;
  79. // unspam, untrash should make the total go up
  80. } elseif ( in_array( $action, array( 'untrash', 'unspam' ) ) ) {
  81. $total = $_POST['_total'] + 1;
  82. }
  83. // The total is calculated based on a page break -OR- a random number. Let's look for both possible outcomes
  84. $comment_count = wp_count_comments( 0 );
  85. $recalc_total = $comment_count->total_comments;
  86. // Check for either possible total
  87. $this->assertTrue( in_array( (int) $xml->response[0]->comment[0]->supplemental[0]->total[0] , array( $total, $recalc_total ) ) );
  88. }
  89. /**
  90. * Test as a non-privileged user (subscriber)
  91. * Expects test to fail
  92. * @param mixed $comment Comment object
  93. * @param string action trash, untrash, etc.
  94. * @return void
  95. */
  96. public function _test_as_subscriber( $comment, $action ) {
  97. // Reset request
  98. $this->_clear_post_action();
  99. // Become a subscriber
  100. $this->_setRole( 'subscriber' );
  101. // Set up the $_POST request
  102. $_POST['id'] = $comment->comment_ID;
  103. $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment->comment_ID );
  104. $_POST[$action] = 1;
  105. $_POST['_total'] = count( $this->_comments );
  106. $_POST['_per_page'] = 100;
  107. $_POST['_page'] = 1;
  108. $_POST['_url'] = admin_url( 'edit-comments.php' );
  109. // Make the request
  110. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  111. $this->_handleAjax( 'delete-comment' );
  112. }
  113. /**
  114. * Test with a bad nonce
  115. * Expects test to fail
  116. * @param mixed $comment Comment object
  117. * @param string action trash, untrash, etc.
  118. * @return void
  119. */
  120. public function _test_with_bad_nonce( $comment, $action ) {
  121. // Reset request
  122. $this->_clear_post_action();
  123. // Become a subscriber
  124. $this->_setRole( 'administrator' );
  125. // Set up the $_POST request
  126. $_POST['id'] = $comment->comment_ID;
  127. $_POST['_ajax_nonce'] = wp_create_nonce( uniqid() );
  128. $_POST[$action] = 1;
  129. $_POST['_total'] = count( $this->_comments );
  130. $_POST['_per_page'] = 100;
  131. $_POST['_page'] = 1;
  132. $_POST['_url'] = admin_url( 'edit-comments.php' );
  133. // Make the request
  134. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  135. $this->_handleAjax( 'delete-comment' );
  136. }
  137. /**
  138. * Test with a bad id
  139. * Expects test to fail
  140. * @param mixed $comment Comment object
  141. * @param string action trash, untrash, etc.
  142. * @return void
  143. */
  144. public function _test_with_bad_id( $comment, $action ) {
  145. // Reset request
  146. $this->_clear_post_action();
  147. // Become a subscriber
  148. $this->_setRole( 'administrator' );
  149. // Set up the $_POST request
  150. $_POST['id'] = 12346789;
  151. $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_12346789' );
  152. $_POST[$action] = 1;
  153. $_POST['_total'] = count( $this->_comments );
  154. $_POST['_per_page'] = 100;
  155. $_POST['_page'] = 1;
  156. $_POST['_url'] = admin_url( 'edit-comments.php' );
  157. // Make the request, look for a timestamp in the exception
  158. try {
  159. $this->_handleAjax( 'delete-comment' );
  160. $this->fail( 'Expected exception: WPAjaxDieStopException' );
  161. } catch ( WPAjaxDieStopException $e ) {
  162. $this->assertEquals( 10, strlen( $e->getMessage() ) );
  163. $this->assertTrue( is_numeric( $e->getMessage() ) );
  164. } catch ( Exception $e ) {
  165. $this->fail( 'Unexpected exception type: ' . get_class( $e ) );
  166. }
  167. }
  168. /**
  169. * Test doubling the action (e.g. trash a trashed comment)
  170. * Expects test to fail
  171. * @param mixed $comment Comment object
  172. * @param string action trash, untrash, etc.
  173. * @return void
  174. */
  175. public function _test_double_action( $comment, $action ) {
  176. // Reset request
  177. $this->_clear_post_action();
  178. // Become a subscriber
  179. $this->_setRole( 'administrator' );
  180. // Set up the $_POST request
  181. $_POST['id'] = $comment->comment_ID;
  182. $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment->comment_ID );
  183. $_POST[$action] = 1;
  184. $_POST['_total'] = count( $this->_comments );
  185. $_POST['_per_page'] = 100;
  186. $_POST['_page'] = 1;
  187. $_POST['_url'] = admin_url( 'edit-comments.php' );
  188. // Make the request
  189. try {
  190. $this->_handleAjax( 'delete-comment' );
  191. } catch ( WPAjaxDieContinueException $e ) {
  192. unset( $e );
  193. }
  194. $this->_last_response = '';
  195. // Force delete the comment
  196. if ( 'delete' == $action ) {
  197. wp_delete_comment( $comment->comment_ID, true );
  198. }
  199. // Make the request again, look for a timestamp in the exception
  200. try {
  201. $this->_handleAjax( 'delete-comment' );
  202. $this->fail( 'Expected exception: WPAjaxDieStopException' );
  203. } catch ( WPAjaxDieStopException $e ) {
  204. $this->assertEquals( 10, strlen( $e->getMessage() ) );
  205. $this->assertTrue( is_numeric( $e->getMessage() ) );
  206. } catch ( Exception $e ) {
  207. $this->fail( 'Unexpected exception type: ' . get_class( $e ) );
  208. }
  209. }
  210. /**
  211. * Delete a comment as an administrator (expects success)
  212. * @return void
  213. */
  214. public function test_ajax_comment_trash_actions_as_administrator() {
  215. // Test trash/untrash
  216. $comment = array_pop( $this->_comments );
  217. $this->_test_as_admin( $comment, 'trash' );
  218. $this->_test_as_admin( $comment, 'untrash' );
  219. // Test spam/unspam
  220. $comment = array_pop( $this->_comments );
  221. $this->_test_as_admin( $comment, 'spam' );
  222. $this->_test_as_admin( $comment, 'unspam' );
  223. // Test delete
  224. $comment = array_pop( $this->_comments );
  225. $this->_test_as_admin( $comment, 'delete' );
  226. }
  227. /**
  228. * Delete a comment as a subscriber (expects permission denied)
  229. * @return void
  230. */
  231. public function test_ajax_comment_trash_actions_as_subscriber() {
  232. // Test trash/untrash
  233. $comment = array_pop( $this->_comments );
  234. $this->_test_as_subscriber( $comment, 'trash' );
  235. $this->_test_as_subscriber( $comment, 'untrash' );
  236. // Test spam/unspam
  237. $comment = array_pop( $this->_comments );
  238. $this->_test_as_subscriber( $comment, 'spam' );
  239. $this->_test_as_subscriber( $comment, 'unspam' );
  240. // Test delete
  241. $comment = array_pop( $this->_comments );
  242. $this->_test_as_subscriber( $comment, 'delete' );
  243. }
  244. /**
  245. * Delete a comment with no id
  246. * @return void
  247. */
  248. public function test_ajax_trash_comment_no_id() {
  249. // Test trash/untrash
  250. $comment = array_pop( $this->_comments );
  251. $this->_test_as_admin( $comment, 'trash' );
  252. $this->_test_as_admin( $comment, 'untrash' );
  253. // Test spam/unspam
  254. $comment = array_pop( $this->_comments );
  255. $this->_test_as_admin( $comment, 'spam' );
  256. $this->_test_as_admin( $comment, 'unspam' );
  257. // Test delete
  258. $comment = array_pop( $this->_comments );
  259. $this->_test_as_admin( $comment, 'delete' );
  260. }
  261. /**
  262. * Delete a comment with a bad nonce
  263. * @return void
  264. */
  265. public function test_ajax_trash_comment_bad_nonce() {
  266. // Test trash/untrash
  267. $comment = array_pop( $this->_comments );
  268. $this->_test_with_bad_nonce( $comment, 'trash' );
  269. $this->_test_with_bad_nonce( $comment, 'untrash' );
  270. // Test spam/unspam
  271. $comment = array_pop( $this->_comments );
  272. $this->_test_with_bad_nonce( $comment, 'spam' );
  273. $this->_test_with_bad_nonce( $comment, 'unspam' );
  274. // Test delete
  275. $comment = array_pop( $this->_comments );
  276. $this->_test_with_bad_nonce( $comment, 'delete' );
  277. }
  278. /**
  279. * Test trashing an already trashed comment, etc.
  280. * @return void
  281. */
  282. public function test_ajax_trash_double_action() {
  283. // Test trash/untrash
  284. $comment = array_pop( $this->_comments );
  285. $this->_test_double_action( $comment, 'trash' );
  286. $this->_test_double_action( $comment, 'untrash' );
  287. // Test spam/unspam
  288. $comment = array_pop( $this->_comments );
  289. $this->_test_double_action( $comment, 'spam' );
  290. $this->_test_double_action( $comment, 'unspam' );
  291. // Test delete
  292. $comment = array_pop( $this->_comments );
  293. $this->_test_double_action( $comment, 'delete' );
  294. }
  295. }