GetComments.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. *
  18. * @var mixed
  19. */
  20. protected static $comment_post = null;
  21. /**
  22. * A post with no comments.
  23. *
  24. * @var mixed
  25. */
  26. protected static $no_comment_post = null;
  27. protected static $comment_ids = array();
  28. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  29. self::$comment_post = $factory->post->create_and_get();
  30. self::$comment_ids = $factory->comment->create_post_comments( self::$comment_post->ID, 5 );
  31. self::$no_comment_post = $factory->post->create_and_get();
  32. }
  33. /**
  34. * Gets comments as a privileged user (administrator).
  35. *
  36. * Expects test to pass.
  37. */
  38. public function test_as_admin() {
  39. // Become an administrator.
  40. $this->_setRole( 'administrator' );
  41. // Set up a default request.
  42. $_POST['_ajax_nonce'] = wp_create_nonce( 'get-comments' );
  43. $_POST['action'] = 'get-comments';
  44. $_POST['p'] = self::$comment_post->ID;
  45. // Make the request.
  46. try {
  47. $this->_handleAjax( 'get-comments' );
  48. } catch ( WPAjaxDieContinueException $e ) {
  49. unset( $e );
  50. }
  51. // Get the response.
  52. $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
  53. // Check the meta data.
  54. $this->assertSame( '1', (string) $xml->response[0]->comments['position'] );
  55. $this->assertSame( '0', (string) $xml->response[0]->comments['id'] );
  56. $this->assertSame( 'get-comments_0', (string) $xml->response['action'] );
  57. // Check the payload.
  58. $this->assertNotEmpty( (string) $xml->response[0]->comments[0]->response_data );
  59. // And supplemental is empty.
  60. $this->assertEmpty( (string) $xml->response[0]->comments[0]->supplemental );
  61. }
  62. /**
  63. * Gets comments as a non-privileged user (subscriber).
  64. *
  65. * Expects test to fail.
  66. */
  67. public function test_as_subscriber() {
  68. // Become a subscriber.
  69. $this->_setRole( 'subscriber' );
  70. // Set up a default request.
  71. $_POST['_ajax_nonce'] = wp_create_nonce( 'get-comments' );
  72. $_POST['action'] = 'get-comments';
  73. $_POST['p'] = self::$comment_post->ID;
  74. // Make the request.
  75. $this->expectException( 'WPAjaxDieStopException' );
  76. $this->expectExceptionMessage( '-1' );
  77. $this->_handleAjax( 'get-comments' );
  78. }
  79. /**
  80. * Gets comments with a bad nonce.
  81. *
  82. * Expects test to fail.
  83. */
  84. public function test_bad_nonce() {
  85. // Become an administrator.
  86. $this->_setRole( 'administrator' );
  87. // Set up a default request.
  88. $_POST['_ajax_nonce'] = wp_create_nonce( uniqid() );
  89. $_POST['action'] = 'get-comments';
  90. $_POST['p'] = self::$comment_post->ID;
  91. // Make the request.
  92. $this->expectException( 'WPAjaxDieStopException' );
  93. $this->expectExceptionMessage( '-1' );
  94. $this->_handleAjax( 'get-comments' );
  95. }
  96. /**
  97. * Gets comments for an invalid post.
  98. *
  99. * Bad post IDs are set to 0, this should return valid XML.
  100. */
  101. public function test_invalid_post() {
  102. // Become an administrator.
  103. $this->_setRole( 'administrator' );
  104. // Set up a default request.
  105. $_POST['_ajax_nonce'] = wp_create_nonce( 'get-comments' );
  106. $_POST['action'] = 'get-comments';
  107. $_POST['p'] = 'b0rk';
  108. // Make the request.
  109. $this->expectException( 'WPAjaxDieStopException' );
  110. $this->expectExceptionMessage( '-1' );
  111. $this->_handleAjax( 'get-comments' );
  112. }
  113. /**
  114. * Gets comments for a post with no comments.
  115. */
  116. public function test_post_with_no_comments() {
  117. // Become an administrator.
  118. $this->_setRole( 'administrator' );
  119. // Set up a default request.
  120. $_POST['_ajax_nonce'] = wp_create_nonce( 'get-comments' );
  121. $_POST['action'] = 'get-comments';
  122. $_POST['p'] = self::$no_comment_post->ID;
  123. // Make the request.
  124. $this->expectException( 'WPAjaxDieStopException' );
  125. $this->expectExceptionMessage( '1' );
  126. $this->_handleAjax( 'get-comments' );
  127. }
  128. }