ReplytoComment.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_ReplytoComment extends WP_Ajax_UnitTestCase {
  15. /**
  16. * A post with at least one comment
  17. * @var mixed
  18. */
  19. protected $_comment_post = null;
  20. /**
  21. * Draft post
  22. * @var mixed
  23. */
  24. protected $_draft_post = null;
  25. /**
  26. * Set up the test fixture
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. $post_id = $this->factory->post->create();
  31. $this->factory->comment->create_post_comments( $post_id, 5 );
  32. $this->_comment_post = get_post( $post_id );
  33. $post_id = $this->factory->post->create( array( 'post_status' => 'draft' ) );
  34. $this->_draft_post = get_post( $post_id );
  35. $_SERVER['REMOTE_ADDR'] = '';
  36. }
  37. /**
  38. * Reply as a privilged user (administrator)
  39. * Expects test to pass
  40. * @return void
  41. */
  42. public function test_as_admin() {
  43. // Become an administrator
  44. $this->_setRole( 'administrator' );
  45. // Get a comment
  46. $comments = get_comments( array(
  47. 'post_id' => $this->_comment_post->ID
  48. ) );
  49. $comment = array_pop( $comments );
  50. // Set up a default request
  51. $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
  52. $_POST['comment_ID'] = $comment->comment_ID;
  53. $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  54. $_POST['comment_post_ID'] = $this->_comment_post->ID;
  55. // Make the request
  56. try {
  57. $this->_handleAjax( 'replyto-comment' );
  58. } catch ( WPAjaxDieContinueException $e ) {
  59. unset( $e );
  60. }
  61. // Get the response
  62. $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
  63. // Check the meta data
  64. $this->assertEquals( -1, (string) $xml->response[0]->comment['position'] );
  65. $this->assertGreaterThan( 0, (int) $xml->response[0]->comment['id'] );
  66. $this->assertNotEmpty( (string) $xml->response['action'] );
  67. // Check the payload
  68. $this->assertNotEmpty( (string) $xml->response[0]->comment[0]->response_data );
  69. // And supplemental is empty
  70. $this->assertEmpty( (string) $xml->response[0]->comment[0]->supplemental );
  71. }
  72. /**
  73. * Reply as a non-privileged user (subscriber)
  74. * Expects test to fail
  75. * @return void
  76. */
  77. public function test_as_subscriber() {
  78. // Become an administrator
  79. $this->_setRole( 'subscriber' );
  80. // Get a comment
  81. $comments = get_comments( array(
  82. 'post_id' => $this->_comment_post->ID
  83. ) );
  84. $comment = array_pop( $comments );
  85. // Set up a default request
  86. $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
  87. $_POST['comment_ID'] = $comment->comment_ID;
  88. $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  89. $_POST['comment_post_ID'] = $this->_comment_post->ID;
  90. // Make the request
  91. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  92. $this->_handleAjax( 'replyto-comment' );
  93. }
  94. /**
  95. * Reply using a bad nonce
  96. * Expects test to fail
  97. * @return void
  98. */
  99. public function test_bad_nonce() {
  100. // Become an administrator
  101. $this->_setRole( 'administrator' );
  102. // Get a comment
  103. $comments = get_comments( array(
  104. 'post_id' => $this->_comment_post->ID
  105. ) );
  106. $comment = array_pop( $comments );
  107. // Set up a default request
  108. $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( uniqid() );
  109. $_POST['comment_ID'] = $comment->comment_ID;
  110. $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  111. $_POST['comment_post_ID'] = $this->_comment_post->ID;
  112. // Make the request
  113. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  114. $this->_handleAjax( 'replyto-comment' );
  115. }
  116. /**
  117. * Reply to an invalid post
  118. * Expects test to fail
  119. * @return void
  120. */
  121. public function test_invalid_post() {
  122. // Become an administrator
  123. $this->_setRole( 'administrator' );
  124. // Set up a default request
  125. $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
  126. $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  127. $_POST['comment_post_ID'] = 123456789;
  128. // Make the request
  129. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  130. $this->_handleAjax( 'replyto-comment' );
  131. }
  132. /**
  133. * Reply to a draft post
  134. * Expects test to fail
  135. * @return void
  136. */
  137. public function test_with_draft_post() {
  138. // Become an administrator
  139. $this->_setRole( 'administrator' );
  140. // Set up a default request
  141. $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
  142. $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  143. $_POST['comment_post_ID'] = $this->_draft_post->ID;
  144. // Make the request
  145. $this->setExpectedException( 'WPAjaxDieStopException', 'ERROR: you are replying to a comment on a draft post.' );
  146. $this->_handleAjax( 'replyto-comment' );
  147. }
  148. /**
  149. * Reply to a post with a simulated database failure
  150. * Expects test to fail
  151. * @global $wpdb
  152. * @return void
  153. */
  154. public function test_blocked_comment() {
  155. global $wpdb;
  156. // Become an administrator
  157. $this->_setRole( 'administrator' );
  158. // Set up a default request
  159. $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
  160. $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  161. $_POST['comment_post_ID'] = $this->_comment_post->ID;
  162. // Block comments from being saved, simulate a DB error
  163. add_filter( 'query', array( $this, '_block_comments' ) );
  164. // Make the request
  165. try {
  166. $wpdb->suppress_errors( true );
  167. $this->_handleAjax( 'replyto-comment' );
  168. $wpdb->suppress_errors( false );
  169. $this->fail();
  170. } catch ( WPAjaxDieStopException $e ) {
  171. $wpdb->suppress_errors( false );
  172. $this->assertContains( '1', $e->getMessage() );
  173. }
  174. }
  175. /**
  176. * Block comments from being saved
  177. * @param string $sql
  178. * @return string
  179. */
  180. public function _block_comments( $sql ) {
  181. global $wpdb;
  182. if ( false !== strpos( $sql, $wpdb->comments ) && 0 === stripos( trim ( $sql ), 'INSERT INTO') ) {
  183. remove_filter( 'query', array( $this, '_block_comments' ) );
  184. return '';
  185. }
  186. return $sql;
  187. }
  188. }