/tests/ajax/DimComment.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests · PHP · 238 lines · 109 code · 40 blank · 89 comment · 2 complexity · d4d9fb3097c3fcb807d137111a274e8c 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_DimComment 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['id']);
  34. unset($_POST['new']);
  35. $this->_last_response = '';
  36. }
  37. /***********************************************************/
  38. /** Test prototype
  39. /***********************************************************/
  40. /**
  41. * Test as a privilged user (administrator)
  42. * Expects test to pass
  43. * @param mixed $comment Comment object
  44. * @return void
  45. */
  46. public function _test_as_admin( $comment ) {
  47. // Reset request
  48. $this->_clear_post_action();
  49. // Become an administrator
  50. $this->_setRole( 'administrator' );
  51. // Set up a default request
  52. $_POST['id'] = $comment->comment_ID;
  53. $_POST['_ajax_nonce'] = wp_create_nonce( 'approve-comment_' . $comment->comment_ID );
  54. $_POST['_total'] = count( $this->_comments );
  55. $_POST['_per_page'] = 100;
  56. $_POST['_page'] = 1;
  57. $_POST['_url'] = admin_url( 'edit-comments.php' );
  58. // Save the comment status
  59. $prev_status = wp_get_comment_status( $comment->comment_ID );
  60. // Make the request
  61. try {
  62. $this->_handleAjax( 'dim-comment' );
  63. } catch ( WPAjaxDieContinueException $e ) {
  64. unset( $e );
  65. }
  66. // Get the response
  67. $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
  68. // Ensure everything is correct
  69. $this->assertEquals( $comment->comment_ID, (string) $xml->response[0]->comment['id'] );
  70. $this->assertEquals( 'dim-comment_' . $comment->comment_ID, (string) $xml->response['action'] );
  71. $this->assertGreaterThanOrEqual( time() - 10, (int) $xml->response[0]->comment[0]->supplemental[0]->time[0] );
  72. $this->assertLessThanOrEqual( time(), (int) $xml->response[0]->comment[0]->supplemental[0]->time[0] );
  73. // Check the status
  74. $current = wp_get_comment_status( $comment->comment_ID );
  75. if (in_array( $prev_status, array( 'unapproved', 'spam') ) ) {
  76. $this->assertEquals( 'approved', $current );
  77. } else {
  78. $this->assertEquals( 'unapproved', $current );
  79. }
  80. // The total is calculated based on a page break -OR- a random number. Let's look for both possible outcomes
  81. $comment_count = wp_count_comments( 0 );
  82. $recalc_total = $comment_count->total_comments;
  83. // Delta is not specified, it will always be 1 lower than the request
  84. $total = $_POST['_total'] - 1;
  85. // Check for either possible total
  86. $this->assertTrue( in_array( (int) $xml->response[0]->comment[0]->supplemental[0]->total[0] , array( $total, $recalc_total ) ) );
  87. }
  88. /**
  89. * Test as a non-privileged user (subscriber)
  90. * Expects test to fail
  91. * @param mixed $comment Comment object
  92. * @return void
  93. */
  94. public function _test_as_subscriber( $comment ) {
  95. // Reset request
  96. $this->_clear_post_action();
  97. // Become a subscriber
  98. $this->_setRole( 'subscriber' );
  99. // Set up the $_POST request
  100. $_POST['id'] = $comment->comment_ID;
  101. $_POST['_ajax_nonce'] = wp_create_nonce( 'approve-comment_' . $comment->comment_ID );
  102. $_POST['_total'] = count( $this->_comments );
  103. $_POST['_per_page'] = 100;
  104. $_POST['_page'] = 1;
  105. $_POST['_url'] = admin_url( 'edit-comments.php' );
  106. // Make the request
  107. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  108. $this->_handleAjax( 'dim-comment' );
  109. }
  110. /**
  111. * Test with a bad nonce
  112. * Expects test to fail
  113. * @param mixed $comment Comment object
  114. * @return void
  115. */
  116. public function _test_with_bad_nonce( $comment ) {
  117. // Reset request
  118. $this->_clear_post_action();
  119. // Become a subscriber
  120. $this->_setRole( 'administrator' );
  121. // Set up the $_POST request
  122. $_POST['id'] = $comment->comment_ID;
  123. $_POST['_ajax_nonce'] = wp_create_nonce( uniqid() );
  124. $_POST['_total'] = count( $this->_comments );
  125. $_POST['_per_page'] = 100;
  126. $_POST['_page'] = 1;
  127. $_POST['_url'] = admin_url( 'edit-comments.php' );
  128. // Make the request
  129. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  130. $this->_handleAjax( 'dim-comment' );
  131. }
  132. /**
  133. * Test with a bad id
  134. * Expects test to fail
  135. * @param mixed $comment Comment object
  136. * @return void
  137. */
  138. public function test_with_bad_id( $comment ) {
  139. // Reset request
  140. $this->_clear_post_action();
  141. // Become a subscriber
  142. $this->_setRole( 'administrator' );
  143. // Set up the $_POST request
  144. $_POST['id'] = 12346789;
  145. $_POST['_ajax_nonce'] = wp_create_nonce( 'dim-comment_12346789' );
  146. $_POST['_total'] = count( $this->_comments );
  147. $_POST['_per_page'] = 100;
  148. $_POST['_page'] = 1;
  149. $_POST['_url'] = admin_url( 'edit-comments.php' );
  150. // Make the request, look for a timestamp in the exception
  151. try {
  152. $this->_handleAjax( 'dim-comment' );
  153. $this->fail( 'Expected exception: WPAjaxDieContinueException' );
  154. } catch ( WPAjaxDieContinueException $e ) {
  155. // Get the response
  156. $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
  157. // Ensure everything is correct
  158. $this->assertEquals( '0', (string) $xml->response[0]->comment['id'] );
  159. $this->assertEquals( 'dim-comment_0', (string) $xml->response['action'] );
  160. $this->assertContains( 'Comment ' . $_POST['id'] . ' does not exist', $this->_last_response );
  161. } catch ( Exception $e ) {
  162. $this->fail( 'Unexpected exception type: ' . get_class( $e ) );
  163. }
  164. }
  165. /**
  166. * Dim a comment as an administrator (expects success)
  167. * @return void
  168. */
  169. public function test_ajax_comment_dim_actions_as_administrator() {
  170. $comment = array_pop( $this->_comments );
  171. $this->_test_as_admin( $comment );
  172. $this->_test_as_admin( $comment );
  173. }
  174. /**
  175. * Dim a comment as a subscriber (expects permission denied)
  176. * @return void
  177. */
  178. public function test_ajax_comment_dim_actions_as_subscriber() {
  179. $comment = array_pop( $this->_comments );
  180. $this->_test_as_subscriber( $comment );
  181. }
  182. /**
  183. * Dim a comment with no id
  184. * @return void
  185. */
  186. public function test_ajax_dim_comment_no_id() {
  187. $comment = array_pop( $this->_comments );
  188. $this->_test_as_admin( $comment );
  189. }
  190. /**
  191. * Dim a comment with a bad nonce
  192. * @return void
  193. */
  194. public function test_ajax_dim_comment_bad_nonce() {
  195. $comment = array_pop( $this->_comments );
  196. $this->_test_with_bad_nonce( $comment );
  197. }
  198. }