PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ajax/ReplytoComment.php

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