GetComments.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_GetComments extends WP_Ajax_UnitTestCase {
  15. /**
  16. * A post with at least one comment
  17. * @var mixed
  18. */
  19. protected $_comment_post = null;
  20. /**
  21. * A post with no comments
  22. * @var mixed
  23. */
  24. protected $_no_comment_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();
  34. $this->_no_comment_post = get_post( $post_id );
  35. unset( $GLOBALS['post_id'] );
  36. }
  37. /**
  38. * Get comments 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. // Set up a default request
  46. $_POST['_ajax_nonce'] = wp_create_nonce( 'get-comments' );
  47. $_POST['action'] = 'get-comments';
  48. $_POST['p'] = $this->_comment_post->ID;
  49. // Make the request
  50. try {
  51. $this->_handleAjax( 'get-comments' );
  52. } catch ( WPAjaxDieContinueException $e ) {
  53. unset( $e );
  54. }
  55. // Get the response
  56. $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
  57. // Check the meta data
  58. $this->assertEquals( 1, (string) $xml->response[0]->comments['position'] );
  59. $this->assertEquals( 0, (string) $xml->response[0]->comments['id'] );
  60. $this->assertEquals( 'get-comments_0', (string) $xml->response['action'] );
  61. // Check the payload
  62. $this->assertNotEmpty( (string) $xml->response[0]->comments[0]->response_data );
  63. // And supplemental is empty
  64. $this->assertEmpty( (string) $xml->response[0]->comments[0]->supplemental );
  65. }
  66. /**
  67. * Get comments as a non-privileged user (subscriber)
  68. * Expects test to fail
  69. * @return void
  70. */
  71. public function test_as_subscriber() {
  72. // Become a subscriber
  73. $this->_setRole( 'subscriber' );
  74. // Set up a default request
  75. $_POST['_ajax_nonce'] = wp_create_nonce( 'get-comments' );
  76. $_POST['action'] = 'get-comments';
  77. $_POST['p'] = $this->_comment_post->ID;
  78. // Make the request
  79. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  80. $this->_handleAjax( 'get-comments' );
  81. }
  82. /**
  83. * Get comments with a bad nonce
  84. * Expects test to fail
  85. * @return void
  86. */
  87. public function test_bad_nonce() {
  88. // Become an administrator
  89. $this->_setRole( 'administrator' );
  90. // Set up a default request
  91. $_POST['_ajax_nonce'] = wp_create_nonce( uniqid() );
  92. $_POST['action'] = 'get-comments';
  93. $_POST['p'] = $this->_comment_post->ID;
  94. // Make the request
  95. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  96. $this->_handleAjax( 'get-comments' );
  97. }
  98. /**
  99. * Get comments for an invalid post
  100. * Bad post IDs are set to 0, this should return valid XML
  101. * @return void
  102. */
  103. public function test_invalid_post() {
  104. // Become an administrator
  105. $this->_setRole( 'administrator' );
  106. // Set up a default request
  107. $_POST['_ajax_nonce'] = wp_create_nonce( 'get-comments' );
  108. $_POST['action'] = 'get-comments';
  109. $_POST['p'] = 'b0rk';
  110. // Make the request
  111. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  112. $this->_handleAjax( 'get-comments' );
  113. }
  114. /**
  115. * Get comments for an invalid post
  116. * Bad post IDs are set to 0, this should return valid XML
  117. * @return void
  118. */
  119. public function test_post_with_no_comments() {
  120. // Become an administrator
  121. $this->_setRole( 'administrator' );
  122. // Set up a default request
  123. $_POST['_ajax_nonce'] = wp_create_nonce( 'get-comments' );
  124. $_POST['action'] = 'get-comments';
  125. $_POST['p'] = $this->_no_comment_post->ID;
  126. // Make the request
  127. $this->setExpectedException( 'WPAjaxDieStopException', '1' );
  128. $this->_handleAjax( 'get-comments' );
  129. }
  130. }