TagSearch.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 tag search functionality
  8. *
  9. * @package WordPress
  10. * @subpackage UnitTests
  11. * @since 3.4.0
  12. * @group ajax
  13. */
  14. class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase {
  15. /**
  16. * List of terms to insert on setup
  17. * @var array
  18. */
  19. private $_terms = array(
  20. 'chattels', 'depo', 'energumen', 'figuriste', 'habergeon', 'impropriation'
  21. );
  22. /**
  23. * Setup
  24. * @todo use a term factory
  25. */
  26. public function setUp() {
  27. parent::setUp();
  28. foreach ( $this->_terms as $term )
  29. wp_insert_term( $term, 'post_tag' );
  30. }
  31. /**
  32. * Test as an admin
  33. */
  34. public function test_post_tag() {
  35. // Become an administrator
  36. $this->_setRole( 'administrator' );
  37. // Set up a default request
  38. $_GET['tax'] = 'post_tag';
  39. $_GET['q'] = 'chat';
  40. // Make the request
  41. try {
  42. $this->_handleAjax( 'ajax-tag-search' );
  43. } catch ( WPAjaxDieContinueException $e ) {
  44. unset( $e );
  45. }
  46. // Ensure we found the right match
  47. $this->assertEquals( $this->_last_response, 'chattels' );
  48. }
  49. /**
  50. * Test with no results
  51. */
  52. public function test_no_results() {
  53. // Become an administrator
  54. $this->_setRole( 'administrator' );
  55. // Set up a default request
  56. $_GET['tax'] = 'post_tag';
  57. $_GET['q'] = md5(uniqid());
  58. // Make the request
  59. // No output, so we get a stop exception
  60. $this->setExpectedException( 'WPAjaxDieStopException', '0' );
  61. $this->_handleAjax( 'ajax-tag-search' );
  62. }
  63. /**
  64. * Test with commas
  65. */
  66. public function test_with_comma() {
  67. // Become an administrator
  68. $this->_setRole( 'administrator' );
  69. // Set up a default request
  70. $_GET['tax'] = 'post_tag';
  71. $_GET['q'] = 'some,nonsense, terms,chat'; // Only the last term in the list is searched
  72. // Make the request
  73. try {
  74. $this->_handleAjax( 'ajax-tag-search' );
  75. } catch ( WPAjaxDieContinueException $e ) {
  76. unset( $e );
  77. }
  78. // Ensure we found the right match
  79. $this->assertEquals( $this->_last_response, 'chattels' );
  80. }
  81. /**
  82. * Test as a logged out user
  83. */
  84. public function test_logged_out() {
  85. // Log out
  86. wp_logout();
  87. // Set up a default request
  88. $_GET['tax'] = 'post_tag';
  89. $_GET['q'] = 'chat';
  90. // Make the request
  91. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  92. $this->_handleAjax( 'ajax-tag-search' );
  93. }
  94. /**
  95. * Test with an invalid taxonomy type
  96. */
  97. public function test_invalid_tax() {
  98. // Become an administrator
  99. $this->_setRole( 'administrator' );
  100. // Set up a default request
  101. $_GET['tax'] = 'invalid-taxonomy';
  102. $_GET['q'] = 'chat';
  103. // Make the request
  104. $this->setExpectedException( 'WPAjaxDieStopException', '0' );
  105. $this->_handleAjax( 'ajax-tag-search' );
  106. }
  107. /**
  108. * Test as an unprivileged user
  109. */
  110. public function test_unprivileged_user() {
  111. // Become an administrator
  112. $this->_setRole( 'subscriber' );
  113. // Set up a default request
  114. $_GET['tax'] = 'post_tag';
  115. $_GET['q'] = 'chat';
  116. // Make the request
  117. $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
  118. $this->_handleAjax( 'ajax-tag-search' );
  119. }
  120. }