EditComment.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_EditComment extends WP_Ajax_UnitTestCase {
  15. /**
  16. * A post with at least one comment
  17. * @var mixed
  18. */
  19. protected $_comment_post = null;
  20. /**
  21. * Set up the test fixture
  22. */
  23. public function setUp() {
  24. parent::setUp();
  25. $post_id = $this->factory->post->create();
  26. $this->factory->comment->create_post_comments( $post_id, 5 );
  27. $this->_comment_post = get_post( $post_id );
  28. }
  29. /**
  30. * Get comments as a privilged user (administrator)
  31. * Expects test to pass
  32. * @return void
  33. */
  34. public function test_as_admin() {
  35. // Become an administrator
  36. $this->_setRole( 'administrator' );
  37. // Get a comment
  38. $comments = get_comments( array(
  39. 'post_id' => $this->_comment_post->ID
  40. ) );
  41. $comment = array_pop( $comments );
  42. // Set up a default request
  43. $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
  44. $_POST['comment_ID'] = $comment->comment_ID;
  45. $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  46. // Make the request
  47. try {
  48. $this->_handleAjax( 'edit-comment' );
  49. } catch ( WPAjaxDieContinueException $e ) {
  50. unset( $e );
  51. }
  52. // Get the response
  53. $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
  54. // Check the meta data
  55. $this->assertEquals( -1, (string) $xml->response[0]->edit_comment['position'] );
  56. $this->assertEquals( $comment->comment_ID, (string) $xml->response[0]->edit_comment['id'] );
  57. $this->assertEquals( 'edit-comment_' . $comment->comment_ID, (string) $xml->response['action'] );
  58. // Check the payload
  59. $this->assertNotEmpty( (string) $xml->response[0]->edit_comment[0]->response_data );
  60. // And supplemental is empty
  61. $this->assertEmpty( (string) $xml->response[0]->edit_comment[0]->supplemental );
  62. }
  63. /**
  64. * Get comments as a non-privileged user (subscriber)
  65. * Expects test to fail
  66. * @return void
  67. */
  68. public function test_as_subscriber() {
  69. // Become an administrator
  70. $this->_setRole( 'subscriber' );
  71. // Get a comment
  72. $comments = get_comments( array(
  73. 'post_id' => $this->_comment_post->ID
  74. ) );
  75. $comment = array_pop( $comments );
  76. // Set up a default request
  77. $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
  78. $_POST['comment_ID'] = $comment->comment_ID;
  79. $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  80. // Make the request
  81. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  82. $this->_handleAjax( 'edit-comment' );
  83. }
  84. /**
  85. * Get comments with a bad nonce
  86. * Expects test to fail
  87. * @return void
  88. */
  89. public function test_bad_nonce() {
  90. // Become an administrator
  91. $this->_setRole( 'administrator' );
  92. // Get a comment
  93. $comments = get_comments( array(
  94. 'post_id' => $this->_comment_post->ID
  95. ) );
  96. $comment = array_pop( $comments );
  97. // Set up a default request
  98. $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( uniqid() );
  99. $_POST['comment_ID'] = $comment->comment_ID;
  100. $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  101. // Make the request
  102. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  103. $this->_handleAjax( 'get-comments' );
  104. }
  105. /**
  106. * Get comments for an invalid post
  107. * This should return valid XML
  108. * @return void
  109. */
  110. public function test_invalid_comment() {
  111. // Become an administrator
  112. $this->_setRole( 'administrator' );
  113. // Set up a default request
  114. $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
  115. $_POST['comment_ID'] = 123456789;
  116. $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  117. // Make the request
  118. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  119. $this->_handleAjax( 'edit-comment' );
  120. }
  121. }